2019-02-14 15:01:46 +00:00
|
|
|
"""Support for HomeMatic sensors."""
|
2016-06-24 08:06:58 +00:00
|
|
|
import logging
|
2018-08-21 19:25:16 +00:00
|
|
|
|
2019-11-01 20:25:33 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
|
|
|
DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
DEVICE_CLASS_POWER,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
ENERGY_WATT_HOUR,
|
|
|
|
POWER_WATT,
|
|
|
|
STATE_UNKNOWN,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import ATTR_DISCOVER_DEVICES, HMDevice
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
HM_STATE_HA_CAST = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"RotaryHandleSensor": {0: "closed", 1: "tilted", 2: "open"},
|
|
|
|
"RotaryHandleSensorIP": {0: "closed", 1: "tilted", 2: "open"},
|
|
|
|
"WaterSensor": {0: "dry", 1: "wet", 2: "water"},
|
|
|
|
"CO2Sensor": {0: "normal", 1: "added", 2: "strong"},
|
|
|
|
"IPSmoke": {0: "off", 1: "primary", 2: "intrusion", 3: "secondary"},
|
|
|
|
"RFSiren": {
|
|
|
|
0: "disarmed",
|
|
|
|
1: "extsens_armed",
|
|
|
|
2: "allsens_armed",
|
|
|
|
3: "alarm_blocked",
|
|
|
|
},
|
2016-06-24 08:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HM_UNIT_HA_CAST = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"HUMIDITY": "%",
|
|
|
|
"TEMPERATURE": "°C",
|
|
|
|
"ACTUAL_TEMPERATURE": "°C",
|
|
|
|
"BRIGHTNESS": "#",
|
|
|
|
"POWER": POWER_WATT,
|
|
|
|
"CURRENT": "mA",
|
|
|
|
"VOLTAGE": "V",
|
|
|
|
"ENERGY_COUNTER": ENERGY_WATT_HOUR,
|
|
|
|
"GAS_POWER": "m3",
|
|
|
|
"GAS_ENERGY_COUNTER": "m3",
|
|
|
|
"LUX": "lx",
|
|
|
|
"ILLUMINATION": "lx",
|
|
|
|
"CURRENT_ILLUMINATION": "lx",
|
|
|
|
"AVERAGE_ILLUMINATION": "lx",
|
|
|
|
"LOWEST_ILLUMINATION": "lx",
|
|
|
|
"HIGHEST_ILLUMINATION": "lx",
|
|
|
|
"RAIN_COUNTER": "mm",
|
|
|
|
"WIND_SPEED": "km/h",
|
|
|
|
"WIND_DIRECTION": "°",
|
|
|
|
"WIND_DIRECTION_RANGE": "°",
|
|
|
|
"SUNSHINEDURATION": "#",
|
|
|
|
"AIR_PRESSURE": "hPa",
|
|
|
|
"FREQUENCY": "Hz",
|
|
|
|
"VALUE": "#",
|
2017-02-23 14:31:32 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 20:25:33 +00:00
|
|
|
HM_DEVICE_CLASS_HA_CAST = {
|
|
|
|
"HUMIDITY": DEVICE_CLASS_HUMIDITY,
|
|
|
|
"TEMPERATURE": DEVICE_CLASS_TEMPERATURE,
|
|
|
|
"ACTUAL_TEMPERATURE": DEVICE_CLASS_TEMPERATURE,
|
|
|
|
"LUX": DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
"CURRENT_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
"AVERAGE_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
"LOWEST_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
"HIGHEST_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
"POWER": DEVICE_CLASS_POWER,
|
|
|
|
"CURRENT": DEVICE_CLASS_POWER,
|
2016-06-24 08:06:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 20:25:33 +00:00
|
|
|
HM_ICON_HA_CAST = {"WIND_SPEED": "mdi:weather-windy", "BRIGHTNESS": "mdi:invert-colors"}
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Set up the HomeMatic sensor platform."""
|
2016-06-28 20:53:53 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2017-02-20 16:07:33 +00:00
|
|
|
devices = []
|
2017-07-06 03:02:16 +00:00
|
|
|
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
|
2017-08-31 19:16:44 +00:00
|
|
|
new_device = HMSensor(conf)
|
2017-02-20 16:07:33 +00:00
|
|
|
devices.append(new_device)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devices)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
|
2016-11-29 19:53:02 +00:00
|
|
|
class HMSensor(HMDevice):
|
2018-08-21 19:25:16 +00:00
|
|
|
"""Representation of a HomeMatic sensor."""
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
# Does a cast exist for this class?
|
|
|
|
name = self._hmdevice.__class__.__name__
|
|
|
|
if name in HM_STATE_HA_CAST:
|
2019-11-01 20:25:33 +00:00
|
|
|
return HM_STATE_HA_CAST[name].get(self._hm_get_state())
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# No cast, return original value
|
|
|
|
return self._hm_get_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2019-11-01 20:25:33 +00:00
|
|
|
return HM_UNIT_HA_CAST.get(self._state)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class to use in the frontend, if any."""
|
|
|
|
return HM_DEVICE_CLASS_HA_CAST.get(self._state)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2017-02-23 14:31:32 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2019-11-01 20:25:33 +00:00
|
|
|
return HM_ICON_HA_CAST.get(self._state)
|
2017-02-23 14:31:32 +00:00
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
def _init_data_struct(self):
|
2017-06-10 08:08:36 +00:00
|
|
|
"""Generate a data dictionary (self._data) from metadata."""
|
2016-06-24 08:06:58 +00:00
|
|
|
if self._state:
|
|
|
|
self._data.update({self._state: STATE_UNKNOWN})
|
|
|
|
else:
|
2018-08-21 19:25:16 +00:00
|
|
|
_LOGGER.critical("Unable to initialize sensor: %s", self._name)
|