2020-02-10 21:09:12 +00:00
|
|
|
"""Support for MelCloud device sensors."""
|
2021-07-20 18:18:09 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-29 14:19:06 +00:00
|
|
|
from collections.abc import Callable
|
2021-07-27 22:07:14 +00:00
|
|
|
from dataclasses import dataclass
|
2021-09-29 14:19:06 +00:00
|
|
|
from typing import Any
|
2021-07-20 18:18:09 +00:00
|
|
|
|
2020-03-10 18:00:07 +00:00
|
|
|
from pymelcloud import DEVICE_TYPE_ATA, DEVICE_TYPE_ATW
|
|
|
|
from pymelcloud.atw_device import Zone
|
2020-02-10 21:09:12 +00:00
|
|
|
|
2021-06-29 17:16:43 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-15 15:37:17 +00:00
|
|
|
SensorDeviceClass,
|
2021-06-29 17:16:43 +00:00
|
|
|
SensorEntity,
|
2021-07-27 22:07:14 +00:00
|
|
|
SensorEntityDescription,
|
2021-12-15 15:37:17 +00:00
|
|
|
SensorStateClass,
|
2021-06-29 17:16:43 +00:00
|
|
|
)
|
2021-07-20 18:18:09 +00:00
|
|
|
from homeassistant.const import ENERGY_KILO_WATT_HOUR, TEMP_CELSIUS
|
2020-02-10 21:09:12 +00:00
|
|
|
|
2020-02-14 20:11:51 +00:00
|
|
|
from . import MelCloudDevice
|
2020-05-01 11:33:46 +00:00
|
|
|
from .const import DOMAIN
|
2020-02-10 21:09:12 +00:00
|
|
|
|
2021-07-20 18:18:09 +00:00
|
|
|
|
2021-07-27 22:07:14 +00:00
|
|
|
@dataclass
|
2021-07-28 09:50:13 +00:00
|
|
|
class MelcloudRequiredKeysMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
value_fn: Callable[[Any], float]
|
|
|
|
enabled: Callable[[Any], bool]
|
2021-07-20 18:18:09 +00:00
|
|
|
|
|
|
|
|
2021-07-28 09:50:13 +00:00
|
|
|
@dataclass
|
|
|
|
class MelcloudSensorEntityDescription(
|
|
|
|
SensorEntityDescription, MelcloudRequiredKeysMixin
|
|
|
|
):
|
|
|
|
"""Describes Melcloud sensor entity."""
|
2021-07-20 18:18:09 +00:00
|
|
|
|
2021-07-27 22:07:14 +00:00
|
|
|
|
|
|
|
ATA_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
|
|
|
|
MelcloudSensorEntityDescription(
|
|
|
|
key="room_temperature",
|
|
|
|
name="Room Temperature",
|
2021-07-20 18:18:09 +00:00
|
|
|
icon="mdi:thermometer",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-15 15:37:17 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-07-28 09:50:13 +00:00
|
|
|
value_fn=lambda x: x.device.room_temperature,
|
|
|
|
enabled=lambda x: True,
|
2021-07-20 18:18:09 +00:00
|
|
|
),
|
2021-07-27 22:07:14 +00:00
|
|
|
MelcloudSensorEntityDescription(
|
|
|
|
key="energy",
|
|
|
|
name="Energy",
|
2021-07-20 18:18:09 +00:00
|
|
|
icon="mdi:factory",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
2021-12-15 15:37:17 +00:00
|
|
|
device_class=SensorDeviceClass.ENERGY,
|
2021-07-28 09:50:13 +00:00
|
|
|
value_fn=lambda x: x.device.total_energy_consumed,
|
|
|
|
enabled=lambda x: x.device.has_energy_consumed_meter,
|
2021-07-20 18:18:09 +00:00
|
|
|
),
|
2021-07-27 22:07:14 +00:00
|
|
|
)
|
|
|
|
ATW_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
|
|
|
|
MelcloudSensorEntityDescription(
|
|
|
|
key="outside_temperature",
|
|
|
|
name="Outside Temperature",
|
2021-07-20 18:18:09 +00:00
|
|
|
icon="mdi:thermometer",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-15 15:37:17 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-07-28 09:50:13 +00:00
|
|
|
value_fn=lambda x: x.device.outside_temperature,
|
|
|
|
enabled=lambda x: True,
|
2021-07-20 18:18:09 +00:00
|
|
|
),
|
2021-07-27 22:07:14 +00:00
|
|
|
MelcloudSensorEntityDescription(
|
|
|
|
key="tank_temperature",
|
|
|
|
name="Tank Temperature",
|
2021-07-20 18:18:09 +00:00
|
|
|
icon="mdi:thermometer",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-15 15:37:17 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-07-28 09:50:13 +00:00
|
|
|
value_fn=lambda x: x.device.tank_temperature,
|
|
|
|
enabled=lambda x: True,
|
2021-07-20 18:18:09 +00:00
|
|
|
),
|
2021-07-27 22:07:14 +00:00
|
|
|
)
|
|
|
|
ATW_ZONE_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
|
|
|
|
MelcloudSensorEntityDescription(
|
|
|
|
key="room_temperature",
|
|
|
|
name="Room Temperature",
|
2021-07-20 18:18:09 +00:00
|
|
|
icon="mdi:thermometer",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-15 15:37:17 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-07-28 09:50:13 +00:00
|
|
|
value_fn=lambda zone: zone.room_temperature,
|
|
|
|
enabled=lambda x: True,
|
2021-07-20 18:18:09 +00:00
|
|
|
),
|
2021-07-27 22:07:14 +00:00
|
|
|
MelcloudSensorEntityDescription(
|
|
|
|
key="flow_temperature",
|
|
|
|
name="Flow Temperature",
|
2021-07-20 18:18:09 +00:00
|
|
|
icon="mdi:thermometer",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-15 15:37:17 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-07-28 09:50:13 +00:00
|
|
|
value_fn=lambda zone: zone.flow_temperature,
|
|
|
|
enabled=lambda x: True,
|
2021-07-20 18:18:09 +00:00
|
|
|
),
|
2021-07-27 22:07:14 +00:00
|
|
|
MelcloudSensorEntityDescription(
|
|
|
|
key="return_temperature",
|
|
|
|
name="Flow Return Temperature",
|
2021-07-20 18:18:09 +00:00
|
|
|
icon="mdi:thermometer",
|
2021-08-11 19:17:47 +00:00
|
|
|
native_unit_of_measurement=TEMP_CELSIUS,
|
2021-12-15 15:37:17 +00:00
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2021-07-28 09:50:13 +00:00
|
|
|
value_fn=lambda zone: zone.return_temperature,
|
|
|
|
enabled=lambda x: True,
|
2021-07-20 18:18:09 +00:00
|
|
|
),
|
2021-07-27 22:07:14 +00:00
|
|
|
)
|
2020-02-10 21:09:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2021-07-27 22:07:14 +00:00
|
|
|
|
|
|
|
entities: list[MelDeviceSensor] = [
|
|
|
|
MelDeviceSensor(mel_device, description)
|
|
|
|
for description in ATA_SENSORS
|
|
|
|
for mel_device in mel_devices[DEVICE_TYPE_ATA]
|
|
|
|
if description.enabled(mel_device)
|
|
|
|
] + [
|
|
|
|
MelDeviceSensor(mel_device, description)
|
|
|
|
for description in ATW_SENSORS
|
|
|
|
for mel_device in mel_devices[DEVICE_TYPE_ATW]
|
|
|
|
if description.enabled(mel_device)
|
|
|
|
]
|
|
|
|
entities.extend(
|
2020-02-10 21:09:12 +00:00
|
|
|
[
|
2021-07-27 22:07:14 +00:00
|
|
|
AtwZoneSensor(mel_device, zone, description)
|
2020-03-10 18:00:07 +00:00
|
|
|
for mel_device in mel_devices[DEVICE_TYPE_ATW]
|
|
|
|
for zone in mel_device.device.zones
|
2021-07-27 22:07:14 +00:00
|
|
|
for description in ATW_ZONE_SENSORS
|
|
|
|
if description.enabled(zone)
|
|
|
|
]
|
2020-02-10 21:09:12 +00:00
|
|
|
)
|
2021-07-27 22:07:14 +00:00
|
|
|
async_add_entities(entities, True)
|
2020-02-10 21:09:12 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class MelDeviceSensor(SensorEntity):
|
2020-02-10 21:09:12 +00:00
|
|
|
"""Representation of a Sensor."""
|
|
|
|
|
2021-07-27 22:07:14 +00:00
|
|
|
entity_description: MelcloudSensorEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
api: MelCloudDevice,
|
|
|
|
description: MelcloudSensorEntityDescription,
|
|
|
|
) -> None:
|
2020-02-10 21:09:12 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-03-10 18:00:07 +00:00
|
|
|
self._api = api
|
2021-07-27 22:07:14 +00:00
|
|
|
self.entity_description = description
|
2020-02-10 21:09:12 +00:00
|
|
|
|
2021-07-27 22:07:14 +00:00
|
|
|
self._attr_name = f"{api.name} {description.name}"
|
|
|
|
self._attr_unique_id = f"{api.device.serial}-{api.device.mac}-{description.key}"
|
2020-02-10 21:09:12 +00:00
|
|
|
|
2021-12-15 15:37:17 +00:00
|
|
|
if description.device_class == SensorDeviceClass.ENERGY:
|
|
|
|
self._attr_state_class = SensorStateClass.TOTAL_INCREASING
|
2021-08-16 14:10:07 +00:00
|
|
|
else:
|
2021-12-15 15:37:17 +00:00
|
|
|
self._attr_state_class = SensorStateClass.MEASUREMENT
|
2020-02-10 21:09:12 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_value(self):
|
2020-02-10 21:09:12 +00:00
|
|
|
"""Return the state of the sensor."""
|
2021-07-27 22:07:14 +00:00
|
|
|
return self.entity_description.value_fn(self._api)
|
2020-02-10 21:09:12 +00:00
|
|
|
|
|
|
|
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
|
2020-03-10 18:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AtwZoneSensor(MelDeviceSensor):
|
|
|
|
"""Air-to-Air device sensor."""
|
|
|
|
|
2021-07-20 18:18:09 +00:00
|
|
|
def __init__(
|
2021-07-27 22:07:14 +00:00
|
|
|
self,
|
|
|
|
api: MelCloudDevice,
|
|
|
|
zone: Zone,
|
|
|
|
description: MelcloudSensorEntityDescription,
|
|
|
|
) -> None:
|
2020-03-10 18:00:07 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-07-27 22:07:14 +00:00
|
|
|
if zone.zone_index != 1:
|
|
|
|
description.key = f"{description.key}-zone-{zone.zone_index}"
|
|
|
|
super().__init__(api, description)
|
2020-03-10 18:00:07 +00:00
|
|
|
self._zone = zone
|
2021-07-27 22:07:14 +00:00
|
|
|
self._attr_name = f"{api.name} {zone.name} {description.name}"
|
2020-03-10 18:00:07 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:47 +00:00
|
|
|
def native_value(self):
|
2020-03-10 18:00:07 +00:00
|
|
|
"""Return zone based state."""
|
2021-07-27 22:07:14 +00:00
|
|
|
return self.entity_description.value_fn(self._zone)
|