2019-02-13 20:21:14 +00:00
|
|
|
"""Support for EnOcean sensors."""
|
2016-08-25 04:35:09 +00:00
|
|
|
import logging
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2016-08-25 04:35:09 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
from homeassistant.components import enocean
|
2016-08-25 04:35:09 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2019-04-24 22:30:46 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_DEVICE_CLASS,
|
|
|
|
CONF_ID,
|
|
|
|
CONF_NAME,
|
|
|
|
DEVICE_CLASS_HUMIDITY,
|
2019-12-25 10:41:00 +00:00
|
|
|
DEVICE_CLASS_POWER,
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
|
|
|
POWER_WATT,
|
2019-12-25 10:41:00 +00:00
|
|
|
STATE_CLOSED,
|
|
|
|
STATE_OPEN,
|
2019-12-05 05:20:26 +00:00
|
|
|
TEMP_CELSIUS,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-08-25 04:35:09 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2016-08-25 04:35:09 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MAX_TEMP = "max_temp"
|
|
|
|
CONF_MIN_TEMP = "min_temp"
|
|
|
|
CONF_RANGE_FROM = "range_from"
|
|
|
|
CONF_RANGE_TO = "range_to"
|
2019-04-24 22:30:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "EnOcean sensor"
|
2019-04-24 22:30:46 +00:00
|
|
|
|
2019-12-25 10:41:00 +00:00
|
|
|
SENSOR_TYPE_HUMIDITY = "humidity"
|
|
|
|
SENSOR_TYPE_POWER = "powersensor"
|
|
|
|
SENSOR_TYPE_TEMPERATURE = "temperature"
|
|
|
|
SENSOR_TYPE_WINDOWHANDLE = "windowhandle"
|
2019-04-24 22:30:46 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2019-12-25 10:41:00 +00:00
|
|
|
SENSOR_TYPE_HUMIDITY: {
|
2019-07-31 19:25:30 +00:00
|
|
|
"name": "Humidity",
|
|
|
|
"unit": "%",
|
|
|
|
"icon": "mdi:water-percent",
|
|
|
|
"class": DEVICE_CLASS_HUMIDITY,
|
2019-04-24 22:30:46 +00:00
|
|
|
},
|
2019-12-25 10:41:00 +00:00
|
|
|
SENSOR_TYPE_POWER: {
|
2019-07-31 19:25:30 +00:00
|
|
|
"name": "Power",
|
|
|
|
"unit": POWER_WATT,
|
|
|
|
"icon": "mdi:power-plug",
|
|
|
|
"class": DEVICE_CLASS_POWER,
|
2019-04-24 22:30:46 +00:00
|
|
|
},
|
2019-12-25 10:41:00 +00:00
|
|
|
SENSOR_TYPE_TEMPERATURE: {
|
2019-07-31 19:25:30 +00:00
|
|
|
"name": "Temperature",
|
|
|
|
"unit": TEMP_CELSIUS,
|
|
|
|
"icon": "mdi:thermometer",
|
|
|
|
"class": DEVICE_CLASS_TEMPERATURE,
|
2019-04-24 22:30:46 +00:00
|
|
|
},
|
2019-12-25 10:41:00 +00:00
|
|
|
SENSOR_TYPE_WINDOWHANDLE: {
|
|
|
|
"name": "WindowHandle",
|
|
|
|
"unit": None,
|
|
|
|
"icon": "mdi:window",
|
|
|
|
"class": None,
|
|
|
|
},
|
2019-04-24 22:30:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 10:41:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
2019-12-25 10:41:00 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CLASS, default=SENSOR_TYPE_POWER): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_MAX_TEMP, default=40): vol.Coerce(int),
|
|
|
|
vol.Optional(CONF_MIN_TEMP, default=0): vol.Coerce(int),
|
|
|
|
vol.Optional(CONF_RANGE_FROM, default=255): cv.positive_int,
|
|
|
|
vol.Optional(CONF_RANGE_TO, default=0): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up an EnOcean sensor device."""
|
2016-08-25 04:35:09 +00:00
|
|
|
dev_id = config.get(CONF_ID)
|
2019-04-24 22:30:46 +00:00
|
|
|
dev_name = config.get(CONF_NAME)
|
2019-12-25 10:41:00 +00:00
|
|
|
sensor_type = config.get(CONF_DEVICE_CLASS)
|
2019-04-24 22:30:46 +00:00
|
|
|
|
2019-12-25 10:41:00 +00:00
|
|
|
if sensor_type == SENSOR_TYPE_TEMPERATURE:
|
2019-04-24 22:30:46 +00:00
|
|
|
temp_min = config.get(CONF_MIN_TEMP)
|
|
|
|
temp_max = config.get(CONF_MAX_TEMP)
|
|
|
|
range_from = config.get(CONF_RANGE_FROM)
|
|
|
|
range_to = config.get(CONF_RANGE_TO)
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities(
|
|
|
|
[
|
|
|
|
EnOceanTemperatureSensor(
|
|
|
|
dev_id, dev_name, temp_min, temp_max, range_from, range_to
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
2019-04-24 22:30:46 +00:00
|
|
|
|
2019-12-25 10:41:00 +00:00
|
|
|
elif sensor_type == SENSOR_TYPE_HUMIDITY:
|
2019-04-24 22:30:46 +00:00
|
|
|
add_entities([EnOceanHumiditySensor(dev_id, dev_name)])
|
2016-08-25 04:35:09 +00:00
|
|
|
|
2019-12-25 10:41:00 +00:00
|
|
|
elif sensor_type == SENSOR_TYPE_POWER:
|
2019-04-24 22:30:46 +00:00
|
|
|
add_entities([EnOceanPowerSensor(dev_id, dev_name)])
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-12-25 10:41:00 +00:00
|
|
|
elif sensor_type == SENSOR_TYPE_WINDOWHANDLE:
|
|
|
|
add_entities([EnOceanWindowHandle(dev_id, dev_name)])
|
|
|
|
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
class EnOceanSensor(enocean.EnOceanDevice):
|
|
|
|
"""Representation of an EnOcean sensor device such as a power meter."""
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
def __init__(self, dev_id, dev_name, sensor_type):
|
2016-05-29 21:28:03 +00:00
|
|
|
"""Initialize the EnOcean sensor device."""
|
2019-04-24 22:30:46 +00:00
|
|
|
super().__init__(dev_id, dev_name)
|
|
|
|
self._sensor_type = sensor_type
|
2019-07-31 19:25:30 +00:00
|
|
|
self._device_class = SENSOR_TYPES[self._sensor_type]["class"]
|
2020-02-24 16:47:52 +00:00
|
|
|
self._dev_name = f"{SENSOR_TYPES[self._sensor_type]['name']} {dev_name}"
|
2019-07-31 19:25:30 +00:00
|
|
|
self._unit_of_measurement = SENSOR_TYPES[self._sensor_type]["unit"]
|
|
|
|
self._icon = SENSOR_TYPES[self._sensor_type]["icon"]
|
2019-04-24 22:30:46 +00:00
|
|
|
self._state = None
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
2019-04-24 22:30:46 +00:00
|
|
|
return self._dev_name
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return self._device_class
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2019-04-24 22:30:46 +00:00
|
|
|
return self._state
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
2019-04-24 22:30:46 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
def value_changed(self, packet):
|
|
|
|
"""Update the internal state of the sensor."""
|
|
|
|
|
|
|
|
|
|
|
|
class EnOceanPowerSensor(EnOceanSensor):
|
|
|
|
"""Representation of an EnOcean power sensor.
|
|
|
|
|
|
|
|
EEPs (EnOcean Equipment Profiles):
|
|
|
|
- A5-12-01 (Automated Meter Reading, Electricity)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, dev_id, dev_name):
|
|
|
|
"""Initialize the EnOcean power sensor device."""
|
2019-12-25 10:41:00 +00:00
|
|
|
super().__init__(dev_id, dev_name, SENSOR_TYPE_POWER)
|
2019-04-24 22:30:46 +00:00
|
|
|
|
|
|
|
def value_changed(self, packet):
|
|
|
|
"""Update the internal state of the sensor."""
|
|
|
|
if packet.rorg != 0xA5:
|
|
|
|
return
|
|
|
|
packet.parse_eep(0x12, 0x01)
|
2019-07-31 19:25:30 +00:00
|
|
|
if packet.parsed["DT"]["raw_value"] == 1:
|
2019-04-24 22:30:46 +00:00
|
|
|
# this packet reports the current value
|
2019-07-31 19:25:30 +00:00
|
|
|
raw_val = packet.parsed["MR"]["raw_value"]
|
|
|
|
divisor = packet.parsed["DIV"]["raw_value"]
|
2019-04-24 22:30:46 +00:00
|
|
|
self._state = raw_val / (10 ** divisor)
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
class EnOceanTemperatureSensor(EnOceanSensor):
|
|
|
|
"""Representation of an EnOcean temperature sensor device.
|
|
|
|
|
|
|
|
EEPs (EnOcean Equipment Profiles):
|
|
|
|
- A5-02-01 to A5-02-1B All 8 Bit Temperature Sensors of A5-02
|
|
|
|
- A5-10-01 to A5-10-14 (Room Operating Panels)
|
|
|
|
- A5-04-01 (Temp. and Humidity Sensor, Range 0°C to +40°C and 0% to 100%)
|
|
|
|
- A5-04-02 (Temp. and Humidity Sensor, Range -20°C to +60°C and 0% to 100%)
|
|
|
|
- A5-10-10 (Temp. and Humidity Sensor and Set Point)
|
|
|
|
- A5-10-12 (Temp. and Humidity Sensor, Set Point and Occupancy Control)
|
|
|
|
- 10 Bit Temp. Sensors are not supported (A5-02-20, A5-02-30)
|
|
|
|
|
|
|
|
For the following EEPs the scales must be set to "0 to 250":
|
|
|
|
- A5-04-01
|
|
|
|
- A5-04-02
|
|
|
|
- A5-10-10 to A5-10-14
|
|
|
|
"""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(self, dev_id, dev_name, scale_min, scale_max, range_from, range_to):
|
2019-04-24 22:30:46 +00:00
|
|
|
"""Initialize the EnOcean temperature sensor device."""
|
2019-12-25 10:41:00 +00:00
|
|
|
super().__init__(dev_id, dev_name, SENSOR_TYPE_TEMPERATURE)
|
2019-04-24 22:30:46 +00:00
|
|
|
self._scale_min = scale_min
|
|
|
|
self._scale_max = scale_max
|
|
|
|
self.range_from = range_from
|
|
|
|
self.range_to = range_to
|
|
|
|
|
|
|
|
def value_changed(self, packet):
|
|
|
|
"""Update the internal state of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if packet.data[0] != 0xA5:
|
2019-04-24 22:30:46 +00:00
|
|
|
return
|
|
|
|
temp_scale = self._scale_max - self._scale_min
|
|
|
|
temp_range = self.range_to - self.range_from
|
|
|
|
raw_val = packet.data[3]
|
|
|
|
temperature = temp_scale / temp_range * (raw_val - self.range_from)
|
|
|
|
temperature += self._scale_min
|
|
|
|
self._state = round(temperature, 1)
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
|
|
|
|
class EnOceanHumiditySensor(EnOceanSensor):
|
|
|
|
"""Representation of an EnOcean humidity sensor device.
|
|
|
|
|
|
|
|
EEPs (EnOcean Equipment Profiles):
|
|
|
|
- A5-04-01 (Temp. and Humidity Sensor, Range 0°C to +40°C and 0% to 100%)
|
|
|
|
- A5-04-02 (Temp. and Humidity Sensor, Range -20°C to +60°C and 0% to 100%)
|
|
|
|
- A5-10-10 to A5-10-14 (Room Operating Panels)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, dev_id, dev_name):
|
|
|
|
"""Initialize the EnOcean humidity sensor device."""
|
2019-12-25 10:41:00 +00:00
|
|
|
super().__init__(dev_id, dev_name, SENSOR_TYPE_HUMIDITY)
|
2019-04-24 22:30:46 +00:00
|
|
|
|
|
|
|
def value_changed(self, packet):
|
|
|
|
"""Update the internal state of the sensor."""
|
|
|
|
if packet.rorg != 0xA5:
|
|
|
|
return
|
|
|
|
humidity = packet.data[2] * 100 / 250
|
|
|
|
self._state = round(humidity, 1)
|
|
|
|
self.schedule_update_ha_state()
|
2019-12-25 10:41:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EnOceanWindowHandle(EnOceanSensor):
|
|
|
|
"""Representation of an EnOcean window handle device.
|
|
|
|
|
|
|
|
EEPs (EnOcean Equipment Profiles):
|
|
|
|
- F6-10-00 (Mechanical handle / Hoppe AG)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, dev_id, dev_name):
|
|
|
|
"""Initialize the EnOcean window handle sensor device."""
|
|
|
|
super().__init__(dev_id, dev_name, SENSOR_TYPE_WINDOWHANDLE)
|
|
|
|
|
|
|
|
def value_changed(self, packet):
|
|
|
|
"""Update the internal state of the sensor."""
|
|
|
|
|
|
|
|
action = (packet.data[1] & 0x70) >> 4
|
|
|
|
|
|
|
|
if action == 0x07:
|
|
|
|
self._state = STATE_CLOSED
|
|
|
|
if action in (0x04, 0x06):
|
|
|
|
self._state = STATE_OPEN
|
|
|
|
if action == 0x05:
|
|
|
|
self._state = "tilt"
|
|
|
|
|
|
|
|
self.schedule_update_ha_state()
|