Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """AVM FRITZ!Tools entities."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from fritzconnection.lib.fritzstatus import FritzStatus
10 
11 from homeassistant.helpers import device_registry as dr
12 from homeassistant.helpers.device_registry import DeviceInfo
13 from homeassistant.helpers.entity import EntityDescription
14 from homeassistant.helpers.update_coordinator import CoordinatorEntity
15 
16 from .const import DEFAULT_DEVICE_NAME, DOMAIN
17 from .coordinator import AvmWrapper, FritzDevice
18 
19 
21  """Entity base class for a device connected to a FRITZ!Box device."""
22 
23  def __init__(self, avm_wrapper: AvmWrapper, device: FritzDevice) -> None:
24  """Initialize a FRITZ!Box device."""
25  super().__init__(avm_wrapper)
26  self._avm_wrapper_avm_wrapper = avm_wrapper
27  self._mac: str = device.mac_address
28  self._name: str = device.hostname or DEFAULT_DEVICE_NAME
29 
30  @property
31  def name(self) -> str:
32  """Return device name."""
33  return self._name
34 
35  @property
36  def ip_address(self) -> str | None:
37  """Return the primary ip address of the device."""
38  if self._mac:
39  return self._avm_wrapper_avm_wrapper.devices[self._mac].ip_address
40  return None
41 
42  @property
43  def mac_address(self) -> str:
44  """Return the mac address of the device."""
45  return self._mac
46 
47  @property
48  def hostname(self) -> str | None:
49  """Return hostname of the device."""
50  if self._mac:
51  return self._avm_wrapper_avm_wrapper.devices[self._mac].hostname
52  return None
53 
54  async def async_process_update(self) -> None:
55  """Update device."""
56  raise NotImplementedError
57 
58  async def async_on_demand_update(self) -> None:
59  """Update state."""
60  await self.async_process_updateasync_process_update()
61  self.async_write_ha_state()
62 
63 
65  """Fritz host entity base class."""
66 
67  def __init__(self, avm_wrapper: AvmWrapper, device_name: str) -> None:
68  """Init device info class."""
69  self._avm_wrapper_avm_wrapper = avm_wrapper
70  self._device_name_device_name = device_name
71 
72  @property
73  def mac_address(self) -> str:
74  """Return the mac address of the main device."""
75  return self._avm_wrapper_avm_wrapper.mac
76 
77  @property
78  def device_info(self) -> DeviceInfo:
79  """Return the device information."""
80  return DeviceInfo(
81  configuration_url=f"http://{self._avm_wrapper.host}",
82  connections={(dr.CONNECTION_NETWORK_MAC, self.mac_addressmac_address)},
83  identifiers={(DOMAIN, self._avm_wrapper_avm_wrapper.unique_id)},
84  manufacturer="AVM",
85  model=self._avm_wrapper_avm_wrapper.model,
86  name=self._device_name_device_name,
87  sw_version=self._avm_wrapper_avm_wrapper.current_firmware,
88  )
89 
90 
91 @dataclass(frozen=True, kw_only=True)
93  """Fritz entity base description."""
94 
95  value_fn: Callable[[FritzStatus, Any], Any] | None
96 
97 
99  """Fritz host coordinator entity base class."""
100 
101  entity_description: FritzEntityDescription
102  _attr_has_entity_name = True
103 
104  def __init__(
105  self,
106  avm_wrapper: AvmWrapper,
107  device_name: str,
108  description: FritzEntityDescription,
109  ) -> None:
110  """Init device info class."""
111  super().__init__(avm_wrapper)
112  self.entity_descriptionentity_description = description
113  self._device_name_device_name = device_name
114  self._attr_unique_id_attr_unique_id = f"{avm_wrapper.unique_id}-{description.key}"
115 
116  async def async_added_to_hass(self) -> None:
117  """When entity is added to hass."""
118  await super().async_added_to_hass()
119  if self.entity_descriptionentity_description.value_fn is not None:
120  self.async_on_remove(
121  await self.coordinator.async_register_entity_updates(
122  self.entity_descriptionentity_description.key, self.entity_descriptionentity_description.value_fn
123  )
124  )
125 
126  @property
127  def device_info(self) -> DeviceInfo:
128  """Return the device information."""
129  return DeviceInfo(
130  configuration_url=f"http://{self.coordinator.host}",
131  connections={(dr.CONNECTION_NETWORK_MAC, self.coordinator.mac)},
132  identifiers={(DOMAIN, self.coordinator.unique_id)},
133  manufacturer="AVM",
134  model=self.coordinator.model,
135  name=self._device_name_device_name,
136  sw_version=self.coordinator.current_firmware,
137  )
Callable[[], None] async_register_entity_updates(self, str key, Callable[[FritzStatus, StateType], Any] update_fn)
Definition: coordinator.py:282
None __init__(self, AvmWrapper avm_wrapper, str device_name, FritzEntityDescription description)
Definition: entity.py:109
None __init__(self, AvmWrapper avm_wrapper, str device_name)
Definition: entity.py:67
None __init__(self, AvmWrapper avm_wrapper, FritzDevice device)
Definition: entity.py:23