1 """Support for ONVIF binary sensors."""
3 from __future__
import annotations
6 BinarySensorDeviceClass,
17 from .const
import DOMAIN
18 from .device
import ONVIFDevice
19 from .entity
import ONVIFBaseEntity
24 config_entry: ConfigEntry,
25 async_add_entities: AddEntitiesCallback,
27 """Set up a ONVIF binary sensor."""
28 device: ONVIFDevice = hass.data[DOMAIN][config_entry.unique_id]
32 for event
in device.events.get_platform(
"binary_sensor")
35 ent_reg = er.async_get(hass)
36 for entry
in er.async_entries_for_config_entry(ent_reg, config_entry.entry_id):
37 if entry.domain ==
"binary_sensor" and entry.unique_id
not in entities:
39 entry.unique_id, device, entry
43 uids_by_platform = device.events.get_uids_by_platform(
"binary_sensor")
46 def async_check_entities() -> None:
47 """Check if we have added an entity for the event."""
48 nonlocal uids_by_platform
49 if not (missing := uids_by_platform.difference(entities)):
51 new_entities: dict[str, ONVIFBinarySensor] = {
55 entities.update(new_entities)
58 device.events.async_add_listener(async_check_entities)
62 """Representation of a binary ONVIF event."""
64 _attr_should_poll =
False
68 self, uid: str, device: ONVIFDevice, entry: er.RegistryEntry |
None =
None
70 """Initialize the ONVIF binary sensor."""
74 BinarySensorDeviceClass, entry.original_device_class
79 event = device.events.get_uid(uid)
82 BinarySensorDeviceClass, event.device_class
86 self.
_attr_name_attr_name = f
"{device.name} {event.name}"
93 """Return true if the binary sensor is on."""
94 if (event := self.device.events.get_uid(self.
_attr_unique_id_attr_unique_id))
is not None:
99 """Connect to dispatcher listening for entity data notifications."""
104 self.
_attr_is_on_attr_is_on = last_state.state == STATE_ON
None async_added_to_hass(self)
None __init__(self, str uid, ONVIFDevice device, er.RegistryEntry|None entry=None)
_attr_entity_registry_enabled_default
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
State|None async_get_last_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)