diff --git a/custom_components/mega/__init__.py b/custom_components/mega/__init__.py index 664cc19..3757f3e 100644 --- a/custom_components/mega/__init__.py +++ b/custom_components/mega/__init__.py @@ -128,8 +128,7 @@ async def get_hub(hass, entry): async def _add_mega(hass: HomeAssistant, entry: ConfigEntry): id = entry.data.get('id', entry.entry_id) hub = await get_hub(hass, entry) - hass.data[DOMAIN][id] = hass.data[DOMAIN]['__def'] = hub - hass.data[DOMAIN][entry.data.get(CONF_HOST)] = hub + hass.data[DOMAIN][id] = hub hass.data[DOMAIN][CONF_ALL][id] = hub if not await hub.authenticate(): raise Exception("not authentificated") diff --git a/custom_components/mega/config_flow.py b/custom_components/mega/config_flow.py index 9429425..2ebdade 100644 --- a/custom_components/mega/config_flow.py +++ b/custom_components/mega/config_flow.py @@ -62,7 +62,7 @@ async def validate_input(hass: core.HomeAssistant, data): class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for mega.""" - VERSION = 10 + VERSION = 11 CONNECTION_CLASS = config_entries.CONN_CLASS_ASSUMED async def async_step_user(self, user_input=None): diff --git a/custom_components/mega/http.py b/custom_components/mega/http.py index 6fe940b..b46a362 100644 --- a/custom_components/mega/http.py +++ b/custom_components/mega/http.py @@ -36,9 +36,10 @@ class MegaView(HomeAssistantView): } for mid in cfg if isinstance(cfg[mid], dict) } _LOGGER.debug('templates: %s', self.templates) + self.hubs = {} async def get(self, request: Request) -> Response: - + _LOGGER.debug('request from %s %s', request.remote, request.headers) hass: HomeAssistant = request.app['hass'] if self.protected: auth = False @@ -63,10 +64,14 @@ class MegaView(HomeAssistantView): _LOGGER.warning(msg) return Response(status=401) - hub: 'h.MegaD' = hass.data.get(DOMAIN).get(request.remote) # TODO: проверить какой remote - if hub is None and request.remote == '::1': - hub = hass.data.get(DOMAIN).get('__def') - if hub is None: + hub: 'h.MegaD' = self.hubs.get(request.remote) + if hub is None and 'mdid' in request.query: + hub = self.hubs.get(request.query['mdid']) + if hub is None: + _LOGGER.warning(f'can not find mdid={request.query["mdid"]} in {list(self.hubs)}') + if hub is None and request.remote in ['::1', '127.0.0.1']: + hub = self.hubs.get('__def') + elif hub is None: return Response(status=400) data = dict(request.query) hass.bus.async_fire( diff --git a/custom_components/mega/hub.py b/custom_components/mega/hub.py index 3fbe02c..801b2fa 100644 --- a/custom_components/mega/hub.py +++ b/custom_components/mega/hub.py @@ -85,6 +85,11 @@ class MegaD: self.http = hass.data.get(DOMAIN, {}).get(CONF_HTTP) if not self.http is None: self.http.allowed_hosts |= {host} + self.http.hubs[host] = self + if len(self.http.hubs) == 1: + self.http.hubs['__def'] = self + if mqtt_id: + self.http.hubs[mqtt_id] = self else: self.http = None self.poll_outs = poll_outs @@ -475,6 +480,7 @@ class MegaD: async def get_config(self, nports=37): ret = defaultdict(lambda: defaultdict(list)) + ret['mqtt_id'] = await self.get_mqtt_id() async for port, pty, m in self.scan_ports(nports): if pty == "0": ret['binary_sensor'][port].append({}) diff --git a/readme.md b/readme.md index c114dbe..fa54f85 100644 --- a/readme.md +++ b/readme.md @@ -58,6 +58,10 @@ wget -q -O - https://raw.githubusercontent.com/andvikt/mega_hacs/master/install. srv: "192.168.1.4:8123" # ip:port вашего HA script: "mega" # это api интеграции, к которому будет обращаться контроллер ``` +Так же необходимо настроить Mega-ID в настройках контроллера, для каждой меги id должен быть разным. + +При любых изменениях настроек контроллера (типы входов, id и тд) необходимо в настройках интеграции нажать `Обновить +объекты` ## Зависимости Для максимальной скорости реакции на команды сервера, рекомендуется выключить `имитацию http-ответа` в