Extracting zoneminder to a new library (#16527)
* Migrating out the zoneminder platform (and camera.zoneminder) to a new library * Clean up the global variable ZM usage * Modify camera.zoneminder to use the new Monitor class implementation * Refactor camera.zoneminder after latest refactor in zm-py * Implementing changes to switch.zoneminder to use zm-py native methods * Complete migrating over sensor.zoneminder to the zm-py library * Tweaking ZoneMinder components from code review * Linting fixes for the zoneminder components * Directly assign value when turning on/off in switch.zoneminder
This commit is contained in:
parent
8ce2d701c2
commit
1ca09ea36f
5 changed files with 76 additions and 242 deletions
|
@ -9,8 +9,8 @@ import logging
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.components.zoneminder import DOMAIN as ZONEMINDER_DOMAIN
|
||||
from homeassistant.const import (CONF_COMMAND_ON, CONF_COMMAND_OFF)
|
||||
from homeassistant.components import zoneminder
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
@ -25,22 +25,20 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the ZoneMinder switch platform."""
|
||||
on_state = config.get(CONF_COMMAND_ON)
|
||||
off_state = config.get(CONF_COMMAND_OFF)
|
||||
from zoneminder.monitor import MonitorState
|
||||
on_state = MonitorState(config.get(CONF_COMMAND_ON))
|
||||
off_state = MonitorState(config.get(CONF_COMMAND_OFF))
|
||||
|
||||
zm_client = hass.data[ZONEMINDER_DOMAIN]
|
||||
|
||||
monitors = zm_client.get_monitors()
|
||||
if not monitors:
|
||||
_LOGGER.warning('Could not fetch monitors from ZoneMinder')
|
||||
return
|
||||
|
||||
switches = []
|
||||
|
||||
monitors = zoneminder.get_state('api/monitors.json')
|
||||
for i in monitors['monitors']:
|
||||
switches.append(
|
||||
ZMSwitchMonitors(
|
||||
int(i['Monitor']['Id']),
|
||||
i['Monitor']['Name'],
|
||||
on_state,
|
||||
off_state
|
||||
)
|
||||
)
|
||||
|
||||
for monitor in monitors:
|
||||
switches.append(ZMSwitchMonitors(monitor, on_state, off_state))
|
||||
add_entities(switches)
|
||||
|
||||
|
||||
|
@ -49,10 +47,9 @@ class ZMSwitchMonitors(SwitchDevice):
|
|||
|
||||
icon = 'mdi:record-rec'
|
||||
|
||||
def __init__(self, monitor_id, monitor_name, on_state, off_state):
|
||||
def __init__(self, monitor, on_state, off_state):
|
||||
"""Initialize the switch."""
|
||||
self._monitor_id = monitor_id
|
||||
self._monitor_name = monitor_name
|
||||
self._monitor = monitor
|
||||
self._on_state = on_state
|
||||
self._off_state = off_state
|
||||
self._state = None
|
||||
|
@ -60,15 +57,11 @@ class ZMSwitchMonitors(SwitchDevice):
|
|||
@property
|
||||
def name(self):
|
||||
"""Return the name of the switch."""
|
||||
return "%s State" % self._monitor_name
|
||||
return '{}\'s State'.format(self._monitor.name)
|
||||
|
||||
def update(self):
|
||||
"""Update the switch value."""
|
||||
monitor = zoneminder.get_state(
|
||||
'api/monitors/%i.json' % self._monitor_id
|
||||
)
|
||||
current_state = monitor['monitor']['Monitor']['Function']
|
||||
self._state = True if current_state == self._on_state else False
|
||||
self._state = self._monitor.function == self._on_state
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
|
@ -77,14 +70,8 @@ class ZMSwitchMonitors(SwitchDevice):
|
|||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the entity on."""
|
||||
zoneminder.change_state(
|
||||
'api/monitors/%i.json' % self._monitor_id,
|
||||
{'Monitor[Function]': self._on_state}
|
||||
)
|
||||
self._monitor.function = self._on_state
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the entity off."""
|
||||
zoneminder.change_state(
|
||||
'api/monitors/%i.json' % self._monitor_id,
|
||||
{'Monitor[Function]': self._off_state}
|
||||
)
|
||||
self._monitor.function = self._off_state
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue