2024-08-20 08:44:06 +00:00
|
|
|
"""DataUpdateCoordinator for Smlight."""
|
|
|
|
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from pysmlight import Api2, Info, Sensors
|
2024-09-11 10:53:08 +00:00
|
|
|
from pysmlight.const import Settings, SettingsProp
|
2024-08-20 08:44:06 +00:00
|
|
|
from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
|
|
from homeassistant.core import HomeAssistant
|
2024-09-05 09:02:05 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
2024-09-06 13:46:08 +00:00
|
|
|
from homeassistant.helpers import issue_registry as ir
|
2024-08-20 08:44:06 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
from homeassistant.helpers.device_registry import format_mac
|
2024-09-06 13:46:08 +00:00
|
|
|
from homeassistant.helpers.issue_registry import IssueSeverity
|
2024-08-20 08:44:06 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
|
|
|
|
from .const import DOMAIN, LOGGER, SCAN_INTERVAL
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SmData:
|
|
|
|
"""SMLIGHT data stored in the DataUpdateCoordinator."""
|
|
|
|
|
|
|
|
sensors: Sensors
|
|
|
|
info: Info
|
|
|
|
|
|
|
|
|
|
|
|
class SmDataUpdateCoordinator(DataUpdateCoordinator[SmData]):
|
|
|
|
"""Class to manage fetching SMLIGHT data."""
|
|
|
|
|
|
|
|
config_entry: ConfigEntry
|
|
|
|
|
|
|
|
def __init__(self, hass: HomeAssistant, host: str) -> None:
|
|
|
|
"""Initialize the coordinator."""
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
name=f"{DOMAIN}_{host}",
|
|
|
|
update_interval=SCAN_INTERVAL,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.unique_id: str | None = None
|
|
|
|
self.client = Api2(host=host, session=async_get_clientsession(hass))
|
2024-09-06 13:46:08 +00:00
|
|
|
self.legacy_api: int = 0
|
2024-08-20 08:44:06 +00:00
|
|
|
|
2024-09-11 10:53:08 +00:00
|
|
|
self.config_entry.async_create_background_task(
|
|
|
|
hass, self.client.sse.client(), "smlight-sse-client"
|
|
|
|
)
|
|
|
|
|
2024-08-20 08:44:06 +00:00
|
|
|
async def _async_setup(self) -> None:
|
|
|
|
"""Authenticate if needed during initial setup."""
|
|
|
|
if await self.client.check_auth_needed():
|
|
|
|
if (
|
|
|
|
CONF_USERNAME in self.config_entry.data
|
|
|
|
and CONF_PASSWORD in self.config_entry.data
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
await self.client.authenticate(
|
|
|
|
self.config_entry.data[CONF_USERNAME],
|
|
|
|
self.config_entry.data[CONF_PASSWORD],
|
|
|
|
)
|
|
|
|
except SmlightAuthError as err:
|
2024-09-05 09:02:05 +00:00
|
|
|
raise ConfigEntryAuthFailed from err
|
|
|
|
else:
|
|
|
|
# Auth required but no credentials available
|
|
|
|
raise ConfigEntryAuthFailed
|
2024-08-20 08:44:06 +00:00
|
|
|
|
|
|
|
info = await self.client.get_info()
|
|
|
|
self.unique_id = format_mac(info.MAC)
|
|
|
|
|
2024-09-06 13:46:08 +00:00
|
|
|
if info.legacy_api:
|
|
|
|
self.legacy_api = info.legacy_api
|
|
|
|
ir.async_create_issue(
|
|
|
|
self.hass,
|
|
|
|
DOMAIN,
|
|
|
|
"unsupported_firmware",
|
|
|
|
is_fixable=False,
|
|
|
|
is_persistent=False,
|
|
|
|
learn_more_url="https://smlight.tech/flasher/#SLZB-06",
|
|
|
|
severity=IssueSeverity.ERROR,
|
|
|
|
translation_key="unsupported_firmware",
|
|
|
|
)
|
|
|
|
|
2024-09-11 10:53:08 +00:00
|
|
|
def update_setting(self, setting: Settings, value: bool | int) -> None:
|
|
|
|
"""Update the sensor value from event."""
|
|
|
|
prop = SettingsProp[setting.name].value
|
|
|
|
setattr(self.data.sensors, prop, value)
|
|
|
|
|
|
|
|
self.async_set_updated_data(self.data)
|
|
|
|
|
2024-08-20 08:44:06 +00:00
|
|
|
async def _async_update_data(self) -> SmData:
|
|
|
|
"""Fetch data from the SMLIGHT device."""
|
|
|
|
try:
|
2024-09-06 13:46:08 +00:00
|
|
|
sensors = Sensors()
|
|
|
|
if not self.legacy_api:
|
|
|
|
sensors = await self.client.get_sensors()
|
|
|
|
|
2024-08-20 08:44:06 +00:00
|
|
|
return SmData(
|
2024-09-06 13:46:08 +00:00
|
|
|
sensors=sensors,
|
2024-08-20 08:44:06 +00:00
|
|
|
info=await self.client.get_info(),
|
|
|
|
)
|
2024-09-05 09:02:05 +00:00
|
|
|
except SmlightAuthError as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
|
|
|
|
2024-08-20 08:44:06 +00:00
|
|
|
except SmlightConnectionError as err:
|
|
|
|
raise UpdateFailed(err) from err
|