2015-04-02 20:04:28 -05:00
|
|
|
"""
|
|
|
|
homeassistant.components.sensor.mysensors
|
2015-05-13 19:18:30 +02:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Support for MySensors sensors.
|
2015-04-02 20:04:28 -05:00
|
|
|
|
2015-10-23 22:48:30 +02:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 13:12:18 +01:00
|
|
|
https://home-assistant.io/components/sensor.mysensors/
|
2015-04-02 20:04:28 -05:00
|
|
|
"""
|
2015-04-02 19:59:44 +02:00
|
|
|
import logging
|
|
|
|
|
2015-04-02 20:04:28 -05:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2015-04-05 17:15:29 -05:00
|
|
|
from homeassistant.const import (
|
2015-11-04 04:53:59 +01:00
|
|
|
ATTR_BATTERY_LEVEL,
|
2015-04-25 20:07:44 -05:00
|
|
|
TEMP_CELCIUS, TEMP_FAHRENHEIT,
|
|
|
|
STATE_ON, STATE_OFF)
|
2015-04-02 19:59:44 +02:00
|
|
|
|
2015-11-04 04:53:59 +01:00
|
|
|
import homeassistant.components.mysensors as mysensors
|
2015-04-02 19:59:44 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-11-04 04:53:59 +01:00
|
|
|
DEPENDENCIES = ['mysensors']
|
2015-04-02 19:59:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Setup the mysensors platform for sensors."""
|
2015-12-09 04:43:18 +01:00
|
|
|
# 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,
|
|
|
|
]
|
2015-11-04 04:53:59 +01:00
|
|
|
v_types = []
|
|
|
|
for _, member in mysensors.CONST.SetReq.__members__.items():
|
2015-12-09 04:43:18 +01:00
|
|
|
if all(test != member.value for test in not_v_types):
|
2015-11-04 04:53:59 +01:00
|
|
|
v_types.append(member)
|
2015-04-02 19:59:44 +02:00
|
|
|
|
2015-11-04 04:53:59 +01:00
|
|
|
@mysensors.mysensors_update
|
2015-12-06 00:29:03 +01:00
|
|
|
def _sensor_update(gateway, port, devices, nid):
|
2015-11-04 04:53:59 +01:00
|
|
|
"""Internal callback for sensor updates."""
|
2015-12-09 04:43:18 +01:00
|
|
|
return (s_types, v_types, MySensorsSensor, add_devices)
|
|
|
|
|
|
|
|
@mysensors.event_update
|
|
|
|
def event_update(event):
|
|
|
|
"""Callback for event updates from the MySensors component."""
|
|
|
|
return _sensor_update
|
|
|
|
|
|
|
|
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, event_update)
|
2015-04-05 17:15:29 -05:00
|
|
|
|
2015-04-05 17:02:48 -05:00
|
|
|
|
2015-11-04 04:53:59 +01:00
|
|
|
class MySensorsSensor(Entity):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Represent the value of a MySensors child node."""
|
2015-04-02 20:04:28 -05:00
|
|
|
|
2015-07-19 23:21:01 -07:00
|
|
|
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
2015-11-04 04:53:59 +01:00
|
|
|
|
2015-12-06 00:29:03 +01:00
|
|
|
def __init__(self, port, node_id, child_id, name, value_type):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Setup class attributes on instantiation.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
port (str): Gateway port.
|
|
|
|
node_id (str): Id of node.
|
|
|
|
child_id (str): Id of child.
|
2015-12-08 02:47:15 +01:00
|
|
|
name (str): Entity name.
|
2015-12-08 01:03:07 +01:00
|
|
|
value_type (str): Value type of child. Value is entity state.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
port (str): Gateway port.
|
|
|
|
node_id (str): Id of node.
|
|
|
|
child_id (str): Id of child.
|
2015-12-08 02:47:15 +01:00
|
|
|
_name (str): Entity name.
|
2015-12-08 01:03:07 +01:00
|
|
|
value_type (str): Value type of child. Value is entity state.
|
|
|
|
battery_level (int): Node battery level.
|
|
|
|
_values (dict): Child values. Non state values set as state attributes.
|
|
|
|
"""
|
2015-12-06 00:29:03 +01:00
|
|
|
self.port = port
|
2015-04-25 20:07:44 -05:00
|
|
|
self.node_id = node_id
|
|
|
|
self.child_id = child_id
|
2015-12-08 01:03:07 +01:00
|
|
|
self._name = name
|
2015-04-25 20:07:44 -05:00
|
|
|
self.value_type = value_type
|
2015-12-08 01:03:07 +01:00
|
|
|
self.battery_level = 0
|
2015-12-06 00:29:03 +01:00
|
|
|
self._values = {}
|
|
|
|
|
|
|
|
def as_dict(self):
|
2015-12-08 02:47:15 +01:00
|
|
|
"""Return a dict representation of this entity."""
|
2015-12-06 00:29:03 +01:00
|
|
|
return {
|
|
|
|
'port': self.port,
|
|
|
|
'name': self._name,
|
|
|
|
'node_id': self.node_id,
|
|
|
|
'child_id': self.child_id,
|
|
|
|
'battery_level': self.battery_level,
|
|
|
|
'value_type': self.value_type,
|
|
|
|
'values': self._values,
|
|
|
|
}
|
2015-04-02 19:59:44 +02:00
|
|
|
|
2015-04-05 17:12:07 -05:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""MySensor gateway pushes its state to HA."""
|
2015-04-05 17:12:07 -05:00
|
|
|
return False
|
|
|
|
|
2015-04-02 19:59:44 +02:00
|
|
|
@property
|
|
|
|
def name(self):
|
2015-12-08 02:47:15 +01:00
|
|
|
"""The name of this entity."""
|
2015-04-02 19:59:44 +02:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Return the state of the device."""
|
2015-12-06 00:29:03 +01:00
|
|
|
if not self._values:
|
|
|
|
return ''
|
|
|
|
return self._values[self.value_type]
|
2015-04-25 20:07:44 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Unit of measurement of this entity."""
|
2015-12-06 00:29:03 +01:00
|
|
|
# pylint:disable=too-many-return-statements
|
|
|
|
if self.value_type == mysensors.CONST.SetReq.V_TEMP:
|
|
|
|
return TEMP_CELCIUS if mysensors.IS_METRIC else TEMP_FAHRENHEIT
|
|
|
|
elif self.value_type == mysensors.CONST.SetReq.V_HUM or \
|
|
|
|
self.value_type == mysensors.CONST.SetReq.V_DIMMER or \
|
|
|
|
self.value_type == mysensors.CONST.SetReq.V_PERCENTAGE or \
|
|
|
|
self.value_type == mysensors.CONST.SetReq.V_LIGHT_LEVEL:
|
2015-04-25 20:07:44 -05:00
|
|
|
return '%'
|
2015-12-06 00:29:03 +01:00
|
|
|
elif self.value_type == mysensors.CONST.SetReq.V_WATT:
|
|
|
|
return 'W'
|
|
|
|
elif self.value_type == mysensors.CONST.SetReq.V_KWH:
|
|
|
|
return 'kWh'
|
|
|
|
elif self.value_type == mysensors.CONST.SetReq.V_VOLTAGE:
|
|
|
|
return 'V'
|
|
|
|
elif self.value_type == mysensors.CONST.SetReq.V_CURRENT:
|
|
|
|
return 'A'
|
|
|
|
elif self.value_type == mysensors.CONST.SetReq.V_IMPEDANCE:
|
|
|
|
return 'ohm'
|
|
|
|
elif mysensors.CONST.SetReq.V_UNIT_PREFIX in self._values:
|
|
|
|
return self._values[mysensors.CONST.SetReq.V_UNIT_PREFIX]
|
2015-04-25 20:07:44 -05:00
|
|
|
return None
|
2015-04-02 19:59:44 +02:00
|
|
|
|
2015-12-06 00:29:03 +01:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Return device specific state attributes."""
|
2015-12-06 00:29:03 +01:00
|
|
|
device_attr = dict(self._values)
|
|
|
|
device_attr.pop(self.value_type, None)
|
|
|
|
return device_attr
|
|
|
|
|
2015-04-02 19:59:44 +02:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Return the state attributes."""
|
2015-12-06 00:29:03 +01:00
|
|
|
data = {
|
|
|
|
mysensors.ATTR_NODE_ID: self.node_id,
|
|
|
|
mysensors.ATTR_CHILD_ID: self.child_id,
|
2015-04-25 20:07:44 -05:00
|
|
|
ATTR_BATTERY_LEVEL: self.battery_level,
|
|
|
|
}
|
|
|
|
|
2015-12-06 00:29:03 +01:00
|
|
|
device_attr = self.device_state_attributes
|
|
|
|
|
|
|
|
if device_attr is not None:
|
|
|
|
data.update(device_attr)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def update_sensor(self, values, battery_level):
|
2015-12-08 01:03:07 +01:00
|
|
|
"""Update the controller with the latest values from a sensor."""
|
2015-12-06 00:29:03 +01:00
|
|
|
for value_type, value in values.items():
|
|
|
|
_LOGGER.info(
|
|
|
|
"%s: value_type %s, value = %s", self._name, value_type, value)
|
|
|
|
if value_type == mysensors.CONST.SetReq.V_TRIPPED:
|
|
|
|
self._values[value_type] = STATE_ON if int(
|
|
|
|
value) == 1 else STATE_OFF
|
|
|
|
else:
|
|
|
|
self._values[value_type] = value
|
|
|
|
|
2015-04-25 20:07:44 -05:00
|
|
|
self.battery_level = battery_level
|
|
|
|
self.update_ha_state()
|