Handle switch state updates from Konnected device (#48167)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Nate Clark 2021-03-21 19:16:34 -04:00 committed by GitHub
parent 9739707f62
commit 2912db84d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View file

@ -360,8 +360,14 @@ class KonnectedView(HomeAssistantView):
try: try:
zone_num = str(payload.get(CONF_ZONE) or PIN_TO_ZONE[payload[CONF_PIN]]) zone_num = str(payload.get(CONF_ZONE) or PIN_TO_ZONE[payload[CONF_PIN]])
payload[CONF_ZONE] = zone_num payload[CONF_ZONE] = zone_num
zone_data = device[CONF_BINARY_SENSORS].get(zone_num) or next( zone_data = (
(s for s in device[CONF_SENSORS] if s[CONF_ZONE] == zone_num), None device[CONF_BINARY_SENSORS].get(zone_num)
or next(
(s for s in device[CONF_SWITCHES] if s[CONF_ZONE] == zone_num), None
)
or next(
(s for s in device[CONF_SENSORS] if s[CONF_ZONE] == zone_num), None
)
) )
except KeyError: except KeyError:
zone_data = None zone_data = None

View file

@ -18,7 +18,7 @@ HANDLERS = decorator.Registry()
@HANDLERS.register("state") @HANDLERS.register("state")
async def async_handle_state_update(hass, context, msg): async def async_handle_state_update(hass, context, msg):
"""Handle a binary sensor state update.""" """Handle a binary sensor or switch state update."""
_LOGGER.debug("[state handler] context: %s msg: %s", context, msg) _LOGGER.debug("[state handler] context: %s msg: %s", context, msg)
entity_id = context.get(ATTR_ENTITY_ID) entity_id = context.get(ATTR_ENTITY_ID)
state = bool(int(msg.get(ATTR_STATE))) state = bool(int(msg.get(ATTR_STATE)))

View file

@ -9,6 +9,8 @@ from homeassistant.const import (
CONF_SWITCHES, CONF_SWITCHES,
CONF_ZONE, CONF_ZONE,
) )
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
from .const import ( from .const import (
@ -130,6 +132,16 @@ class KonnectedSwitch(ToggleEntity):
state, state,
) )
@callback
def async_set_state(self, state):
"""Update the switch state."""
self._set_state(state)
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Store entity_id.""" """Store entity_id and register state change callback."""
self._data["entity_id"] = self.entity_id self._data["entity_id"] = self.entity_id
self.async_on_remove(
async_dispatcher_connect(
self.hass, f"konnected.{self.entity_id}.update", self.async_set_state
)
)