2019-02-14 04:35:12 +00:00
|
|
|
"""Support for wired switches attached to a Konnected device."""
|
2018-05-15 17:58:00 +00:00
|
|
|
import logging
|
|
|
|
|
2018-08-14 19:15:33 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_STATE,
|
|
|
|
CONF_DEVICES,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_SWITCHES,
|
2020-02-11 21:04:42 +00:00
|
|
|
CONF_ZONE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
|
|
|
|
2020-02-11 21:04:42 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ACTIVATION,
|
|
|
|
CONF_MOMENTARY,
|
|
|
|
CONF_PAUSE,
|
|
|
|
CONF_REPEAT,
|
|
|
|
DOMAIN as KONNECTED_DOMAIN,
|
|
|
|
STATE_HIGH,
|
|
|
|
STATE_LOW,
|
|
|
|
)
|
2018-05-15 17:58:00 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-02-11 21:04:42 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up switches attached to a Konnected device from a config entry."""
|
2018-05-17 18:19:05 +00:00
|
|
|
data = hass.data[KONNECTED_DOMAIN]
|
2020-02-11 21:04:42 +00:00
|
|
|
device_id = config_entry.data["id"]
|
2018-08-14 19:15:33 +00:00
|
|
|
switches = [
|
2020-02-11 21:04:42 +00:00
|
|
|
KonnectedSwitch(device_id, zone_data.get(CONF_ZONE), zone_data)
|
|
|
|
for zone_data in data[CONF_DEVICES][device_id][CONF_SWITCHES]
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(switches)
|
2018-05-15 17:58:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class KonnectedSwitch(ToggleEntity):
|
|
|
|
"""Representation of a Konnected switch."""
|
|
|
|
|
2020-02-11 21:04:42 +00:00
|
|
|
def __init__(self, device_id, zone_num, data):
|
2019-02-14 04:35:12 +00:00
|
|
|
"""Initialize the Konnected switch."""
|
2018-05-15 17:58:00 +00:00
|
|
|
self._data = data
|
|
|
|
self._device_id = device_id
|
2020-02-11 21:04:42 +00:00
|
|
|
self._zone_num = zone_num
|
2018-05-15 17:58:00 +00:00
|
|
|
self._activation = self._data.get(CONF_ACTIVATION, STATE_HIGH)
|
2018-08-14 19:15:33 +00:00
|
|
|
self._momentary = self._data.get(CONF_MOMENTARY)
|
|
|
|
self._pause = self._data.get(CONF_PAUSE)
|
|
|
|
self._repeat = self._data.get(CONF_REPEAT)
|
2018-05-17 18:19:05 +00:00
|
|
|
self._state = self._boolean_state(self._data.get(ATTR_STATE))
|
2019-03-04 15:56:41 +00:00
|
|
|
self._name = self._data.get(CONF_NAME)
|
2020-02-28 11:39:29 +00:00
|
|
|
self._unique_id = (
|
|
|
|
f"{device_id}-{self._zone_num}-{self._momentary}-"
|
|
|
|
f"{self._pause}-{self._repeat}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-03-04 15:56:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return the unique id."""
|
|
|
|
return self._unique_id
|
2018-05-15 17:58:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the switch."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the status of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
2018-08-24 21:29:25 +00:00
|
|
|
@property
|
2020-02-11 21:04:42 +00:00
|
|
|
def panel(self):
|
2018-08-24 21:29:25 +00:00
|
|
|
"""Return the Konnected HTTP client."""
|
2020-02-11 21:04:42 +00:00
|
|
|
device_data = self.hass.data[KONNECTED_DOMAIN][CONF_DEVICES][self._device_id]
|
|
|
|
return device_data.get("panel")
|
2018-08-24 21:29:25 +00:00
|
|
|
|
2020-02-11 21:04:42 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return the device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(KONNECTED_DOMAIN, self._device_id)},
|
|
|
|
}
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
2018-05-15 17:58:00 +00:00
|
|
|
"""Send a command to turn on the switch."""
|
2020-02-11 21:04:42 +00:00
|
|
|
resp = await self.panel.update_switch(
|
|
|
|
self._zone_num,
|
2018-08-14 19:15:33 +00:00
|
|
|
int(self._activation == STATE_HIGH),
|
|
|
|
self._momentary,
|
|
|
|
self._repeat,
|
2019-07-31 19:25:30 +00:00
|
|
|
self._pause,
|
2018-08-14 19:15:33 +00:00
|
|
|
)
|
2018-05-17 18:19:05 +00:00
|
|
|
|
|
|
|
if resp.get(ATTR_STATE) is not None:
|
2018-08-14 19:15:33 +00:00
|
|
|
self._set_state(True)
|
|
|
|
|
|
|
|
if self._momentary and resp.get(ATTR_STATE) != -1:
|
|
|
|
# Immediately set the state back off for momentary switches
|
2018-09-14 19:31:41 +00:00
|
|
|
self._set_state(False)
|
2018-05-15 17:58:00 +00:00
|
|
|
|
2020-02-11 21:04:42 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2018-05-15 17:58:00 +00:00
|
|
|
"""Send a command to turn off the switch."""
|
2020-02-11 21:04:42 +00:00
|
|
|
resp = await self.panel.update_switch(
|
|
|
|
self._zone_num, int(self._activation == STATE_LOW)
|
|
|
|
)
|
2018-05-17 18:19:05 +00:00
|
|
|
|
|
|
|
if resp.get(ATTR_STATE) is not None:
|
|
|
|
self._set_state(self._boolean_state(resp.get(ATTR_STATE)))
|
|
|
|
|
|
|
|
def _boolean_state(self, int_state):
|
|
|
|
if int_state is None:
|
|
|
|
return False
|
|
|
|
if int_state == 0:
|
|
|
|
return self._activation == STATE_LOW
|
|
|
|
if int_state == 1:
|
|
|
|
return self._activation == STATE_HIGH
|
2018-05-15 17:58:00 +00:00
|
|
|
|
|
|
|
def _set_state(self, state):
|
|
|
|
self._state = state
|
2020-02-11 21:04:42 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug(
|
2020-02-11 21:04:42 +00:00
|
|
|
"Setting status of %s actuator zone %s to %s",
|
2019-07-31 19:25:30 +00:00
|
|
|
self._device_id,
|
|
|
|
self.name,
|
|
|
|
state,
|
|
|
|
)
|
2018-05-15 17:58:00 +00:00
|
|
|
|
2018-05-17 18:19:05 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Store entity_id."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._data["entity_id"] = self.entity_id
|