Skip to content

Commit

Permalink
adds support for momentary and beep/blink switches
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisisnate committed Aug 14, 2018
1 parent 3fbb56d commit c279ff9
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
34 changes: 22 additions & 12 deletions homeassistant/components/konnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
)

Expand Down Expand Up @@ -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']]
Expand All @@ -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,
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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',
Expand Down
32 changes: 23 additions & 9 deletions homeassistant/components/switch/konnected.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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)


Expand All @@ -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(
Expand All @@ -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."""
Expand Down

0 comments on commit c279ff9

Please sign in to comment.