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
|
|
|
|
|
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."""
|
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
def __init__(self, host_name, client):
|
2019-01-17 18:52:53 +00:00
|
|
|
"""Initialize availability sensor."""
|
|
|
|
self._state = None
|
2020-10-07 14:28:49 +00:00
|
|
|
self._name = host_name
|
2019-01-17 18:52:53 +00:00
|
|
|
self._client = client
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this binary sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this device, from component DEVICE_CLASSES."""
|
2021-12-16 00:07:12 +00:00
|
|
|
return BinarySensorDeviceClass.CONNECTIVITY
|
2019-01-17 18:52:53 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the state of this sensor (availability of ZoneMinder)."""
|
|
|
|
self._state = self._client.is_available
|