Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base classes for ONVIF entities."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
6 from homeassistant.helpers.entity import Entity
7 
8 from .const import DOMAIN
9 from .device import ONVIFDevice
10 
11 
13  """Base class common to all ONVIF entities."""
14 
15  def __init__(self, device: ONVIFDevice) -> None:
16  """Initialize the ONVIF entity."""
17  self.device: ONVIFDevice = device
18 
19  @property
20  def available(self):
21  """Return True if device is available."""
22  return self.device.available
23 
24  @property
25  def mac_or_serial(self) -> str:
26  """Return MAC or serial, for unique_id generation.
27 
28  MAC address is not always available, and given the number
29  of non-conformant ONVIF devices we have historically supported,
30  we cannot guarantee serial number either. Due to this, we have
31  adopted an either/or approach in the config entry setup, and can
32  guarantee that one or the other will be populated.
33  See: https://github.com/home-assistant/core/issues/35883
34  """
35  return (
36  self.device.info.mac or self.device.info.serial_number # type:ignore[return-value]
37  )
38 
39  @property
40  def device_info(self) -> DeviceInfo:
41  """Return a device description for device registry."""
42  connections: set[tuple[str, str]] = set()
43  if self.device.info.mac:
44  connections = {(CONNECTION_NETWORK_MAC, self.device.info.mac)}
45  return DeviceInfo(
46  connections=connections,
47  identifiers={(DOMAIN, self.mac_or_serialmac_or_serial)},
48  manufacturer=self.device.info.manufacturer,
49  model=self.device.info.model,
50  name=self.device.name,
51  sw_version=self.device.info.fw_version,
52  configuration_url=f"http://{self.device.host}:{self.device.port}",
53  )
None __init__(self, ONVIFDevice device)
Definition: entity.py:15