Files
homeassistant-core/homeassistant/components/axis/binary_sensor.py
T

129 lines
4.3 KiB
Python
Raw Normal View History

2019-02-13 21:21:14 +01:00
"""Support for Axis binary sensors."""
2022-07-30 11:04:31 +02:00
from __future__ import annotations
2023-01-18 17:27:13 +01:00
from collections.abc import Callable
2017-05-12 17:51:54 +02:00
from datetime import timedelta
2023-01-09 12:43:40 +01:00
from axis.models.event import Event, EventGroup, EventOperation, EventTopic
2019-05-20 07:45:31 +02:00
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2018-10-29 06:52:30 +01:00
from homeassistant.helpers.event import async_track_point_in_utc_time
2017-05-12 17:51:54 +02:00
from homeassistant.util.dt import utcnow
2019-05-20 07:45:31 +02:00
from .const import DOMAIN as AXIS_DOMAIN
2022-07-30 11:04:31 +02:00
from .device import AxisNetworkDevice
2023-01-18 17:27:13 +01:00
from .entity import AxisEventEntity
2017-05-12 17:51:54 +02:00
DEVICE_CLASS = {
2023-01-09 12:43:40 +01:00
EventGroup.INPUT: BinarySensorDeviceClass.CONNECTIVITY,
EventGroup.LIGHT: BinarySensorDeviceClass.LIGHT,
EventGroup.MOTION: BinarySensorDeviceClass.MOTION,
EventGroup.SOUND: BinarySensorDeviceClass.SOUND,
}
2023-01-09 12:43:40 +01:00
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,
)
2017-05-12 17:51:54 +02:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
2019-03-24 16:16:50 +01:00
"""Set up a Axis binary sensor."""
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.entry_id]
2019-03-24 16:16:50 +01:00
@callback
2023-01-09 12:43:40 +01:00
def async_create_entity(event: Event) -> None:
"""Create Axis binary sensor entity."""
async_add_entities([AxisBinarySensor(event, device)])
2019-05-20 07:45:31 +02:00
2023-01-09 12:43:40 +01:00
device.api.event.subscribe(
async_create_entity,
topic_filter=EVENT_TOPICS,
operation_filter=EventOperation.INITIALIZED,
2019-07-31 12:25:30 -07:00
)
2017-05-12 17:51:54 +02:00
2023-01-18 17:27:13 +01:00
class AxisBinarySensor(AxisEventEntity, BinarySensorEntity):
2017-05-12 17:51:54 +02:00
"""Representation of a binary Axis event."""
2023-01-09 12:43:40 +01:00
def __init__(self, event: Event, device: AxisNetworkDevice) -> None:
2018-01-21 07:35:38 +01:00
"""Initialize the Axis binary sensor."""
2019-05-20 07:45:31 +02:00
super().__init__(event, device)
2023-01-18 17:27:13 +01:00
self.cancel_scheduled_update: Callable[[], None] | None = None
2023-01-18 17:27:13 +01:00
self._attr_device_class = DEVICE_CLASS.get(event.group)
2023-01-02 18:14:14 +01:00
self._attr_is_on = event.is_tripped
2023-01-18 17:27:13 +01:00
self._set_name(event)
2023-01-18 17:27:13 +01:00
@callback
def async_event_callback(self, event: Event) -> None:
"""Update the sensor's state, if needed."""
self._attr_is_on = event.is_tripped
2019-03-24 16:16:50 +01:00
2020-05-14 10:49:27 +02:00
@callback
def scheduled_update(now):
"""Timer callback for sensor update."""
self.cancel_scheduled_update = None
self.async_write_ha_state()
2018-10-29 06:52:30 +01:00
2020-05-14 10:49:27 +02:00
if self.cancel_scheduled_update is not None:
self.cancel_scheduled_update()
self.cancel_scheduled_update = None
2023-01-18 17:27:13 +01:00
if self.is_on or self.device.option_trigger_time == 0:
2020-04-01 14:19:51 -07:00
self.async_write_ha_state()
2019-03-24 16:16:50 +01:00
return
2018-10-29 06:52:30 +01:00
2020-05-14 10:49:27 +02:00
self.cancel_scheduled_update = async_track_point_in_utc_time(
self.hass,
scheduled_update,
utcnow() + timedelta(seconds=self.device.option_trigger_time),
2019-07-31 12:25:30 -07:00
)
2017-05-12 17:51:54 +02:00
2023-01-18 17:27:13 +01:00
@callback
def _set_name(self, event: Event) -> None:
"""Set binary sensor name."""
2019-07-31 12:25:30 -07:00
if (
2023-01-18 17:27:13 +01:00
event.group == EventGroup.INPUT
and event.id in self.device.api.vapix.ports
and self.device.api.vapix.ports[event.id].name
2019-07-31 12:25:30 -07:00
):
2023-01-18 17:27:13 +01:00
self._attr_name = self.device.api.vapix.ports[event.id].name
2017-05-12 17:51:54 +02:00
2023-01-18 17:27:13 +01:00
elif event.group == EventGroup.MOTION:
2023-01-09 12:43:40 +01:00
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 (
2023-01-18 17:27:13 +01:00
event.topic_base == event_topic
and event_data
2023-01-18 17:27:13 +01:00
and event.id in event_data
):
2023-01-18 17:27:13 +01:00
self._attr_name = f"{self._event_type} {event_data[event.id].name}"
break