core/homeassistant/components/smlight/coordinator.py

155 lines
5.0 KiB
Python
Raw Normal View History

New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
"""DataUpdateCoordinator for Smlight."""
from __future__ import annotations
from abc import abstractmethod
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
from dataclasses import dataclass
from typing import TYPE_CHECKING
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
from pysmlight import Api2, Info, Sensors
from pysmlight.const import Settings, SettingsProp
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError
from pysmlight.web import Firmware
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed
from homeassistant.helpers import issue_registry as ir
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.issue_registry import IssueSeverity
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN, LOGGER, SCAN_FIRMWARE_INTERVAL, SCAN_INTERVAL
if TYPE_CHECKING:
from . import SmConfigEntry
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
@dataclass
class SmData:
"""SMLIGHT data stored in the DataUpdateCoordinator."""
sensors: Sensors
info: Info
@dataclass
class SmFwData:
"""SMLIGHT firmware data stored in the FirmwareUpdateCoordinator."""
info: Info
esp_firmware: list[Firmware] | None
zb_firmware: list[Firmware] | None
class SmBaseDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
"""Base Coordinator for SMLIGHT."""
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
config_entry: SmConfigEntry
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
def __init__(self, hass: HomeAssistant, host: str, client: Api2) -> None:
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
"""Initialize the coordinator."""
super().__init__(
hass,
LOGGER,
name=f"{DOMAIN}_{host}",
update_interval=SCAN_INTERVAL,
)
self.client = client
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
self.unique_id: str | None = None
self.legacy_api: int = 0
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
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:
raise ConfigEntryAuthFailed from err
else:
# Auth required but no credentials available
raise ConfigEntryAuthFailed
New Integration: SMLIGHT SLZB-06 Adapters Integration (#118675) * Initial SMLIGHT integration Signed-off-by: Tim Lunn <tl@smlight.tech> * Generated content Signed-off-by: Tim Lunn <tl@smlight.tech> * Cleanup LOGGING * Use runtime data * Call super first * coordinator instance attributes * Move coordinatorEntity and attr to base class * cleanup sensors * update strings to use sentence case * Improve reauth flow on incorrect credentials * Use fixture for config_flow tests and test to completion * Split uptime hndling into a new uptime sensor entity * Drop server side events and internet callback will bring this back with binary sensor Platform * consolidate coordinator setup * entity always include connections * get_hostname tweak * Add tests for init, coordinator and sensor * Use custom type SmConfigEntry * update sensor snapshot * Drop reauth flow for later PR * Use _async_setup for initial setup * drop internet to be set later * sensor fixes * config flow re * typing fixes * Bump pysmlight dependency to 0.0.12 * dont trigger invalid auth message when first loading auth step * Merge uptime sensors back into main sensor class * clarify uptime handling * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * address review comments * pass host as parameter to the dataCoordinator * drop uptime sensors for a later PR * update sensor test snapshot * move coordinator unique_id to _async_setup * fix CI * Apply suggestions from code review Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * drop invalid_auth test tag * use snapshot_platform, update fixtures * Finish all tests with abort or create entry * drop coordinator tests and remove hostname support * add test for update failure on connection error * use freezer for update_failed test * fix pysmlight imports --------- Signed-off-by: Tim Lunn <tl@smlight.tech> Co-authored-by: Tim Lunn <tim@feathertop.org> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-08-20 08:44:06 +00:00
info = await self.client.get_info()
self.unique_id = format_mac(info.MAC)
self.legacy_api = info.legacy_api
if info.legacy_api == 2:
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",
)
async def _async_update_data(self) -> _DataT:
try:
return await self._internal_update_data()
except SmlightAuthError as err:
raise ConfigEntryAuthFailed from err
except SmlightConnectionError as err:
raise UpdateFailed(err) from err
@abstractmethod
async def _internal_update_data(self) -> _DataT:
"""Update coordinator data."""
class SmDataUpdateCoordinator(SmBaseDataUpdateCoordinator[SmData]):
"""Class to manage fetching SMLIGHT sensor data."""
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)
async def _internal_update_data(self) -> SmData:
"""Fetch sensor data from the SMLIGHT device."""
sensors = Sensors()
if not self.legacy_api:
sensors = await self.client.get_sensors()
return SmData(
sensors=sensors,
info=await self.client.get_info(),
)
class SmFirmwareUpdateCoordinator(SmBaseDataUpdateCoordinator[SmFwData]):
"""Class to manage fetching SMLIGHT firmware update data from cloud."""
def __init__(self, hass: HomeAssistant, host: str, client: Api2) -> None:
"""Initialize the coordinator."""
super().__init__(hass, host, client)
self.update_interval = SCAN_FIRMWARE_INTERVAL
# only one update can run at a time (core or zibgee)
self.in_progress = False
async def _internal_update_data(self) -> SmFwData:
"""Fetch data from the SMLIGHT device."""
info = await self.client.get_info()
return SmFwData(
info=info,
esp_firmware=await self.client.get_firmware_version(info.fw_channel),
zb_firmware=await self.client.get_firmware_version(
info.fw_channel, device=info.model, mode="zigbee"
),
)