core/homeassistant/components/zoneminder/binary_sensor.py

43 lines
1.4 KiB
Python
Raw Normal View History

"""Support for ZoneMinder binary sensors."""
from __future__ import annotations
2022-09-19 13:05:29 +00:00
from zoneminder.zm import ZoneMinder
from homeassistant.components.binary_sensor import (
2021-12-16 00:07:12 +00:00
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2020-10-07 14:28:49 +00:00
from . import DOMAIN as ZONEMINDER_DOMAIN
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)
class ZMAvailabilitySensor(BinarySensorEntity):
"""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:
"""Initialize availability sensor."""
2022-09-19 13:05:29 +00:00
self._attr_name = host_name
self._client = client
2022-09-06 12:01:09 +00:00
def update(self) -> None:
"""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