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
This commit is contained in:
dontinelli 2024-09-02 19:50:09 +02:00 committed by GitHub
parent 9f558d13e6
commit 5300eddf33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 133 additions and 29 deletions

View File

@ -84,38 +84,47 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] =
SolarLogCoordinatorSensorEntityDescription( SolarLogCoordinatorSensorEntityDescription(
key="yield_day", key="yield_day",
translation_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, 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( SolarLogCoordinatorSensorEntityDescription(
key="yield_yesterday", key="yield_yesterday",
translation_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, 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( SolarLogCoordinatorSensorEntityDescription(
key="yield_month", key="yield_month",
translation_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, 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( SolarLogCoordinatorSensorEntityDescription(
key="yield_year", key="yield_year",
translation_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, device_class=SensorDeviceClass.ENERGY,
value_fn=lambda data: round(data.yield_year / 1000, 3), value_fn=lambda data: data.yield_year,
), ),
SolarLogCoordinatorSensorEntityDescription( SolarLogCoordinatorSensorEntityDescription(
key="yield_total", key="yield_total",
translation_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, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL, 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( SolarLogCoordinatorSensorEntityDescription(
key="consumption_ac", key="consumption_ac",
@ -128,38 +137,48 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] =
SolarLogCoordinatorSensorEntityDescription( SolarLogCoordinatorSensorEntityDescription(
key="consumption_day", key="consumption_day",
translation_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, 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( SolarLogCoordinatorSensorEntityDescription(
key="consumption_yesterday", key="consumption_yesterday",
translation_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, 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( SolarLogCoordinatorSensorEntityDescription(
key="consumption_month", key="consumption_month",
translation_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, 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( SolarLogCoordinatorSensorEntityDescription(
key="consumption_year", key="consumption_year",
translation_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, 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( SolarLogCoordinatorSensorEntityDescription(
key="consumption_total", key="consumption_total",
translation_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, device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL, 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( SolarLogCoordinatorSensorEntityDescription(
key="self_consumption_year", key="self_consumption_year",
@ -190,6 +209,7 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] =
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
value_fn=lambda data: data.capacity, value_fn=lambda data: data.capacity,
), ),
SolarLogCoordinatorSensorEntityDescription( SolarLogCoordinatorSensorEntityDescription(
@ -198,6 +218,7 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] =
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
value_fn=lambda data: data.efficiency, value_fn=lambda data: data.efficiency,
), ),
SolarLogCoordinatorSensorEntityDescription( SolarLogCoordinatorSensorEntityDescription(
@ -214,6 +235,7 @@ SOLARLOG_SENSOR_TYPES: tuple[SolarLogCoordinatorSensorEntityDescription, ...] =
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.POWER_FACTOR, device_class=SensorDeviceClass.POWER_FACTOR,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
suggested_display_precision=1,
value_fn=lambda data: data.usage, value_fn=lambda data: data.usage,
), ),
) )
@ -230,11 +252,15 @@ INVERTER_SENSOR_TYPES: tuple[SolarLogInverterSensorEntityDescription, ...] = (
SolarLogInverterSensorEntityDescription( SolarLogInverterSensorEntityDescription(
key="consumption_year", key="consumption_year",
translation_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, device_class=SensorDeviceClass.ENERGY,
value_fn=lambda inverter: None suggested_display_precision=3,
value_fn=(
lambda inverter: None
if inverter.consumption_year is None if inverter.consumption_year is None
else round(inverter.consumption_year / 1000, 3), else inverter.consumption_year
),
), ),
) )

View File

@ -71,6 +71,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -170,6 +176,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -322,6 +334,9 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 1,
}),
}), }),
'original_device_class': <SensorDeviceClass.POWER_FACTOR: 'power_factor'>, 'original_device_class': <SensorDeviceClass.POWER_FACTOR: 'power_factor'>,
'original_icon': None, 'original_icon': None,
@ -422,6 +437,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -446,7 +467,7 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': '0.005', 'state': '0.00531',
}) })
# --- # ---
# name: test_all_entities[sensor.solarlog_consumption_month-entry] # name: test_all_entities[sensor.solarlog_consumption_month-entry]
@ -470,6 +491,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -520,6 +547,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -569,6 +602,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -617,6 +656,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -641,7 +686,7 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': '0.007', 'state': '0.00734',
}) })
# --- # ---
# name: test_all_entities[sensor.solarlog_efficiency-entry] # name: test_all_entities[sensor.solarlog_efficiency-entry]
@ -667,6 +712,9 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 1,
}),
}), }),
'original_device_class': <SensorDeviceClass.POWER_FACTOR: 'power_factor'>, 'original_device_class': <SensorDeviceClass.POWER_FACTOR: 'power_factor'>,
'original_icon': None, 'original_icon': None,
@ -1017,6 +1065,9 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 1,
}),
}), }),
'original_device_class': <SensorDeviceClass.POWER_FACTOR: 'power_factor'>, 'original_device_class': <SensorDeviceClass.POWER_FACTOR: 'power_factor'>,
'original_icon': None, 'original_icon': None,
@ -1168,6 +1219,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -1192,7 +1249,7 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': '0.004', 'state': '0.00421',
}) })
# --- # ---
# name: test_all_entities[sensor.solarlog_yield_month-entry] # name: test_all_entities[sensor.solarlog_yield_month-entry]
@ -1216,6 +1273,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -1266,6 +1329,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -1315,6 +1384,9 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -1339,7 +1411,7 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': '1.023', 'state': '1.0230',
}) })
# --- # ---
# name: test_all_entities[sensor.solarlog_yield_yesterday-entry] # name: test_all_entities[sensor.solarlog_yield_yesterday-entry]
@ -1363,6 +1435,12 @@
}), }),
'name': None, 'name': None,
'options': dict({ 'options': dict({
'sensor': dict({
'suggested_display_precision': 3,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfEnergy.KILO_WATT_HOUR: 'kWh'>,
}),
}), }),
'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>, 'original_device_class': <SensorDeviceClass.ENERGY: 'energy'>,
'original_icon': None, 'original_icon': None,
@ -1387,6 +1465,6 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': '0.005', 'state': '0.00521',
}) })
# --- # ---