diff --git a/homeassistant/components/zoneminder/__init__.py b/homeassistant/components/zoneminder/__init__.py index 258841e20d0..2a7859ebba4 100644 --- a/homeassistant/components/zoneminder/__init__.py +++ b/homeassistant/components/zoneminder/__init__.py @@ -12,6 +12,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, CONF_PATH, CONF_SSL, CONF_USERNAME, CONF_VERIFY_SSL, ATTR_NAME, ATTR_ID) +from homeassistant.helpers.discovery import async_load_platform _LOGGER = logging.getLogger(__name__) @@ -93,4 +94,8 @@ def setup(hass, config): schema=SET_RUN_STATE_SCHEMA ) + hass.async_create_task( + async_load_platform(hass, 'binary_sensor', DOMAIN, {}, config) + ) + return success diff --git a/homeassistant/components/zoneminder/binary_sensor.py b/homeassistant/components/zoneminder/binary_sensor.py new file mode 100644 index 00000000000..e206ffa80f1 --- /dev/null +++ b/homeassistant/components/zoneminder/binary_sensor.py @@ -0,0 +1,50 @@ +""" +Support for ZoneMinder Binary Sensor. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/binary_sensor.zoneminder/ +""" +from homeassistant.components.binary_sensor import BinarySensorDevice + +from homeassistant.components.zoneminder import DOMAIN as ZONEMINDER_DOMAIN + +DEPENDENCIES = ['zoneminder'] + + +async def async_setup_platform(hass, config, add_entities, + discovery_info=None): + """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) + return True + + +class ZMAvailabilitySensor(BinarySensorDevice): + """Representation of the availability of ZoneMinder as a binary sensor.""" + + def __init__(self, host_name, client): + """Initialize availability sensor.""" + self._state = None + self._name = host_name + 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.""" + return 'connectivity' + + def update(self): + """Update the state of this sensor (availability of ZoneMinder).""" + self._state = self._client.is_available