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