mirror of
https://github.com/andvikt/mega_hacs.git
synced 2025-12-11 00:54:28 +05:00
add device registry
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user