From 131edea576df941fea2f2c91560c13b297ec3049 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Sun, 14 Apr 2024 17:41:25 +0200 Subject: [PATCH] Replace lambda by attrgetter in enphase_envoy platform value_fn (#115569) --- .../components/enphase_envoy/binary_sensor.py | 5 +- .../components/enphase_envoy/number.py | 7 ++- .../components/enphase_envoy/sensor.py | 59 ++++++++++--------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/enphase_envoy/binary_sensor.py b/homeassistant/components/enphase_envoy/binary_sensor.py index dfa619f07d8..dbd8498467f 100644 --- a/homeassistant/components/enphase_envoy/binary_sensor.py +++ b/homeassistant/components/enphase_envoy/binary_sensor.py @@ -4,6 +4,7 @@ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass +from operator import attrgetter from pyenphase import EnvoyEncharge, EnvoyEnpower @@ -36,7 +37,7 @@ ENCHARGE_SENSORS = ( translation_key="communicating", device_class=BinarySensorDeviceClass.CONNECTIVITY, entity_category=EntityCategory.DIAGNOSTIC, - value_fn=lambda encharge: encharge.communicating, + value_fn=attrgetter("communicating"), ), EnvoyEnchargeBinarySensorEntityDescription( key="dc_switch", @@ -60,7 +61,7 @@ ENPOWER_SENSORS = ( translation_key="communicating", device_class=BinarySensorDeviceClass.CONNECTIVITY, entity_category=EntityCategory.DIAGNOSTIC, - value_fn=lambda enpower: enpower.communicating, + value_fn=attrgetter("communicating"), ), EnvoyEnpowerBinarySensorEntityDescription( key="mains_oper_state", diff --git a/homeassistant/components/enphase_envoy/number.py b/homeassistant/components/enphase_envoy/number.py index 61d9aabb469..38bb18ad768 100644 --- a/homeassistant/components/enphase_envoy/number.py +++ b/homeassistant/components/enphase_envoy/number.py @@ -4,6 +4,7 @@ from __future__ import annotations from collections.abc import Awaitable, Callable from dataclasses import dataclass +from operator import attrgetter from typing import Any from pyenphase import Envoy, EnvoyDryContactSettings @@ -47,14 +48,14 @@ RELAY_ENTITIES = ( translation_key="cutoff_battery_level", device_class=NumberDeviceClass.BATTERY, entity_category=EntityCategory.CONFIG, - value_fn=lambda relay: relay.soc_low, + value_fn=attrgetter("soc_low"), ), EnvoyRelayNumberEntityDescription( key="soc_high", translation_key="restore_battery_level", device_class=NumberDeviceClass.BATTERY, entity_category=EntityCategory.CONFIG, - value_fn=lambda relay: relay.soc_high, + value_fn=attrgetter("soc_high"), ), ) @@ -63,7 +64,7 @@ STORAGE_RESERVE_SOC_ENTITY = EnvoyStorageSettingsNumberEntityDescription( translation_key="reserve_soc", native_unit_of_measurement=PERCENTAGE, device_class=NumberDeviceClass.BATTERY, - value_fn=lambda storage_settings: storage_settings.reserved_soc, + value_fn=attrgetter("reserved_soc"), update_fn=lambda envoy, value: envoy.set_reserve_soc(int(value)), ) diff --git a/homeassistant/components/enphase_envoy/sensor.py b/homeassistant/components/enphase_envoy/sensor.py index df06502e94e..13445d8897a 100644 --- a/homeassistant/components/enphase_envoy/sensor.py +++ b/homeassistant/components/enphase_envoy/sensor.py @@ -6,6 +6,7 @@ from collections.abc import Callable from dataclasses import dataclass, replace import datetime import logging +from operator import attrgetter from typing import TYPE_CHECKING from pyenphase import ( @@ -73,7 +74,7 @@ INVERTER_SENSORS = ( native_unit_of_measurement=UnitOfPower.WATT, state_class=SensorStateClass.MEASUREMENT, device_class=SensorDeviceClass.POWER, - value_fn=lambda inverter: inverter.last_report_watts, + value_fn=attrgetter("last_report_watts"), ), EnvoyInverterSensorEntityDescription( key=LAST_REPORTED_KEY, @@ -102,7 +103,7 @@ PRODUCTION_SENSORS = ( device_class=SensorDeviceClass.POWER, suggested_unit_of_measurement=UnitOfPower.KILO_WATT, suggested_display_precision=3, - value_fn=lambda production: production.watts_now, + value_fn=attrgetter("watts_now"), on_phase=None, ), EnvoyProductionSensorEntityDescription( @@ -113,7 +114,7 @@ PRODUCTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, suggested_display_precision=2, - value_fn=lambda production: production.watt_hours_today, + value_fn=attrgetter("watt_hours_today"), on_phase=None, ), EnvoyProductionSensorEntityDescription( @@ -123,7 +124,7 @@ PRODUCTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, suggested_display_precision=1, - value_fn=lambda production: production.watt_hours_last_7_days, + value_fn=attrgetter("watt_hours_last_7_days"), on_phase=None, ), EnvoyProductionSensorEntityDescription( @@ -134,7 +135,7 @@ PRODUCTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR, suggested_display_precision=3, - value_fn=lambda production: production.watt_hours_lifetime, + value_fn=attrgetter("watt_hours_lifetime"), on_phase=None, ), ) @@ -173,7 +174,7 @@ CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.POWER, suggested_unit_of_measurement=UnitOfPower.KILO_WATT, suggested_display_precision=3, - value_fn=lambda consumption: consumption.watts_now, + value_fn=attrgetter("watts_now"), on_phase=None, ), EnvoyConsumptionSensorEntityDescription( @@ -184,7 +185,7 @@ CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, suggested_display_precision=2, - value_fn=lambda consumption: consumption.watt_hours_today, + value_fn=attrgetter("watt_hours_today"), on_phase=None, ), EnvoyConsumptionSensorEntityDescription( @@ -194,7 +195,7 @@ CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, suggested_display_precision=1, - value_fn=lambda consumption: consumption.watt_hours_last_7_days, + value_fn=attrgetter("watt_hours_last_7_days"), on_phase=None, ), EnvoyConsumptionSensorEntityDescription( @@ -205,7 +206,7 @@ CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR, suggested_display_precision=3, - value_fn=lambda consumption: consumption.watt_hours_lifetime, + value_fn=attrgetter("watt_hours_lifetime"), on_phase=None, ), ) @@ -247,7 +248,7 @@ CT_NET_CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR, suggested_display_precision=3, - value_fn=lambda ct: ct.energy_delivered, + value_fn=attrgetter("energy_delivered"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -258,7 +259,7 @@ CT_NET_CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR, suggested_display_precision=3, - value_fn=lambda ct: ct.energy_received, + value_fn=attrgetter("energy_received"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -269,7 +270,7 @@ CT_NET_CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.POWER, suggested_unit_of_measurement=UnitOfPower.KILO_WATT, suggested_display_precision=3, - value_fn=lambda ct: ct.active_power, + value_fn=attrgetter("active_power"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -280,7 +281,7 @@ CT_NET_CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.FREQUENCY, suggested_display_precision=1, entity_registry_enabled_default=False, - value_fn=lambda ct: ct.frequency, + value_fn=attrgetter("frequency"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -292,7 +293,7 @@ CT_NET_CONSUMPTION_SENSORS = ( suggested_unit_of_measurement=UnitOfElectricPotential.VOLT, suggested_display_precision=1, entity_registry_enabled_default=False, - value_fn=lambda ct: ct.voltage, + value_fn=attrgetter("voltage"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -301,7 +302,7 @@ CT_NET_CONSUMPTION_SENSORS = ( device_class=SensorDeviceClass.ENUM, options=list(CtMeterStatus), entity_registry_enabled_default=False, - value_fn=lambda ct: ct.metering_status, + value_fn=attrgetter("metering_status"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -337,7 +338,7 @@ CT_PRODUCTION_SENSORS = ( device_class=SensorDeviceClass.ENUM, options=list(CtMeterStatus), entity_registry_enabled_default=False, - value_fn=lambda ct: ct.metering_status, + value_fn=attrgetter("metering_status"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -374,7 +375,7 @@ CT_STORAGE_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR, suggested_display_precision=3, - value_fn=lambda ct: ct.energy_delivered, + value_fn=attrgetter("energy_delivered"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -385,7 +386,7 @@ CT_STORAGE_SENSORS = ( device_class=SensorDeviceClass.ENERGY, suggested_unit_of_measurement=UnitOfEnergy.MEGA_WATT_HOUR, suggested_display_precision=3, - value_fn=lambda ct: ct.energy_received, + value_fn=attrgetter("energy_received"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -396,7 +397,7 @@ CT_STORAGE_SENSORS = ( device_class=SensorDeviceClass.POWER, suggested_unit_of_measurement=UnitOfPower.KILO_WATT, suggested_display_precision=3, - value_fn=lambda ct: ct.active_power, + value_fn=attrgetter("active_power"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -408,7 +409,7 @@ CT_STORAGE_SENSORS = ( suggested_unit_of_measurement=UnitOfElectricPotential.VOLT, suggested_display_precision=1, entity_registry_enabled_default=False, - value_fn=lambda ct: ct.voltage, + value_fn=attrgetter("voltage"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -417,7 +418,7 @@ CT_STORAGE_SENSORS = ( device_class=SensorDeviceClass.ENUM, options=list(CtMeterStatus), entity_registry_enabled_default=False, - value_fn=lambda ct: ct.metering_status, + value_fn=attrgetter("metering_status"), on_phase=None, ), EnvoyCTSensorEntityDescription( @@ -471,7 +472,7 @@ ENCHARGE_INVENTORY_SENSORS = ( key="temperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, - value_fn=lambda encharge: encharge.temperature, + value_fn=attrgetter("temperature"), ), EnvoyEnchargeSensorEntityDescription( key=LAST_REPORTED_KEY, @@ -486,7 +487,7 @@ ENCHARGE_POWER_SENSORS = ( key="soc", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.BATTERY, - value_fn=lambda encharge: encharge.soc, + value_fn=attrgetter("soc"), ), EnvoyEnchargePowerSensorEntityDescription( key="apparent_power_mva", @@ -515,7 +516,7 @@ ENPOWER_SENSORS = ( key="temperature", native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT, device_class=SensorDeviceClass.TEMPERATURE, - value_fn=lambda enpower: enpower.temperature, + value_fn=attrgetter("temperature"), ), EnvoyEnpowerSensorEntityDescription( key=LAST_REPORTED_KEY, @@ -543,35 +544,35 @@ ENCHARGE_AGGREGATE_SENSORS = ( key="battery_level", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.BATTERY, - value_fn=lambda encharge: encharge.state_of_charge, + value_fn=attrgetter("state_of_charge"), ), EnvoyEnchargeAggregateSensorEntityDescription( key="reserve_soc", translation_key="reserve_soc", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.BATTERY, - value_fn=lambda encharge: encharge.reserve_state_of_charge, + value_fn=attrgetter("reserve_state_of_charge"), ), EnvoyEnchargeAggregateSensorEntityDescription( key="available_energy", translation_key="available_energy", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda encharge: encharge.available_energy, + value_fn=attrgetter("available_energy"), ), EnvoyEnchargeAggregateSensorEntityDescription( key="reserve_energy", translation_key="reserve_energy", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda encharge: encharge.backup_reserve, + value_fn=attrgetter("backup_reserve"), ), EnvoyEnchargeAggregateSensorEntityDescription( key="max_capacity", translation_key="max_capacity", native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, device_class=SensorDeviceClass.ENERGY, - value_fn=lambda encharge: encharge.max_available_capacity, + value_fn=attrgetter("max_available_capacity"), ), )