mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Add S_TYPES to platform type and fix persistence
* Add S_TYPES to platform type. * Fix persistence update on startup. * Clean up code.
This commit is contained in:
parent
9463c84603
commit
1e52d5c7f2
@ -12,6 +12,7 @@ import logging
|
|||||||
from homeassistant.helpers import (validate_config)
|
from homeassistant.helpers import (validate_config)
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
EVENT_HOMEASSISTANT_START,
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
TEMP_CELCIUS)
|
TEMP_CELCIUS)
|
||||||
|
|
||||||
@ -94,9 +95,15 @@ def setup(hass, config): # noqa
|
|||||||
gateway.debug = config[DOMAIN].get(CONF_DEBUG, False)
|
gateway.debug = config[DOMAIN].get(CONF_DEBUG, False)
|
||||||
gateway.start()
|
gateway.start()
|
||||||
|
|
||||||
|
def persistence_update(event):
|
||||||
|
"""Callback to trigger update from persistence file."""
|
||||||
|
for _ in range(2):
|
||||||
|
for nid in gateway.sensors:
|
||||||
|
gateway.event_callback('persistence', nid)
|
||||||
|
|
||||||
if persistence:
|
if persistence:
|
||||||
for nid in gateway.sensors:
|
hass.bus.listen_once(
|
||||||
gateway.event_callback('node_update', nid)
|
EVENT_HOMEASSISTANT_START, persistence_update)
|
||||||
|
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||||
lambda event: gateway.stop())
|
lambda event: gateway.stop())
|
||||||
@ -134,33 +141,52 @@ def mysensors_update(platform_type):
|
|||||||
"""Decorator for callback function for mysensor updates."""
|
"""Decorator for callback function for mysensor updates."""
|
||||||
def wrapper(gateway, port, devices, nid):
|
def wrapper(gateway, port, devices, nid):
|
||||||
"""Wrapper function in the decorator."""
|
"""Wrapper function in the decorator."""
|
||||||
sensor = gateway.sensors[nid]
|
if gateway.sensors[nid].sketch_name is None:
|
||||||
if sensor.sketch_name is None:
|
|
||||||
_LOGGER.info('No sketch_name: node %s', nid)
|
_LOGGER.info('No sketch_name: node %s', nid)
|
||||||
return
|
return
|
||||||
if nid not in devices:
|
if nid not in devices:
|
||||||
devices[nid] = {}
|
devices[nid] = {}
|
||||||
node = devices[nid]
|
node = devices[nid]
|
||||||
new_devices = []
|
new_devices = []
|
||||||
# Get platform specific V_TYPES, class and add_devices function.
|
# Get platform specific S_TYPES, V_TYPES, class and add_devices.
|
||||||
platform_v_types, platform_class, add_devices = platform_type(
|
(platform_s_types,
|
||||||
gateway, port, devices, nid)
|
platform_v_types,
|
||||||
for child_id, child in sensor.children.items():
|
platform_class,
|
||||||
|
add_devices) = platform_type(gateway, port, devices, nid)
|
||||||
|
for child_id, child in gateway.sensors[nid].children.items():
|
||||||
if child_id not in node:
|
if child_id not in node:
|
||||||
node[child_id] = {}
|
node[child_id] = {}
|
||||||
for value_type, _ in child.values.items():
|
for value_type, _ in child.values.items():
|
||||||
if ((value_type not in node[child_id]) and
|
if (value_type not in node[child_id] and
|
||||||
(value_type in platform_v_types)):
|
child.type in platform_s_types and
|
||||||
|
value_type in platform_v_types):
|
||||||
name = '{} {}.{}'.format(
|
name = '{} {}.{}'.format(
|
||||||
sensor.sketch_name, nid, child.id)
|
gateway.sensors[nid].sketch_name, nid, child.id)
|
||||||
node[child_id][value_type] = platform_class(
|
node[child_id][value_type] = platform_class(
|
||||||
port, nid, child_id, name, value_type)
|
port, nid, child_id, name, value_type)
|
||||||
new_devices.append(node[child_id][value_type])
|
new_devices.append(node[child_id][value_type])
|
||||||
elif value_type in platform_v_types:
|
elif (child.type in platform_s_types and
|
||||||
|
value_type in platform_v_types):
|
||||||
node[child_id][value_type].update_sensor(
|
node[child_id][value_type].update_sensor(
|
||||||
child.values, sensor.battery_level)
|
child.values, gateway.sensors[nid].battery_level)
|
||||||
if new_devices:
|
if new_devices:
|
||||||
_LOGGER.info('adding new devices: %s', new_devices)
|
_LOGGER.info('adding new devices: %s', new_devices)
|
||||||
add_devices(new_devices)
|
add_devices(new_devices)
|
||||||
return
|
return
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def event_update(update):
|
||||||
|
"""Decorator for callback function for mysensor event updates."""
|
||||||
|
def wrapper(event):
|
||||||
|
"""Wrapper function in the decorator."""
|
||||||
|
_LOGGER.info(
|
||||||
|
'update %s: node %s', event.data[ATTR_UPDATE_TYPE],
|
||||||
|
event.data[ATTR_NODE_ID])
|
||||||
|
sensor_update = update(event)
|
||||||
|
sensor_update(GATEWAYS[event.data[ATTR_PORT]],
|
||||||
|
event.data[ATTR_PORT],
|
||||||
|
event.data[ATTR_DEVICES],
|
||||||
|
event.data[ATTR_NODE_ID])
|
||||||
|
return
|
||||||
|
return wrapper
|
||||||
|
@ -23,31 +23,49 @@ DEPENDENCIES = ['mysensors']
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the mysensors platform for sensors."""
|
"""Setup the mysensors platform for sensors."""
|
||||||
# Define the V_TYPES that the platform should handle as states.
|
# Define the S_TYPES and V_TYPES that the platform should handle as states.
|
||||||
|
s_types = [
|
||||||
|
mysensors.CONST.Presentation.S_TEMP,
|
||||||
|
mysensors.CONST.Presentation.S_HUM,
|
||||||
|
mysensors.CONST.Presentation.S_BARO,
|
||||||
|
mysensors.CONST.Presentation.S_WIND,
|
||||||
|
mysensors.CONST.Presentation.S_RAIN,
|
||||||
|
mysensors.CONST.Presentation.S_UV,
|
||||||
|
mysensors.CONST.Presentation.S_WEIGHT,
|
||||||
|
mysensors.CONST.Presentation.S_POWER,
|
||||||
|
mysensors.CONST.Presentation.S_DISTANCE,
|
||||||
|
mysensors.CONST.Presentation.S_LIGHT_LEVEL,
|
||||||
|
mysensors.CONST.Presentation.S_IR,
|
||||||
|
mysensors.CONST.Presentation.S_WATER,
|
||||||
|
mysensors.CONST.Presentation.S_AIR_QUALITY,
|
||||||
|
mysensors.CONST.Presentation.S_CUSTOM,
|
||||||
|
mysensors.CONST.Presentation.S_DUST,
|
||||||
|
mysensors.CONST.Presentation.S_SCENE_CONTROLLER,
|
||||||
|
mysensors.CONST.Presentation.S_COLOR_SENSOR,
|
||||||
|
mysensors.CONST.Presentation.S_MULTIMETER,
|
||||||
|
]
|
||||||
|
not_v_types = [
|
||||||
|
mysensors.CONST.SetReq.V_ARMED,
|
||||||
|
mysensors.CONST.SetReq.V_STATUS,
|
||||||
|
mysensors.CONST.SetReq.V_LIGHT,
|
||||||
|
mysensors.CONST.SetReq.V_LOCK_STATUS,
|
||||||
|
]
|
||||||
v_types = []
|
v_types = []
|
||||||
for _, member in mysensors.CONST.SetReq.__members__.items():
|
for _, member in mysensors.CONST.SetReq.__members__.items():
|
||||||
if (member.value != mysensors.CONST.SetReq.V_ARMED and
|
if all(test != member.value for test in not_v_types):
|
||||||
member.value != mysensors.CONST.SetReq.V_STATUS and
|
|
||||||
member.value != mysensors.CONST.SetReq.V_LIGHT and
|
|
||||||
member.value != mysensors.CONST.SetReq.V_LOCK_STATUS):
|
|
||||||
v_types.append(member)
|
v_types.append(member)
|
||||||
|
|
||||||
@mysensors.mysensors_update
|
@mysensors.mysensors_update
|
||||||
def _sensor_update(gateway, port, devices, nid):
|
def _sensor_update(gateway, port, devices, nid):
|
||||||
"""Internal callback for sensor updates."""
|
"""Internal callback for sensor updates."""
|
||||||
return (v_types, MySensorsSensor, add_devices)
|
return (s_types, v_types, MySensorsSensor, add_devices)
|
||||||
|
|
||||||
def sensor_update(event):
|
@mysensors.event_update
|
||||||
"""Callback for sensor updates from the MySensors component."""
|
def event_update(event):
|
||||||
_LOGGER.info(
|
"""Callback for event updates from the MySensors component."""
|
||||||
'update %s: node %s', event.data[mysensors.ATTR_UPDATE_TYPE],
|
return _sensor_update
|
||||||
event.data[mysensors.ATTR_NODE_ID])
|
|
||||||
_sensor_update(mysensors.GATEWAYS[event.data[mysensors.ATTR_PORT]],
|
|
||||||
event.data[mysensors.ATTR_PORT],
|
|
||||||
event.data[mysensors.ATTR_DEVICES],
|
|
||||||
event.data[mysensors.ATTR_NODE_ID])
|
|
||||||
|
|
||||||
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, sensor_update)
|
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, event_update)
|
||||||
|
|
||||||
|
|
||||||
class MySensorsSensor(Entity):
|
class MySensorsSensor(Entity):
|
||||||
|
@ -22,31 +22,38 @@ DEPENDENCIES = ['mysensors']
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Setup the mysensors platform for switches."""
|
"""Setup the mysensors platform for switches."""
|
||||||
# Define the V_TYPES that the platform should handle as states.
|
# Define the S_TYPES and V_TYPES that the platform should handle as states.
|
||||||
v_types = []
|
s_types = [
|
||||||
for _, member in mysensors.CONST.SetReq.__members__.items():
|
mysensors.CONST.Presentation.S_DOOR,
|
||||||
if (member.value == mysensors.CONST.SetReq.V_ARMED or
|
mysensors.CONST.Presentation.S_MOTION,
|
||||||
member.value == mysensors.CONST.SetReq.V_STATUS or
|
mysensors.CONST.Presentation.S_SMOKE,
|
||||||
member.value == mysensors.CONST.SetReq.V_LIGHT or
|
mysensors.CONST.Presentation.S_LIGHT,
|
||||||
member.value == mysensors.CONST.SetReq.V_LOCK_STATUS):
|
mysensors.CONST.Presentation.S_BINARY,
|
||||||
v_types.append(member)
|
mysensors.CONST.Presentation.S_LOCK,
|
||||||
|
mysensors.CONST.Presentation.S_SPRINKLER,
|
||||||
|
mysensors.CONST.Presentation.S_WATER_LEAK,
|
||||||
|
mysensors.CONST.Presentation.S_SOUND,
|
||||||
|
mysensors.CONST.Presentation.S_VIBRATION,
|
||||||
|
mysensors.CONST.Presentation.S_MOISTURE,
|
||||||
|
]
|
||||||
|
v_types = [
|
||||||
|
mysensors.CONST.SetReq.V_ARMED,
|
||||||
|
mysensors.CONST.SetReq.V_STATUS,
|
||||||
|
mysensors.CONST.SetReq.V_LIGHT,
|
||||||
|
mysensors.CONST.SetReq.V_LOCK_STATUS,
|
||||||
|
]
|
||||||
|
|
||||||
@mysensors.mysensors_update
|
@mysensors.mysensors_update
|
||||||
def _sensor_update(gateway, port, devices, nid):
|
def _sensor_update(gateway, port, devices, nid):
|
||||||
"""Internal callback for sensor updates."""
|
"""Internal callback for sensor updates."""
|
||||||
return (v_types, MySensorsSwitch, add_devices)
|
return (s_types, v_types, MySensorsSwitch, add_devices)
|
||||||
|
|
||||||
def sensor_update(event):
|
@mysensors.event_update
|
||||||
"""Callback for sensor updates from the MySensors component."""
|
def event_update(event):
|
||||||
_LOGGER.info(
|
"""Callback for event updates from the MySensors component."""
|
||||||
'update %s: node %s', event.data[mysensors.ATTR_UPDATE_TYPE],
|
return _sensor_update
|
||||||
event.data[mysensors.ATTR_NODE_ID])
|
|
||||||
_sensor_update(mysensors.GATEWAYS[event.data[mysensors.ATTR_PORT]],
|
|
||||||
event.data[mysensors.ATTR_PORT],
|
|
||||||
event.data[mysensors.ATTR_DEVICES],
|
|
||||||
event.data[mysensors.ATTR_NODE_ID])
|
|
||||||
|
|
||||||
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, sensor_update)
|
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, event_update)
|
||||||
|
|
||||||
|
|
||||||
class MySensorsSwitch(SwitchDevice):
|
class MySensorsSwitch(SwitchDevice):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user