2019-02-14 15:01:46 +00:00
|
|
|
"""Support for ISY994 switches."""
|
2022-02-03 16:02:05 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
2021-05-18 19:15:47 +00:00
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
from pyisy.constants import ISY_VALUE_UNKNOWN, PROTO_GROUP
|
|
|
|
|
2020-05-05 00:21:40 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH, SwitchEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-23 07:55:20 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
from .const import _LOGGER, DOMAIN as ISY994_DOMAIN, ISY994_NODES, ISY994_PROGRAMS
|
2020-05-05 04:03:12 +00:00
|
|
|
from .entity import ISYNodeEntity, ISYProgramEntity
|
2020-05-09 19:49:00 +00:00
|
|
|
from .helpers import migrate_old_unique_ids
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-12 07:49:46 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Set up the ISY994 switch platform."""
|
2020-05-09 19:49:00 +00:00
|
|
|
hass_isy_data = hass.data[ISY994_DOMAIN][entry.entry_id]
|
2022-02-03 16:02:05 +00:00
|
|
|
entities: list[ISYSwitchProgramEntity | ISYSwitchEntity] = []
|
2020-05-09 19:49:00 +00:00
|
|
|
for node in hass_isy_data[ISY994_NODES][SWITCH]:
|
2022-02-03 16:02:05 +00:00
|
|
|
entities.append(ISYSwitchEntity(node))
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2020-05-09 19:49:00 +00:00
|
|
|
for name, status, actions in hass_isy_data[ISY994_PROGRAMS][SWITCH]:
|
2022-02-03 16:02:05 +00:00
|
|
|
entities.append(ISYSwitchProgramEntity(name, status, actions))
|
2015-04-12 21:18:14 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
await migrate_old_unique_ids(hass, SWITCH, entities)
|
|
|
|
async_add_entities(entities)
|
2015-04-12 21:18:14 +00:00
|
|
|
|
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYSwitchEntity(ISYNodeEntity, SwitchEntity):
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Representation of an ISY994 switch device."""
|
2015-04-12 21:18:14 +00:00
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
@property
|
2022-02-03 16:02:05 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Get whether the ISY994 device is in the on state."""
|
2020-05-12 02:32:19 +00:00
|
|
|
if self._node.status == ISY_VALUE_UNKNOWN:
|
|
|
|
return None
|
|
|
|
return bool(self._node.status)
|
2015-04-13 02:30:14 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-05-08 04:15:42 +00:00
|
|
|
"""Send the turn off command to the ISY994 switch."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.turn_off():
|
2020-07-05 21:04:19 +00:00
|
|
|
_LOGGER.debug("Unable to turn off switch")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-05-08 04:15:42 +00:00
|
|
|
"""Send the turn on command to the ISY994 switch."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._node.turn_on():
|
2020-07-05 21:04:19 +00:00
|
|
|
_LOGGER.debug("Unable to turn on switch")
|
2015-04-13 02:30:14 +00:00
|
|
|
|
2020-05-08 04:15:42 +00:00
|
|
|
@property
|
2022-02-03 16:02:05 +00:00
|
|
|
def icon(self) -> str | None:
|
2020-05-08 04:15:42 +00:00
|
|
|
"""Get the icon for groups."""
|
|
|
|
if hasattr(self._node, "protocol") and self._node.protocol == PROTO_GROUP:
|
|
|
|
return "mdi:google-circles-communities" # Matches isy scene icon
|
|
|
|
return super().icon
|
|
|
|
|
2015-04-13 02:30:14 +00:00
|
|
|
|
2020-05-05 04:03:12 +00:00
|
|
|
class ISYSwitchProgramEntity(ISYProgramEntity, SwitchEntity):
|
2016-09-11 18:18:53 +00:00
|
|
|
"""A representation of an ISY994 program switch."""
|
2015-04-13 02:30:14 +00:00
|
|
|
|
2016-09-11 18:18:53 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Get whether the ISY994 switch program is on."""
|
2020-05-12 02:32:19 +00:00
|
|
|
return bool(self._node.status)
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn on command to the ISY994 switch program."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._actions.run_then():
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Unable to turn on switch")
|
2016-09-11 18:18:53 +00:00
|
|
|
|
2022-02-03 16:02:05 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2016-09-11 18:18:53 +00:00
|
|
|
"""Send the turn off command to the ISY994 switch program."""
|
2021-05-18 19:15:47 +00:00
|
|
|
if not await self._actions.run_else():
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Unable to turn off switch")
|
2020-05-05 04:03:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Get the icon for programs."""
|
|
|
|
return "mdi:script-text-outline" # Matches isy program icon
|