mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Use EntityDescription - melcloud (#53572)
* Use EntityDescription - melcloud * Fix pylint errors * Fix test * Fix coverage exclude comments
This commit is contained in:
parent
3908aabc13
commit
14c257e1b7
@ -1,7 +1,8 @@
|
|||||||
"""Support for MelCloud device sensors."""
|
"""Support for MelCloud device sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Callable, NamedTuple
|
from dataclasses import dataclass
|
||||||
|
from typing import Any, Callable
|
||||||
|
|
||||||
from pymelcloud import DEVICE_TYPE_ATA, DEVICE_TYPE_ATW
|
from pymelcloud import DEVICE_TYPE_ATA, DEVICE_TYPE_ATW
|
||||||
from pymelcloud.atw_device import Zone
|
from pymelcloud.atw_device import Zone
|
||||||
@ -11,6 +12,7 @@ from homeassistant.components.sensor import (
|
|||||||
DEVICE_CLASS_TEMPERATURE,
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.const import ENERGY_KILO_WATT_HOUR, TEMP_CELSIUS
|
from homeassistant.const import ENERGY_KILO_WATT_HOUR, TEMP_CELSIUS
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
@ -19,130 +21,146 @@ from . import MelCloudDevice
|
|||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
class SensorMetadata(NamedTuple):
|
@dataclass
|
||||||
"""Metadata for an individual sensor."""
|
class MelcloudSensorEntityDescription(SensorEntityDescription):
|
||||||
|
"""Describes Melcloud sensor entity."""
|
||||||
|
|
||||||
measurement_name: str
|
_value_fn: Callable[[Any], float] | None = None
|
||||||
icon: str
|
_enabled: Callable[[Any], bool] | None = None
|
||||||
unit: str
|
|
||||||
device_class: str
|
def __post_init__(self) -> None:
|
||||||
value_fn: Callable[[Any], float]
|
"""Ensure all required fields are set."""
|
||||||
enabled: Callable[[Any], bool]
|
if self._value_fn is None: # pragma: no cover
|
||||||
|
raise TypeError
|
||||||
|
if self._enabled is None: # pragma: no cover
|
||||||
|
raise TypeError
|
||||||
|
self.value_fn = self._value_fn
|
||||||
|
self.enabled = self._enabled
|
||||||
|
|
||||||
|
|
||||||
ATA_SENSORS: dict[str, SensorMetadata] = {
|
ATA_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
|
||||||
"room_temperature": SensorMetadata(
|
MelcloudSensorEntityDescription(
|
||||||
"Room Temperature",
|
key="room_temperature",
|
||||||
|
name="Room Temperature",
|
||||||
icon="mdi:thermometer",
|
icon="mdi:thermometer",
|
||||||
unit=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
value_fn=lambda x: x.device.room_temperature,
|
_value_fn=lambda x: x.device.room_temperature,
|
||||||
enabled=lambda x: True,
|
_enabled=lambda x: True,
|
||||||
),
|
),
|
||||||
"energy": SensorMetadata(
|
MelcloudSensorEntityDescription(
|
||||||
"Energy",
|
key="energy",
|
||||||
|
name="Energy",
|
||||||
icon="mdi:factory",
|
icon="mdi:factory",
|
||||||
unit=ENERGY_KILO_WATT_HOUR,
|
unit_of_measurement=ENERGY_KILO_WATT_HOUR,
|
||||||
device_class=DEVICE_CLASS_ENERGY,
|
device_class=DEVICE_CLASS_ENERGY,
|
||||||
value_fn=lambda x: x.device.total_energy_consumed,
|
_value_fn=lambda x: x.device.total_energy_consumed,
|
||||||
enabled=lambda x: x.device.has_energy_consumed_meter,
|
_enabled=lambda x: x.device.has_energy_consumed_meter,
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
ATW_SENSORS: dict[str, SensorMetadata] = {
|
ATW_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
|
||||||
"outside_temperature": SensorMetadata(
|
MelcloudSensorEntityDescription(
|
||||||
"Outside Temperature",
|
key="outside_temperature",
|
||||||
|
name="Outside Temperature",
|
||||||
icon="mdi:thermometer",
|
icon="mdi:thermometer",
|
||||||
unit=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
value_fn=lambda x: x.device.outside_temperature,
|
_value_fn=lambda x: x.device.outside_temperature,
|
||||||
enabled=lambda x: True,
|
_enabled=lambda x: True,
|
||||||
),
|
),
|
||||||
"tank_temperature": SensorMetadata(
|
MelcloudSensorEntityDescription(
|
||||||
"Tank Temperature",
|
key="tank_temperature",
|
||||||
|
name="Tank Temperature",
|
||||||
icon="mdi:thermometer",
|
icon="mdi:thermometer",
|
||||||
unit=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
value_fn=lambda x: x.device.tank_temperature,
|
_value_fn=lambda x: x.device.tank_temperature,
|
||||||
enabled=lambda x: True,
|
_enabled=lambda x: True,
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
ATW_ZONE_SENSORS: dict[str, SensorMetadata] = {
|
ATW_ZONE_SENSORS: tuple[MelcloudSensorEntityDescription, ...] = (
|
||||||
"room_temperature": SensorMetadata(
|
MelcloudSensorEntityDescription(
|
||||||
"Room Temperature",
|
key="room_temperature",
|
||||||
|
name="Room Temperature",
|
||||||
icon="mdi:thermometer",
|
icon="mdi:thermometer",
|
||||||
unit=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
value_fn=lambda zone: zone.room_temperature,
|
_value_fn=lambda zone: zone.room_temperature,
|
||||||
enabled=lambda x: True,
|
_enabled=lambda x: True,
|
||||||
),
|
),
|
||||||
"flow_temperature": SensorMetadata(
|
MelcloudSensorEntityDescription(
|
||||||
"Flow Temperature",
|
key="flow_temperature",
|
||||||
|
name="Flow Temperature",
|
||||||
icon="mdi:thermometer",
|
icon="mdi:thermometer",
|
||||||
unit=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
value_fn=lambda zone: zone.flow_temperature,
|
_value_fn=lambda zone: zone.flow_temperature,
|
||||||
enabled=lambda x: True,
|
_enabled=lambda x: True,
|
||||||
),
|
),
|
||||||
"return_temperature": SensorMetadata(
|
MelcloudSensorEntityDescription(
|
||||||
"Flow Return Temperature",
|
key="return_temperature",
|
||||||
|
name="Flow Return Temperature",
|
||||||
icon="mdi:thermometer",
|
icon="mdi:thermometer",
|
||||||
unit=TEMP_CELSIUS,
|
unit_of_measurement=TEMP_CELSIUS,
|
||||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
value_fn=lambda zone: zone.return_temperature,
|
_value_fn=lambda zone: zone.return_temperature,
|
||||||
enabled=lambda x: True,
|
_enabled=lambda x: True,
|
||||||
),
|
),
|
||||||
}
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry, async_add_entities):
|
async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
"""Set up MELCloud device sensors based on config_entry."""
|
"""Set up MELCloud device sensors based on config_entry."""
|
||||||
mel_devices = hass.data[DOMAIN].get(entry.entry_id)
|
mel_devices = hass.data[DOMAIN].get(entry.entry_id)
|
||||||
async_add_entities(
|
|
||||||
[
|
entities: list[MelDeviceSensor] = [
|
||||||
MelDeviceSensor(mel_device, measurement, metadata)
|
MelDeviceSensor(mel_device, description)
|
||||||
for measurement, metadata in ATA_SENSORS.items()
|
for description in ATA_SENSORS
|
||||||
for mel_device in mel_devices[DEVICE_TYPE_ATA]
|
for mel_device in mel_devices[DEVICE_TYPE_ATA]
|
||||||
if metadata.enabled(mel_device)
|
if description.enabled(mel_device)
|
||||||
]
|
] + [
|
||||||
+ [
|
MelDeviceSensor(mel_device, description)
|
||||||
MelDeviceSensor(mel_device, measurement, metadata)
|
for description in ATW_SENSORS
|
||||||
for measurement, metadata in ATW_SENSORS.items()
|
|
||||||
for mel_device in mel_devices[DEVICE_TYPE_ATW]
|
for mel_device in mel_devices[DEVICE_TYPE_ATW]
|
||||||
if metadata.enabled(mel_device)
|
if description.enabled(mel_device)
|
||||||
]
|
]
|
||||||
+ [
|
entities.extend(
|
||||||
AtwZoneSensor(mel_device, zone, measurement, metadata)
|
[
|
||||||
|
AtwZoneSensor(mel_device, zone, description)
|
||||||
for mel_device in mel_devices[DEVICE_TYPE_ATW]
|
for mel_device in mel_devices[DEVICE_TYPE_ATW]
|
||||||
for zone in mel_device.device.zones
|
for zone in mel_device.device.zones
|
||||||
for measurement, metadata, in ATW_ZONE_SENSORS.items()
|
for description in ATW_ZONE_SENSORS
|
||||||
if metadata.enabled(zone)
|
if description.enabled(zone)
|
||||||
],
|
]
|
||||||
True,
|
|
||||||
)
|
)
|
||||||
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class MelDeviceSensor(SensorEntity):
|
class MelDeviceSensor(SensorEntity):
|
||||||
"""Representation of a Sensor."""
|
"""Representation of a Sensor."""
|
||||||
|
|
||||||
def __init__(self, api: MelCloudDevice, measurement, metadata: SensorMetadata):
|
entity_description: MelcloudSensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
api: MelCloudDevice,
|
||||||
|
description: MelcloudSensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._api = api
|
self._api = api
|
||||||
self._metadata = metadata
|
self.entity_description = description
|
||||||
|
|
||||||
self._attr_device_class = metadata.device_class
|
self._attr_name = f"{api.name} {description.name}"
|
||||||
self._attr_icon = metadata.icon
|
self._attr_unique_id = f"{api.device.serial}-{api.device.mac}-{description.key}"
|
||||||
self._attr_name = f"{api.name} {metadata.measurement_name}"
|
|
||||||
self._attr_unique_id = f"{api.device.serial}-{api.device.mac}-{measurement}"
|
|
||||||
self._attr_unit_of_measurement = metadata.unit
|
|
||||||
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
|
|
||||||
if metadata.device_class == DEVICE_CLASS_ENERGY:
|
if description.device_class == DEVICE_CLASS_ENERGY:
|
||||||
self._attr_last_reset = dt_util.utc_from_timestamp(0)
|
self._attr_last_reset = dt_util.utc_from_timestamp(0)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._metadata.value_fn(self._api)
|
return self.entity_description.value_fn(self._api)
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Retrieve latest state."""
|
"""Retrieve latest state."""
|
||||||
@ -158,18 +176,19 @@ class AtwZoneSensor(MelDeviceSensor):
|
|||||||
"""Air-to-Air device sensor."""
|
"""Air-to-Air device sensor."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, api: MelCloudDevice, zone: Zone, measurement, metadata: SensorMetadata
|
self,
|
||||||
):
|
api: MelCloudDevice,
|
||||||
|
zone: Zone,
|
||||||
|
description: MelcloudSensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
if zone.zone_index == 1:
|
if zone.zone_index != 1:
|
||||||
full_measurement = measurement
|
description.key = f"{description.key}-zone-{zone.zone_index}"
|
||||||
else:
|
super().__init__(api, description)
|
||||||
full_measurement = f"{measurement}-zone-{zone.zone_index}"
|
|
||||||
super().__init__(api, full_measurement, metadata)
|
|
||||||
self._zone = zone
|
self._zone = zone
|
||||||
self._attr_name = f"{api.name} {zone.name} {metadata.measurement_name}"
|
self._attr_name = f"{api.name} {zone.name} {description.name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return zone based state."""
|
"""Return zone based state."""
|
||||||
return self._metadata.value_fn(self._zone)
|
return self.entity_description.value_fn(self._zone)
|
||||||
|
@ -37,15 +37,13 @@ def test_zone_unique_ids(mock_device, mock_zone_1, mock_zone_2):
|
|||||||
sensor_1 = AtwZoneSensor(
|
sensor_1 = AtwZoneSensor(
|
||||||
mock_device,
|
mock_device,
|
||||||
mock_zone_1,
|
mock_zone_1,
|
||||||
"room_temperature",
|
ATW_ZONE_SENSORS[0], # room_temperature
|
||||||
ATW_ZONE_SENSORS["room_temperature"],
|
|
||||||
)
|
)
|
||||||
assert sensor_1.unique_id == "1234-11:11:11:11:11:11-room_temperature"
|
assert sensor_1.unique_id == "1234-11:11:11:11:11:11-room_temperature"
|
||||||
|
|
||||||
sensor_2 = AtwZoneSensor(
|
sensor_2 = AtwZoneSensor(
|
||||||
mock_device,
|
mock_device,
|
||||||
mock_zone_2,
|
mock_zone_2,
|
||||||
"room_temperature",
|
ATW_ZONE_SENSORS[0], # room_temperature
|
||||||
ATW_ZONE_SENSORS["flow_temperature"],
|
|
||||||
)
|
)
|
||||||
assert sensor_2.unique_id == "1234-11:11:11:11:11:11-room_temperature-zone-2"
|
assert sensor_2.unique_id == "1234-11:11:11:11:11:11-room_temperature-zone-2"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user