mirror of
https://github.com/andvikt/mega_hacs.git
synced 2025-12-11 17:14: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)
|
_hubs.pop(entry.entry_id)
|
||||||
unsub = _subs.pop(entry.entry_id)
|
unsub = _subs.pop(entry.entry_id)
|
||||||
unsub()
|
unsub()
|
||||||
|
hass.data[DOMAIN].pop(id)
|
||||||
|
|
||||||
|
|
||||||
async def _save_service(hass: HomeAssistant, call: ServiceCall):
|
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 def scan():
|
||||||
async for port, pty, m in hub.scan_ports():
|
async for port, pty, m in hub.scan_ports():
|
||||||
if pty == "0":
|
if pty == "0":
|
||||||
sensor = MegaBinarySensor(mega_id=mid, port=port)
|
sensor = MegaBinarySensor(mega_id=mid, port=port, config_entry=config_entry)
|
||||||
devices.append(sensor)
|
devices.append(sensor)
|
||||||
|
|
||||||
async_add_devices(devices)
|
async_add_devices(devices)
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
data_schema=vol.Schema({
|
data_schema=vol.Schema({
|
||||||
vol.Optional(CONF_SCAN_INTERVAL, default=e[CONF_SCAN_INTERVAL]): int,
|
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_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,
|
# vol.Optional(CONF_INVERT, default=''): str,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import State
|
from homeassistant.core import State
|
||||||
from .hub import MegaD
|
from .hub import MegaD
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
@@ -19,12 +20,14 @@ class BaseMegaEntity(RestoreEntity):
|
|||||||
self,
|
self,
|
||||||
mega_id: str,
|
mega_id: str,
|
||||||
port: int,
|
port: int,
|
||||||
|
config_entry: ConfigEntry = None,
|
||||||
id_suffix=None,
|
id_suffix=None,
|
||||||
name=None,
|
name=None,
|
||||||
unique_id=None
|
unique_id=None,
|
||||||
):
|
):
|
||||||
self._state: State = None
|
self._state: State = None
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.config_entry = config_entry
|
||||||
self._mega_id = mega_id
|
self._mega_id = mega_id
|
||||||
self._lg = None
|
self._lg = None
|
||||||
self._unique_id = unique_id or f"mega_{mega_id}_{port}" + \
|
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}" + \
|
self._name = name or f"{mega_id}_{port}" + \
|
||||||
(f"_{id_suffix}" if id_suffix else "")
|
(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
|
@property
|
||||||
def lg(self) -> logging.Logger:
|
def lg(self) -> logging.Logger:
|
||||||
if self._lg is None:
|
if self._lg is None:
|
||||||
|
|||||||
@@ -238,36 +238,37 @@ class MegaD:
|
|||||||
return await req.text()
|
return await req.text()
|
||||||
|
|
||||||
async def scan_port(self, port):
|
async def scan_port(self, port):
|
||||||
if port in self._scanned:
|
async with self.lck:
|
||||||
return self._scanned[port]
|
if port in self._scanned:
|
||||||
url = f'http://{self.host}/{self.sec}/?pt={port}'
|
return self._scanned[port]
|
||||||
self.lg.debug(
|
url = f'http://{self.host}/{self.sec}/?pt={port}'
|
||||||
f'scan port %s: %s', port, url
|
self.lg.debug(
|
||||||
)
|
f'scan port %s: %s', port, url
|
||||||
async with aiohttp.request('get', url) as req:
|
)
|
||||||
html = await req.text()
|
async with aiohttp.request('get', url) as req:
|
||||||
tree = BeautifulSoup(html, features="lxml")
|
html = await req.text()
|
||||||
pty = tree.find('select', attrs={'name': 'pty'})
|
tree = BeautifulSoup(html, features="lxml")
|
||||||
if pty is None:
|
pty = tree.find('select', attrs={'name': 'pty'})
|
||||||
return
|
if pty is None:
|
||||||
else:
|
|
||||||
pty = pty.find(selected=True)
|
|
||||||
if pty:
|
|
||||||
pty = pty['value']
|
|
||||||
else:
|
|
||||||
return
|
return
|
||||||
if pty in ['0', '1']:
|
else:
|
||||||
m = tree.find('select', attrs={'name': 'm'})
|
pty = pty.find(selected=True)
|
||||||
if m:
|
if pty:
|
||||||
m = m.find(selected=True)['value']
|
pty = pty['value']
|
||||||
self._scanned[port] = (pty, m)
|
else:
|
||||||
return pty, m
|
return
|
||||||
elif pty == '3':
|
if pty in ['0', '1']:
|
||||||
m = tree.find('select', attrs={'name': 'd'})
|
m = tree.find('select', attrs={'name': 'm'})
|
||||||
if m:
|
if m:
|
||||||
m = m.find(selected=True)['value']
|
m = m.find(selected=True)['value']
|
||||||
self._scanned[port] = (pty, m)
|
self._scanned[port] = (pty, m)
|
||||||
return 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,):
|
async def scan_ports(self,):
|
||||||
for x in range(38):
|
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 def scan_ports():
|
||||||
async for port, pty, m in hub.scan_ports():
|
async for port, pty, m in hub.scan_ports():
|
||||||
if pty == "1" and m in ['0', '1']:
|
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)
|
devices.append(light)
|
||||||
async_add_devices(devices)
|
async_add_devices(devices)
|
||||||
|
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ async def async_setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
return True
|
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]
|
key = conf[CONF_KEY]
|
||||||
return Mega1WSensor(
|
return Mega1WSensor(
|
||||||
key=key,
|
key=key,
|
||||||
@@ -78,7 +78,8 @@ def _make_entity(mid: str, port: int, conf: dict):
|
|||||||
patt=PATTERNS.get(key),
|
patt=PATTERNS.get(key),
|
||||||
unit_of_measurement=UNITS.get(key, UNITS[TEMP]), # TODO: make other units, make options in config flow
|
unit_of_measurement=UNITS.get(key, UNITS[TEMP]), # TODO: make other units, make options in config flow
|
||||||
device_class=CLASSES.get(key, CLASSES[TEMP]),
|
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={
|
||||||
CONF_TYPE: W1,
|
CONF_TYPE: W1,
|
||||||
CONF_KEY: key,
|
CONF_KEY: key,
|
||||||
})
|
},
|
||||||
|
config_entry=config_entry,
|
||||||
|
)
|
||||||
devices.append(sensor)
|
devices.append(sensor)
|
||||||
hub.sensors.append(sensor)
|
hub.sensors.append(sensor)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user