1 """Support for Axis binary sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime, timedelta
9 from axis.interfaces.applications.fence_guard
import FenceGuardHandler
10 from axis.interfaces.applications.loitering_guard
import LoiteringGuardHandler
11 from axis.interfaces.applications.motion_guard
import MotionGuardHandler
12 from axis.interfaces.applications.vmd4
import Vmd4Handler
13 from axis.models.event
import Event, EventTopic
16 BinarySensorDeviceClass,
18 BinarySensorEntityDescription,
24 from .
import AxisConfigEntry
25 from .entity
import AxisEventDescription, AxisEventEntity
26 from .hub
import AxisHub
29 @dataclass(frozen=True, kw_only=True)
31 """Axis binary sensor entity description."""
36 """Make sure event ID is int."""
46 """Validate event ID is int."""
47 _, _, profile_id = event.id.partition(
"Profile")
53 """Validate event ID is int."""
54 _, _, profile_id = event.id.partition(
"Scenario")
60 handler: FenceGuardHandler
61 | LoiteringGuardHandler
67 """Get guard suite item name."""
68 if handler.initialized
and (profiles := handler[
"0"].profiles):
69 for profile_id, profile
in profiles.items():
70 camera_id = profile.camera
71 if event.id == f
"Camera{camera_id}Profile{profile_id}":
72 return f
"{event_type} {profile.name}"
78 """Fence guard name."""
84 """Loitering guard name."""
90 """Motion guard name."""
96 """Motion detection 4 name."""
102 """Get object analytics name."""
103 if hub.api.vapix.object_analytics.initialized
and (
104 scenarios := hub.api.vapix.object_analytics[
"0"].scenarios
106 for scenario_id, scenario
in scenarios.items():
107 device_id = scenario.devices[0][
"id"]
108 if event.id == f
"Device{device_id}Scenario{scenario_id}":
109 return f
"Object Analytics {scenario.name}"
113 ENTITY_DESCRIPTIONS = (
115 key=
"Input port state",
116 device_class=BinarySensorDeviceClass.CONNECTIVITY,
117 event_topic=(EventTopic.PORT_INPUT, EventTopic.PORT_SUPERVISED_INPUT),
118 name_fn=
lambda hub, event: hub.api.vapix.ports[event.id].name,
122 key=
"Day/Night vision state",
123 device_class=BinarySensorDeviceClass.LIGHT,
124 event_topic=EventTopic.DAY_NIGHT_VISION,
127 key=
"Sound trigger state",
128 device_class=BinarySensorDeviceClass.SOUND,
129 event_topic=EventTopic.SOUND_TRIGGER_LEVEL,
132 key=
"Motion sensors state",
133 device_class=BinarySensorDeviceClass.MOTION,
136 EventTopic.MOTION_DETECTION,
137 EventTopic.MOTION_DETECTION_3,
141 key=
"Motion detection 4 state",
142 device_class=BinarySensorDeviceClass.MOTION,
143 event_topic=EventTopic.MOTION_DETECTION_4,
144 name_fn=motion_detection_4_name_fn,
145 supported_fn=guard_suite_supported_fn,
148 key=
"Fence guard state",
149 device_class=BinarySensorDeviceClass.MOTION,
150 event_topic=EventTopic.FENCE_GUARD,
151 name_fn=fence_guard_name_fn,
152 supported_fn=guard_suite_supported_fn,
155 key=
"Loitering guard state",
156 device_class=BinarySensorDeviceClass.MOTION,
157 event_topic=EventTopic.LOITERING_GUARD,
158 name_fn=loitering_guard_name_fn,
159 supported_fn=guard_suite_supported_fn,
162 key=
"Motion guard state",
163 device_class=BinarySensorDeviceClass.MOTION,
164 event_topic=EventTopic.MOTION_GUARD,
165 name_fn=motion_guard_name_fn,
166 supported_fn=guard_suite_supported_fn,
169 key=
"Object analytics state",
170 device_class=BinarySensorDeviceClass.MOTION,
171 event_topic=EventTopic.OBJECT_ANALYTICS,
172 name_fn=object_analytics_name_fn,
173 supported_fn=object_analytics_supported_fn,
180 config_entry: AxisConfigEntry,
181 async_add_entities: AddEntitiesCallback,
183 """Set up a Axis binary sensor."""
184 config_entry.runtime_data.entity_loader.register_platform(
185 async_add_entities, AxisBinarySensor, ENTITY_DESCRIPTIONS
190 """Representation of a binary Axis event."""
192 entity_description: AxisBinarySensorDescription
195 self, hub: AxisHub, description: AxisBinarySensorDescription, event: Event
197 """Initialize the Axis binary sensor."""
198 super().
__init__(hub, description, event)
205 """Update the sensor's state, if needed."""
209 def scheduled_update(now: datetime) ->
None:
210 """Timer callback for sensor update."""
218 if self.is_on
or self.
hubhub.config.trigger_time == 0:
None __init__(self, AxisHub hub, AxisBinarySensorDescription description, Event event)
None async_event_callback(self, Event event)
None async_write_ha_state(self)
str fence_guard_name_fn(AxisHub hub, Event event)
bool object_analytics_supported_fn(AxisHub hub, Event event)
None async_setup_entry(HomeAssistant hass, AxisConfigEntry config_entry, AddEntitiesCallback async_add_entities)
str object_analytics_name_fn(AxisHub hub, Event event)
bool guard_suite_supported_fn(AxisHub hub, Event event)
bool event_id_is_int(str event_id)
str guard_suite_name_fn(FenceGuardHandler|LoiteringGuardHandler|MotionGuardHandler|Vmd4Handler handler, Event event, str event_type)
str motion_detection_4_name_fn(AxisHub hub, Event event)
str motion_guard_name_fn(AxisHub hub, Event event)
str loitering_guard_name_fn(AxisHub hub, Event event)
CALLBACK_TYPE async_call_later(HomeAssistant hass, float|timedelta delay, HassJob[[datetime], Coroutine[Any, Any, None]|None]|Callable[[datetime], Coroutine[Any, Any, None]|None] action)