2019-02-13 20:21:14 +00:00
|
|
|
"""Support for toggling Amcrest IP camera settings."""
|
2018-03-31 21:15:25 +00:00
|
|
|
import logging
|
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_SWITCHES
|
2018-03-31 21:15:25 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
from .const import DATA_AMCREST
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-03-31 21:15:25 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
# Switch types are defined like: Name, icon
|
|
|
|
SWITCHES = {
|
|
|
|
'motion_detection': ['Motion Detection', 'mdi:run-fast'],
|
|
|
|
'motion_recording': ['Motion Recording', 'mdi:record-rec']
|
|
|
|
}
|
|
|
|
|
2018-03-31 21:15:25 +00:00
|
|
|
|
2019-02-13 20:21:14 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2018-03-31 21:15:25 +00:00
|
|
|
"""Set up the IP Amcrest camera switch platform."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
name = discovery_info[CONF_NAME]
|
2019-04-25 05:39:49 +00:00
|
|
|
device = hass.data[DATA_AMCREST]['devices'][name]
|
|
|
|
async_add_entities(
|
|
|
|
[AmcrestSwitch(name, device, setting)
|
|
|
|
for setting in discovery_info[CONF_SWITCHES]],
|
|
|
|
True)
|
2018-03-31 21:15:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AmcrestSwitch(ToggleEntity):
|
|
|
|
"""Representation of an Amcrest IP camera switch."""
|
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
def __init__(self, name, device, setting):
|
2018-03-31 21:15:25 +00:00
|
|
|
"""Initialize the Amcrest switch."""
|
2019-04-25 05:39:49 +00:00
|
|
|
self._name = '{} {}'.format(name, SWITCHES[setting][0])
|
|
|
|
self._api = device.api
|
2018-03-31 21:15:25 +00:00
|
|
|
self._setting = setting
|
2019-04-25 05:39:49 +00:00
|
|
|
self._state = False
|
2018-03-31 21:15:25 +00:00
|
|
|
self._icon = SWITCHES[setting][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the switch if any."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
2019-04-25 05:39:49 +00:00
|
|
|
return self._state
|
2018-03-31 21:15:25 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn setting on."""
|
|
|
|
if self._setting == 'motion_detection':
|
2019-04-25 05:39:49 +00:00
|
|
|
self._api.motion_detection = 'true'
|
2018-03-31 21:15:25 +00:00
|
|
|
elif self._setting == 'motion_recording':
|
2019-04-25 05:39:49 +00:00
|
|
|
self._api.motion_recording = 'true'
|
2018-03-31 21:15:25 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn setting off."""
|
|
|
|
if self._setting == 'motion_detection':
|
2019-04-25 05:39:49 +00:00
|
|
|
self._api.motion_detection = 'false'
|
2018-03-31 21:15:25 +00:00
|
|
|
elif self._setting == 'motion_recording':
|
2019-04-25 05:39:49 +00:00
|
|
|
self._api.motion_recording = 'false'
|
2018-03-31 21:15:25 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update setting state."""
|
|
|
|
_LOGGER.debug("Polling state for setting: %s ", self._name)
|
|
|
|
|
|
|
|
if self._setting == 'motion_detection':
|
2019-04-25 05:39:49 +00:00
|
|
|
detection = self._api.is_motion_detector_on()
|
2018-03-31 21:15:25 +00:00
|
|
|
elif self._setting == 'motion_recording':
|
2019-04-25 05:39:49 +00:00
|
|
|
detection = self._api.is_record_on_motion_detection()
|
2018-03-31 21:15:25 +00:00
|
|
|
|
2019-04-25 05:39:49 +00:00
|
|
|
self._state = detection
|
2018-03-31 21:15:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon for the switch."""
|
|
|
|
return self._icon
|