2023-08-18 06:40:23 +00:00
|
|
|
"""Support for Comelit."""
|
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
|
|
|
from typing import Any
|
|
|
|
|
2023-09-24 12:51:56 +00:00
|
|
|
from aiocomelit import ComeliteSerialBridgeApi, ComelitSerialBridgeObject
|
|
|
|
from aiocomelit.const import BRIDGE
|
2023-08-18 06:40:23 +00:00
|
|
|
import aiohttp
|
|
|
|
|
2023-09-24 12:51:56 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-08-18 06:40:23 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
2023-09-24 12:51:56 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2023-08-18 06:40:23 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
|
|
|
from .const import _LOGGER, DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
class ComelitSerialBridge(DataUpdateCoordinator):
|
|
|
|
"""Queries Comelit Serial Bridge."""
|
|
|
|
|
2023-09-24 12:51:56 +00:00
|
|
|
config_entry: ConfigEntry
|
|
|
|
|
2023-08-18 06:40:23 +00:00
|
|
|
def __init__(self, hass: HomeAssistant, host: str, pin: int) -> None:
|
|
|
|
"""Initialize the scanner."""
|
|
|
|
|
|
|
|
self._host = host
|
|
|
|
self._pin = pin
|
|
|
|
|
2023-09-22 20:38:33 +00:00
|
|
|
self.api = ComeliteSerialBridgeApi(host, pin)
|
2023-08-18 06:40:23 +00:00
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
hass=hass,
|
|
|
|
logger=_LOGGER,
|
|
|
|
name=f"{DOMAIN}-{host}-coordinator",
|
|
|
|
update_interval=timedelta(seconds=5),
|
|
|
|
)
|
2023-09-24 12:51:56 +00:00
|
|
|
device_registry = dr.async_get(self.hass)
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=self.config_entry.entry_id,
|
|
|
|
identifiers={(DOMAIN, self.config_entry.entry_id)},
|
|
|
|
model=BRIDGE,
|
|
|
|
name=f"{BRIDGE} ({self.api.host})",
|
|
|
|
**self.basic_device_info,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def basic_device_info(self) -> dict:
|
|
|
|
"""Set basic device info."""
|
|
|
|
|
|
|
|
return {
|
|
|
|
"manufacturer": "Comelit",
|
|
|
|
"hw_version": "20003101",
|
|
|
|
}
|
|
|
|
|
|
|
|
def platform_device_info(
|
|
|
|
self, device: ComelitSerialBridgeObject, platform: str
|
|
|
|
) -> dr.DeviceInfo:
|
|
|
|
"""Set platform device info."""
|
|
|
|
|
|
|
|
return dr.DeviceInfo(
|
|
|
|
identifiers={
|
|
|
|
(DOMAIN, f"{self.config_entry.entry_id}-{platform}-{device.index}")
|
|
|
|
},
|
|
|
|
via_device=(DOMAIN, self.config_entry.entry_id),
|
|
|
|
name=device.name,
|
|
|
|
model=f"{BRIDGE} {platform}",
|
|
|
|
**self.basic_device_info,
|
|
|
|
)
|
2023-08-18 06:40:23 +00:00
|
|
|
|
|
|
|
async def _async_update_data(self) -> dict[str, Any]:
|
|
|
|
"""Update router data."""
|
|
|
|
_LOGGER.debug("Polling Comelit Serial Bridge host: %s", self._host)
|
|
|
|
try:
|
|
|
|
logged = await self.api.login()
|
|
|
|
except (asyncio.exceptions.TimeoutError, aiohttp.ClientConnectorError) as err:
|
|
|
|
_LOGGER.warning("Connection error for %s", self._host)
|
|
|
|
raise UpdateFailed(f"Error fetching data: {repr(err)}") from err
|
|
|
|
|
|
|
|
if not logged:
|
|
|
|
raise ConfigEntryAuthFailed
|
|
|
|
|
|
|
|
devices_data = await self.api.get_all_devices()
|
|
|
|
await self.api.logout()
|
|
|
|
|
2023-09-11 06:49:10 +00:00
|
|
|
return devices_data
|