hass-core/homeassistant/components/sensor/mysensors.py

94 lines
2.6 KiB
Python
Raw Normal View History

"""
homeassistant.components.sensor.mysensors
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MySensors sensors.
Config:
sensor:
- platform: mysensors
port: '/dev/ttyACM0'
"""
2015-04-02 19:59:44 +02:00
import logging
# pylint: disable=no-name-in-module, import-error
import homeassistant.external.pymysensors.mysensors.mysensors as mysensors
import homeassistant.external.pymysensors.mysensors.const as const
from homeassistant.helpers.entity import Entity
from homeassistant.const import ATTR_BATTERY_LEVEL
2015-04-02 19:59:44 +02:00
CONF_PORT = "port"
2015-04-02 19:59:44 +02:00
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Setup the mysensors platform. """
devices = {} # keep track of devices added to HA
2015-04-02 19:59:44 +02:00
def sensor_update(update_type, nid):
""" Callback for sensor updates from the MySensors gateway. """
sensor = gateway.sensors[nid]
if sensor.sketch_name is None:
return
if nid not in devices:
devices[nid] = MySensorsNode(sensor.sketch_name)
add_devices([devices[nid]])
2015-04-02 19:59:44 +02:00
devices[nid].battery_level = sensor.battery_level
for child_id, child in sensor.children.items():
devices[nid].update_child(child_id, child)
2015-04-02 19:59:44 +02:00
port = config.get(CONF_PORT)
if port is None:
_LOGGER.error("Missing required key 'port'")
return False
2015-04-02 19:59:44 +02:00
gateway = mysensors.SerialGateway(port, sensor_update)
gateway.start()
2015-04-02 19:59:44 +02:00
class MySensorsNode(Entity):
""" Represents a MySensors node. """
2015-04-02 19:59:44 +02:00
def __init__(self, name):
self._name = name
self.battery_level = 0
self.children = {}
2015-04-02 19:59:44 +02:00
@property
def name(self):
""" The name of this sensor. """
2015-04-02 19:59:44 +02:00
return self._name
@property
def state(self):
""" Returns the state of the device. """
return ''
2015-04-02 19:59:44 +02:00
@property
def state_attributes(self):
""" Returns the state attributes. """
attrs = {}
attrs[ATTR_BATTERY_LEVEL] = self.battery_level
2015-04-02 19:59:44 +02:00
for child in self.children.values():
for value_type, value in child.values.items():
attrs[value_type] = value
2015-04-02 19:59:44 +02:00
return attrs
def update_child(self, child_id, child):
""" Sets the values of a child sensor. """
self.children[child_id] = MySensorsChildSensor(
const.Presentation(child.type).name,
{const.SetReq(t).name: v for t, v in child.values.items()})
2015-04-02 19:59:44 +02:00
class MySensorsChildSensor():
""" Represents a MySensors child sensor. """
# pylint: disable=too-few-public-methods
def __init__(self, child_type, values):
self.type = child_type
self.values = values