From 5300eddf3336330f955694070441fcaed0fc1650 Mon Sep 17 00:00:00 2001 From: dontinelli <73341522+dontinelli@users.noreply.github.com> Date: Mon, 2 Sep 2024 19:50:09 +0200 Subject: [PATCH] Remove roundig in Solarlog and add suggested_display_precision (#125094) * Remove roundig and add suggested_display_precision * Add suggested_unit_of_measurement * Put lamda in parentheses --- homeassistant/components/solarlog/sensor.py | 74 +++++++++++----- .../solarlog/snapshots/test_sensor.ambr | 88 +++++++++++++++++-- 2 files changed, 133 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/solarlog/sensor.py b/homeassistant/components/solarlog/sensor.py index 498429f70cf..91e18da1cb2 100644 --- a/homeassistant/components/solarlog/sensor.py +++ b/homeassistant/components/solarlog/sensor.py @@ -84,38 +84,47 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] = SolarLogCoordinatorSensorEntityDescription( key="yield_day", translation_key="yield_day", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.yield_day / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_day, ), SolarLogCoordinatorSensorEntityDescription( key="yield_yesterday", translation_key="yield_yesterday", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.yield_yesterday / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_yesterday, ), SolarLogCoordinatorSensorEntityDescription( key="yield_month", translation_key="yield_month", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.yield_month / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_month, ), SolarLogCoordinatorSensorEntityDescription( key="yield_year", translation_key="yield_year", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.yield_year / 1000, 3), + value_fn=lambda data: data.yield_year, ), SolarLogCoordinatorSensorEntityDescription( key="yield_total", translation_key="yield_total", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL, - value_fn=lambda data: round(data.yield_total / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.yield_total, ), SolarLogCoordinatorSensorEntityDescription( key="consumption_ac", @@ -128,38 +137,48 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] = SolarLogCoordinatorSensorEntityDescription( key="consumption_day", translation_key="consumption_day", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.consumption_day / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_day, ), SolarLogCoordinatorSensorEntityDescription( key="consumption_yesterday", translation_key="consumption_yesterday", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.consumption_yesterday / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_yesterday, ), SolarLogCoordinatorSensorEntityDescription( key="consumption_month", translation_key="consumption_month", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.consumption_month / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_month, ), SolarLogCoordinatorSensorEntityDescription( key="consumption_year", translation_key="consumption_year", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda data: round(data.consumption_year / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_year, ), SolarLogCoordinatorSensorEntityDescription( key="consumption_total", translation_key="consumption_total", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, state_class=SensorStateClass.TOTAL, - value_fn=lambda data: round(data.consumption_total / 1000, 3), + suggested_display_precision=3, + value_fn=lambda data: data.consumption_total, ), SolarLogCoordinatorSensorEntityDescription( key="self_consumption_year", @@ -190,6 +209,7 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] = native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, value_fn=lambda data: data.capacity, ), SolarLogCoordinatorSensorEntityDescription( @@ -198,6 +218,7 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] = native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, value_fn=lambda data: data.efficiency, ), SolarLogCoordinatorSensorEntityDescription( @@ -214,6 +235,7 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] = native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.POWER_FACTOR, state_class=SensorStateClass.MEASUREMENT, + suggested_display_precision=1, value_fn=lambda data: data.usage, ), ) @@ -230,11 +252,15 @@ INVERTER_SENSOR_TYPES: tuple[SolarLogInverterSensorEntityDescription, ...] = ( SolarLogInverterSensorEntityDescription( key="consumption_year", translation_key="consumption_year", - native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda inverter: None - if inverter.consumption_year is None - else round(inverter.consumption_year / 1000, 3), + suggested_display_precision=3, + value_fn=( + lambda inverter: None + if inverter.consumption_year is None + else inverter.consumption_year + ), ), ) diff --git a/tests/components/solarlog/snapshots/test_sensor.ambr b/tests/components/solarlog/snapshots/test_sensor.ambr index 6fccbd89dba..9f95e04a38f 100644 --- a/tests/components/solarlog/snapshots/test_sensor.ambr +++ b/tests/components/solarlog/snapshots/test_sensor.ambr @@ -71,6 +71,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -170,6 +176,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -322,6 +334,9 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), }), 'original_device_class': , 'original_icon': None, @@ -422,6 +437,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -446,7 +467,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.005', + 'state': '0.00531', }) # --- # name: test_all_entities[sensor.solarlog_consumption_month-entry] @@ -470,6 +491,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -520,6 +547,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -569,6 +602,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -617,6 +656,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -641,7 +686,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.007', + 'state': '0.00734', }) # --- # name: test_all_entities[sensor.solarlog_efficiency-entry] @@ -667,6 +712,9 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), }), 'original_device_class': , 'original_icon': None, @@ -1017,6 +1065,9 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), }), 'original_device_class': , 'original_icon': None, @@ -1168,6 +1219,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1192,7 +1249,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.004', + 'state': '0.00421', }) # --- # name: test_all_entities[sensor.solarlog_yield_month-entry] @@ -1216,6 +1273,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1266,6 +1329,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1315,6 +1384,9 @@ }), 'name': None, 'options': dict({ + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1339,7 +1411,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '1.023', + 'state': '1.0230', }) # --- # name: test_all_entities[sensor.solarlog_yield_yesterday-entry] @@ -1363,6 +1435,12 @@ }), 'name': None, 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 3, + }), + 'sensor.private': dict({ + 'suggested_unit_of_measurement': , + }), }), 'original_device_class': , 'original_icon': None, @@ -1387,6 +1465,6 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': '0.005', + 'state': '0.00521', }) # ---