From d7cd1a2b4b1c14e1c41d42228aa1e32f5b492882 Mon Sep 17 00:00:00 2001 From: Rohan Kapoor Date: Thu, 11 Oct 2018 00:38:31 -0700 Subject: [PATCH] Implement ZoneMinder run states (#17198) --- .coveragerc | 2 +- CODEOWNERS | 2 +- homeassistant/components/sensor/zoneminder.py | 24 +++++++++++++++++++ .../{zoneminder.py => zoneminder/__init__.py} | 18 ++++++++++++-- .../components/zoneminder/services.yaml | 6 +++++ requirements_all.txt | 2 +- 6 files changed, 49 insertions(+), 5 deletions(-) rename homeassistant/components/{zoneminder.py => zoneminder/__init__.py} (78%) create mode 100644 homeassistant/components/zoneminder/services.yaml diff --git a/.coveragerc b/.coveragerc index 297c7edb9a7..a4f25bd73f8 100644 --- a/.coveragerc +++ b/.coveragerc @@ -385,7 +385,7 @@ omit = homeassistant/components/zigbee.py homeassistant/components/*/zigbee.py - homeassistant/components/zoneminder.py + homeassistant/components/zoneminder/* homeassistant/components/*/zoneminder.py homeassistant/components/tuya.py diff --git a/CODEOWNERS b/CODEOWNERS index 93fd0418833..ed8d8531a6a 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -233,7 +233,7 @@ homeassistant/components/*/xiaomi_aqara.py @danielhiversen @syssi homeassistant/components/*/xiaomi_miio.py @rytilahti @syssi # Z -homeassistant/components/zoneminder.py @rohankapoorcom +homeassistant/components/zoneminder/ @rohankapoorcom homeassistant/components/*/zoneminder.py @rohankapoorcom # Other code diff --git a/homeassistant/components/sensor/zoneminder.py b/homeassistant/components/sensor/zoneminder.py index d4164bbf721..8e4b57f1f38 100644 --- a/homeassistant/components/sensor/zoneminder.py +++ b/homeassistant/components/sensor/zoneminder.py @@ -54,6 +54,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): for sensor in config[CONF_MONITORED_CONDITIONS]: sensors.append(ZMSensorEvents(monitor, include_archived, sensor)) + sensors.append(ZMSensorRunState(zm_client)) add_entities(sensors) @@ -114,3 +115,26 @@ class ZMSensorEvents(Entity): """Update the sensor.""" self._state = self._monitor.get_events( self.time_period, self._include_archived) + + +class ZMSensorRunState(Entity): + """Get the ZoneMinder run state.""" + + def __init__(self, client): + """Initialize run state sensor.""" + self._state = None + self._client = client + + @property + def name(self): + """Return the name of the sensor.""" + return 'Run State' + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + def update(self): + """Update the sensor.""" + self._state = self._client.get_active_state() diff --git a/homeassistant/components/zoneminder.py b/homeassistant/components/zoneminder/__init__.py similarity index 78% rename from homeassistant/components/zoneminder.py rename to homeassistant/components/zoneminder/__init__.py index 3f6d8ba7fcf..e5d0c7a5a92 100644 --- a/homeassistant/components/zoneminder.py +++ b/homeassistant/components/zoneminder/__init__.py @@ -10,12 +10,12 @@ import voluptuous as vol from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, CONF_PATH, CONF_SSL, CONF_USERNAME, - CONF_VERIFY_SSL) + CONF_VERIFY_SSL, ATTR_NAME) import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) -REQUIREMENTS = ['zm-py==0.0.5'] +REQUIREMENTS = ['zm-py==0.1.0'] CONF_PATH_ZMS = 'path_zms' @@ -38,6 +38,11 @@ CONFIG_SCHEMA = vol.Schema({ }) }, extra=vol.ALLOW_EXTRA) +SERVICE_SET_RUN_STATE = 'set_run_state' +SET_RUN_STATE_SCHEMA = vol.Schema({ + vol.Required(ATTR_NAME): cv.string +}) + def setup(hass, config): """Set up the ZoneMinder component.""" @@ -57,4 +62,13 @@ def setup(hass, config): conf.get(CONF_PATH_ZMS), conf.get(CONF_VERIFY_SSL)) + def set_active_state(call): + """Set the ZoneMinder run state to the given state name.""" + return hass.data[DOMAIN].set_active_state(call.data[ATTR_NAME]) + + hass.services.register( + DOMAIN, SERVICE_SET_RUN_STATE, set_active_state, + schema=SET_RUN_STATE_SCHEMA + ) + return hass.data[DOMAIN].login() diff --git a/homeassistant/components/zoneminder/services.yaml b/homeassistant/components/zoneminder/services.yaml new file mode 100644 index 00000000000..e6346d2f384 --- /dev/null +++ b/homeassistant/components/zoneminder/services.yaml @@ -0,0 +1,6 @@ +set_run_state: + description: Set the ZoneMinder run state + fields: + name: + description: The string name of the ZoneMinder run state to set as active. + example: 'Home' \ No newline at end of file diff --git a/requirements_all.txt b/requirements_all.txt index e0118145c4a..e24a1f5681d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1594,4 +1594,4 @@ zigpy-xbee==0.1.1 zigpy==0.2.0 # homeassistant.components.zoneminder -zm-py==0.0.5 +zm-py==0.1.0