2019-03-04 15:56:41 +00:00
|
|
|
"""Handle Konnected messages."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
ATTR_STATE,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
2019-10-21 08:21:40 +00:00
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-10-21 08:21:40 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.util import decorator
|
2019-03-04 15:56:41 +00:00
|
|
|
|
2019-10-21 08:21:40 +00:00
|
|
|
from .const import CONF_INVERSE, SIGNAL_DS18B20_NEW, SIGNAL_SENSOR_UPDATE
|
2019-03-04 15:56:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
HANDLERS = decorator.Registry()
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@HANDLERS.register("state")
|
2019-03-04 15:56:41 +00:00
|
|
|
async def async_handle_state_update(hass, context, msg):
|
|
|
|
"""Handle a binary sensor state update."""
|
|
|
|
_LOGGER.debug("[state handler] context: %s msg: %s", context, msg)
|
|
|
|
entity_id = context.get(ATTR_ENTITY_ID)
|
|
|
|
state = bool(int(msg.get(ATTR_STATE)))
|
2019-03-28 02:54:01 +00:00
|
|
|
if context.get(CONF_INVERSE):
|
2019-03-04 15:56:41 +00:00
|
|
|
state = not state
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE.format(entity_id), state)
|
2019-03-04 15:56:41 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@HANDLERS.register("temp")
|
2019-03-04 15:56:41 +00:00
|
|
|
async def async_handle_temp_update(hass, context, msg):
|
|
|
|
"""Handle a temperature sensor state update."""
|
|
|
|
_LOGGER.debug("[temp handler] context: %s msg: %s", context, msg)
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id, temp = context.get(DEVICE_CLASS_TEMPERATURE), msg.get("temp")
|
2019-03-04 15:56:41 +00:00
|
|
|
if entity_id:
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE.format(entity_id), temp)
|
2019-03-04 15:56:41 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@HANDLERS.register("humi")
|
2019-03-04 15:56:41 +00:00
|
|
|
async def async_handle_humi_update(hass, context, msg):
|
|
|
|
"""Handle a humidity sensor state update."""
|
|
|
|
_LOGGER.debug("[humi handler] context: %s msg: %s", context, msg)
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id, humi = context.get(DEVICE_CLASS_HUMIDITY), msg.get("humi")
|
2019-03-04 15:56:41 +00:00
|
|
|
if entity_id:
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE.format(entity_id), humi)
|
2019-03-04 15:56:41 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@HANDLERS.register("addr")
|
2019-03-04 15:56:41 +00:00
|
|
|
async def async_handle_addr_update(hass, context, msg):
|
|
|
|
"""Handle an addressable sensor update."""
|
|
|
|
_LOGGER.debug("[addr handler] context: %s msg: %s", context, msg)
|
2019-07-31 19:25:30 +00:00
|
|
|
addr, temp = msg.get("addr"), msg.get("temp")
|
2019-03-04 15:56:41 +00:00
|
|
|
entity_id = context.get(addr)
|
|
|
|
if entity_id:
|
2019-07-31 19:25:30 +00:00
|
|
|
async_dispatcher_send(hass, SIGNAL_SENSOR_UPDATE.format(entity_id), temp)
|
2019-03-04 15:56:41 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
msg["device_id"] = context.get("device_id")
|
|
|
|
msg["temperature"] = temp
|
|
|
|
msg["addr"] = addr
|
2019-03-04 15:56:41 +00:00
|
|
|
async_dispatcher_send(hass, SIGNAL_DS18B20_NEW, msg)
|