Merge pull request #1034 from MartinHjelmare/refactor-s_v_types

Mysensors: Refactor s_types and v_types mapping
This commit is contained in:
Martin Hjelmare 2016-01-30 01:04:06 +01:00
commit a8f7bc2324
3 changed files with 70 additions and 67 deletions

View File

@ -113,8 +113,7 @@ def setup(hass, config):
return True return True
def pf_callback_factory( def pf_callback_factory(map_sv_types, devices, add_devices, entity_class):
s_types, v_types, devices, add_devices, entity_class):
"""Return a new callback for the platform.""" """Return a new callback for the platform."""
def mysensors_callback(gateway, node_id): def mysensors_callback(gateway, node_id):
"""Callback for mysensors platform.""" """Callback for mysensors platform."""
@ -125,7 +124,8 @@ def pf_callback_factory(
for child in gateway.sensors[node_id].children.values(): for child in gateway.sensors[node_id].children.values():
for value_type in child.values.keys(): for value_type in child.values.keys():
key = node_id, child.id, value_type key = node_id, child.id, value_type
if child.type not in s_types or value_type not in v_types: if child.type not in map_sv_types or \
value_type not in map_sv_types[child.type]:
continue continue
if key in devices: if key in devices:
devices[key].update_ha_state(True) devices[key].update_ha_state(True)

View File

@ -30,51 +30,57 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for gateway in mysensors.GATEWAYS.values(): for gateway in mysensors.GATEWAYS.values():
# Define the S_TYPES and V_TYPES that the platform should handle as # Define the S_TYPES and V_TYPES that the platform should handle as
# states. # states. Map them in a defaultdict(list).
s_types = [ pres = gateway.const.Presentation
gateway.const.Presentation.S_DOOR, set_req = gateway.const.SetReq
gateway.const.Presentation.S_MOTION, map_sv_types = {
gateway.const.Presentation.S_SMOKE, pres.S_DOOR: [set_req.V_TRIPPED],
gateway.const.Presentation.S_TEMP, pres.S_MOTION: [set_req.V_TRIPPED],
gateway.const.Presentation.S_HUM, pres.S_SMOKE: [set_req.V_TRIPPED],
gateway.const.Presentation.S_BARO, pres.S_TEMP: [set_req.V_TEMP],
gateway.const.Presentation.S_WIND, pres.S_HUM: [set_req.V_HUM],
gateway.const.Presentation.S_RAIN, pres.S_BARO: [set_req.V_PRESSURE, set_req.V_FORECAST],
gateway.const.Presentation.S_UV, pres.S_WIND: [set_req.V_WIND, set_req.V_GUST],
gateway.const.Presentation.S_WEIGHT, pres.S_RAIN: [set_req.V_RAIN, set_req.V_RAINRATE],
gateway.const.Presentation.S_POWER, pres.S_UV: [set_req.V_UV],
gateway.const.Presentation.S_DISTANCE, pres.S_WEIGHT: [set_req.V_WEIGHT, set_req.V_IMPEDANCE],
gateway.const.Presentation.S_LIGHT_LEVEL, pres.S_POWER: [set_req.V_WATT, set_req.V_KWH],
gateway.const.Presentation.S_IR, pres.S_DISTANCE: [set_req.V_DISTANCE],
gateway.const.Presentation.S_WATER, pres.S_LIGHT_LEVEL: [set_req.V_LIGHT_LEVEL],
gateway.const.Presentation.S_AIR_QUALITY, pres.S_IR: [set_req.V_IR_SEND, set_req.V_IR_RECEIVE],
gateway.const.Presentation.S_CUSTOM, pres.S_WATER: [set_req.V_FLOW, set_req.V_VOLUME],
gateway.const.Presentation.S_DUST, pres.S_CUSTOM: [set_req.V_VAR1,
gateway.const.Presentation.S_SCENE_CONTROLLER, set_req.V_VAR2,
] set_req.V_VAR3,
not_v_types = [ set_req.V_VAR4,
gateway.const.SetReq.V_ARMED, set_req.V_VAR5],
gateway.const.SetReq.V_LIGHT, pres.S_SCENE_CONTROLLER: [set_req.V_SCENE_ON,
gateway.const.SetReq.V_LOCK_STATUS, set_req.V_SCENE_OFF],
gateway.const.SetReq.V_UNIT_PREFIX, }
] if float(gateway.version) < 1.5:
map_sv_types.update({
pres.S_AIR_QUALITY: [set_req.V_DUST_LEVEL],
pres.S_DUST: [set_req.V_DUST_LEVEL],
})
if float(gateway.version) >= 1.5: if float(gateway.version) >= 1.5:
s_types.extend([ map_sv_types.update({
gateway.const.Presentation.S_COLOR_SENSOR, pres.S_COLOR_SENSOR: [set_req.V_RGB],
gateway.const.Presentation.S_MULTIMETER, pres.S_MULTIMETER: [set_req.V_VOLTAGE,
gateway.const.Presentation.S_SPRINKLER, set_req.V_CURRENT,
gateway.const.Presentation.S_WATER_LEAK, set_req.V_IMPEDANCE],
gateway.const.Presentation.S_SOUND, pres.S_SPRINKLER: [set_req.V_TRIPPED],
gateway.const.Presentation.S_VIBRATION, pres.S_WATER_LEAK: [set_req.V_TRIPPED],
gateway.const.Presentation.S_MOISTURE, pres.S_SOUND: [set_req.V_TRIPPED, set_req.V_LEVEL],
]) pres.S_VIBRATION: [set_req.V_TRIPPED, set_req.V_LEVEL],
not_v_types.extend([gateway.const.SetReq.V_STATUS, ]) pres.S_MOISTURE: [set_req.V_TRIPPED, set_req.V_LEVEL],
v_types = [member for member in gateway.const.SetReq pres.S_AIR_QUALITY: [set_req.V_LEVEL],
if member.value not in not_v_types] pres.S_DUST: [set_req.V_LEVEL],
})
map_sv_types[pres.S_LIGHT_LEVEL].append(set_req.V_LEVEL)
devices = {} devices = {}
gateway.platform_callbacks.append(mysensors.pf_callback_factory( gateway.platform_callbacks.append(mysensors.pf_callback_factory(
s_types, v_types, devices, add_devices, MySensorsSensor)) map_sv_types, devices, add_devices, MySensorsSensor))
class MySensorsSensor(Entity): class MySensorsSensor(Entity):

View File

@ -29,33 +29,30 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
for gateway in mysensors.GATEWAYS.values(): for gateway in mysensors.GATEWAYS.values():
# Define the S_TYPES and V_TYPES that the platform should handle as # Define the S_TYPES and V_TYPES that the platform should handle as
# states. # states. Map them in a defaultdict(list).
s_types = [ pres = gateway.const.Presentation
gateway.const.Presentation.S_DOOR, set_req = gateway.const.SetReq
gateway.const.Presentation.S_MOTION, map_sv_types = {
gateway.const.Presentation.S_SMOKE, pres.S_DOOR: [set_req.V_ARMED],
gateway.const.Presentation.S_LIGHT, pres.S_MOTION: [set_req.V_ARMED],
gateway.const.Presentation.S_LOCK, pres.S_SMOKE: [set_req.V_ARMED],
] pres.S_LIGHT: [set_req.V_LIGHT],
v_types = [ pres.S_LOCK: [set_req.V_LOCK_STATUS],
gateway.const.SetReq.V_ARMED, }
gateway.const.SetReq.V_LIGHT,
gateway.const.SetReq.V_LOCK_STATUS,
]
if float(gateway.version) >= 1.5: if float(gateway.version) >= 1.5:
s_types.extend([ map_sv_types.update({
gateway.const.Presentation.S_BINARY, pres.S_BINARY: [set_req.V_STATUS, set_req.V_LIGHT],
gateway.const.Presentation.S_SPRINKLER, pres.S_SPRINKLER: [set_req.V_STATUS],
gateway.const.Presentation.S_WATER_LEAK, pres.S_WATER_LEAK: [set_req.V_ARMED],
gateway.const.Presentation.S_SOUND, pres.S_SOUND: [set_req.V_ARMED],
gateway.const.Presentation.S_VIBRATION, pres.S_VIBRATION: [set_req.V_ARMED],
gateway.const.Presentation.S_MOISTURE, pres.S_MOISTURE: [set_req.V_ARMED],
]) })
v_types.extend([gateway.const.SetReq.V_STATUS, ]) map_sv_types[pres.S_LIGHT].append(set_req.V_STATUS)
devices = {} devices = {}
gateway.platform_callbacks.append(mysensors.pf_callback_factory( gateway.platform_callbacks.append(mysensors.pf_callback_factory(
s_types, v_types, devices, add_devices, MySensorsSwitch)) map_sv_types, devices, add_devices, MySensorsSwitch))
class MySensorsSwitch(SwitchDevice): class MySensorsSwitch(SwitchDevice):