Add ISY programs and support for all device types (#3082)

*  ISY Lock, Binary Sensor, Cover devices, Sensors and Fan support
* Support for ISY Programs
This commit is contained in:
Teagan Glenn authored and Johann Kellerman committed 2016-09-11 20:18:53 +02:00
1 parent 8c7a1b4b05
commit 05a3b610ff
9 files changed
+1057 -321

No files matched your search

+82 -63
View File
@@ -2,87 +2,106 @@
Support for ISY994 switches.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/isy994/
https://home-assistant.io/components/switch.isy994/
"""
import logging
from typing import Callable # noqa
from homeassistant.components.isy994 import (
HIDDEN_STRING, ISY, SENSOR_STRING, ISYDeviceABC)
from homeassistant.const import STATE_OFF, STATE_ON # STATE_OPEN, STATE_CLOSED
from homeassistant.components.switch import SwitchDevice, DOMAIN
import homeassistant.components.isy994 as isy
from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNKNOWN
from homeassistant.helpers.typing import ConfigType # noqa
_LOGGER = logging.getLogger(__name__)
VALUE_TO_STATE = {
False: STATE_OFF,
True: STATE_ON,
}
UOM = ['2', '78']
STATES = [STATE_OFF, STATE_ON, 'true', 'false']
# The frontend doesn't seem to fully support the open and closed states yet.
# Once it does, the HA.doors programs should report open and closed instead of
# off and on. It appears that on should be open and off should be closed.
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the ISY994 platform."""
# pylint: disable=too-many-locals
logger = logging.getLogger(__name__)
devs = []
# verify connection
if ISY is None or not ISY.connected:
logger.error('A connection has not been made to the ISY controller.')
# pylint: disable=unused-argument
def setup_platform(hass, config: ConfigType,
add_devices: Callable[[list], None], discovery_info=None):
"""Set up the ISY994 switch platform."""
if isy.ISY is None or not isy.ISY.connected:
_LOGGER.error('A connection has not been made to the ISY controller.')
return False
# Import not dimmable nodes and groups
for (path, node) in ISY.nodes:
if not node.dimmable and SENSOR_STRING not in node.name:
if HIDDEN_STRING in path:
node.name += HIDDEN_STRING
devs.append(ISYSwitchDevice(node))
devices = []
# Import ISY doors programs
for folder_name, states in (('HA.doors', [STATE_ON, STATE_OFF]),
('HA.switches', [STATE_ON, STATE_OFF])):
for node in isy.filter_nodes(isy.NODES, units=UOM,
states=STATES):
if not node.dimmable:
devices.append(ISYSwitchDevice(node))
for node in isy.GROUPS:
devices.append(ISYSwitchDevice(node))
for program in isy.PROGRAMS.get(DOMAIN, []):
try:
folder = ISY.programs['My Programs'][folder_name]
except KeyError:
# HA.doors folder does not exist
status = program[isy.KEY_STATUS]
actions = program[isy.KEY_ACTIONS]
assert actions.dtype == 'program', 'Not a program'
except (KeyError, AssertionError):
pass
else:
for dtype, name, node_id in folder.children:
if dtype is 'folder':
custom_switch = folder[node_id]
try:
actions = custom_switch['actions'].leaf
assert actions.dtype == 'program', 'Not a program'
node = custom_switch['status'].leaf
except (KeyError, AssertionError):
pass
else:
devs.append(ISYProgramDevice(name, node, actions,
states))
devices.append(ISYSwitchProgram(program.name, status, actions))
add_devices(devs)
add_devices(devices)
class ISYSwitchDevice(ISYDeviceABC):
"""Representation of an ISY switch."""
class ISYSwitchDevice(isy.ISYDevice, SwitchDevice):
"""Representation of an ISY994 switch device."""
_domain = 'switch'
_dtype = 'binary'
_states = [STATE_ON, STATE_OFF]
def __init__(self, node) -> None:
"""Initialize the ISY994 switch device."""
isy.ISYDevice.__init__(self, node)
@property
def is_on(self) -> bool:
"""Get whether the ISY994 device is in the on state."""
return self.state == STATE_ON
@property
def state(self) -> str:
"""Get the state of the ISY994 device."""
return VALUE_TO_STATE.get(bool(self.value), STATE_UNKNOWN)
def turn_off(self, **kwargs) -> None:
"""Send the turn on command to the ISY994 switch."""
if not self._node.off():
_LOGGER.debug('Unable to turn on switch.')
def turn_on(self, **kwargs) -> None:
"""Send the turn off command to the ISY994 switch."""
if not self._node.on():
_LOGGER.debug('Unable to turn on switch.')
class ISYProgramDevice(ISYSwitchDevice):
"""Representation of an ISY door."""
class ISYSwitchProgram(ISYSwitchDevice):
"""A representation of an ISY994 program switch."""
_domain = 'switch'
_dtype = 'binary'
def __init__(self, name, node, actions, states):
"""Initialize the switch."""
super().__init__(node)
self._states = states
def __init__(self, name: str, node, actions) -> None:
"""Initialize the ISY994 switch program."""
ISYSwitchDevice.__init__(self, node)
self._name = name
self.action_node = actions
self._actions = actions
def turn_on(self, **kwargs):
"""Turn the device on/close the device."""
self.action_node.runThen()
@property
def is_on(self) -> bool:
"""Get whether the ISY994 switch program is on."""
return bool(self.value)
def turn_off(self, **kwargs):
"""Turn the device off/open the device."""
self.action_node.runElse()
def turn_on(self, **kwargs) -> None:
"""Send the turn on command to the ISY994 switch program."""
if not self._actions.runThen():
_LOGGER.error('Unable to turn on switch')
def turn_off(self, **kwargs) -> None:
"""Send the turn off command to the ISY994 switch program."""
if not self._actions.runElse():
_LOGGER.error('Unable to turn off switch')