2020-05-11 17:12:12 +00:00
|
|
|
"""Support for ONVIF binary sensors."""
|
2021-03-18 12:21:46 +00:00
|
|
|
from __future__ import annotations
|
2020-05-11 17:12:12 +00:00
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-04-27 05:42:53 +00:00
|
|
|
from homeassistant.const import STATE_ON
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-04-27 05:42:53 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2022-01-03 10:35:02 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-04-27 05:42:53 +00:00
|
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
2020-05-11 17:12:12 +00:00
|
|
|
|
|
|
|
from .base import ONVIFBaseEntity
|
|
|
|
from .const import DOMAIN
|
2022-04-27 05:42:53 +00:00
|
|
|
from .device import ONVIFDevice
|
2020-05-11 17:12:12 +00:00
|
|
|
|
|
|
|
|
2022-01-03 10:35:02 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-05-11 17:12:12 +00:00
|
|
|
"""Set up a ONVIF binary sensor."""
|
|
|
|
device = hass.data[DOMAIN][config_entry.unique_id]
|
|
|
|
|
|
|
|
entities = {
|
|
|
|
event.uid: ONVIFBinarySensor(event.uid, device)
|
|
|
|
for event in device.events.get_platform("binary_sensor")
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:42:53 +00:00
|
|
|
ent_reg = er.async_get(hass)
|
|
|
|
for entry in er.async_entries_for_config_entry(ent_reg, config_entry.entry_id):
|
|
|
|
if entry.domain == "binary_sensor" and entry.unique_id not in entities:
|
|
|
|
entities[entry.unique_id] = ONVIFBinarySensor(
|
|
|
|
entry.unique_id, device, entry
|
|
|
|
)
|
|
|
|
|
2020-05-11 17:12:12 +00:00
|
|
|
async_add_entities(entities.values())
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_check_entities():
|
|
|
|
"""Check if we have added an entity for the event."""
|
|
|
|
new_entities = []
|
|
|
|
for event in device.events.get_platform("binary_sensor"):
|
|
|
|
if event.uid not in entities:
|
|
|
|
entities[event.uid] = ONVIFBinarySensor(event.uid, device)
|
|
|
|
new_entities.append(entities[event.uid])
|
|
|
|
async_add_entities(new_entities)
|
|
|
|
|
|
|
|
device.events.async_add_listener(async_check_entities)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-04-27 05:42:53 +00:00
|
|
|
class ONVIFBinarySensor(ONVIFBaseEntity, RestoreEntity, BinarySensorEntity):
|
2020-05-11 17:12:12 +00:00
|
|
|
"""Representation of a binary ONVIF event."""
|
|
|
|
|
2022-04-21 18:38:20 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
2022-04-27 05:42:53 +00:00
|
|
|
def __init__(self, uid, device: ONVIFDevice, entry: er.RegistryEntry | None = None):
|
2020-05-11 17:12:12 +00:00
|
|
|
"""Initialize the ONVIF binary sensor."""
|
2022-04-21 18:38:20 +00:00
|
|
|
self._attr_unique_id = uid
|
2022-04-27 05:42:53 +00:00
|
|
|
if entry is not None:
|
|
|
|
self._attr_device_class = entry.original_device_class
|
|
|
|
self._attr_entity_category = entry.entity_category
|
|
|
|
self._attr_name = entry.name
|
|
|
|
else:
|
|
|
|
event = device.events.get_uid(uid)
|
|
|
|
self._attr_device_class = event.device_class
|
|
|
|
self._attr_entity_category = event.entity_category
|
|
|
|
self._attr_entity_registry_enabled_default = event.entity_enabled
|
|
|
|
self._attr_name = f"{device.name} {event.name}"
|
|
|
|
self._attr_is_on = event.value
|
2022-04-21 18:38:20 +00:00
|
|
|
|
|
|
|
super().__init__(device)
|
2020-05-11 17:12:12 +00:00
|
|
|
|
2022-04-27 05:42:53 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool | None:
|
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
if (event := self.device.events.get_uid(self._attr_unique_id)) is not None:
|
|
|
|
return event.value
|
|
|
|
return self._attr_is_on
|
|
|
|
|
2020-05-11 17:12:12 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Connect to dispatcher listening for entity data notifications."""
|
|
|
|
self.async_on_remove(
|
|
|
|
self.device.events.async_add_listener(self.async_write_ha_state)
|
|
|
|
)
|
2022-04-27 05:42:53 +00:00
|
|
|
if (last_state := await self.async_get_last_state()) is not None:
|
|
|
|
self._attr_is_on = last_state.state == STATE_ON
|