2019-10-05 19:46:16 +00:00
|
|
|
"""Base class for IKEA TRADFRI."""
|
2021-09-18 21:24:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
from abc import abstractmethod
|
2022-07-19 13:21:17 +00:00
|
|
|
from collections.abc import Callable, Coroutine
|
2020-09-05 21:02:32 +00:00
|
|
|
from functools import wraps
|
2022-01-27 10:12:52 +00:00
|
|
|
from typing import Any, cast
|
2019-10-05 19:46:16 +00:00
|
|
|
|
2021-09-18 21:24:35 +00:00
|
|
|
from pytradfri.command import Command
|
2021-09-20 12:33:50 +00:00
|
|
|
from pytradfri.device import Device
|
2022-02-23 08:34:32 +00:00
|
|
|
from pytradfri.error import RequestError
|
2019-10-05 19:46:16 +00:00
|
|
|
|
|
|
|
from homeassistant.core import callback
|
2022-01-27 10:12:52 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
2019-12-01 05:23:39 +00:00
|
|
|
|
2022-04-05 12:00:45 +00:00
|
|
|
from .const import DOMAIN, LOGGER
|
2022-01-27 10:12:52 +00:00
|
|
|
from .coordinator import TradfriDeviceDataUpdateCoordinator
|
2019-10-05 19:46:16 +00:00
|
|
|
|
|
|
|
|
2021-09-20 12:33:50 +00:00
|
|
|
def handle_error(
|
|
|
|
func: Callable[[Command | list[Command]], Any]
|
2022-07-19 13:21:17 +00:00
|
|
|
) -> Callable[[Command | list[Command]], Coroutine[Any, Any, None]]:
|
2020-09-05 21:02:32 +00:00
|
|
|
"""Handle tradfri api call error."""
|
|
|
|
|
|
|
|
@wraps(func)
|
2021-09-20 12:33:50 +00:00
|
|
|
async def wrapper(command: Command | list[Command]) -> None:
|
2020-09-05 21:02:32 +00:00
|
|
|
"""Decorate api call."""
|
|
|
|
try:
|
|
|
|
await func(command)
|
2022-02-23 08:34:32 +00:00
|
|
|
except RequestError as err:
|
2022-04-05 12:00:45 +00:00
|
|
|
LOGGER.error("Unable to execute command %s: %s", command, err)
|
2020-09-05 21:02:32 +00:00
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2022-03-21 13:20:35 +00:00
|
|
|
class TradfriBaseEntity(CoordinatorEntity[TradfriDeviceDataUpdateCoordinator]):
|
2022-01-27 10:12:52 +00:00
|
|
|
"""Base Tradfri device."""
|
2019-10-13 20:59:28 +00:00
|
|
|
|
2021-09-18 21:24:35 +00:00
|
|
|
def __init__(
|
2021-09-20 12:33:50 +00:00
|
|
|
self,
|
2022-01-27 10:12:52 +00:00
|
|
|
device_coordinator: TradfriDeviceDataUpdateCoordinator,
|
2021-09-20 12:33:50 +00:00
|
|
|
gateway_id: str,
|
2022-01-27 10:12:52 +00:00
|
|
|
api: Callable[[Command | list[Command]], Any],
|
2021-09-18 21:24:35 +00:00
|
|
|
) -> None:
|
2019-10-05 19:46:16 +00:00
|
|
|
"""Initialize a device."""
|
2022-01-27 10:12:52 +00:00
|
|
|
super().__init__(device_coordinator)
|
|
|
|
|
2019-10-05 19:46:16 +00:00
|
|
|
self._gateway_id = gateway_id
|
2021-10-27 16:27:16 +00:00
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
self._device: Device = device_coordinator.data
|
2019-10-05 19:46:16 +00:00
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
self._device_id = self._device.id
|
|
|
|
self._api = handle_error(api)
|
|
|
|
self._attr_name = self._device.name
|
2019-10-05 19:46:16 +00:00
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
self._attr_unique_id = f"{self._gateway_id}-{self._device.id}"
|
2019-10-05 19:46:16 +00:00
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
@abstractmethod
|
2019-10-05 19:46:16 +00:00
|
|
|
@callback
|
2022-01-27 10:12:52 +00:00
|
|
|
def _refresh(self) -> None:
|
|
|
|
"""Refresh device data."""
|
2021-11-08 17:41:25 +00:00
|
|
|
|
|
|
|
@callback
|
2022-01-27 10:12:52 +00:00
|
|
|
def _handle_coordinator_update(self) -> None:
|
2023-02-03 22:08:48 +00:00
|
|
|
"""Handle updated data from the coordinator.
|
2022-01-27 10:12:52 +00:00
|
|
|
|
|
|
|
Tests fails without this method.
|
|
|
|
"""
|
|
|
|
self._refresh()
|
|
|
|
super()._handle_coordinator_update()
|
2021-11-08 17:41:25 +00:00
|
|
|
|
2019-10-13 20:59:28 +00:00
|
|
|
@property
|
2021-09-20 12:33:50 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2019-10-13 20:59:28 +00:00
|
|
|
"""Return the device info."""
|
|
|
|
info = self._device.device_info
|
2021-10-22 10:31:12 +00:00
|
|
|
return DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, self._device.id)},
|
|
|
|
manufacturer=info.manufacturer,
|
|
|
|
model=info.model_number,
|
2022-02-10 07:19:59 +00:00
|
|
|
name=self._device.name,
|
2021-10-22 10:31:12 +00:00
|
|
|
sw_version=info.firmware_version,
|
|
|
|
via_device=(DOMAIN, self._gateway_id),
|
|
|
|
)
|
2021-11-06 15:46:51 +00:00
|
|
|
|
2022-01-27 10:12:52 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if entity is available."""
|
|
|
|
return cast(bool, self._device.reachable) and super().available
|