2017-01-08 23:33:35 +00:00
|
|
|
"""
|
|
|
|
Support for Insteon switch devices via local hub support.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/switch.insteon_local/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
2017-01-14 17:42:45 +00:00
|
|
|
|
2017-01-08 23:33:35 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2018-07-18 09:54:27 +00:00
|
|
|
from homeassistant import util
|
2017-01-08 23:33:35 +00:00
|
|
|
|
2017-01-14 17:42:45 +00:00
|
|
|
_CONFIGURING = {}
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-01-08 23:33:35 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['insteon_local']
|
2017-01-14 17:42:45 +00:00
|
|
|
DOMAIN = 'switch'
|
2017-01-08 23:33:35 +00:00
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
2017-01-21 22:00:56 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
2017-01-08 23:33:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-01-14 17:42:45 +00:00
|
|
|
"""Set up the Insteon local switch platform."""
|
2017-01-08 23:33:35 +00:00
|
|
|
insteonhub = hass.data['insteon_local']
|
2018-01-08 17:18:10 +00:00
|
|
|
if discovery_info is None:
|
2017-01-08 23:33:35 +00:00
|
|
|
return
|
|
|
|
|
2018-01-08 17:18:10 +00:00
|
|
|
linked = discovery_info['linked']
|
|
|
|
device_list = []
|
|
|
|
for device_id in linked:
|
|
|
|
if linked[device_id]['cat_type'] == 'switch':
|
|
|
|
device = insteonhub.switch(device_id)
|
|
|
|
device_list.append(
|
|
|
|
InsteonLocalSwitchDevice(device)
|
|
|
|
)
|
2017-01-08 23:33:35 +00:00
|
|
|
|
2018-01-08 17:18:10 +00:00
|
|
|
add_devices(device_list)
|
2017-01-08 23:33:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InsteonLocalSwitchDevice(SwitchDevice):
|
|
|
|
"""An abstract Class for an Insteon node."""
|
|
|
|
|
2018-01-08 17:18:10 +00:00
|
|
|
def __init__(self, node):
|
2017-01-08 23:33:35 +00:00
|
|
|
"""Initialize the device."""
|
|
|
|
self.node = node
|
|
|
|
self._state = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Return the name of the node."""
|
2018-01-08 17:18:10 +00:00
|
|
|
return self.node.device_id
|
2017-01-08 23:33:35 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2017-01-14 17:42:45 +00:00
|
|
|
"""Return the ID of this Insteon node."""
|
2018-01-30 09:39:39 +00:00
|
|
|
return self.node.device_id
|
2017-01-08 23:33:35 +00:00
|
|
|
|
|
|
|
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
|
|
|
|
def update(self):
|
|
|
|
"""Get the updated status of the switch."""
|
|
|
|
resp = self.node.status(0)
|
2017-03-10 10:14:31 +00:00
|
|
|
|
|
|
|
while 'error' in resp and resp['error'] is True:
|
|
|
|
resp = self.node.status(0)
|
|
|
|
|
2017-01-08 23:33:35 +00:00
|
|
|
if 'cmd2' in resp:
|
|
|
|
self._state = int(resp['cmd2'], 16) > 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the boolean response if the node is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn device on."""
|
|
|
|
self.node.on()
|
|
|
|
self._state = True
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn device off."""
|
|
|
|
self.node.off()
|
|
|
|
self._state = False
|