Bump axis to v46 (#85431)
This commit is contained in:
parent
54168c9bdb
commit
1cdd535f21
10 changed files with 111 additions and 127 deletions
|
@ -3,21 +3,7 @@ from __future__ import annotations
|
|||
|
||||
from datetime import timedelta
|
||||
|
||||
from axis.event_stream import (
|
||||
CLASS_INPUT,
|
||||
CLASS_LIGHT,
|
||||
CLASS_MOTION,
|
||||
CLASS_OUTPUT,
|
||||
CLASS_PTZ,
|
||||
CLASS_SOUND,
|
||||
AxisBinaryEvent,
|
||||
AxisEvent,
|
||||
FenceGuard,
|
||||
LoiteringGuard,
|
||||
MotionGuard,
|
||||
ObjectAnalytics,
|
||||
Vmd4,
|
||||
)
|
||||
from axis.models.event import Event, EventGroup, EventOperation, EventTopic
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
|
@ -25,7 +11,6 @@ from homeassistant.components.binary_sensor import (
|
|||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
@ -35,12 +20,27 @@ from .const import DOMAIN as AXIS_DOMAIN
|
|||
from .device import AxisNetworkDevice
|
||||
|
||||
DEVICE_CLASS = {
|
||||
CLASS_INPUT: BinarySensorDeviceClass.CONNECTIVITY,
|
||||
CLASS_LIGHT: BinarySensorDeviceClass.LIGHT,
|
||||
CLASS_MOTION: BinarySensorDeviceClass.MOTION,
|
||||
CLASS_SOUND: BinarySensorDeviceClass.SOUND,
|
||||
EventGroup.INPUT: BinarySensorDeviceClass.CONNECTIVITY,
|
||||
EventGroup.LIGHT: BinarySensorDeviceClass.LIGHT,
|
||||
EventGroup.MOTION: BinarySensorDeviceClass.MOTION,
|
||||
EventGroup.SOUND: BinarySensorDeviceClass.SOUND,
|
||||
}
|
||||
|
||||
EVENT_TOPICS = (
|
||||
EventTopic.DAY_NIGHT_VISION,
|
||||
EventTopic.FENCE_GUARD,
|
||||
EventTopic.LOITERING_GUARD,
|
||||
EventTopic.MOTION_DETECTION,
|
||||
EventTopic.MOTION_DETECTION_3,
|
||||
EventTopic.MOTION_DETECTION_4,
|
||||
EventTopic.MOTION_GUARD,
|
||||
EventTopic.OBJECT_ANALYTICS,
|
||||
EventTopic.PIR,
|
||||
EventTopic.PORT_INPUT,
|
||||
EventTopic.PORT_SUPERVISED_INPUT,
|
||||
EventTopic.SOUND_TRIGGER_LEVEL,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
|
@ -51,26 +51,21 @@ async def async_setup_entry(
|
|||
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.unique_id]
|
||||
|
||||
@callback
|
||||
def async_add_sensor(event_id):
|
||||
"""Add binary sensor from Axis device."""
|
||||
event: AxisEvent = device.api.event[event_id]
|
||||
def async_create_entity(event: Event) -> None:
|
||||
"""Create Axis binary sensor entity."""
|
||||
async_add_entities([AxisBinarySensor(event, device)])
|
||||
|
||||
if event.group not in (CLASS_OUTPUT, CLASS_PTZ) and not (
|
||||
event.group == CLASS_LIGHT and event.type == "Light"
|
||||
):
|
||||
async_add_entities([AxisBinarySensor(event, device)])
|
||||
|
||||
config_entry.async_on_unload(
|
||||
async_dispatcher_connect(hass, device.signal_new_event, async_add_sensor)
|
||||
device.api.event.subscribe(
|
||||
async_create_entity,
|
||||
topic_filter=EVENT_TOPICS,
|
||||
operation_filter=EventOperation.INITIALIZED,
|
||||
)
|
||||
|
||||
|
||||
class AxisBinarySensor(AxisEventBase, BinarySensorEntity):
|
||||
"""Representation of a binary Axis event."""
|
||||
|
||||
event: AxisBinaryEvent
|
||||
|
||||
def __init__(self, event: AxisEvent, device: AxisNetworkDevice) -> None:
|
||||
def __init__(self, event: Event, device: AxisNetworkDevice) -> None:
|
||||
"""Initialize the Axis binary sensor."""
|
||||
super().__init__(event, device)
|
||||
self.cancel_scheduled_update = None
|
||||
|
@ -110,26 +105,27 @@ class AxisBinarySensor(AxisEventBase, BinarySensorEntity):
|
|||
def name(self) -> str | None:
|
||||
"""Return the name of the event."""
|
||||
if (
|
||||
self.event.group == CLASS_INPUT
|
||||
self.event.group == EventGroup.INPUT
|
||||
and self.event.id in self.device.api.vapix.ports
|
||||
and self.device.api.vapix.ports[self.event.id].name
|
||||
):
|
||||
return self.device.api.vapix.ports[self.event.id].name
|
||||
|
||||
if self.event.group == CLASS_MOTION:
|
||||
if self.event.group == EventGroup.MOTION:
|
||||
|
||||
for event_class, event_data in (
|
||||
(FenceGuard, self.device.api.vapix.fence_guard),
|
||||
(LoiteringGuard, self.device.api.vapix.loitering_guard),
|
||||
(MotionGuard, self.device.api.vapix.motion_guard),
|
||||
(ObjectAnalytics, self.device.api.vapix.object_analytics),
|
||||
(Vmd4, self.device.api.vapix.vmd4),
|
||||
for event_topic, event_data in (
|
||||
(EventTopic.FENCE_GUARD, self.device.api.vapix.fence_guard),
|
||||
(EventTopic.LOITERING_GUARD, self.device.api.vapix.loitering_guard),
|
||||
(EventTopic.MOTION_GUARD, self.device.api.vapix.motion_guard),
|
||||
(EventTopic.OBJECT_ANALYTICS, self.device.api.vapix.object_analytics),
|
||||
(EventTopic.MOTION_DETECTION_4, self.device.api.vapix.vmd4),
|
||||
):
|
||||
|
||||
if (
|
||||
isinstance(self.event, event_class)
|
||||
self.event.topic_base == event_topic
|
||||
and event_data
|
||||
and self.event.id in event_data
|
||||
):
|
||||
return f"{self.event.type} {event_data[self.event.id].name}"
|
||||
return f"{self.event_type} {event_data[self.event.id].name}"
|
||||
|
||||
return self._attr_name
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue