2017-02-07 19:47:11 +00:00
|
|
|
"""
|
|
|
|
Support for tracking MySensors devices.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/device_tracker.mysensors/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components import mysensors
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
DEPENDENCIES = ['mysensors']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_scanner(hass, config, see, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the MySensors tracker."""
|
2017-04-06 07:21:21 +00:00
|
|
|
def mysensors_callback(gateway, msg):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up callback for mysensors platform."""
|
2017-04-06 07:21:21 +00:00
|
|
|
node = gateway.sensors[msg.node_id]
|
2017-02-07 19:47:11 +00:00
|
|
|
if node.sketch_name is None:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.debug("No sketch_name: node %s", msg.node_id)
|
2017-02-07 19:47:11 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
pres = gateway.const.Presentation
|
|
|
|
set_req = gateway.const.SetReq
|
|
|
|
|
2017-04-12 02:17:09 +00:00
|
|
|
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:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.error("Payload for V_POSITION %s is not of format "
|
|
|
|
"latitude, longitude, altitude", position)
|
2017-04-12 02:17:09 +00:00
|
|
|
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
|
|
|
|
)
|
2017-02-07 19:47:11 +00:00
|
|
|
|
|
|
|
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
|
|
|
|
|
|
|
for gateway in gateways:
|
2017-02-13 10:23:28 +00:00
|
|
|
if float(gateway.protocol_version) < 2.0:
|
|
|
|
continue
|
2017-02-07 19:47:11 +00:00
|
|
|
gateway.platform_callbacks.append(mysensors_callback)
|
|
|
|
|
|
|
|
return True
|