mirror of
https://github.com/home-assistant/core.git
synced 2026-07-11 00:29:29 +01:00
9d404b749a
* Initial commit coordinator * More coordinator implementation * More coordinator implementation * Allow integration reload * Move API calls to try/catch block * Move back fixture * Remove coordinator test file * Ensure unchanged file * Ensure unchanged conftest.py file * Remove coordinator key check * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Import RequestError * Move async_setup_platforms to end of setup_entry * Remove centralised handling of device data and device controllers * Remove platform_type argument * Remove exception * Remove the correct exception * Refactor coordinator error handling * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Remove platform type from base class * Remove timeout context manager * Refactor exception callback * Simplify starting device observation * Update test * Move observe start into update method * Remove await self.coordinator.async_request_refresh() * Refactor cover.py * Uncomment const.py * Add back extra_state_attributes * Update homeassistant/components/tradfri/coordinator.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Refactor switch platform * Expose switch state * Refactor sensor platform * Put back accidentally deleted code * Add set_hub_available * Apply suggestions from code review Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Fix tests for fan platform * Update homeassistant/components/tradfri/base_class.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update homeassistant/components/tradfri/base_class.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Fix non-working tests * Refresh sensor state * Remove commented line * Add group coordinator * Add groups during setup * Refactor light platform * Fix tests * Move outside of try...except * Remove error handler * Remove unneeded methods * Update sensor * Update .coveragerc * Move signal * Add signals for groups * Fix signal Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
96 lines
2.6 KiB
Python
96 lines
2.6 KiB
Python
"""Base class for IKEA TRADFRI."""
|
|
from __future__ import annotations
|
|
|
|
from abc import abstractmethod
|
|
from collections.abc import Callable
|
|
from functools import wraps
|
|
import logging
|
|
from typing import Any, cast
|
|
|
|
from pytradfri.command import Command
|
|
from pytradfri.device import Device
|
|
from pytradfri.error import PytradfriError
|
|
|
|
from homeassistant.core import callback
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from .const import DOMAIN
|
|
from .coordinator import TradfriDeviceDataUpdateCoordinator
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
def handle_error(
|
|
func: Callable[[Command | list[Command]], Any]
|
|
) -> Callable[[str], Any]:
|
|
"""Handle tradfri api call error."""
|
|
|
|
@wraps(func)
|
|
async def wrapper(command: Command | list[Command]) -> None:
|
|
"""Decorate api call."""
|
|
try:
|
|
await func(command)
|
|
except PytradfriError as err:
|
|
_LOGGER.error("Unable to execute command %s: %s", command, err)
|
|
|
|
return wrapper
|
|
|
|
|
|
class TradfriBaseEntity(CoordinatorEntity):
|
|
"""Base Tradfri device."""
|
|
|
|
coordinator: TradfriDeviceDataUpdateCoordinator
|
|
|
|
def __init__(
|
|
self,
|
|
device_coordinator: TradfriDeviceDataUpdateCoordinator,
|
|
gateway_id: str,
|
|
api: Callable[[Command | list[Command]], Any],
|
|
) -> None:
|
|
"""Initialize a device."""
|
|
super().__init__(device_coordinator)
|
|
|
|
self._gateway_id = gateway_id
|
|
|
|
self._device: Device = device_coordinator.data
|
|
|
|
self._device_id = self._device.id
|
|
self._api = handle_error(api)
|
|
self._attr_name = self._device.name
|
|
|
|
self._attr_unique_id = f"{self._gateway_id}-{self._device.id}"
|
|
|
|
@abstractmethod
|
|
@callback
|
|
def _refresh(self) -> None:
|
|
"""Refresh device data."""
|
|
|
|
@callback
|
|
def _handle_coordinator_update(self) -> None:
|
|
"""
|
|
Handle updated data from the coordinator.
|
|
|
|
Tests fails without this method.
|
|
"""
|
|
self._refresh()
|
|
super()._handle_coordinator_update()
|
|
|
|
@property
|
|
def device_info(self) -> DeviceInfo:
|
|
"""Return the device info."""
|
|
info = self._device.device_info
|
|
return DeviceInfo(
|
|
identifiers={(DOMAIN, self._device.id)},
|
|
manufacturer=info.manufacturer,
|
|
model=info.model_number,
|
|
name=self._attr_name,
|
|
sw_version=info.firmware_version,
|
|
via_device=(DOMAIN, self._gateway_id),
|
|
)
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
"""Return if entity is available."""
|
|
return cast(bool, self._device.reachable) and super().available
|