Add device class for Landis+Gyr GJ energy sensor (#89522)

This commit is contained in:
Vincent Knoop Pathuis 2023-03-16 11:12:05 +01:00 committed by GitHub
parent 46a5aa71ec
commit 273d794f7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 51 deletions

View file

@ -32,20 +32,11 @@ from homeassistant.helpers.update_coordinator import (
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from . import DOMAIN from . import DOMAIN
from .const import GJ_TO_MWH
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
HEAT_METER_SENSOR_TYPES = ( HEAT_METER_SENSOR_TYPES = (
SensorEntityDescription(
key="heat_usage",
icon="mdi:fire",
name="Heat usage",
native_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL,
),
SensorEntityDescription( SensorEntityDescription(
key="volume_usage_m3", key="volume_usage_m3",
icon="mdi:fire", icon="mdi:fire",
@ -54,23 +45,14 @@ HEAT_METER_SENSOR_TYPES = (
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
), ),
# Diagnostic entity for debugging, this will match the value in GJ indicated on the meter's display
SensorEntityDescription( SensorEntityDescription(
key="heat_usage_gj", key="heat_usage_gj",
icon="mdi:fire", icon="mdi:fire",
name="Heat usage GJ", name="Heat usage GJ",
native_unit_of_measurement="GJ", native_unit_of_measurement=UnitOfEnergy.GIGA_JOULE,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="heat_previous_year",
icon="mdi:fire",
name="Heat usage previous year",
native_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
entity_category=EntityCategory.DIAGNOSTIC, state_class=SensorStateClass.TOTAL,
), ),
# Diagnostic entity for debugging, this will match the value in GJ of previous year indicated on the meter's display
SensorEntityDescription( SensorEntityDescription(
key="heat_previous_year_gj", key="heat_previous_year_gj",
icon="mdi:fire", icon="mdi:fire",
@ -293,19 +275,4 @@ class HeatMeterSensor(
else: else:
self._attr_native_value = asdict(self.coordinator.data)[self.key] self._attr_native_value = asdict(self.coordinator.data)[self.key]
if self.key == "heat_usage":
self._attr_native_value = convert_gj_to_mwh(
self.coordinator.data.heat_usage_gj
)
if self.key == "heat_previous_year":
self._attr_native_value = convert_gj_to_mwh(
self.coordinator.data.heat_previous_year_gj
)
self.async_write_ha_state() self.async_write_ha_state()
def convert_gj_to_mwh(gigajoule) -> float:
"""Convert GJ to MWh using the conversion value."""
return round(gigajoule * GJ_TO_MWH, 5)

View file

@ -83,18 +83,18 @@ async def test_create_sensors(
await hass.services.async_call( await hass.services.async_call(
HA_DOMAIN, HA_DOMAIN,
SERVICE_UPDATE_ENTITY, SERVICE_UPDATE_ENTITY,
{ATTR_ENTITY_ID: "sensor.heat_meter_heat_usage"}, {ATTR_ENTITY_ID: "sensor.heat_meter_heat_usage_gj"},
blocking=True, blocking=True,
) )
await hass.async_block_till_done() await hass.async_block_till_done()
# check if 26 attributes have been created # check if 26 attributes have been created
assert len(hass.states.async_all()) == 27 assert len(hass.states.async_all()) == 25
state = hass.states.get("sensor.heat_meter_heat_usage") state = hass.states.get("sensor.heat_meter_heat_usage_gj")
assert state assert state
assert state.state == "34.16669" assert state.state == "123.0"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.MEGA_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.GIGA_JOULE
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY
@ -132,17 +132,17 @@ async def test_restore_state(mock_heat_meter, hass: HomeAssistant) -> None:
[ [
( (
State( State(
"sensor.heat_meter_heat_usage", "sensor.heat_meter_heat_usage_gj",
"34167", "34167",
attributes={ attributes={
ATTR_LAST_RESET: last_reset, ATTR_LAST_RESET: last_reset,
ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.MEGA_WATT_HOUR, ATTR_UNIT_OF_MEASUREMENT: UnitOfEnergy.GIGA_JOULE,
ATTR_STATE_CLASS: SensorStateClass.TOTAL, ATTR_STATE_CLASS: SensorStateClass.TOTAL,
}, },
), ),
{ {
"native_value": 34167, "native_value": 34167,
"native_unit_of_measurement": UnitOfEnergy.MEGA_WATT_HOUR, "native_unit_of_measurement": UnitOfEnergy.GIGA_JOULE,
"icon": "mdi:fire", "icon": "mdi:fire",
"last_reset": last_reset, "last_reset": last_reset,
}, },
@ -194,10 +194,10 @@ async def test_restore_state(mock_heat_meter, hass: HomeAssistant) -> None:
await hass.async_block_till_done() await hass.async_block_till_done()
# restore from cache # restore from cache
state = hass.states.get("sensor.heat_meter_heat_usage") state = hass.states.get("sensor.heat_meter_heat_usage_gj")
assert state assert state
assert state.state == "34167" assert state.state == "34167"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.MEGA_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.GIGA_JOULE
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL
state = hass.states.get("sensor.heat_meter_volume_usage") state = hass.states.get("sensor.heat_meter_volume_usage")
@ -240,21 +240,21 @@ async def test_exception_on_polling(mock_heat_meter, hass: HomeAssistant) -> Non
await hass.services.async_call( await hass.services.async_call(
HA_DOMAIN, HA_DOMAIN,
SERVICE_UPDATE_ENTITY, SERVICE_UPDATE_ENTITY,
{ATTR_ENTITY_ID: "sensor.heat_meter_heat_usage"}, {ATTR_ENTITY_ID: "sensor.heat_meter_heat_usage_gj"},
blocking=True, blocking=True,
) )
await hass.async_block_till_done() await hass.async_block_till_done()
# check if initial setup succeeded # check if initial setup succeeded
state = hass.states.get("sensor.heat_meter_heat_usage") state = hass.states.get("sensor.heat_meter_heat_usage_gj")
assert state assert state
assert state.state == "34.16669" assert state.state == "123.0"
# Now 'disable' the connection and wait for polling and see if it fails # Now 'disable' the connection and wait for polling and see if it fails
mock_heat_meter().read.side_effect = serial.serialutil.SerialException mock_heat_meter().read.side_effect = serial.serialutil.SerialException
async_fire_time_changed(hass, dt_util.utcnow() + POLLING_INTERVAL) async_fire_time_changed(hass, dt_util.utcnow() + POLLING_INTERVAL)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("sensor.heat_meter_heat_usage") state = hass.states.get("sensor.heat_meter_heat_usage_gj")
assert state.state == STATE_UNAVAILABLE assert state.state == STATE_UNAVAILABLE
# Now 'enable' and see if next poll succeeds # Now 'enable' and see if next poll succeeds
@ -270,6 +270,6 @@ async def test_exception_on_polling(mock_heat_meter, hass: HomeAssistant) -> Non
mock_heat_meter().read.side_effect = None mock_heat_meter().read.side_effect = None
async_fire_time_changed(hass, dt_util.utcnow() + POLLING_INTERVAL) async_fire_time_changed(hass, dt_util.utcnow() + POLLING_INTERVAL)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("sensor.heat_meter_heat_usage") state = hass.states.get("sensor.heat_meter_heat_usage_gj")
assert state assert state
assert state.state == "34.44447" assert state.state == "124.0"