2019-02-14 04:35:12 +00:00
|
|
|
"""Support for MySensors sensors."""
|
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
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-05 18:54:43 +00:00
|
|
|
ENERGY_KILO_WATT_HOUR,
|
|
|
|
POWER_WATT,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
)
|
2015-04-02 17:59:44 +00:00
|
|
|
|
2018-03-23 17:06:07 +00:00
|
|
|
SENSORS = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"V_TEMP": [None, "mdi:thermometer"],
|
|
|
|
"V_HUM": ["%", "mdi:water-percent"],
|
|
|
|
"V_DIMMER": ["%", "mdi:percent"],
|
|
|
|
"V_PERCENTAGE": ["%", "mdi:percent"],
|
|
|
|
"V_PRESSURE": [None, "mdi:gauge"],
|
2019-08-29 20:22:29 +00:00
|
|
|
"V_FORECAST": [None, "mdi:weather-partly-cloudy"],
|
2019-07-31 19:25:30 +00:00
|
|
|
"V_RAIN": [None, "mdi:weather-rainy"],
|
|
|
|
"V_RAINRATE": [None, "mdi:weather-rainy"],
|
|
|
|
"V_WIND": [None, "mdi:weather-windy"],
|
|
|
|
"V_GUST": [None, "mdi:weather-windy"],
|
|
|
|
"V_DIRECTION": ["°", "mdi:compass"],
|
|
|
|
"V_WEIGHT": ["kg", "mdi:weight-kilogram"],
|
|
|
|
"V_DISTANCE": ["m", "mdi:ruler"],
|
|
|
|
"V_IMPEDANCE": ["ohm", None],
|
|
|
|
"V_WATT": [POWER_WATT, None],
|
|
|
|
"V_KWH": [ENERGY_KILO_WATT_HOUR, None],
|
|
|
|
"V_LIGHT_LEVEL": ["%", "mdi:white-balance-sunny"],
|
|
|
|
"V_FLOW": ["m", "mdi:gauge"],
|
|
|
|
"V_VOLUME": ["m³", None],
|
|
|
|
"V_LEVEL": {
|
|
|
|
"S_SOUND": ["dB", "mdi:volume-high"],
|
|
|
|
"S_VIBRATION": ["Hz", None],
|
|
|
|
"S_LIGHT_LEVEL": ["lx", "mdi:white-balance-sunny"],
|
|
|
|
},
|
|
|
|
"V_VOLTAGE": ["V", "mdi:flash"],
|
|
|
|
"V_CURRENT": ["A", "mdi:flash-auto"],
|
|
|
|
"V_PH": ["pH", None],
|
|
|
|
"V_ORP": ["mV", None],
|
|
|
|
"V_EC": ["μS/cm", None],
|
|
|
|
"V_VAR": ["var", None],
|
|
|
|
"V_VA": ["VA", None],
|
2018-03-23 17:06:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 17:59:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, 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(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
discovery_info,
|
|
|
|
MySensorsSensor,
|
|
|
|
async_add_entities=async_add_entities,
|
|
|
|
)
|
2015-04-02 17:59:44 +00:00
|
|
|
|
2015-04-05 22:02:48 +00:00
|
|
|
|
2018-06-25 11:58:16 +00:00
|
|
|
class MySensorsSensor(mysensors.device.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
|
|
|
|
2018-03-23 17:06:07 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
_, icon = self._get_sensor_type()
|
|
|
|
return icon
|
|
|
|
|
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."""
|
2016-02-20 01:11:15 +00:00
|
|
|
set_req = self.gateway.const.SetReq
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
float(self.gateway.protocol_version) >= 1.5
|
|
|
|
and set_req.V_UNIT_PREFIX in self._values
|
|
|
|
):
|
2018-03-23 17:06:07 +00:00
|
|
|
return self._values[set_req.V_UNIT_PREFIX]
|
|
|
|
unit, _ = self._get_sensor_type()
|
2017-02-05 20:07:30 +00:00
|
|
|
return unit
|
2018-03-23 17:06:07 +00:00
|
|
|
|
|
|
|
def _get_sensor_type(self):
|
|
|
|
"""Return list with unit and icon of sensor type."""
|
|
|
|
pres = self.gateway.const.Presentation
|
|
|
|
set_req = self.gateway.const.SetReq
|
|
|
|
SENSORS[set_req.V_TEMP.name][0] = (
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS if self.gateway.metric else TEMP_FAHRENHEIT
|
|
|
|
)
|
2018-03-23 17:06:07 +00:00
|
|
|
sensor_type = SENSORS.get(set_req(self.value_type).name, [None, None])
|
|
|
|
if isinstance(sensor_type, dict):
|
2019-07-31 19:25:30 +00:00
|
|
|
sensor_type = sensor_type.get(pres(self.child_type).name, [None, None])
|
2018-03-23 17:06:07 +00:00
|
|
|
return sensor_type
|