2020-02-10 21:09:12 +00:00
|
|
|
"""Support for MelCloud device sensors."""
|
|
|
|
import logging
|
|
|
|
|
2020-02-14 20:11:51 +00:00
|
|
|
from pymelcloud import DEVICE_TYPE_ATA
|
2020-02-10 21:09:12 +00:00
|
|
|
|
|
|
|
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2020-02-14 20:11:51 +00:00
|
|
|
from . import MelCloudDevice
|
2020-02-10 21:09:12 +00:00
|
|
|
from .const import DOMAIN, TEMP_UNIT_LOOKUP
|
|
|
|
|
|
|
|
ATTR_MEASUREMENT_NAME = "measurement_name"
|
|
|
|
ATTR_ICON = "icon"
|
|
|
|
ATTR_UNIT_FN = "unit_fn"
|
|
|
|
ATTR_DEVICE_CLASS = "device_class"
|
|
|
|
ATTR_VALUE_FN = "value_fn"
|
2020-02-14 20:11:51 +00:00
|
|
|
ATTR_ENABLED_FN = "enabled"
|
2020-02-10 21:09:12 +00:00
|
|
|
|
|
|
|
SENSORS = {
|
|
|
|
"room_temperature": {
|
|
|
|
ATTR_MEASUREMENT_NAME: "Room Temperature",
|
|
|
|
ATTR_ICON: "mdi:thermometer",
|
|
|
|
ATTR_UNIT_FN: lambda x: TEMP_UNIT_LOOKUP.get(x.device.temp_unit, TEMP_CELSIUS),
|
|
|
|
ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
|
|
|
|
ATTR_VALUE_FN: lambda x: x.device.room_temperature,
|
2020-02-14 20:11:51 +00:00
|
|
|
ATTR_ENABLED_FN: lambda x: True,
|
2020-02-10 21:09:12 +00:00
|
|
|
},
|
|
|
|
"energy": {
|
|
|
|
ATTR_MEASUREMENT_NAME: "Energy",
|
|
|
|
ATTR_ICON: "mdi:factory",
|
|
|
|
ATTR_UNIT_FN: lambda x: "kWh",
|
|
|
|
ATTR_DEVICE_CLASS: None,
|
|
|
|
ATTR_VALUE_FN: lambda x: x.device.total_energy_consumed,
|
2020-02-14 20:11:51 +00:00
|
|
|
ATTR_ENABLED_FN: lambda x: x.device.has_energy_consumed_meter,
|
2020-02-10 21:09:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up MELCloud device sensors based on config_entry."""
|
|
|
|
mel_devices = hass.data[DOMAIN].get(entry.entry_id)
|
|
|
|
async_add_entities(
|
|
|
|
[
|
2020-02-14 20:11:51 +00:00
|
|
|
MelCloudSensor(mel_device, measurement, definition)
|
2020-02-10 21:09:12 +00:00
|
|
|
for measurement, definition in SENSORS.items()
|
|
|
|
for mel_device in mel_devices[DEVICE_TYPE_ATA]
|
2020-02-14 20:11:51 +00:00
|
|
|
if definition[ATTR_ENABLED_FN](mel_device)
|
2020-02-10 21:09:12 +00:00
|
|
|
],
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MelCloudSensor(Entity):
|
|
|
|
"""Representation of a Sensor."""
|
|
|
|
|
2020-02-14 20:11:51 +00:00
|
|
|
def __init__(self, device: MelCloudDevice, measurement, definition):
|
2020-02-10 21:09:12 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._api = device
|
|
|
|
self._name_slug = device.name
|
|
|
|
self._measurement = measurement
|
|
|
|
self._def = definition
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return f"{self._api.device.serial}-{self._api.device.mac}-{self._measurement}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
return self._def[ATTR_ICON]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return f"{self._name_slug} {self._def[ATTR_MEASUREMENT_NAME]}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._def[ATTR_VALUE_FN](self._api)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._def[ATTR_UNIT_FN](self._api)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return device class."""
|
|
|
|
return self._def[ATTR_DEVICE_CLASS]
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Retrieve latest state."""
|
|
|
|
await self._api.async_update()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a device description for device registry."""
|
|
|
|
return self._api.device_info
|