Home Assistant Unofficial Reference 2024.12.1
hub.py
Go to the documentation of this file.
1 """Axis network device abstraction."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING, Any
6 
7 import axis
8 
9 from homeassistant.core import Event, HomeAssistant, callback
10 from homeassistant.helpers import device_registry as dr
11 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac
12 from homeassistant.helpers.dispatcher import async_dispatcher_send
13 
14 from ..const import ATTR_MANUFACTURER, DOMAIN as AXIS_DOMAIN
15 from .config import AxisConfig
16 from .entity_loader import AxisEntityLoader
17 from .event_source import AxisEventSource
18 
19 if TYPE_CHECKING:
20  from .. import AxisConfigEntry
21 
22 
23 class AxisHub:
24  """Manages a Axis device."""
25 
26  def __init__(
27  self, hass: HomeAssistant, config_entry: AxisConfigEntry, api: axis.AxisDevice
28  ) -> None:
29  """Initialize the device."""
30  self.hasshass = hass
31  self.configconfig = AxisConfig.from_config_entry(config_entry)
32  self.entity_loaderentity_loader = AxisEntityLoader(self)
33  self.event_sourceevent_source = AxisEventSource(hass, config_entry, api)
34  self.apiapi = api
35 
36  self.fw_versionfw_version = api.vapix.firmware_version
37  self.product_typeproduct_type = api.vapix.product_type
38  self.unique_idunique_id = format_mac(api.vapix.serial_number)
39 
40  self.additional_diagnostics: dict[str, Any] = {}
41 
42  @property
43  def available(self) -> bool:
44  """Connection state to the device."""
45  return self.event_sourceevent_source.available
46 
47  # Signals
48 
49  @property
50  def signal_reachable(self) -> str:
51  """Device specific event to signal a change in connection status."""
52  return self.event_sourceevent_source.signal_reachable
53 
54  @property
55  def signal_new_address(self) -> str:
56  """Device specific event to signal a change in device address."""
57  return f"axis_new_address_{self.config.entry.entry_id}"
58 
59  @staticmethod
61  hass: HomeAssistant, config_entry: AxisConfigEntry
62  ) -> None:
63  """Handle signals of device getting new address.
64 
65  Called when config entry is updated.
66  This is a static method because a class method (bound method),
67  cannot be used with weak references.
68  """
69  hub = config_entry.runtime_data
70  hub.config = AxisConfig.from_config_entry(config_entry)
71  hub.event_source.config_entry = config_entry
72  hub.api.config.host = hub.config.host
73  async_dispatcher_send(hass, hub.signal_new_address)
74 
75  async def async_update_device_registry(self) -> None:
76  """Update device registry."""
77  device_registry = dr.async_get(self.hasshass)
78  device_registry.async_get_or_create(
79  config_entry_id=self.configconfig.entry.entry_id,
80  configuration_url=self.apiapi.config.url,
81  connections={(CONNECTION_NETWORK_MAC, self.unique_idunique_id)},
82  identifiers={(AXIS_DOMAIN, self.unique_idunique_id)},
83  manufacturer=ATTR_MANUFACTURER,
84  model=f"{self.config.model} {self.product_type}",
85  name=self.configconfig.name,
86  sw_version=self.fw_versionfw_version,
87  )
88 
89  # Setup and teardown methods
90 
91  @callback
92  def setup(self) -> None:
93  """Set up the device events."""
94  self.entity_loaderentity_loader.initialize_platforms()
95  self.event_sourceevent_source.setup()
96 
97  async def shutdown(self, event: Event) -> None:
98  """Stop the event stream."""
99  self.event_sourceevent_source.teardown()
100 
101  @callback
102  def teardown(self) -> None:
103  """Reset this device to default state."""
104  self.event_sourceevent_source.teardown()
None __init__(self, HomeAssistant hass, AxisConfigEntry config_entry, axis.AxisDevice api)
Definition: hub.py:28
None shutdown(self, Event event)
Definition: hub.py:97
None async_new_address_callback(HomeAssistant hass, AxisConfigEntry config_entry)
Definition: hub.py:62
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193