2019-03-24 15:16:50 +00:00
|
|
|
"""Axis network device abstraction."""
|
|
|
|
|
2024-03-02 16:32:51 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-28 09:41:03 +00:00
|
|
|
from typing import Any
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2019-10-12 19:50:30 +00:00
|
|
|
import axis
|
2024-04-24 18:47:22 +00:00
|
|
|
|
2022-02-02 09:36:04 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2024-01-02 19:55:59 +00:00
|
|
|
from homeassistant.core import Event, HomeAssistant, callback
|
2021-10-20 11:03:52 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2024-03-12 20:01:20 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, format_mac
|
2019-03-24 15:16:50 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
|
2024-03-09 18:00:25 +00:00
|
|
|
from ..const import ATTR_MANUFACTURER, DOMAIN as AXIS_DOMAIN
|
|
|
|
from .config import AxisConfig
|
2024-03-23 20:52:00 +00:00
|
|
|
from .entity_loader import AxisEntityLoader
|
2024-04-24 18:47:22 +00:00
|
|
|
from .event_source import AxisEventSource
|
2019-03-24 15:16:50 +00:00
|
|
|
|
|
|
|
|
2024-03-02 16:32:51 +00:00
|
|
|
class AxisHub:
|
2019-03-24 15:16:50 +00:00
|
|
|
"""Manages a Axis device."""
|
|
|
|
|
2022-07-30 09:04:31 +00:00
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistant, config_entry: ConfigEntry, api: axis.AxisDevice
|
|
|
|
) -> None:
|
2019-03-24 15:16:50 +00:00
|
|
|
"""Initialize the device."""
|
|
|
|
self.hass = hass
|
2024-03-09 18:00:25 +00:00
|
|
|
self.config = AxisConfig.from_config_entry(config_entry)
|
2024-03-23 20:52:00 +00:00
|
|
|
self.entity_loader = AxisEntityLoader(self)
|
2024-04-24 18:47:22 +00:00
|
|
|
self.event_source = AxisEventSource(hass, config_entry, api)
|
2022-07-28 09:41:03 +00:00
|
|
|
self.api = api
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2022-07-28 09:41:03 +00:00
|
|
|
self.fw_version = api.vapix.firmware_version
|
|
|
|
self.product_type = api.vapix.product_type
|
2024-03-12 20:01:20 +00:00
|
|
|
self.unique_id = format_mac(api.vapix.serial_number)
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2023-07-23 15:56:58 +00:00
|
|
|
self.additional_diagnostics: dict[str, Any] = {}
|
|
|
|
|
2024-03-02 16:32:51 +00:00
|
|
|
@callback
|
|
|
|
@staticmethod
|
|
|
|
def get_hub(hass: HomeAssistant, config_entry: ConfigEntry) -> AxisHub:
|
|
|
|
"""Get Axis hub from config entry."""
|
|
|
|
hub: AxisHub = hass.data[AXIS_DOMAIN][config_entry.entry_id]
|
|
|
|
return hub
|
|
|
|
|
2024-04-24 18:47:22 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Connection state to the device."""
|
|
|
|
return self.event_source.available
|
|
|
|
|
2020-10-16 16:54:48 +00:00
|
|
|
# Signals
|
|
|
|
|
2020-05-14 08:49:27 +00:00
|
|
|
@property
|
2024-01-02 19:55:59 +00:00
|
|
|
def signal_reachable(self) -> str:
|
2020-05-14 08:49:27 +00:00
|
|
|
"""Device specific event to signal a change in connection status."""
|
2024-04-24 18:47:22 +00:00
|
|
|
return self.event_source.signal_reachable
|
2020-05-14 08:49:27 +00:00
|
|
|
|
|
|
|
@property
|
2024-01-02 19:55:59 +00:00
|
|
|
def signal_new_address(self) -> str:
|
2020-05-14 08:49:27 +00:00
|
|
|
"""Device specific event to signal a change in device address."""
|
2024-03-12 20:01:20 +00:00
|
|
|
return f"axis_new_address_{self.config.entry.entry_id}"
|
2020-05-14 08:49:27 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-02-02 09:36:04 +00:00
|
|
|
async def async_new_address_callback(
|
2024-03-02 16:32:51 +00:00
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
2022-02-02 09:36:04 +00:00
|
|
|
) -> None:
|
2020-05-14 08:49:27 +00:00
|
|
|
"""Handle signals of device getting new address.
|
|
|
|
|
2020-05-25 21:13:34 +00:00
|
|
|
Called when config entry is updated.
|
2020-05-14 08:49:27 +00:00
|
|
|
This is a static method because a class method (bound method),
|
2023-02-03 10:37:16 +00:00
|
|
|
cannot be used with weak references.
|
2020-05-14 08:49:27 +00:00
|
|
|
"""
|
2024-03-02 16:32:51 +00:00
|
|
|
hub = AxisHub.get_hub(hass, config_entry)
|
2024-03-09 18:00:25 +00:00
|
|
|
hub.config = AxisConfig.from_config_entry(config_entry)
|
2024-04-24 18:47:22 +00:00
|
|
|
hub.event_source.config_entry = config_entry
|
2024-03-09 18:00:25 +00:00
|
|
|
hub.api.config.host = hub.config.host
|
2024-03-02 16:32:51 +00:00
|
|
|
async_dispatcher_send(hass, hub.signal_new_address)
|
2020-05-14 08:49:27 +00:00
|
|
|
|
2022-07-30 09:04:31 +00:00
|
|
|
async def async_update_device_registry(self) -> None:
|
2019-03-27 17:25:01 +00:00
|
|
|
"""Update device registry."""
|
2021-10-20 11:03:52 +00:00
|
|
|
device_registry = dr.async_get(self.hass)
|
2019-03-27 17:25:01 +00:00
|
|
|
device_registry.async_get_or_create(
|
2024-03-09 18:00:25 +00:00
|
|
|
config_entry_id=self.config.entry.entry_id,
|
2021-10-22 09:09:50 +00:00
|
|
|
configuration_url=self.api.config.url,
|
2024-03-12 20:01:20 +00:00
|
|
|
connections={(CONNECTION_NETWORK_MAC, self.unique_id)},
|
|
|
|
identifiers={(AXIS_DOMAIN, self.unique_id)},
|
2020-05-14 08:49:27 +00:00
|
|
|
manufacturer=ATTR_MANUFACTURER,
|
2024-03-09 18:00:25 +00:00
|
|
|
model=f"{self.config.model} {self.product_type}",
|
|
|
|
name=self.config.name,
|
2019-07-31 19:25:30 +00:00
|
|
|
sw_version=self.fw_version,
|
2019-03-27 17:25:01 +00:00
|
|
|
)
|
|
|
|
|
2020-10-16 16:54:48 +00:00
|
|
|
# Setup and teardown methods
|
|
|
|
|
2024-03-03 17:42:44 +00:00
|
|
|
@callback
|
|
|
|
def setup(self) -> None:
|
2022-07-28 09:41:03 +00:00
|
|
|
"""Set up the device events."""
|
2024-03-23 20:52:00 +00:00
|
|
|
self.entity_loader.initialize_platforms()
|
2024-04-24 18:47:22 +00:00
|
|
|
self.event_source.setup()
|
2020-05-25 21:13:34 +00:00
|
|
|
|
2024-01-02 19:55:59 +00:00
|
|
|
async def shutdown(self, event: Event) -> None:
|
2019-03-24 15:16:50 +00:00
|
|
|
"""Stop the event stream."""
|
2024-04-24 18:47:22 +00:00
|
|
|
self.event_source.teardown()
|
2019-03-24 15:16:50 +00:00
|
|
|
|
2024-03-03 17:42:44 +00:00
|
|
|
@callback
|
|
|
|
def teardown(self) -> None:
|
2019-04-15 22:06:45 +00:00
|
|
|
"""Reset this device to default state."""
|
2024-04-24 18:47:22 +00:00
|
|
|
self.event_source.teardown()
|