core/homeassistant/components/android_ip_webcam/switch.py

89 lines
2.7 KiB
Python
Raw Normal View History

"""Support for Android IP Webcam settings."""
from homeassistant.components.switch import SwitchEntity
from . import (
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_NAME,
CONF_SWITCHES,
DATA_IP_WEBCAM,
ICON_MAP,
KEY_MAP,
AndroidIPCamEntity,
)
2017-03-08 15:46:41 +00:00
2019-07-31 19:25:30 +00:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
2017-03-08 15:46:41 +00:00
"""Set up the IP Webcam switch platform."""
if discovery_info is None:
return
host = discovery_info[CONF_HOST]
name = discovery_info[CONF_NAME]
switches = discovery_info[CONF_SWITCHES]
ipcam = hass.data[DATA_IP_WEBCAM][host]
all_switches = []
for setting in switches:
all_switches.append(IPWebcamSettingsSwitch(name, host, ipcam, setting))
async_add_entities(all_switches, True)
2017-03-08 15:46:41 +00:00
class IPWebcamSettingsSwitch(AndroidIPCamEntity, SwitchEntity):
2017-03-08 15:46:41 +00:00
"""An abstract class for an IP Webcam setting."""
def __init__(self, name, host, ipcam, setting):
"""Initialize the settings switch."""
super().__init__(host, ipcam)
self._setting = setting
self._mapped_name = KEY_MAP.get(self._setting, self._setting)
self._name = f"{name} {self._mapped_name}"
2017-03-08 15:46:41 +00:00
self._state = False
@property
def name(self):
"""Return the name of the node."""
2017-03-08 15:46:41 +00:00
return self._name
async def async_update(self):
2017-03-08 15:46:41 +00:00
"""Get the updated status of the switch."""
self._state = bool(self._ipcam.current_settings.get(self._setting))
2017-03-08 15:46:41 +00:00
@property
def is_on(self):
"""Return the boolean response if the node is on."""
return self._state
async def async_turn_on(self, **kwargs):
2017-03-08 15:46:41 +00:00
"""Turn device on."""
2019-07-31 19:25:30 +00:00
if self._setting == "torch":
await self._ipcam.torch(activate=True)
2019-07-31 19:25:30 +00:00
elif self._setting == "focus":
await self._ipcam.focus(activate=True)
2019-07-31 19:25:30 +00:00
elif self._setting == "video_recording":
await self._ipcam.record(record=True)
2017-03-08 15:46:41 +00:00
else:
await self._ipcam.change_setting(self._setting, True)
2017-03-08 15:46:41 +00:00
self._state = True
self.async_write_ha_state()
2017-03-08 15:46:41 +00:00
async def async_turn_off(self, **kwargs):
2017-03-08 15:46:41 +00:00
"""Turn device off."""
2019-07-31 19:25:30 +00:00
if self._setting == "torch":
await self._ipcam.torch(activate=False)
2019-07-31 19:25:30 +00:00
elif self._setting == "focus":
await self._ipcam.focus(activate=False)
2019-07-31 19:25:30 +00:00
elif self._setting == "video_recording":
await self._ipcam.record(record=False)
2017-03-08 15:46:41 +00:00
else:
await self._ipcam.change_setting(self._setting, False)
2017-03-08 15:46:41 +00:00
self._state = False
self.async_write_ha_state()
2017-03-08 15:46:41 +00:00
@property
def icon(self):
"""Return the icon for the switch."""
2019-07-31 19:25:30 +00:00
return ICON_MAP.get(self._setting, "mdi:flash")