2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ZoneMinder binary sensors."""
|
2022-01-04 09:52:30 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-09-19 13:05:29 +00:00
|
|
|
from zoneminder.zm import ZoneMinder
|
|
|
|
|
2020-09-12 18:21:57 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-16 00:07:12 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-12 18:21:57 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2022-01-04 09:52:30 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-01-17 18:52:53 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
from . import DOMAIN as ZONEMINDER_DOMAIN
|
2019-01-17 18:52:53 +00:00
|
|
|
|
|
|
|
|
2022-01-04 09:52:30 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2020-10-07 14:28:49 +00:00
|
|
|
"""Set up the ZoneMinder binary sensor platform."""
|
|
|
|
sensors = []
|
|
|
|
for host_name, zm_client in hass.data[ZONEMINDER_DOMAIN].items():
|
|
|
|
sensors.append(ZMAvailabilitySensor(host_name, zm_client))
|
|
|
|
add_entities(sensors)
|
2019-01-17 18:52:53 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class ZMAvailabilitySensor(BinarySensorEntity):
|
2019-01-17 18:52:53 +00:00
|
|
|
"""Representation of the availability of ZoneMinder as a binary sensor."""
|
|
|
|
|
2022-09-19 13:05:29 +00:00
|
|
|
_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
|
|
|
|
|
|
|
|
def __init__(self, host_name: str, client: ZoneMinder) -> None:
|
2019-01-17 18:52:53 +00:00
|
|
|
"""Initialize availability sensor."""
|
2022-09-19 13:05:29 +00:00
|
|
|
self._attr_name = host_name
|
2019-01-17 18:52:53 +00:00
|
|
|
self._client = client
|
|
|
|
|
2022-09-06 12:01:09 +00:00
|
|
|
def update(self) -> None:
|
2019-01-17 18:52:53 +00:00
|
|
|
"""Update the state of this sensor (availability of ZoneMinder)."""
|
2022-09-19 13:05:29 +00:00
|
|
|
self._attr_is_on = self._client.is_available
|