diff --git a/homeassistant/components/konnected.py b/homeassistant/components/konnected.py index a3e9ff86ed012a..37a0aed5e61a24 100644 --- a/homeassistant/components/konnected.py +++ b/homeassistant/components/konnected.py @@ -32,6 +32,10 @@ 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 @@ 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 @@ def save_data(self): 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 @@ def save_data(self): else: initial_state = None - actuators[pin] = { + a = { + 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(a) + _LOGGER.debug('Set up actuator %s', a) device_data = { 'client': self.client, @@ -237,11 +249,10 @@ def sensor_configuration(self): 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 @@ async def put(self, request: Request, device_id, 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', diff --git a/homeassistant/components/switch/konnected.py b/homeassistant/components/switch/konnected.py index 53c6406b28a896..5223d14383eb9c 100644 --- a/homeassistant/components/switch/konnected.py +++ b/homeassistant/components/switch/konnected.py @@ -8,10 +8,11 @@ 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 @@ def __init__(self, device_id, pin_num, data, client): 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,20 @@ def is_on(self): 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 + ) + + """Update state based on response from device""" if resp.get(ATTR_STATE) is not None: - self._set_state(self._boolean_state(resp.get(ATTR_STATE))) + self._set_state(True) + + """Immediately set the state back off for momentary switches""" + if self._momentary and resp.get(ATTR_STATE) != -1: + self._set_state(self._boolean_state(False)) def turn_off(self, **kwargs): """Send a command to turn off the switch."""