diff --git a/custom_components/mega/__init__.py b/custom_components/mega/__init__.py index 613e8cc..0b10e80 100644 --- a/custom_components/mega/__init__.py +++ b/custom_components/mega/__init__.py @@ -17,7 +17,7 @@ from homeassistant.components import mqtt from homeassistant.config_entries import ConfigEntry from .const import DOMAIN, CONF_INVERT, CONF_RELOAD, PLATFORMS, CONF_PORTS, CONF_CUSTOM, CONF_SKIP, CONF_PORT_TO_SCAN, \ CONF_MQTT_INPUTS, CONF_HTTP, CONF_RESPONSE_TEMPLATE, CONF_ACTION, CONF_GET_VALUE, CONF_ALLOW_HOSTS, \ - CONF_CONV_TEMPLATE, CONF_ALL, CONF_FORCE_D + CONF_CONV_TEMPLATE, CONF_ALL, CONF_FORCE_D, CONF_DEF_RESPONSE from .hub import MegaD from .config_flow import ConfigFlow from .http import MegaView @@ -53,6 +53,11 @@ CONFIG_SCHEMA = vol.Schema( vol.Optional(CONF_ALLOW_HOSTS): [str], vol.Required(str, description='id меги из веб-интерфейса'): { vol.Optional(CONF_FORCE_D, description='Принудительно слать d после срабатывания входа', default=False): bool, + vol.Optional( + CONF_DEF_RESPONSE, + description='Ответ по умолчанию', + default='' + ): cv.template, vol.Optional(int, description='номер порта'): vol.Any( CUSTOMIZE_PORT, CUSTOMIZE_DS2413, diff --git a/custom_components/mega/config_flow.py b/custom_components/mega/config_flow.py index 60485ce..bd1d453 100644 --- a/custom_components/mega/config_flow.py +++ b/custom_components/mega/config_flow.py @@ -57,7 +57,7 @@ async def validate_input(hass: core.HomeAssistant, data): class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for mega.""" - VERSION = 7 + VERSION = 8 CONNECTION_CLASS = config_entries.CONN_CLASS_ASSUMED async def async_step_user(self, user_input=None): diff --git a/custom_components/mega/const.py b/custom_components/mega/const.py index 7379290..e3a53d6 100644 --- a/custom_components/mega/const.py +++ b/custom_components/mega/const.py @@ -28,6 +28,7 @@ CONF_ALLOW_HOSTS = 'allow_hosts' CONF_CONV_TEMPLATE = 'conv_template' CONF_POLL_OUTS = 'poll_outs' CONF_FORCE_D = 'force_d' +CONF_DEF_RESPONSE = 'def_response' PLATFORMS = [ "light", "switch", diff --git a/custom_components/mega/http.py b/custom_components/mega/http.py index daade3d..230e387 100644 --- a/custom_components/mega/http.py +++ b/custom_components/mega/http.py @@ -71,7 +71,7 @@ class MegaView(HomeAssistantView): hub.values[port] = data for cb in self.callbacks[hub.id][port]: cb(data) - template: Template = self.templates.get(hub.id, {}).get(port) + template: Template = self.templates.get(hub.id, {}).get(port, hub.def_response) if hub.update_all and update_all: asyncio.create_task(self.later_update(hub)) if template is not None: @@ -79,8 +79,10 @@ class MegaView(HomeAssistantView): ret = template.async_render(data) _LOGGER.debug('response %s', ret) Response(body='', content_type='text/plain', headers={'Server': 's', 'Date': 'n'}) - if ret: + if 'd' in ret: await hub.request(pt=port, cmd=ret) + else: + await hub.request(cmd=ret) return ret async def later_update(self, hub): diff --git a/custom_components/mega/hub.py b/custom_components/mega/hub.py index 5ff0c97..6ae3096 100644 --- a/custom_components/mega/hub.py +++ b/custom_components/mega/hub.py @@ -19,7 +19,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import ( TEMP, HUM, PRESS, LUX, PATT_SPLIT, DOMAIN, - CONF_HTTP, EVENT_BINARY_SENSOR, CONF_CUSTOM, CONF_FORCE_D + CONF_HTTP, EVENT_BINARY_SENSOR, CONF_CUSTOM, CONF_FORCE_D, CONF_DEF_RESPONSE ) from .entities import set_events_off, BaseMegaEntity from .exceptions import CannotConnect, NoPort @@ -171,6 +171,10 @@ class MegaD: def force_d(self): return self.customize.get(CONF_FORCE_D, False) + @property + def def_response(self): + return self.customize.get(CONF_DEF_RESPONSE, None) + @property def is_online(self): return (datetime.now() - self.last_update).total_seconds() < (self.poll_interval + 10) @@ -264,6 +268,8 @@ class MegaD: ret = ret.split(';') elif '/' in ret: ret = ret.split('/') + else: + ret = [ret] ret = {'value': dict([ x.split(':') for x in ret if x.count(':') == 1 ])}