Handle switch state updates from Konnected device (#48167)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
9739707f62
commit
2912db84d7
3 changed files with 22 additions and 4 deletions
|
@ -360,9 +360,15 @@ 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 = (
|
||||||
|
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
|
(s for s in device[CONF_SENSORS] if s[CONF_ZONE] == zone_num), None
|
||||||
)
|
)
|
||||||
|
)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
zone_data = None
|
zone_data = None
|
||||||
|
|
||||||
|
|
|
@ -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)))
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue