Skip to content

Commit

Permalink
Fix mysensors callback (#7057)
Browse files Browse the repository at this point in the history
* Fix mysensors callback

* All messages was not triggering proper updates. Fix by checking all
  child value types each update.

* Upgrade mysensors dep

* Fix pickle persistence when upgrading.
  • Loading branch information
MartinHjelmare authored and balloob committed Apr 12, 2017
1 parent ed01201 commit 4e38866
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 56 deletions.
54 changes: 28 additions & 26 deletions homeassistant/components/device_tracker/mysensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,39 @@ def mysensors_callback(gateway, msg):
"""Callback for mysensors platform."""
node = gateway.sensors[msg.node_id]
if node.sketch_name is None:
_LOGGER.info('No sketch_name: node %s', msg.node_id)
_LOGGER.debug('No sketch_name: node %s', msg.node_id)
return

pres = gateway.const.Presentation
set_req = gateway.const.SetReq

for child in node.children.values():
position = child.values.get(set_req.V_POSITION)
if child.type != pres.S_GPS or position is None:
continue
try:
latitude, longitude, _ = position.split(',')
except ValueError:
_LOGGER.error('Payload for V_POSITION %s is not of format '
'latitude,longitude,altitude', position)
continue
name = '{} {} {}'.format(
node.sketch_name, msg.node_id, child.id)
attr = {
mysensors.ATTR_CHILD_ID: child.id,
mysensors.ATTR_DESCRIPTION: child.description,
mysensors.ATTR_DEVICE: gateway.device,
mysensors.ATTR_NODE_ID: msg.node_id,
}
see(
dev_id=slugify(name),
host_name=name,
gps=(latitude, longitude),
battery=node.battery_level,
attributes=attr
)
child = node.children.get(msg.child_id)
if child is None:
return
position = child.values.get(set_req.V_POSITION)
if child.type != pres.S_GPS or position is None:
return
try:
latitude, longitude, _ = position.split(',')
except ValueError:
_LOGGER.error('Payload for V_POSITION %s is not of format '
'latitude,longitude,altitude', position)
return
name = '{} {} {}'.format(
node.sketch_name, msg.node_id, child.id)
attr = {
mysensors.ATTR_CHILD_ID: child.id,
mysensors.ATTR_DESCRIPTION: child.description,
mysensors.ATTR_DEVICE: gateway.device,
mysensors.ATTR_NODE_ID: msg.node_id,
}
see(
dev_id=slugify(name),
host_name=name,
gps=(latitude, longitude),
battery=node.battery_level,
attributes=attr
)

gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)

Expand Down
56 changes: 27 additions & 29 deletions homeassistant/components/mysensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
MQTT_COMPONENT = 'mqtt'
REQUIREMENTS = [
'https://github.com/theolind/pymysensors/archive/'
'ff3476b70edc9c995b939cddb9d51f8d2d018581.zip#pymysensors==0.9.0']
'c6990eaaa741444a638608e6e00488195e2ca74c.zip#pymysensors==0.9.1']


def is_socket_address(value):
Expand Down Expand Up @@ -206,12 +206,9 @@ def gw_start(event):
for node_id in gateway.sensors:
node = gateway.sensors[node_id]
for child_id in node.children:
child = node.children[child_id]
for value_type in child.values:
msg = mysensors.Message().modify(
node_id=node_id, child_id=child_id, type=1,
sub_type=value_type)
gateway.event_callback(msg)
msg = mysensors.Message().modify(
node_id=node_id, child_id=child_id)
gateway.event_callback(msg)
gateway.start()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
lambda event: gateway.stop())
Expand Down Expand Up @@ -274,32 +271,33 @@ def mysensors_callback(gateway, msg):
_LOGGER.debug('No sketch_name: node %s', msg.node_id)
return
child = gateway.sensors[msg.node_id].children.get(msg.child_id)
if child is None or child.values.get(msg.sub_type) is None:
if child is None:
return
key = msg.node_id, child.id, msg.sub_type
if child.type not in map_sv_types or \
msg.sub_type not in map_sv_types[child.type]:
return
if key in devices:
for value_type in child.values:
key = msg.node_id, child.id, value_type
if child.type not in map_sv_types or \
value_type not in map_sv_types[child.type]:
continue
if key in devices:
if add_devices:
devices[key].schedule_update_ha_state(True)
else:
devices[key].update()
continue
name = '{} {} {}'.format(
gateway.sensors[msg.node_id].sketch_name, msg.node_id,
child.id)
if isinstance(entity_class, dict):
device_class = entity_class[child.type]
else:
device_class = entity_class
devices[key] = device_class(
gateway, msg.node_id, child.id, name, value_type)
if add_devices:
devices[key].schedule_update_ha_state(True)
_LOGGER.info('Adding new devices: %s', [devices[key]])
add_devices([devices[key]], True)
else:
devices[key].update()
return
name = '{} {} {}'.format(
gateway.sensors[msg.node_id].sketch_name, msg.node_id,
child.id)
if isinstance(entity_class, dict):
device_class = entity_class[child.type]
else:
device_class = entity_class
devices[key] = device_class(
gateway, msg.node_id, child.id, name, msg.sub_type)
if add_devices:
_LOGGER.info('Adding new devices: %s', [devices[key]])
add_devices([devices[key]], True)
else:
devices[key].update()
return mysensors_callback


Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ https://github.com/tfriedel/python-lightify/archive/d6eadcf311e6e21746182d1480e9
https://github.com/thecynic/pylutron/archive/v0.1.0.zip#pylutron==0.1.0

# homeassistant.components.mysensors
https://github.com/theolind/pymysensors/archive/ff3476b70edc9c995b939cddb9d51f8d2d018581.zip#pymysensors==0.9.0
https://github.com/theolind/pymysensors/archive/c6990eaaa741444a638608e6e00488195e2ca74c.zip#pymysensors==0.9.1

# homeassistant.components.sensor.modem_callerid
https://github.com/vroomfonde1/basicmodem/archive/0.7.zip#basicmodem==0.7
Expand Down

0 comments on commit 4e38866

Please sign in to comment.