2015-04-03 01:04:28 +00:00
|
|
|
"""
|
2015-05-13 17:18:30 +00:00
|
|
|
Support for MySensors sensors.
|
2015-04-03 01:04:28 +00:00
|
|
|
|
2015-10-23 20:48:30 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/sensor.mysensors/
|
2015-04-03 01:04:28 +00:00
|
|
|
"""
|
2016-04-30 13:27:59 +00:00
|
|
|
from homeassistant.components import mysensors
|
2017-08-25 15:58:05 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN
|
2016-04-30 13:27:59 +00:00
|
|
|
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
|
2015-04-02 17:59:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the MySensors platform for sensors."""
|
2017-08-25 15:58:05 +00:00
|
|
|
mysensors.setup_mysensors_platform(
|
|
|
|
hass, DOMAIN, discovery_info, MySensorsSensor, add_devices=add_devices)
|
2015-04-02 17:59:44 +00:00
|
|
|
|
2015-04-05 22:02:48 +00:00
|
|
|
|
2017-08-25 15:58:05 +00:00
|
|
|
class MySensorsSensor(mysensors.MySensorsEntity):
|
2016-07-12 14:46:29 +00:00
|
|
|
"""Representation of a MySensors Sensor child node."""
|
2015-04-02 17:59:44 +00:00
|
|
|
|
2016-10-05 05:45:38 +00:00
|
|
|
@property
|
|
|
|
def force_update(self):
|
|
|
|
"""Return True if state updates should be forced.
|
|
|
|
|
|
|
|
If True, a state change will be triggered anytime the state property is
|
|
|
|
updated, not just when the value changes.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
2015-04-02 17:59:44 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Return the state of the device."""
|
2016-04-30 13:27:59 +00:00
|
|
|
return self._values.get(self.value_type)
|
2015-04-26 01:07:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity."""
|
2017-02-05 20:07:30 +00:00
|
|
|
pres = self.gateway.const.Presentation
|
2016-02-20 01:11:15 +00:00
|
|
|
set_req = self.gateway.const.SetReq
|
2016-01-27 14:26:52 +00:00
|
|
|
unit_map = {
|
2016-04-20 03:30:44 +00:00
|
|
|
set_req.V_TEMP: (TEMP_CELSIUS
|
2016-02-20 01:11:15 +00:00
|
|
|
if self.gateway.metric else TEMP_FAHRENHEIT),
|
|
|
|
set_req.V_HUM: '%',
|
|
|
|
set_req.V_DIMMER: '%',
|
|
|
|
set_req.V_LIGHT_LEVEL: '%',
|
2017-02-05 20:07:30 +00:00
|
|
|
set_req.V_DIRECTION: '°',
|
2016-02-20 01:11:15 +00:00
|
|
|
set_req.V_WEIGHT: 'kg',
|
|
|
|
set_req.V_DISTANCE: 'm',
|
|
|
|
set_req.V_IMPEDANCE: 'ohm',
|
|
|
|
set_req.V_WATT: 'W',
|
|
|
|
set_req.V_KWH: 'kWh',
|
|
|
|
set_req.V_FLOW: 'm',
|
2017-02-05 20:07:30 +00:00
|
|
|
set_req.V_VOLUME: 'm³',
|
2016-02-20 01:11:15 +00:00
|
|
|
set_req.V_VOLTAGE: 'V',
|
|
|
|
set_req.V_CURRENT: 'A',
|
2016-01-27 14:26:52 +00:00
|
|
|
}
|
2016-08-27 20:41:21 +00:00
|
|
|
if float(self.gateway.protocol_version) >= 1.5:
|
2016-02-20 01:11:15 +00:00
|
|
|
if set_req.V_UNIT_PREFIX in self._values:
|
2016-01-27 14:26:52 +00:00
|
|
|
return self._values[
|
2016-02-20 01:11:15 +00:00
|
|
|
set_req.V_UNIT_PREFIX]
|
2017-02-05 20:07:30 +00:00
|
|
|
unit_map.update({
|
|
|
|
set_req.V_PERCENTAGE: '%',
|
|
|
|
set_req.V_LEVEL: {
|
|
|
|
pres.S_SOUND: 'dB', pres.S_VIBRATION: 'Hz',
|
|
|
|
pres.S_LIGHT_LEVEL: 'lux'}})
|
2016-10-02 06:23:31 +00:00
|
|
|
if float(self.gateway.protocol_version) >= 2.0:
|
|
|
|
unit_map.update({
|
|
|
|
set_req.V_ORP: 'mV',
|
|
|
|
set_req.V_EC: 'μS/cm',
|
|
|
|
set_req.V_VAR: 'var',
|
|
|
|
set_req.V_VA: 'VA',
|
|
|
|
})
|
2017-02-05 20:07:30 +00:00
|
|
|
unit = unit_map.get(self.value_type)
|
|
|
|
if isinstance(unit, dict):
|
|
|
|
unit = unit.get(self.child_type)
|
|
|
|
return unit
|