Files
home-assistant-core/homeassistant/components/isy994/switch.py
T
shbatm 4be1006b71 ISY994 migration to PyISY v2 (Structure Changes to enable upgrade, Part 1) (#35212)
* ISY994 Structure updates in prep for PyISYv2 (Part 1)

- Correct node categorization.
- Move constants to separate file.
- Consolidate Logging to Constants file.
- Use Home Assistant Constants where possible.
- Use string literals where possible.
- Rename "domain" to "platform" in most places.
- Add additional device support (NODE_FILTER updates).

* Update categorize_programs per review

* add @shbatm as codeowner for ISY994 integration
2020-05-04 19:21:40 -05:00

68 lines
2.2 KiB
Python

"""Support for ISY994 switches."""
from typing import Callable
from homeassistant.components.switch import DOMAIN as SWITCH, SwitchEntity
from homeassistant.helpers.typing import ConfigType
from . import ISY994_NODES, ISY994_PROGRAMS, ISYDevice
from .const import _LOGGER
def setup_platform(
hass, config: ConfigType, add_entities: Callable[[list], None], discovery_info=None
):
"""Set up the ISY994 switch platform."""
devices = []
for node in hass.data[ISY994_NODES][SWITCH]:
if not node.dimmable:
devices.append(ISYSwitchDevice(node))
for name, status, actions in hass.data[ISY994_PROGRAMS][SWITCH]:
devices.append(ISYSwitchProgram(name, status, actions))
add_entities(devices)
class ISYSwitchDevice(ISYDevice, SwitchEntity):
"""Representation of an ISY994 switch device."""
@property
def is_on(self) -> bool:
"""Get whether the ISY994 device is in the on state."""
return bool(self.value)
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 ISYSwitchProgram(ISYSwitchDevice):
"""A representation of an ISY994 program switch."""
def __init__(self, name: str, node, actions) -> None:
"""Initialize the ISY994 switch program."""
super().__init__(node)
self._name = name
self._actions = actions
@property
def is_on(self) -> bool:
"""Get whether the ISY994 switch program is on."""
return bool(self.value)
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")