From 010cad08c05988dbcedff9232fb4f76d4ce1f691 Mon Sep 17 00:00:00 2001 From: Gerben Jongerius Date: Fri, 31 Jan 2025 12:12:07 +0100 Subject: [PATCH] Add tariff sensor and peak sensors (#136919) --- homeassistant/components/youless/sensor.py | 34 +++- homeassistant/components/youless/strings.json | 9 + tests/components/youless/__init__.py | 5 + tests/components/youless/fixtures/device.json | 2 +- tests/components/youless/fixtures/phase.json | 15 ++ .../youless/snapshots/test_sensor.ambr | 176 +++++++++++++++++- tests/components/youless/test_init.py | 2 +- 7 files changed, 231 insertions(+), 12 deletions(-) create mode 100644 tests/components/youless/fixtures/phase.json diff --git a/homeassistant/components/youless/sensor.py b/homeassistant/components/youless/sensor.py index 3afb215ed5f5a..db8244c0b0662 100644 --- a/homeassistant/components/youless/sensor.py +++ b/homeassistant/components/youless/sensor.py @@ -36,7 +36,7 @@ class YouLessSensorEntityDescription(SensorEntityDescription): """Describes a YouLess sensor entity.""" device_group: str - value_func: Callable[[YoulessAPI], float | None] + value_func: Callable[[YoulessAPI], float | None | str] SENSOR_TYPES: tuple[YouLessSensorEntityDescription, ...] = ( @@ -212,6 +212,38 @@ class YouLessSensorEntityDescription(SensorEntityDescription): lambda device: device.phase3.current.value if device.phase1 else None ), ), + YouLessSensorEntityDescription( + key="tariff", + device_group="power", + translation_key="active_tariff", + device_class=SensorDeviceClass.ENUM, + options=["1", "2"], + value_func=( + lambda device: str(device.current_tariff) if device.current_tariff else None + ), + ), + YouLessSensorEntityDescription( + key="average_peak", + device_group="power", + translation_key="average_peak", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + value_func=( + lambda device: device.average_power.value if device.average_power else None + ), + ), + YouLessSensorEntityDescription( + key="month_peak", + device_group="power", + translation_key="month_peak", + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + value_func=( + lambda device: device.peak_power.value if device.peak_power else None + ), + ), YouLessSensorEntityDescription( key="delivery_low", device_group="delivery", diff --git a/homeassistant/components/youless/strings.json b/homeassistant/components/youless/strings.json index 8a3f6cb5d8b4f..c735e2b2ff2e5 100644 --- a/homeassistant/components/youless/strings.json +++ b/homeassistant/components/youless/strings.json @@ -52,6 +52,9 @@ "active_current_phase_a": { "name": "Current phase {phase}" }, + "active_tariff": { + "name": "Tariff" + }, "total_energy_import_tariff_kwh": { "name": "Energy import tariff {tariff}" }, @@ -66,6 +69,12 @@ }, "active_s0_w": { "name": "Current usage" + }, + "average_peak": { + "name": "Average peak" + }, + "month_peak": { + "name": "Month peak" } } } diff --git a/tests/components/youless/__init__.py b/tests/components/youless/__init__.py index 8770a7e2dc8fe..03db24cb7f76e 100644 --- a/tests/components/youless/__init__.py +++ b/tests/components/youless/__init__.py @@ -25,6 +25,11 @@ async def init_component(hass: HomeAssistant) -> MockConfigEntry: json=load_json_array_fixture("enologic.json", youless.DOMAIN), headers={"Content-Type": "application/json"}, ) + mock.get( + "http://1.1.1.1/f", + json=load_json_object_fixture("phase.json", youless.DOMAIN), + headers={"Content-Type": "application/json"}, + ) entry = MockConfigEntry( domain=youless.DOMAIN, diff --git a/tests/components/youless/fixtures/device.json b/tests/components/youless/fixtures/device.json index 7d089851923bc..82d07dba739a0 100644 --- a/tests/components/youless/fixtures/device.json +++ b/tests/components/youless/fixtures/device.json @@ -1,5 +1,5 @@ { "model": "LS120", - "fw": "1.4.2-EL", + "fw": "1.5.1-EL", "mac": "de2:2d2:3d23" } diff --git a/tests/components/youless/fixtures/phase.json b/tests/components/youless/fixtures/phase.json new file mode 100644 index 0000000000000..8a5aa3215ef8f --- /dev/null +++ b/tests/components/youless/fixtures/phase.json @@ -0,0 +1,15 @@ +{ + "tr": 1, + "i1": 0.123, + "v1": 240, + "l1": 462, + "v2": 240, + "l2": 230, + "i2": 0.123, + "v3": 240, + "l3": 230, + "i3": 0.123, + "pp": 1200, + "pts": 2501301621, + "pa": 400 +} diff --git a/tests/components/youless/snapshots/test_sensor.ambr b/tests/components/youless/snapshots/test_sensor.ambr index 0647d854d2a80..9e79b5b9b5ec7 100644 --- a/tests/components/youless/snapshots/test_sensor.ambr +++ b/tests/components/youless/snapshots/test_sensor.ambr @@ -152,6 +152,57 @@ 'state': '1624.264', }) # --- +# name: test_sensors[sensor.power_meter_average_peak-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.power_meter_average_peak', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Average peak', + 'platform': 'youless', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'average_peak', + 'unique_id': 'youless_localhost_average_peak', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.power_meter_average_peak-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'Power meter Average peak', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.power_meter_average_peak', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '400', + }) +# --- # name: test_sensors[sensor.power_meter_current_phase_1-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -200,7 +251,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '0.123', }) # --- # name: test_sensors[sensor.power_meter_current_phase_2-entry] @@ -251,7 +302,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '0.123', }) # --- # name: test_sensors[sensor.power_meter_current_phase_3-entry] @@ -302,7 +353,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '0.123', }) # --- # name: test_sensors[sensor.power_meter_current_power_usage-entry] @@ -458,6 +509,57 @@ 'state': '4490.631', }) # --- +# name: test_sensors[sensor.power_meter_month_peak-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.power_meter_month_peak', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Month peak', + 'platform': 'youless', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'month_peak', + 'unique_id': 'youless_localhost_month_peak', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.power_meter_month_peak-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'Power meter Month peak', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.power_meter_month_peak', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1200', + }) +# --- # name: test_sensors[sensor.power_meter_power_phase_1-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -506,7 +608,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '462', }) # --- # name: test_sensors[sensor.power_meter_power_phase_2-entry] @@ -557,7 +659,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '230', }) # --- # name: test_sensors[sensor.power_meter_power_phase_3-entry] @@ -608,7 +710,63 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '230', + }) +# --- +# name: test_sensors[sensor.power_meter_tariff-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + '1', + '2', + ]), + }), + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.power_meter_tariff', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Tariff', + 'platform': 'youless', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'active_tariff', + 'unique_id': 'youless_localhost_tariff', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[sensor.power_meter_tariff-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'Power meter Tariff', + 'options': list([ + '1', + '2', + ]), + }), + 'context': , + 'entity_id': 'sensor.power_meter_tariff', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1', }) # --- # name: test_sensors[sensor.power_meter_total_energy_import-entry] @@ -710,7 +868,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '240', }) # --- # name: test_sensors[sensor.power_meter_voltage_phase_2-entry] @@ -761,7 +919,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '240', }) # --- # name: test_sensors[sensor.power_meter_voltage_phase_3-entry] @@ -812,7 +970,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': '240', }) # --- # name: test_sensors[sensor.s0_meter_current_usage-entry] diff --git a/tests/components/youless/test_init.py b/tests/components/youless/test_init.py index 29db8c66af0b5..9f0956cea359a 100644 --- a/tests/components/youless/test_init.py +++ b/tests/components/youless/test_init.py @@ -15,4 +15,4 @@ async def test_async_setup_entry(hass: HomeAssistant) -> None: assert await setup.async_setup_component(hass, youless.DOMAIN, {}) assert entry.state is ConfigEntryState.LOADED - assert len(hass.states.async_entity_ids()) == 19 + assert len(hass.states.async_entity_ids()) == 22