2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ZoneMinder switches."""
|
2016-10-15 03:56:40 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2020-10-07 14:28:49 +00:00
|
|
|
from zoneminder.monitor import MonitorState
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import CONF_COMMAND_OFF, CONF_COMMAND_ON
|
2020-10-07 14:28:49 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
from . import DOMAIN as ZONEMINDER_DOMAIN
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-10-29 20:10:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
2020-10-07 14:28:49 +00:00
|
|
|
vol.Required(CONF_COMMAND_ON): cv.string,
|
|
|
|
vol.Required(CONF_COMMAND_OFF): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
|
|
|
"""Set up the ZoneMinder switch platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
on_state = MonitorState(config.get(CONF_COMMAND_ON))
|
|
|
|
off_state = MonitorState(config.get(CONF_COMMAND_OFF))
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2018-09-15 06:44:48 +00:00
|
|
|
switches = []
|
2020-10-07 14:28:49 +00:00
|
|
|
for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
|
|
|
|
monitors = zm_client.get_monitors()
|
|
|
|
if not monitors:
|
|
|
|
_LOGGER.warning("Could not fetch monitors from ZoneMinder")
|
|
|
|
return
|
2019-01-16 09:15:13 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
for monitor in monitors:
|
|
|
|
switches.append(ZMSwitchMonitors(monitor, on_state, off_state))
|
|
|
|
add_entities(switches)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class ZMSwitchMonitors(SwitchEntity):
|
2016-10-29 20:10:42 +00:00
|
|
|
"""Representation of a ZoneMinder switch."""
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
icon = "mdi:record-rec"
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
def __init__(self, monitor, on_state, off_state):
|
2016-10-15 03:56:40 +00:00
|
|
|
"""Initialize the switch."""
|
2018-09-15 06:44:48 +00:00
|
|
|
self._monitor = monitor
|
2016-10-15 03:56:40 +00:00
|
|
|
self._on_state = on_state
|
|
|
|
self._off_state = off_state
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the switch."""
|
2019-09-03 19:15:31 +00:00
|
|
|
return f"{self._monitor.name} State"
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the switch value."""
|
2018-09-15 06:44:48 +00:00
|
|
|
self._state = self._monitor.function == self._on_state
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self._state
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-10-15 03:56:40 +00:00
|
|
|
"""Turn the entity on."""
|
2018-09-15 06:44:48 +00:00
|
|
|
self._monitor.function = self._on_state
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-10-15 03:56:40 +00:00
|
|
|
"""Turn the entity off."""
|
2018-09-15 06:44:48 +00:00
|
|
|
self._monitor.function = self._off_state
|