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

139 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
2017-05-12 17:51:54 +02:00
from datetime import timedelta
from axis.event_stream import (
CLASS_INPUT,
CLASS_LIGHT,
CLASS_MOTION,
CLASS_OUTPUT,
CLASS_PTZ,
CLASS_SOUND,
2022-07-30 11:04:31 +02:00
AxisBinaryEvent,
AxisEvent,
FenceGuard,
LoiteringGuard,
MotionGuard,
ObjectAnalytics,
Vmd4,
)
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
2019-03-24 16:16:50 +01:00
from homeassistant.helpers.dispatcher import async_dispatcher_connect
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 .axis_base import AxisEventBase
from .const import DOMAIN as AXIS_DOMAIN
2022-07-30 11:04:31 +02:00
from .device import AxisNetworkDevice
2017-05-12 17:51:54 +02:00
DEVICE_CLASS = {
CLASS_INPUT: BinarySensorDeviceClass.CONNECTIVITY,
CLASS_LIGHT: BinarySensorDeviceClass.LIGHT,
CLASS_MOTION: BinarySensorDeviceClass.MOTION,
CLASS_SOUND: BinarySensorDeviceClass.SOUND,
}
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."""
2022-07-30 11:04:31 +02:00
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.unique_id]
2019-03-24 16:16:50 +01:00
@callback
def async_add_sensor(event_id):
2019-03-24 16:16:50 +01:00
"""Add binary sensor from Axis device."""
2022-07-30 11:04:31 +02:00
event: AxisEvent = device.api.event[event_id]
2019-05-20 07:45:31 +02:00
if event.CLASS not in (CLASS_OUTPUT, CLASS_PTZ) and not (
event.CLASS == CLASS_LIGHT and event.TYPE == "Light"
):
async_add_entities([AxisBinarySensor(event, device)])
2019-03-24 16:16:50 +01:00
config_entry.async_on_unload(
2020-05-14 10:49:27 +02:00
async_dispatcher_connect(hass, device.signal_new_event, async_add_sensor)
2019-07-31 12:25:30 -07:00
)
2017-05-12 17:51:54 +02:00
class AxisBinarySensor(AxisEventBase, BinarySensorEntity):
2017-05-12 17:51:54 +02:00
"""Representation of a binary Axis event."""
2022-07-30 11:04:31 +02:00
event: AxisBinaryEvent
def __init__(self, event: AxisEvent, 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)
2020-05-14 10:49:27 +02:00
self.cancel_scheduled_update = None
self._attr_device_class = DEVICE_CLASS.get(self.event.CLASS)
@callback
def update_callback(self, no_delay=False):
"""Update the sensor's state, if needed.
Parameter no_delay is True when device_event_reachable is sent.
"""
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
if self.is_on or self.device.option_trigger_time == 0 or no_delay:
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
@property
2022-07-30 11:04:31 +02:00
def is_on(self) -> bool:
2017-05-12 17:51:54 +02:00
"""Return true if event is active."""
2019-03-24 16:16:50 +01:00
return self.event.is_tripped
2017-05-12 17:51:54 +02:00
2018-10-29 06:52:30 +01:00
@property
2022-07-30 11:04:31 +02:00
def name(self) -> str | None:
2018-10-29 06:52:30 +01:00
"""Return the name of the event."""
2019-07-31 12:25:30 -07:00
if (
self.event.CLASS == CLASS_INPUT
and self.event.id in self.device.api.vapix.ports
2019-07-31 12:25:30 -07:00
and self.device.api.vapix.ports[self.event.id].name
):
return self.device.api.vapix.ports[self.event.id].name
2017-05-12 17:51:54 +02:00
if self.event.CLASS == CLASS_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),
):
if (
isinstance(self.event, event_class)
and event_data
and self.event.id in event_data
):
return f"{self.event.TYPE} {event_data[self.event.id].name}"
return self._attr_name