Add Melcloud device class and state class (#52276)
This commit is contained in:
parent
2576dd9da9
commit
ba7ad8a58f
2 changed files with 30 additions and 32 deletions
|
@ -2,14 +2,19 @@
|
||||||
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
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
DEVICE_CLASS_ENERGY,
|
||||||
|
DEVICE_CLASS_TEMPERATURE,
|
||||||
|
STATE_CLASS_MEASUREMENT,
|
||||||
|
SensorEntity,
|
||||||
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_DEVICE_CLASS,
|
ATTR_DEVICE_CLASS,
|
||||||
ATTR_ICON,
|
ATTR_ICON,
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
TEMP_CELSIUS,
|
TEMP_CELSIUS,
|
||||||
)
|
)
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from . import MelCloudDevice
|
from . import MelCloudDevice
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
@ -32,7 +37,7 @@ ATA_SENSORS = {
|
||||||
ATTR_MEASUREMENT_NAME: "Energy",
|
ATTR_MEASUREMENT_NAME: "Energy",
|
||||||
ATTR_ICON: "mdi:factory",
|
ATTR_ICON: "mdi:factory",
|
||||||
ATTR_UNIT: ENERGY_KILO_WATT_HOUR,
|
ATTR_UNIT: ENERGY_KILO_WATT_HOUR,
|
||||||
ATTR_DEVICE_CLASS: None,
|
ATTR_DEVICE_CLASS: DEVICE_CLASS_ENERGY,
|
||||||
ATTR_VALUE_FN: lambda x: x.device.total_energy_consumed,
|
ATTR_VALUE_FN: lambda x: x.device.total_energy_consumed,
|
||||||
ATTR_ENABLED_FN: lambda x: x.device.has_energy_consumed_meter,
|
ATTR_ENABLED_FN: lambda x: x.device.has_energy_consumed_meter,
|
||||||
},
|
},
|
||||||
|
@ -116,40 +121,23 @@ class MelDeviceSensor(SensorEntity):
|
||||||
def __init__(self, api: MelCloudDevice, measurement, definition):
|
def __init__(self, api: MelCloudDevice, measurement, definition):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._api = api
|
self._api = api
|
||||||
self._name_slug = api.name
|
|
||||||
self._measurement = measurement
|
|
||||||
self._def = definition
|
self._def = definition
|
||||||
|
|
||||||
@property
|
self._attr_device_class = definition[ATTR_DEVICE_CLASS]
|
||||||
def unique_id(self):
|
self._attr_icon = definition[ATTR_ICON]
|
||||||
"""Return a unique ID."""
|
self._attr_name = f"{api.name} {definition[ATTR_MEASUREMENT_NAME]}"
|
||||||
return f"{self._api.device.serial}-{self._api.device.mac}-{self._measurement}"
|
self._attr_unique_id = f"{api.device.serial}-{api.device.mac}-{measurement}"
|
||||||
|
self._attr_unit_of_measurement = definition[ATTR_UNIT]
|
||||||
|
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
|
|
||||||
@property
|
if self.device_class == DEVICE_CLASS_ENERGY:
|
||||||
def icon(self):
|
self._attr_last_reset = dt_util.utc_from_timestamp(0)
|
||||||
"""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
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._def[ATTR_VALUE_FN](self._api)
|
return self._def[ATTR_VALUE_FN](self._api)
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return self._def[ATTR_UNIT]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return device class."""
|
|
||||||
return self._def[ATTR_DEVICE_CLASS]
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Retrieve latest state."""
|
"""Retrieve latest state."""
|
||||||
await self._api.async_update()
|
await self._api.async_update()
|
||||||
|
@ -171,7 +159,7 @@ class AtwZoneSensor(MelDeviceSensor):
|
||||||
full_measurement = f"{measurement}-zone-{zone.zone_index}"
|
full_measurement = f"{measurement}-zone-{zone.zone_index}"
|
||||||
super().__init__(api, full_measurement, definition)
|
super().__init__(api, full_measurement, definition)
|
||||||
self._zone = zone
|
self._zone = zone
|
||||||
self._name_slug = f"{api.name} {zone.name}"
|
self._attr_name = f"{api.name} {zone.name} {definition[ATTR_MEASUREMENT_NAME]}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
|
|
@ -3,7 +3,7 @@ from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.melcloud.sensor import AtwZoneSensor
|
from homeassistant.components.melcloud.sensor import ATW_ZONE_SENSORS, AtwZoneSensor
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
@ -34,8 +34,18 @@ def mock_zone_2():
|
||||||
|
|
||||||
def test_zone_unique_ids(mock_device, mock_zone_1, mock_zone_2):
|
def test_zone_unique_ids(mock_device, mock_zone_1, mock_zone_2):
|
||||||
"""Test unique id generation correctness."""
|
"""Test unique id generation correctness."""
|
||||||
sensor_1 = AtwZoneSensor(mock_device, mock_zone_1, "room_temperature", {})
|
sensor_1 = AtwZoneSensor(
|
||||||
|
mock_device,
|
||||||
|
mock_zone_1,
|
||||||
|
"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(mock_device, mock_zone_2, "room_temperature", {})
|
sensor_2 = AtwZoneSensor(
|
||||||
|
mock_device,
|
||||||
|
mock_zone_2,
|
||||||
|
"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…
Add table
Add a link
Reference in a new issue