From c4f4510941152341db25c12546adafeabea29cfd Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 15 Jan 2021 09:10:42 +0300 Subject: [PATCH] add device registry --- custom_components/mega/__init__.py | 1 + custom_components/mega/binary_sensor.py | 2 +- custom_components/mega/config_flow.py | 2 +- custom_components/mega/entities.py | 22 ++++++++- custom_components/mega/hub.py | 59 +++++++++++++------------ custom_components/mega/light.py | 2 +- custom_components/mega/sensor.py | 9 ++-- 7 files changed, 61 insertions(+), 36 deletions(-) diff --git a/custom_components/mega/__init__.py b/custom_components/mega/__init__.py index 032a5ce..8ea1927 100644 --- a/custom_components/mega/__init__.py +++ b/custom_components/mega/__init__.py @@ -130,6 +130,7 @@ async def async_remove_entry(hass, entry) -> None: _hubs.pop(entry.entry_id) unsub = _subs.pop(entry.entry_id) unsub() + hass.data[DOMAIN].pop(id) async def _save_service(hass: HomeAssistant, call: ServiceCall): diff --git a/custom_components/mega/binary_sensor.py b/custom_components/mega/binary_sensor.py index a616f94..28b77df 100644 --- a/custom_components/mega/binary_sensor.py +++ b/custom_components/mega/binary_sensor.py @@ -65,7 +65,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn async def scan(): async for port, pty, m in hub.scan_ports(): if pty == "0": - sensor = MegaBinarySensor(mega_id=mid, port=port) + sensor = MegaBinarySensor(mega_id=mid, port=port, config_entry=config_entry) devices.append(sensor) async_add_devices(devices) diff --git a/custom_components/mega/config_flow.py b/custom_components/mega/config_flow.py index 8ca061e..f3c50ed 100644 --- a/custom_components/mega/config_flow.py +++ b/custom_components/mega/config_flow.py @@ -103,7 +103,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow): data_schema=vol.Schema({ vol.Optional(CONF_SCAN_INTERVAL, default=e[CONF_SCAN_INTERVAL]): int, vol.Optional(CONF_PORT_TO_SCAN, default=e.get(CONF_PORT_TO_SCAN, 0)): int, - vol.Optional(CONF_RELOAD, default=False): bool, + # vol.Optional(CONF_RELOAD, default=False): bool, # vol.Optional(CONF_INVERT, default=''): str, }), ) diff --git a/custom_components/mega/entities.py b/custom_components/mega/entities.py index 99a7c6c..efc05ac 100644 --- a/custom_components/mega/entities.py +++ b/custom_components/mega/entities.py @@ -3,6 +3,7 @@ import asyncio import json import logging +from homeassistant.config_entries import ConfigEntry from homeassistant.core import State from .hub import MegaD from homeassistant.helpers.restore_state import RestoreEntity @@ -19,12 +20,14 @@ class BaseMegaEntity(RestoreEntity): self, mega_id: str, port: int, + config_entry: ConfigEntry = None, id_suffix=None, name=None, - unique_id=None + unique_id=None, ): self._state: State = None self.port = port + self.config_entry = config_entry self._mega_id = mega_id self._lg = None self._unique_id = unique_id or f"mega_{mega_id}_{port}" + \ @@ -32,6 +35,23 @@ class BaseMegaEntity(RestoreEntity): self._name = name or f"{mega_id}_{port}" + \ (f"_{id_suffix}" if id_suffix else "") + @property + def device_info(self): + return { + "identifiers": { + # Serial numbers are unique identifiers within a specific domain + (DOMAIN, f'{self._mega_id}', self.port), + }, + "config_entries": [ + self.config_entry, + ], + "name": f'port {self.port}', + "manufacturer": 'ab-log.ru', + # "model": self.light.productname, + # "sw_version": self.light.swversion, + "via_device": (DOMAIN, self._mega_id), + } + @property def lg(self) -> logging.Logger: if self._lg is None: diff --git a/custom_components/mega/hub.py b/custom_components/mega/hub.py index ad38d83..51cce24 100644 --- a/custom_components/mega/hub.py +++ b/custom_components/mega/hub.py @@ -238,36 +238,37 @@ class MegaD: return await req.text() async def scan_port(self, port): - if port in self._scanned: - return self._scanned[port] - url = f'http://{self.host}/{self.sec}/?pt={port}' - self.lg.debug( - f'scan port %s: %s', port, url - ) - async with aiohttp.request('get', url) as req: - html = await req.text() - tree = BeautifulSoup(html, features="lxml") - pty = tree.find('select', attrs={'name': 'pty'}) - if pty is None: - return - else: - pty = pty.find(selected=True) - if pty: - pty = pty['value'] - else: + async with self.lck: + if port in self._scanned: + return self._scanned[port] + url = f'http://{self.host}/{self.sec}/?pt={port}' + self.lg.debug( + f'scan port %s: %s', port, url + ) + async with aiohttp.request('get', url) as req: + html = await req.text() + tree = BeautifulSoup(html, features="lxml") + pty = tree.find('select', attrs={'name': 'pty'}) + if pty is None: return - if pty in ['0', '1']: - m = tree.find('select', attrs={'name': 'm'}) - if m: - m = m.find(selected=True)['value'] - self._scanned[port] = (pty, m) - return pty, m - elif pty == '3': - m = tree.find('select', attrs={'name': 'd'}) - if m: - m = m.find(selected=True)['value'] - self._scanned[port] = (pty, m) - return pty, m + else: + pty = pty.find(selected=True) + if pty: + pty = pty['value'] + else: + return + if pty in ['0', '1']: + m = tree.find('select', attrs={'name': 'm'}) + if m: + m = m.find(selected=True)['value'] + self._scanned[port] = (pty, m) + return pty, m + elif pty == '3': + m = tree.find('select', attrs={'name': 'd'}) + if m: + m = m.find(selected=True)['value'] + self._scanned[port] = (pty, m) + return pty, m async def scan_ports(self,): for x in range(38): diff --git a/custom_components/mega/light.py b/custom_components/mega/light.py index 38367cb..42d7421 100644 --- a/custom_components/mega/light.py +++ b/custom_components/mega/light.py @@ -81,7 +81,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn async def scan_ports(): async for port, pty, m in hub.scan_ports(): if pty == "1" and m in ['0', '1']: - light = MegaLight(mega_id=mid, port=port, dimmer=m == '1') + light = MegaLight(mega_id=mid, port=port, dimmer=m == '1', config_entry=config_entry) devices.append(light) async_add_devices(devices) diff --git a/custom_components/mega/sensor.py b/custom_components/mega/sensor.py index fcd8ca1..1728664 100644 --- a/custom_components/mega/sensor.py +++ b/custom_components/mega/sensor.py @@ -69,7 +69,7 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None): return True -def _make_entity(mid: str, port: int, conf: dict): +def _make_entity(config_entry, mid: str, port: int, conf: dict): key = conf[CONF_KEY] return Mega1WSensor( key=key, @@ -78,7 +78,8 @@ def _make_entity(mid: str, port: int, conf: dict): patt=PATTERNS.get(key), unit_of_measurement=UNITS.get(key, UNITS[TEMP]), # TODO: make other units, make options in config flow device_class=CLASSES.get(key, CLASSES[TEMP]), - id_suffix=key + id_suffix=key, + config_entry=config_entry ) @@ -106,7 +107,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn conf={ CONF_TYPE: W1, CONF_KEY: key, - }) + }, + config_entry=config_entry, + ) devices.append(sensor) hub.sensors.append(sensor)