From 234ebdcb840e96203ad1107c0dfcfc0d067462a3 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 4 Jul 2023 08:39:24 +0200 Subject: [PATCH] Add entity translations for P1 Monitor (#95811) --- homeassistant/components/p1_monitor/sensor.py | 75 +++++++--------- .../components/p1_monitor/strings.json | 88 +++++++++++++++++++ tests/components/p1_monitor/test_sensor.py | 31 ++++--- 3 files changed, 142 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/p1_monitor/sensor.py b/homeassistant/components/p1_monitor/sensor.py index f192dd44300..21a878fa187 100644 --- a/homeassistant/components/p1_monitor/sensor.py +++ b/homeassistant/components/p1_monitor/sensor.py @@ -4,7 +4,6 @@ from __future__ import annotations from typing import Literal from homeassistant.components.sensor import ( - DOMAIN as SENSOR_DOMAIN, SensorDeviceClass, SensorEntity, SensorEntityDescription, @@ -39,7 +38,7 @@ from .const import ( SENSORS_SMARTMETER: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="gas_consumption", - name="Gas Consumption", + translation_key="gas_consumption", entity_registry_enabled_default=False, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, device_class=SensorDeviceClass.GAS, @@ -47,49 +46,49 @@ SENSORS_SMARTMETER: tuple[SensorEntityDescription, ...] = ( ), SensorEntityDescription( key="power_consumption", - name="Power Consumption", + translation_key="power_consumption", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="energy_consumption_high", - name="Energy Consumption - High Tariff", + translation_key="energy_consumption_high", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="energy_consumption_low", - name="Energy Consumption - Low Tariff", + translation_key="energy_consumption_low", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="power_production", - name="Power Production", + translation_key="power_production", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="energy_production_high", - name="Energy Production - High Tariff", + translation_key="energy_production_high", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="energy_production_low", - name="Energy Production - Low Tariff", + translation_key="energy_production_low", native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="energy_tariff_period", - name="Energy Tariff Period", + translation_key="energy_tariff_period", icon="mdi:calendar-clock", ), ) @@ -97,84 +96,84 @@ SENSORS_SMARTMETER: tuple[SensorEntityDescription, ...] = ( SENSORS_PHASES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="voltage_phase_l1", - name="Voltage Phase L1", + translation_key="voltage_phase_l1", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="voltage_phase_l2", - name="Voltage Phase L2", + translation_key="voltage_phase_l2", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="voltage_phase_l3", - name="Voltage Phase L3", + translation_key="voltage_phase_l3", native_unit_of_measurement=UnitOfElectricPotential.VOLT, device_class=SensorDeviceClass.VOLTAGE, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="current_phase_l1", - name="Current Phase L1", + translation_key="current_phase_l1", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="current_phase_l2", - name="Current Phase L2", + translation_key="current_phase_l2", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="current_phase_l3", - name="Current Phase L3", + translation_key="current_phase_l3", native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, device_class=SensorDeviceClass.CURRENT, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="power_consumed_phase_l1", - name="Power Consumed Phase L1", + translation_key="power_consumed_phase_l1", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="power_consumed_phase_l2", - name="Power Consumed Phase L2", + translation_key="power_consumed_phase_l2", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="power_consumed_phase_l3", - name="Power Consumed Phase L3", + translation_key="power_consumed_phase_l3", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="power_produced_phase_l1", - name="Power Produced Phase L1", + translation_key="power_produced_phase_l1", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="power_produced_phase_l2", - name="Power Produced Phase L2", + translation_key="power_produced_phase_l2", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="power_produced_phase_l3", - name="Power Produced Phase L3", + translation_key="power_produced_phase_l3", native_unit_of_measurement=UnitOfPower.WATT, device_class=SensorDeviceClass.POWER, state_class=SensorStateClass.MEASUREMENT, @@ -184,32 +183,32 @@ SENSORS_PHASES: tuple[SensorEntityDescription, ...] = ( SENSORS_SETTINGS: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="gas_consumption_price", - name="Gas Consumption Price", + translation_key="gas_consumption_price", entity_registry_enabled_default=False, state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfVolume.CUBIC_METERS}", ), SensorEntityDescription( key="energy_consumption_price_low", - name="Energy Consumption Price - Low", + translation_key="energy_consumption_price_low", state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}", ), SensorEntityDescription( key="energy_consumption_price_high", - name="Energy Consumption Price - High", + translation_key="energy_consumption_price_high", state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}", ), SensorEntityDescription( key="energy_production_price_low", - name="Energy Production Price - Low", + translation_key="energy_production_price_low", state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}", ), SensorEntityDescription( key="energy_production_price_high", - name="Energy Production Price - High", + translation_key="energy_production_price_high", state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=f"{CURRENCY_EURO}/{UnitOfEnergy.KILO_WATT_HOUR}", ), @@ -218,21 +217,21 @@ SENSORS_SETTINGS: tuple[SensorEntityDescription, ...] = ( SENSORS_WATERMETER: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="consumption_day", - name="Consumption Day", + translation_key="consumption_day", state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.LITERS, device_class=SensorDeviceClass.WATER, ), SensorEntityDescription( key="consumption_total", - name="Consumption Total", + translation_key="consumption_total", state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=UnitOfVolume.CUBIC_METERS, device_class=SensorDeviceClass.WATER, ), SensorEntityDescription( key="pulse_count", - name="Pulse Count", + translation_key="pulse_count", ), ) @@ -248,7 +247,6 @@ async def async_setup_entry( coordinator=coordinator, description=description, name="SmartMeter", - service_key="smartmeter", service=SERVICE_SMARTMETER, ) for description in SENSORS_SMARTMETER @@ -258,7 +256,6 @@ async def async_setup_entry( coordinator=coordinator, description=description, name="Phases", - service_key="phases", service=SERVICE_PHASES, ) for description in SENSORS_PHASES @@ -268,7 +265,6 @@ async def async_setup_entry( coordinator=coordinator, description=description, name="Settings", - service_key="settings", service=SERVICE_SETTINGS, ) for description in SENSORS_SETTINGS @@ -279,7 +275,6 @@ async def async_setup_entry( coordinator=coordinator, description=description, name="WaterMeter", - service_key="watermeter", service=SERVICE_WATERMETER, ) for description in SENSORS_WATERMETER @@ -292,30 +287,28 @@ class P1MonitorSensorEntity( ): """Defines an P1 Monitor sensor.""" + _attr_has_entity_name = True + def __init__( self, *, coordinator: P1MonitorDataUpdateCoordinator, description: SensorEntityDescription, - service_key: Literal["smartmeter", "watermeter", "phases", "settings"], name: str, - service: str, + service: Literal["smartmeter", "watermeter", "phases", "settings"], ) -> None: """Initialize P1 Monitor sensor.""" super().__init__(coordinator=coordinator) - self._service_key = service_key + self._service_key = service - self.entity_id = f"{SENSOR_DOMAIN}.{service}_{description.key}" self.entity_description = description self._attr_unique_id = ( - f"{coordinator.config_entry.entry_id}_{service_key}_{description.key}" + f"{coordinator.config_entry.entry_id}_{service}_{description.key}" ) self._attr_device_info = DeviceInfo( entry_type=DeviceEntryType.SERVICE, - identifiers={ - (DOMAIN, f"{coordinator.config_entry.entry_id}_{service_key}") - }, + identifiers={(DOMAIN, f"{coordinator.config_entry.entry_id}_{service}")}, configuration_url=f"http://{coordinator.config_entry.data[CONF_HOST]}", manufacturer="P1 Monitor", name=name, diff --git a/homeassistant/components/p1_monitor/strings.json b/homeassistant/components/p1_monitor/strings.json index 0c745554e9d..781ca109235 100644 --- a/homeassistant/components/p1_monitor/strings.json +++ b/homeassistant/components/p1_monitor/strings.json @@ -14,5 +14,93 @@ "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" } + }, + "entity": { + "sensor": { + "gas_consumption": { + "name": "Gas consumption" + }, + "power_consumption": { + "name": "Power consumption" + }, + "energy_consumption_high": { + "name": "Energy consumption - High tariff" + }, + "energy_consumption_low": { + "name": "Energy consumption - Low tariff" + }, + "power_production": { + "name": "Power production" + }, + "energy_production_high": { + "name": "Energy production - High tariff" + }, + "energy_production_low": { + "name": "Energy production - Low tariff" + }, + "energy_tariff_period": { + "name": "Energy tariff period" + }, + "voltage_phase_l1": { + "name": "Voltage phase L1" + }, + "voltage_phase_l2": { + "name": "Voltage phase L2" + }, + "voltage_phase_l3": { + "name": "Voltage phase L3" + }, + "current_phase_l1": { + "name": "Current phase L1" + }, + "current_phase_l2": { + "name": "Current phase L2" + }, + "current_phase_l3": { + "name": "Current phase L3" + }, + "power_consumed_phase_l1": { + "name": "Power consumed phase L1" + }, + "power_consumed_phase_l2": { + "name": "Power consumed phase L2" + }, + "power_consumed_phase_l3": { + "name": "Power consumed phase L3" + }, + "power_produced_phase_l1": { + "name": "Power produced phase L1" + }, + "power_produced_phase_l2": { + "name": "Power produced phase L2" + }, + "power_produced_phase_l3": { + "name": "Power produced phase L3" + }, + "gas_consumption_price": { + "name": "Gas consumption price" + }, + "energy_consumption_price_low": { + "name": "Energy consumption price - Low" + }, + "energy_consumption_price_high": { + "name": "Energy consumption price - High" + }, + "energy_production_price_low": { + "name": "Energy production price - Low" + }, + "energy_production_price_high": { + "name": "Energy production price - High" + }, + "consumption_day": { + "name": "Consumption day" + }, + "consumption_total": { + "name": "Consumption total" + }, + "pulse_count": { + "name": "Pulse count" + } + } } } diff --git a/tests/components/p1_monitor/test_sensor.py b/tests/components/p1_monitor/test_sensor.py index 14ff3b1e519..f84df458d4b 100644 --- a/tests/components/p1_monitor/test_sensor.py +++ b/tests/components/p1_monitor/test_sensor.py @@ -43,20 +43,23 @@ async def test_smartmeter( assert state assert entry.unique_id == f"{entry_id}_smartmeter_power_consumption" assert state.state == "877" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Power Consumption" + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "SmartMeter Power consumption" assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert ATTR_ICON not in state.attributes - state = hass.states.get("sensor.smartmeter_energy_consumption_high") - entry = entity_registry.async_get("sensor.smartmeter_energy_consumption_high") + state = hass.states.get("sensor.smartmeter_energy_consumption_high_tariff") + entry = entity_registry.async_get( + "sensor.smartmeter_energy_consumption_high_tariff" + ) assert entry assert state assert entry.unique_id == f"{entry_id}_smartmeter_energy_consumption_high" assert state.state == "2770.133" assert ( - state.attributes.get(ATTR_FRIENDLY_NAME) == "Energy Consumption - High Tariff" + state.attributes.get(ATTR_FRIENDLY_NAME) + == "SmartMeter Energy consumption - High tariff" ) assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR @@ -69,7 +72,7 @@ async def test_smartmeter( assert state assert entry.unique_id == f"{entry_id}_smartmeter_energy_tariff_period" assert state.state == "high" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Energy Tariff Period" + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "SmartMeter Energy tariff period" assert state.attributes.get(ATTR_ICON) == "mdi:calendar-clock" assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes @@ -100,7 +103,7 @@ async def test_phases( assert state assert entry.unique_id == f"{entry_id}_phases_voltage_phase_l1" assert state.state == "233.6" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Voltage Phase L1" + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Phases Voltage phase L1" assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT assert ( state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfElectricPotential.VOLT @@ -114,7 +117,7 @@ async def test_phases( assert state assert entry.unique_id == f"{entry_id}_phases_current_phase_l1" assert state.state == "1.6" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Current Phase L1" + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Phases Current phase L1" assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT assert ( state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfElectricCurrent.AMPERE @@ -128,7 +131,7 @@ async def test_phases( assert state assert entry.unique_id == f"{entry_id}_phases_power_consumed_phase_l1" assert state.state == "315" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Power Consumed Phase L1" + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Phases Power consumed phase L1" assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER @@ -160,7 +163,10 @@ async def test_settings( assert state assert entry.unique_id == f"{entry_id}_settings_energy_consumption_price_low" assert state.state == "0.20522" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Energy Consumption Price - Low" + assert ( + state.attributes.get(ATTR_FRIENDLY_NAME) + == "Settings Energy consumption price - Low" + ) assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT assert ( state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) @@ -173,7 +179,10 @@ async def test_settings( assert state assert entry.unique_id == f"{entry_id}_settings_energy_production_price_low" assert state.state == "0.20522" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Energy Production Price - Low" + assert ( + state.attributes.get(ATTR_FRIENDLY_NAME) + == "Settings Energy production price - Low" + ) assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.MEASUREMENT assert ( state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) @@ -205,7 +214,7 @@ async def test_watermeter( assert state assert entry.unique_id == f"{entry_id}_watermeter_consumption_day" assert state.state == "112.0" - assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Consumption Day" + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "WaterMeter Consumption day" assert state.attributes.get(ATTR_STATE_CLASS) is SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfVolume.LITERS