"""
Support for ZoneMinder Sensors.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.zoneminder/
"""
import logging

from homeassistant.const import STATE_UNKNOWN
from homeassistant.helpers.entity import Entity
import homeassistant.components.zoneminder as zoneminder

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['zoneminder']


def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the ZoneMinder sensor platform."""
    sensors = []

    monitors = zoneminder.get_state('api/monitors.json')
    for i in monitors['monitors']:
        sensors.append(
            ZMSensorMonitors(int(i['Monitor']['Id']), i['Monitor']['Name'])
        )
        sensors.append(
            ZMSensorEvents(int(i['Monitor']['Id']), i['Monitor']['Name'])
        )

    add_devices(sensors)


class ZMSensorMonitors(Entity):
    """Get the status of each ZoneMinder monitor."""

    def __init__(self, monitor_id, monitor_name):
        """Initiate monitor sensor."""
        self._monitor_id = monitor_id
        self._monitor_name = monitor_name
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return '{} Status'.format(self._monitor_name)

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    def update(self):
        """Update the sensor."""
        monitor = zoneminder.get_state(
            'api/monitors/%i.json' % self._monitor_id
        )
        if monitor['monitor']['Monitor']['Function'] is None:
            self._state = STATE_UNKNOWN
        else:
            self._state = monitor['monitor']['Monitor']['Function']


class ZMSensorEvents(Entity):
    """Get the number of events for each monitor."""

    def __init__(self, monitor_id, monitor_name):
        """Initiate event sensor."""
        self._monitor_id = monitor_id
        self._monitor_name = monitor_name
        self._state = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return '{} Events'.format(self._monitor_name)

    @property
    def unit_of_measurement(self):
        """Return the unit of measurement of this entity, if any."""
        return 'Events'

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    def update(self):
        """Update the sensor."""
        event = zoneminder.get_state(
            'api/events/index/MonitorId:%i.json' % self._monitor_id
        )

        self._state = event['pagination']['count']

@@ -181,16 +181,102 @@
 ogging%0A%0A
+import voluptuous as vol%0A%0Afrom homeassistant.components.sensor import PLATFORM_SCHEMA%0A
 from hom
@@ -417,16 +417,69 @@
 neminder
+%0Aimport homeassistant.helpers.config_validation as cv
 %0A%0A_LOGGE
@@ -542,16 +542,236 @@
 nder'%5D%0A%0A
+CONF_INCLUDE_ARCHIVED = %22include_archived%22%0A%0ADEFAULT_INCLUDE_ARCHIVED = False%0A%0APLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(%7B%0A    vol.Optional(CONF_INCLUDE_ARCHIVED, default=DEFAULT_INCLUDE_ARCHIVED):%0A        cv.boolean,%0A%7D)%0A%0A
 %0Adef set
@@ -876,24 +876,82 @@
 latform.%22%22%22%0A
+    include_archived = config.get(CONF_INCLUDE_ARCHIVED)%0A%0A
     sensors 
@@ -1246,32 +1246,77 @@
 onitor'%5D%5B'Name'%5D
+,%0A                           include_archived
 )%0A        )%0A%0A   
@@ -2333,32 +2333,50 @@
 id, monitor_name
+, include_archived
 ):%0A        %22%22%22In
@@ -2471,32 +2471,82 @@
  = monitor_name%0A
+        self._include_archived = include_archived%0A
         self._st
@@ -2990,32 +2990,141 @@
  the sensor.%22%22%22%0A
+        archived_filter = '/Archived:0'%0A        if self._include_archived:%0A            archived_filter = ''%0A%0A
         event = 
@@ -3187,16 +3187,18 @@
 torId:%25i
+%25s
 .json' %25
@@ -3190,32 +3190,33 @@
 Id:%25i%25s.json' %25 
+(
 self._monitor_id
@@ -3207,32 +3207,104 @@
 self._monitor_id
+,%0A                                                      archived_filter)
 %0A        )%0A%0A    

Support excluding archived events (#4445)