Home Assistant Unofficial Reference 2024.12.1
handlers.py
Go to the documentation of this file.
1 """Handle Konnected messages."""
2 
3 import logging
4 
5 from homeassistant.components.sensor import SensorDeviceClass
6 from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE
7 from homeassistant.helpers.dispatcher import async_dispatcher_send
8 from homeassistant.util import decorator
9 
10 from .const import CONF_INVERSE, SIGNAL_DS18B20_NEW
11 
12 _LOGGER = logging.getLogger(__name__)
13 HANDLERS = decorator.Registry() # type: ignore[var-annotated]
14 
15 
16 @HANDLERS.register("state")
17 async def async_handle_state_update(hass, context, msg):
18  """Handle a binary sensor or switch state update."""
19  _LOGGER.debug("[state handler] context: %s msg: %s", context, msg)
20  entity_id = context.get(ATTR_ENTITY_ID)
21  state = bool(int(msg.get(ATTR_STATE)))
22  if context.get(CONF_INVERSE):
23  state = not state
24 
25  async_dispatcher_send(hass, f"konnected.{entity_id}.update", state)
26 
27 
28 @HANDLERS.register("temp")
29 async def async_handle_temp_update(hass, context, msg):
30  """Handle a temperature sensor state update."""
31  _LOGGER.debug("[temp handler] context: %s msg: %s", context, msg)
32  entity_id, temp = context.get(SensorDeviceClass.TEMPERATURE), msg.get("temp")
33  if entity_id:
34  async_dispatcher_send(hass, f"konnected.{entity_id}.update", temp)
35 
36 
37 @HANDLERS.register("humi")
38 async def async_handle_humi_update(hass, context, msg):
39  """Handle a humidity sensor state update."""
40  _LOGGER.debug("[humi handler] context: %s msg: %s", context, msg)
41  entity_id, humi = context.get(SensorDeviceClass.HUMIDITY), msg.get("humi")
42  if entity_id:
43  async_dispatcher_send(hass, f"konnected.{entity_id}.update", humi)
44 
45 
46 @HANDLERS.register("addr")
47 async def async_handle_addr_update(hass, context, msg):
48  """Handle an addressable sensor update."""
49  _LOGGER.debug("[addr handler] context: %s msg: %s", context, msg)
50  addr, temp = msg.get("addr"), msg.get("temp")
51  if entity_id := context.get(addr):
52  async_dispatcher_send(hass, f"konnected.{entity_id}.update", temp)
53  else:
54  msg["device_id"] = context.get("device_id")
55  msg["temperature"] = temp
56  msg["addr"] = addr
57  async_dispatcher_send(hass, SIGNAL_DS18B20_NEW, msg)
def async_handle_addr_update(hass, context, msg)
Definition: handlers.py:47
def async_handle_state_update(hass, context, msg)
Definition: handlers.py:17
def async_handle_humi_update(hass, context, msg)
Definition: handlers.py:38
def async_handle_temp_update(hass, context, msg)
Definition: handlers.py:29
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193