adds support for momentary and beep/blink switches (#15973)
parent
e776f88eec
commit
c7f7912bca
|
@ -32,6 +32,10 @@ DOMAIN = 'konnected'
|
||||||
|
|
||||||
CONF_ACTIVATION = 'activation'
|
CONF_ACTIVATION = 'activation'
|
||||||
CONF_API_HOST = 'api_host'
|
CONF_API_HOST = 'api_host'
|
||||||
|
CONF_MOMENTARY = 'momentary'
|
||||||
|
CONF_PAUSE = 'pause'
|
||||||
|
CONF_REPEAT = 'repeat'
|
||||||
|
|
||||||
STATE_LOW = 'low'
|
STATE_LOW = 'low'
|
||||||
STATE_HIGH = 'high'
|
STATE_HIGH = 'high'
|
||||||
|
|
||||||
|
@ -53,7 +57,13 @@ _SWITCH_SCHEMA = vol.All(
|
||||||
vol.Exclusive(CONF_ZONE, 'a_pin'): vol.Any(*ZONE_TO_PIN),
|
vol.Exclusive(CONF_ZONE, 'a_pin'): vol.Any(*ZONE_TO_PIN),
|
||||||
vol.Optional(CONF_NAME): cv.string,
|
vol.Optional(CONF_NAME): cv.string,
|
||||||
vol.Optional(CONF_ACTIVATION, default=STATE_HIGH):
|
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)
|
}), cv.has_at_least_one_key(CONF_PIN, CONF_ZONE)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -185,7 +195,7 @@ class KonnectedDevice:
|
||||||
sensors[pin].get('name'),
|
sensors[pin].get('name'),
|
||||||
sensors[pin].get(ATTR_STATE))
|
sensors[pin].get(ATTR_STATE))
|
||||||
|
|
||||||
actuators = {}
|
actuators = []
|
||||||
for entity in self.config().get(CONF_SWITCHES) or []:
|
for entity in self.config().get(CONF_SWITCHES) or []:
|
||||||
if 'zone' in entity:
|
if 'zone' in entity:
|
||||||
pin = ZONE_TO_PIN[entity['zone']]
|
pin = ZONE_TO_PIN[entity['zone']]
|
||||||
|
@ -200,16 +210,18 @@ class KonnectedDevice:
|
||||||
else:
|
else:
|
||||||
initial_state = None
|
initial_state = None
|
||||||
|
|
||||||
actuators[pin] = {
|
act = {
|
||||||
|
CONF_PIN: pin,
|
||||||
CONF_NAME: entity.get(
|
CONF_NAME: entity.get(
|
||||||
CONF_NAME, 'Konnected {} Actuator {}'.format(
|
CONF_NAME, 'Konnected {} Actuator {}'.format(
|
||||||
self.device_id[6:], PIN_TO_ZONE[pin])),
|
self.device_id[6:], PIN_TO_ZONE[pin])),
|
||||||
ATTR_STATE: initial_state,
|
ATTR_STATE: initial_state,
|
||||||
CONF_ACTIVATION: entity[CONF_ACTIVATION],
|
CONF_ACTIVATION: entity[CONF_ACTIVATION],
|
||||||
}
|
CONF_MOMENTARY: entity.get(CONF_MOMENTARY),
|
||||||
_LOGGER.debug('Set up actuator %s (initial state: %s)',
|
CONF_PAUSE: entity.get(CONF_PAUSE),
|
||||||
actuators[pin].get(CONF_NAME),
|
CONF_REPEAT: entity.get(CONF_REPEAT)}
|
||||||
actuators[pin].get(ATTR_STATE))
|
actuators.append(act)
|
||||||
|
_LOGGER.debug('Set up actuator %s', act)
|
||||||
|
|
||||||
device_data = {
|
device_data = {
|
||||||
'client': self.client,
|
'client': self.client,
|
||||||
|
@ -237,11 +249,10 @@ class KonnectedDevice:
|
||||||
|
|
||||||
def actuator_configuration(self):
|
def actuator_configuration(self):
|
||||||
"""Return the configuration map for syncing actuators."""
|
"""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]
|
'trigger': (0 if data.get(CONF_ACTIVATION) in [0, STATE_LOW]
|
||||||
else 1)}
|
else 1)}
|
||||||
for p, data in
|
for data in self.stored_configuration[CONF_SWITCHES]]
|
||||||
self.stored_configuration[CONF_SWITCHES].items()]
|
|
||||||
|
|
||||||
def sync_device_config(self):
|
def sync_device_config(self):
|
||||||
"""Sync the new pin configuration to the Konnected device."""
|
"""Sync the new pin configuration to the Konnected device."""
|
||||||
|
@ -320,8 +331,7 @@ class KonnectedView(HomeAssistantView):
|
||||||
if device is None:
|
if device is None:
|
||||||
return self.json_message('unregistered device',
|
return self.json_message('unregistered device',
|
||||||
status_code=HTTP_BAD_REQUEST)
|
status_code=HTTP_BAD_REQUEST)
|
||||||
pin_data = device[CONF_BINARY_SENSORS].get(pin_num) or \
|
pin_data = device[CONF_BINARY_SENSORS].get(pin_num)
|
||||||
device[CONF_SWITCHES].get(pin_num)
|
|
||||||
|
|
||||||
if pin_data is None:
|
if pin_data is None:
|
||||||
return self.json_message('unregistered sensor/actuator',
|
return self.json_message('unregistered sensor/actuator',
|
||||||
|
|
|
@ -8,10 +8,11 @@ https://home-assistant.io/components/switch.konnected/
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.konnected import (
|
from homeassistant.components.konnected import (
|
||||||
DOMAIN as KONNECTED_DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION,
|
DOMAIN as KONNECTED_DOMAIN, PIN_TO_ZONE, CONF_ACTIVATION, CONF_MOMENTARY,
|
||||||
STATE_LOW, STATE_HIGH)
|
CONF_PAUSE, CONF_REPEAT, STATE_LOW, STATE_HIGH)
|
||||||
from homeassistant.helpers.entity import ToggleEntity
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -27,9 +28,9 @@ async def async_setup_platform(hass, config, async_add_devices,
|
||||||
data = hass.data[KONNECTED_DOMAIN]
|
data = hass.data[KONNECTED_DOMAIN]
|
||||||
device_id = discovery_info['device_id']
|
device_id = discovery_info['device_id']
|
||||||
client = data[CONF_DEVICES][device_id]['client']
|
client = data[CONF_DEVICES][device_id]['client']
|
||||||
switches = [KonnectedSwitch(device_id, pin_num, pin_data, client)
|
switches = [
|
||||||
for pin_num, pin_data in
|
KonnectedSwitch(device_id, pin_data.get(CONF_PIN), pin_data, client)
|
||||||
data[CONF_DEVICES][device_id][CONF_SWITCHES].items()]
|
for pin_data in data[CONF_DEVICES][device_id][CONF_SWITCHES]]
|
||||||
async_add_devices(switches)
|
async_add_devices(switches)
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,6 +43,9 @@ class KonnectedSwitch(ToggleEntity):
|
||||||
self._device_id = device_id
|
self._device_id = device_id
|
||||||
self._pin_num = pin_num
|
self._pin_num = pin_num
|
||||||
self._activation = self._data.get(CONF_ACTIVATION, STATE_HIGH)
|
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._state = self._boolean_state(self._data.get(ATTR_STATE))
|
||||||
self._name = self._data.get(
|
self._name = self._data.get(
|
||||||
'name', 'Konnected {} Actuator {}'.format(
|
'name', 'Konnected {} Actuator {}'.format(
|
||||||
|
@ -62,10 +66,19 @@ class KonnectedSwitch(ToggleEntity):
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Send a command to turn on the switch."""
|
"""Send a command to turn on the switch."""
|
||||||
resp = self._client.put_device(
|
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:
|
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):
|
def turn_off(self, **kwargs):
|
||||||
"""Send a command to turn off the switch."""
|
"""Send a command to turn off the switch."""
|
||||||
|
|
Loading…
Reference in New Issue