Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Support for Velbus devices."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Awaitable, Callable, Coroutine
6 from functools import wraps
7 from typing import Any, Concatenate
8 
9 from duotecno.unit import BaseUnit
10 
11 from homeassistant.exceptions import HomeAssistantError
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity import Entity
14 
15 from .const import DOMAIN
16 
17 
19  """Representation of a Duotecno entity."""
20 
21  _attr_should_poll = False
22 
23  def __init__(self, unit: BaseUnit) -> None:
24  """Initialize a Duotecno entity."""
25  self._unit_unit = unit
26  self._attr_name_attr_name = unit.get_name()
27  self._attr_device_info_attr_device_info = DeviceInfo(
28  identifiers={
29  (DOMAIN, str(unit.get_node_address())),
30  },
31  manufacturer="Duotecno",
32  name=unit.get_node_name(),
33  )
34  self._attr_unique_id_attr_unique_id = f"{unit.get_node_address()}-{unit.get_number()}"
35 
36  async def async_added_to_hass(self) -> None:
37  """When added to hass."""
38  self._unit_unit.on_status_update(self._on_update_on_update)
39 
40  async def _on_update(self) -> None:
41  """When a unit has an update."""
42  self.async_write_ha_stateasync_write_ha_state()
43 
44  @property
45  def available(self) -> bool:
46  """Available state for the unit."""
47  return self._unit_unit.is_available()
48 
49 
50 def api_call[_T: DuotecnoEntity, **_P](
51  func: Callable[Concatenate[_T, _P], Awaitable[None]],
52 ) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]:
53  """Catch command exceptions."""
54 
55  @wraps(func)
56  async def cmd_wrapper(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
57  """Wrap all command methods."""
58  try:
59  await func(self, *args, **kwargs)
60  except OSError as exc:
61  raise HomeAssistantError(
62  f"Error calling {func.__name__} on entity {self.entity_id}"
63  ) from exc
64 
65  return cmd_wrapper