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_PIN,
|
|
|
|
CONF_SWITCHES,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
|
|
|
|
|
|
|
from . 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__)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-05-15 17:58:00 +00:00
|
|
|
"""Set switches attached to a Konnected device."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2018-05-17 18:19:05 +00:00
|
|
|
data = hass.data[KONNECTED_DOMAIN]
|
2019-07-31 19:25:30 +00:00
|
|
|
device_id = discovery_info["device_id"]
|
2018-08-14 19:15:33 +00:00
|
|
|
switches = [
|
2018-08-24 21:29:25 +00:00
|
|
|
KonnectedSwitch(device_id, pin_data.get(CONF_PIN), pin_data)
|
2019-07-31 19:25:30 +00:00
|
|
|
for pin_data in data[CONF_DEVICES][device_id][CONF_SWITCHES]
|
|
|
|
]
|
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."""
|
|
|
|
|
2018-08-24 21:29:25 +00:00
|
|
|
def __init__(self, device_id, pin_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
|
|
|
|
self._pin_num = pin_num
|
|
|
|
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)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._unique_id = "{}-{}-{}-{}-{}".format(
|
|
|
|
device_id, self._pin_num, self._momentary, self._pause, self._repeat
|
|
|
|
)
|
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
|
|
|
|
def client(self):
|
|
|
|
"""Return the Konnected HTTP client."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.hass.data[KONNECTED_DOMAIN][CONF_DEVICES][self._device_id].get(
|
|
|
|
"client"
|
|
|
|
)
|
2018-08-24 21:29:25 +00:00
|
|
|
|
2018-05-15 17:58:00 +00:00
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Send a command to turn on the switch."""
|
2018-08-24 21:29:25 +00:00
|
|
|
resp = self.client.put_device(
|
2018-08-14 19:15:33 +00:00
|
|
|
self._pin_num,
|
|
|
|
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
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Send a command to turn off the switch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
resp = self.client.put_device(self._pin_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
|
|
|
|
self.schedule_update_ha_state()
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Setting status of %s actuator pin %s to %s",
|
|
|
|
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
|