Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """BaseEntity to support multiple LinkPlay platforms."""
2 
3 from collections.abc import Callable, Coroutine
4 from typing import Any, Concatenate
5 
6 from linkplay.bridge import LinkPlayBridge
7 
8 from homeassistant.exceptions import HomeAssistantError
9 from homeassistant.helpers import device_registry as dr
10 from homeassistant.helpers.entity import Entity
11 
12 from . import DOMAIN, LinkPlayRequestException
13 from .utils import MANUFACTURER_GENERIC, get_info_from_project
14 
15 
16 def exception_wrap[_LinkPlayEntityT: LinkPlayBaseEntity, **_P, _R](
17  func: Callable[Concatenate[_LinkPlayEntityT, _P], Coroutine[Any, Any, _R]],
18 ) -> Callable[Concatenate[_LinkPlayEntityT, _P], Coroutine[Any, Any, _R]]:
19  """Define a wrapper to catch exceptions and raise HomeAssistant errors."""
20 
21  async def _wrap(self: _LinkPlayEntityT, *args: _P.args, **kwargs: _P.kwargs) -> _R:
22  try:
23  return await func(self, *args, **kwargs)
24  except LinkPlayRequestException as err:
25  raise HomeAssistantError(
26  f"Exception occurred when communicating with API {func}: {err}"
27  ) from err
28 
29  return _wrap
30 
31 
33  """Representation of a LinkPlay base entity."""
34 
35  _attr_has_entity_name = True
36 
37  def __init__(self, bridge: LinkPlayBridge) -> None:
38  """Initialize the LinkPlay media player."""
39 
40  self._bridge_bridge = bridge
41 
42  manufacturer, model = get_info_from_project(bridge.device.properties["project"])
43  model_id = None
44  if model != MANUFACTURER_GENERIC:
45  model_id = bridge.device.properties["project"]
46 
47  self._attr_device_info_attr_device_info = dr.DeviceInfo(
48  configuration_url=bridge.endpoint,
49  connections={(dr.CONNECTION_NETWORK_MAC, bridge.device.properties["MAC"])},
50  hw_version=bridge.device.properties["hardware"],
51  identifiers={(DOMAIN, bridge.device.uuid)},
52  manufacturer=manufacturer,
53  model=model,
54  model_id=model_id,
55  name=bridge.device.name,
56  sw_version=bridge.device.properties["firmware"],
57  )
None __init__(self, LinkPlayBridge bridge)
Definition: entity.py:37
tuple[str, str] get_info_from_project(str project)
Definition: utils.py:75