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 velbusaio.channels import Channel as VelbusChannel
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 Velbus entity."""
20 
21  _attr_should_poll: bool = False
22 
23  def __init__(self, channel: VelbusChannel) -> None:
24  """Initialize a Velbus entity."""
25  self._channel_channel = channel
26  self._attr_name_attr_name = channel.get_name()
27  self._attr_device_info_attr_device_info = DeviceInfo(
28  identifiers={
29  (DOMAIN, str(channel.get_module_address())),
30  },
31  manufacturer="Velleman",
32  model=channel.get_module_type_name(),
33  name=channel.get_full_name(),
34  sw_version=channel.get_module_sw_version(),
35  )
36  serial = channel.get_module_serial() or str(channel.get_module_address())
37  self._attr_unique_id_attr_unique_id = f"{serial}-{channel.get_channel_number()}"
38 
39  async def async_added_to_hass(self) -> None:
40  """Add listener for state changes."""
41  self._channel_channel.on_status_update(self._on_update_on_update)
42 
43  async def _on_update(self) -> None:
44  self.async_write_ha_stateasync_write_ha_state()
45 
46 
47 def api_call[_T: VelbusEntity, **_P](
48  func: Callable[Concatenate[_T, _P], Awaitable[None]],
49 ) -> Callable[Concatenate[_T, _P], Coroutine[Any, Any, None]]:
50  """Catch command exceptions."""
51 
52  @wraps(func)
53  async def cmd_wrapper(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> None:
54  """Wrap all command methods."""
55  try:
56  await func(self, *args, **kwargs)
57  except OSError as exc:
58  raise HomeAssistantError(
59  f"Could not execute {func.__name__} service for {self.name}"
60  ) from exc
61 
62  return cmd_wrapper
None __init__(self, VelbusChannel channel)
Definition: entity.py:23