Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Represent the Netgear router and its devices."""
2 
3 from __future__ import annotations
4 
5 from abc import abstractmethod
6 
7 from homeassistant.const import CONF_HOST
8 from homeassistant.core import callback
9 from homeassistant.helpers import device_registry as dr
10 from homeassistant.helpers.device_registry import DeviceInfo
11 from homeassistant.helpers.entity import Entity
13  CoordinatorEntity,
14  DataUpdateCoordinator,
15 )
16 
17 from .const import DOMAIN
18 from .router import NetgearRouter
19 
20 
22  """Base class for a device connected to a Netgear router."""
23 
24  _attr_has_entity_name = True
25 
26  def __init__(
27  self, coordinator: DataUpdateCoordinator, router: NetgearRouter, device: dict
28  ) -> None:
29  """Initialize a Netgear device."""
30  super().__init__(coordinator)
31  self._router_router = router
32  self._device_device = device
33  self._mac_mac = device["mac"]
34  self._device_name_device_name = self.get_device_nameget_device_name()
35  self._active_active = device["active"]
36  self._attr_unique_id_attr_unique_id = self._mac_mac
37  self._attr_device_info_attr_device_info = DeviceInfo(
38  connections={(dr.CONNECTION_NETWORK_MAC, self._mac_mac)},
39  default_name=self._device_name_device_name,
40  default_model=device["device_model"],
41  via_device=(DOMAIN, router.unique_id),
42  )
43 
44  def get_device_name(self):
45  """Return the name of the given device or the MAC if we don't know."""
46  name = self._device_device["name"]
47  if not name or name == "--":
48  name = self._mac_mac
49 
50  return name
51 
52  @abstractmethod
53  @callback
54  def async_update_device(self) -> None:
55  """Update the Netgear device."""
56 
57  @callback
58  def _handle_coordinator_update(self) -> None:
59  """Handle updated data from the coordinator."""
60  self.async_update_deviceasync_update_device()
62 
63 
65  """Base class for a Netgear router entity without coordinator."""
66 
67  _attr_has_entity_name = True
68 
69  def __init__(self, router: NetgearRouter) -> None:
70  """Initialize a Netgear device."""
71  self._router_router = router
72 
73  configuration_url = None
74  if host := router.entry.data[CONF_HOST]:
75  configuration_url = f"http://{host}/"
76 
77  self._attr_unique_id_attr_unique_id = router.serial_number
78  self._attr_device_info_attr_device_info = DeviceInfo(
79  identifiers={(DOMAIN, router.unique_id)},
80  manufacturer="Netgear",
81  name=router.device_name,
82  model=router.model,
83  sw_version=router.firmware_version,
84  hw_version=router.hardware_version,
85  configuration_url=configuration_url,
86  )
87 
88 
90  """Base class for a Netgear router entity."""
91 
92  def __init__(
93  self, coordinator: DataUpdateCoordinator, router: NetgearRouter
94  ) -> None:
95  """Initialize a Netgear device."""
96  CoordinatorEntity.__init__(self, coordinator)
97  NetgearRouterEntity.__init__(self, router)
98 
99  @abstractmethod
100  @callback
101  def async_update_device(self) -> None:
102  """Update the Netgear device."""
103 
104  @callback
105  def _handle_coordinator_update(self) -> None:
106  """Handle updated data from the coordinator."""
107  self.async_update_deviceasync_update_device()
None __init__(self, DataUpdateCoordinator coordinator, NetgearRouter router, dict device)
Definition: entity.py:28
None __init__(self, DataUpdateCoordinator coordinator, NetgearRouter router)
Definition: entity.py:94