2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Abode Security System switches."""
|
2019-10-12 20:02:12 +00:00
|
|
|
import abodepy.helpers.constants as CONST
|
|
|
|
|
2017-08-29 15:34:19 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2020-03-04 22:54:28 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-08-29 15:34:19 +00:00
|
|
|
|
2019-10-13 18:01:04 +00:00
|
|
|
from . import AbodeAutomation, AbodeDevice
|
|
|
|
from .const import DOMAIN
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-02-08 15:58:07 +00:00
|
|
|
DEVICE_TYPES = [CONST.TYPE_SWITCH, CONST.TYPE_VALVE]
|
|
|
|
|
2020-03-04 22:54:28 +00:00
|
|
|
ICON = "mdi:robot"
|
|
|
|
|
2017-08-29 15:34:19 +00:00
|
|
|
|
2019-10-13 18:01:04 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Abode switch devices."""
|
|
|
|
data = hass.data[DOMAIN]
|
2017-08-29 15:34:19 +00:00
|
|
|
|
2019-11-09 06:35:45 +00:00
|
|
|
entities = []
|
2017-08-29 15:34:19 +00:00
|
|
|
|
2020-02-08 15:58:07 +00:00
|
|
|
for device_type in DEVICE_TYPES:
|
|
|
|
for device in data.abode.get_devices(generic_type=device_type):
|
|
|
|
entities.append(AbodeSwitch(data, device))
|
2017-09-18 15:39:41 +00:00
|
|
|
|
2020-03-04 22:54:28 +00:00
|
|
|
for automation in data.abode.get_automations():
|
2020-03-18 00:58:05 +00:00
|
|
|
entities.append(AbodeAutomationSwitch(data, automation))
|
2017-09-18 15:39:41 +00:00
|
|
|
|
2019-11-09 06:35:45 +00:00
|
|
|
async_add_entities(entities)
|
2017-08-29 15:34:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AbodeSwitch(AbodeDevice, SwitchDevice):
|
|
|
|
"""Representation of an Abode switch."""
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn on the device."""
|
|
|
|
self._device.switch_on()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn off the device."""
|
|
|
|
self._device.switch_off()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._device.is_on
|
2017-09-18 15:39:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AbodeAutomationSwitch(AbodeAutomation, SwitchDevice):
|
|
|
|
"""A switch implementation for Abode automations."""
|
|
|
|
|
2020-03-04 22:54:28 +00:00
|
|
|
async def async_added_to_hass(self):
|
2020-03-18 00:58:05 +00:00
|
|
|
"""Set up trigger automation service."""
|
2020-03-04 22:54:28 +00:00
|
|
|
await super().async_added_to_hass()
|
|
|
|
|
|
|
|
signal = f"abode_trigger_automation_{self.entity_id}"
|
|
|
|
async_dispatcher_connect(self.hass, signal, self.trigger)
|
|
|
|
|
2017-09-18 15:39:41 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2020-03-04 22:54:28 +00:00
|
|
|
"""Enable the automation."""
|
|
|
|
if self._automation.enable(True):
|
|
|
|
self.schedule_update_ha_state()
|
2017-09-18 15:39:41 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2020-03-04 22:54:28 +00:00
|
|
|
"""Disable the automation."""
|
|
|
|
if self._automation.enable(False):
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def trigger(self):
|
|
|
|
"""Trigger the automation."""
|
|
|
|
self._automation.trigger()
|
2017-09-18 15:39:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2020-03-04 22:54:28 +00:00
|
|
|
"""Return True if the automation is enabled."""
|
|
|
|
return self._automation.is_enabled
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the robot icon to match Home Assistant automations."""
|
|
|
|
return ICON
|