* refoss * refoss * refoss * refoss * refoss modify * ip * 8.22 * format * format * format * bugfix * test * test * test * test * test * test * 9.1 * refosss * refoss * refoss * refoss * refoss * refoss * refoss * refoss * test * requirements_test_all.txt * codeowners * refoss * Review feedback repair * strings * refoss * refoss * refoss * 1.1.1 * 1.1.2 * refoss * refoss * refoss.1.1.7 * refoss-gree * 1.1.7 * test * refoss * test refoss * test refoss * refoss-test * refoss * refoss * test * test * refoss * CODEOWNERS * fix * Update homeassistant/components/refoss/__init__.py --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
"""Refoss integration."""
|
|
from __future__ import annotations
|
|
|
|
from refoss_ha.device import DeviceInfo
|
|
from refoss_ha.device_manager import async_build_base_device
|
|
from refoss_ha.discovery import Discovery, Listener
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DOMAIN
|
|
from .coordinator import RefossDataUpdateCoordinator
|
|
|
|
|
|
class DiscoveryService(Listener):
|
|
"""Discovery event handler for refoss devices."""
|
|
|
|
def __init__(self, hass: HomeAssistant, discovery: Discovery) -> None:
|
|
"""Init discovery service."""
|
|
self.hass = hass
|
|
|
|
self.discovery = discovery
|
|
self.discovery.add_listener(self)
|
|
|
|
hass.data[DOMAIN].setdefault(COORDINATORS, [])
|
|
|
|
async def device_found(self, device_info: DeviceInfo) -> None:
|
|
"""Handle new device found on the network."""
|
|
|
|
device = await async_build_base_device(device_info)
|
|
if device is None:
|
|
return None
|
|
|
|
coordo = RefossDataUpdateCoordinator(self.hass, device)
|
|
self.hass.data[DOMAIN][COORDINATORS].append(coordo)
|
|
await coordo.async_refresh()
|
|
|
|
async_dispatcher_send(self.hass, DISPATCH_DEVICE_DISCOVERED, coordo)
|
|
|
|
async def device_update(self, device_info: DeviceInfo) -> None:
|
|
"""Handle updates in device information, update if ip has changed."""
|
|
for coordinator in self.hass.data[DOMAIN][COORDINATORS]:
|
|
if coordinator.device.device_info.mac == device_info.mac:
|
|
coordinator.device.device_info.inner_ip = device_info.inner_ip
|
|
await coordinator.async_refresh()
|