adds support for momentary and beep/blink switches (#15973)

pull/15978/head
Nate Clark 2018-08-14 15:15:33 -04:00 committed by Paulus Schoutsen
parent e776f88eec
commit c7f7912bca
2 changed files with 43 additions and 20 deletions

View File

@ -32,6 +32,10 @@ DOMAIN = 'konnected'
CONF_ACTIVATION = 'activation'
CONF_API_HOST = 'api_host'
CONF_MOMENTARY = 'momentary'
CONF_PAUSE = 'pause'
CONF_REPEAT = 'repeat'
STATE_LOW = 'low'
STATE_HIGH = 'high'
@ -53,7 +57,13 @@ _SWITCH_SCHEMA = vol.All(
vol.Exclusive(CONF_ZONE, 'a_pin'): vol.Any(*ZONE_TO_PIN),
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_ACTIVATION, default=STATE_HIGH):
vol.All(vol.Lower, vol.Any(STATE_HIGH, STATE_LOW))
vol.All(vol.Lower, vol.Any(STATE_HIGH, STATE_LOW)),
vol.Optional(CONF_MOMENTARY):
vol.All(vol.Coerce(int), vol.Range(min=10)),
vol.Optional(CONF_PAUSE):
vol.All(vol.Coerce(int), vol.Range(min=10)),
vol.Optional(CONF_REPEAT):
vol.All(vol.Coerce(int), vol.Range(min=-1)),
}), cv.has_at_least_one_key(CONF_PIN, CONF_ZONE)
)
@ -185,7 +195,7 @@ class KonnectedDevice:
sensors[pin].get('name'),
sensors[pin].get(ATTR_STATE))
actuators = {}
actuators = []
for entity in self.config().get(CONF_SWITCHES) or []:
if 'zone' in entity:
pin = ZONE_TO_PIN[entity['zone']]
@ -200,16 +210,18 @@ class KonnectedDevice:
else:
initial_state = None
actuators[pin] = {
act = {
CONF_PIN: pin,
CONF_NAME: entity.get(
CONF_NAME, 'Konnected {} Actuator {}'.format(
self.device_id[6:], PIN_TO_ZONE[pin])),
ATTR_STATE: initial_state,
CONF_ACTIVATION: entity[CONF_ACTIVATION],
}
_LOGGER.debug('Set up actuator %s (initial state: %s)',
actuators[pin].get(CONF_NAME),
actuators[pin].get(ATTR_STATE))
CONF_MOMENTARY: entity.get(CONF_MOMENTARY),
CONF_PAUSE: entity.get(CONF_PAUSE),
CONF_REPEAT: entity.get(CONF_REPEAT)}
actuators.append(act)
_LOGGER.debug('Set up actuator %s', act)
device_data = {
'client': self.client,
@ -237,11 +249,10 @@ class KonnectedDevice:
def actuator_configuration(self):
"""Return the configuration map for syncing actuators."""
return [{'pin': p,
return [{'pin': data.get(CONF_PIN),
'trigger': (0 if data.get(CONF_ACTIVATION) in [0, STATE_LOW]
else 1)}
for p, data in
self.stored_configuration[CONF_SWITCHES].items()]
for data in self.stored_configuration[CONF_SWITCHES]]
def sync_device_config(self):
"""Sync the new pin configuration to the Konnected device."""
@ -320,8 +331,7 @@ class KonnectedView(HomeAssistantView):
if device is None:
return self.json_message('unregistered device',
status_code=HTTP_BAD_REQUEST)
pin_data = device[CONF_BINARY_SENSORS].get(pin_num) or \
device[CONF_SWITCHES].get(pin_num)
pin_data = device[CONF_BINARY_SENSORS].get(pin_num)
if pin_data is None:
return self.json_message('unregistered sensor/actuator',

View File

@ -8,10 +8,11 @@ https://home-assistant.io/components/switch.konnected/
import logging
from homeassistant.components.konnected import (
DOMAIN as KONNECTED_DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION,
STATE_LOW, STATE_HIGH)
DOMAIN as KONNECTED_DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION, CONF_MOMENTARY,
CONF_PAUSE, CONF_REPEAT, STATE_LOW, STATE_HIGH)
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.const import (CONF_DEVICES, CONF_SWITCHES, ATTR_STATE)
from homeassistant.const import (
CONF_DEVICES, CONF_SWITCHES, CONF_PIN, ATTR_STATE)
_LOGGER = logging.getLogger(__name__)
@ -27,9 +28,9 @@ async def async_setup_platform(hass, config, async_add_devices,
data = hass.data[KONNECTED_DOMAIN]
device_id = discovery_info['device_id']
client = data[CONF_DEVICES][device_id]['client']
switches = [KonnectedSwitch(device_id, pin_num, pin_data, client)
for pin_num, pin_data in
data[CONF_DEVICES][device_id][CONF_SWITCHES].items()]
switches = [
KonnectedSwitch(device_id, pin_data.get(CONF_PIN), pin_data, client)
for pin_data in data[CONF_DEVICES][device_id][CONF_SWITCHES]]
async_add_devices(switches)
@ -42,6 +43,9 @@ class KonnectedSwitch(ToggleEntity):
self._device_id = device_id
self._pin_num = pin_num
self._activation = self._data.get(CONF_ACTIVATION, STATE_HIGH)
self._momentary = self._data.get(CONF_MOMENTARY)
self._pause = self._data.get(CONF_PAUSE)
self._repeat = self._data.get(CONF_REPEAT)
self._state = self._boolean_state(self._data.get(ATTR_STATE))
self._name = self._data.get(
'name', 'Konnected {} Actuator {}'.format(
@ -62,10 +66,19 @@ class KonnectedSwitch(ToggleEntity):
def turn_on(self, **kwargs):
"""Send a command to turn on the switch."""
resp = self._client.put_device(
self._pin_num, int(self._activation == STATE_HIGH))
self._pin_num,
int(self._activation == STATE_HIGH),
self._momentary,
self._repeat,
self._pause
)
if resp.get(ATTR_STATE) is not None:
self._set_state(self._boolean_state(resp.get(ATTR_STATE)))
self._set_state(True)
if self._momentary and resp.get(ATTR_STATE) != -1:
# Immediately set the state back off for momentary switches
self._set_state(self._boolean_state(False))
def turn_off(self, **kwargs):
"""Send a command to turn off the switch."""