From 2a80a63ac23364239c458ca0a187b6e929910bed Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 7 Aug 2023 22:10:36 -1000 Subject: [PATCH] Cleanup enphase_envoy sensor inheritance (#98012) --- .../components/enphase_envoy/sensor.py | 104 +++++++++--------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/homeassistant/components/enphase_envoy/sensor.py b/homeassistant/components/enphase_envoy/sensor.py index 6bbd8dc89d3..37063f5e53f 100644 --- a/homeassistant/components/enphase_envoy/sensor.py +++ b/homeassistant/components/enphase_envoy/sensor.py @@ -7,6 +7,7 @@ import datetime import logging from pyenphase import ( + EnvoyData, EnvoyEncharge, EnvoyEnchargePower, EnvoyInverter, @@ -303,7 +304,30 @@ async def async_setup_entry( async_add_entities(entities) -class EnvoyEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity): +class EnvoyBaseEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity): + """Defines a base envoy entity.""" + + def __init__( + self, + coordinator: EnphaseUpdateCoordinator, + description: SensorEntityDescription, + ) -> None: + """Init the envoy base entity.""" + self.entity_description = description + serial_number = coordinator.envoy.serial_number + assert serial_number is not None + self.envoy_serial_num = serial_number + super().__init__(coordinator) + + @property + def data(self) -> EnvoyData: + """Return envoy data.""" + data = self.coordinator.envoy.data + assert data is not None + return data + + +class EnvoyEntity(EnvoyBaseEntity, SensorEntity): """Envoy inverter entity.""" _attr_icon = ICON @@ -315,19 +339,15 @@ class EnvoyEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity): description: SensorEntityDescription, ) -> None: """Initialize Envoy entity.""" - self.entity_description = description - envoy_name = coordinator.name - envoy_serial_num = coordinator.envoy.serial_number - assert envoy_serial_num is not None - self._attr_unique_id = f"{envoy_serial_num}_{description.key}" + super().__init__(coordinator, description) + self._attr_unique_id = f"{self.envoy_serial_num}_{description.key}" self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, envoy_serial_num)}, + identifiers={(DOMAIN, self.envoy_serial_num)}, manufacturer="Enphase", model=coordinator.envoy.part_number or "Envoy", - name=envoy_name, + name=coordinator.name, sw_version=str(coordinator.envoy.firmware), ) - super().__init__(coordinator) class EnvoyProductionEntity(EnvoyEntity): @@ -338,10 +358,9 @@ class EnvoyProductionEntity(EnvoyEntity): @property def native_value(self) -> int | None: """Return the state of the sensor.""" - envoy = self.coordinator.envoy - assert envoy.data is not None - assert envoy.data.system_production is not None - return self.entity_description.value_fn(envoy.data.system_production) + system_production = self.data.system_production + assert system_production is not None + return self.entity_description.value_fn(system_production) class EnvoyConsumptionEntity(EnvoyEntity): @@ -352,13 +371,12 @@ class EnvoyConsumptionEntity(EnvoyEntity): @property def native_value(self) -> int | None: """Return the state of the sensor.""" - envoy = self.coordinator.envoy - assert envoy.data is not None - assert envoy.data.system_consumption is not None - return self.entity_description.value_fn(envoy.data.system_consumption) + system_consumption = self.data.system_consumption + assert system_consumption is not None + return self.entity_description.value_fn(system_consumption) -class EnvoyInverterEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity): +class EnvoyInverterEntity(EnvoyBaseEntity, SensorEntity): """Envoy inverter entity.""" _attr_icon = ICON @@ -372,10 +390,9 @@ class EnvoyInverterEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEnt serial_number: str, ) -> None: """Initialize Envoy inverter entity.""" - self.entity_description = description + super().__init__(coordinator, description) self._serial_number = serial_number key = description.key - if key == INVERTERS_KEY: # Originally there was only one inverter sensor, so we don't want to # break existing installations by changing the unique_id. @@ -384,32 +401,25 @@ class EnvoyInverterEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEnt # Additional sensors have a unique_id that includes the # sensor key. self._attr_unique_id = f"{serial_number}_{key}" - - envoy_serial_num = coordinator.envoy.serial_number - assert envoy_serial_num is not None self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, serial_number)}, name=f"Inverter {serial_number}", manufacturer="Enphase", model="Inverter", - via_device=(DOMAIN, envoy_serial_num), + via_device=(DOMAIN, self.envoy_serial_num), ) - super().__init__(coordinator) @property def native_value(self) -> datetime.datetime | float: """Return the state of the sensor.""" - envoy = self.coordinator.envoy - assert envoy.data is not None - assert envoy.data.inverters is not None - inverter = envoy.data.inverters[self._serial_number] - return self.entity_description.value_fn(inverter) + inverters = self.data.inverters + assert inverters is not None + return self.entity_description.value_fn(inverters[self._serial_number]) -class EnvoyEnchargeEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEntity): +class EnvoyEnchargeEntity(EnvoyBaseEntity, SensorEntity): """Envoy Encharge sensor entity.""" - entity_description: EnvoyEnchargeSensorEntityDescription | EnvoyEnchargePowerSensorEntityDescription _attr_has_entity_name = True def __init__( @@ -420,23 +430,19 @@ class EnvoyEnchargeEntity(CoordinatorEntity[EnphaseUpdateCoordinator], SensorEnt serial_number: str, ) -> None: """Initialize Encharge entity.""" - self.entity_description = description + super().__init__(coordinator, description) self._serial_number = serial_number - envoy_serial_num = coordinator.envoy.serial_number - assert envoy_serial_num is not None self._attr_unique_id = f"{serial_number}_{description.key}" - assert coordinator.envoy.data is not None - assert coordinator.envoy.data.encharge_inventory is not None - encharge = coordinator.envoy.data.encharge_inventory[self._serial_number] + encharge_inventory = self.data.encharge_inventory + assert encharge_inventory is not None self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, serial_number)}, manufacturer="Enphase", model="Encharge", name=f"Encharge {serial_number}", - sw_version=str(encharge.firmware_version), - via_device=(DOMAIN, envoy_serial_num), + sw_version=str(encharge_inventory[self._serial_number].firmware_version), + via_device=(DOMAIN, self.envoy_serial_num), ) - super().__init__(coordinator) class EnvoyEnchargeInventoryEntity(EnvoyEnchargeEntity): @@ -447,11 +453,9 @@ class EnvoyEnchargeInventoryEntity(EnvoyEnchargeEntity): @property def native_value(self) -> int | float | datetime.datetime | None: """Return the state of the inventory sensors.""" - envoy = self.coordinator.envoy - assert envoy.data is not None - assert envoy.data.encharge_inventory is not None - encharge = envoy.data.encharge_inventory[self._serial_number] - return self.entity_description.value_fn(encharge) + encharge_inventory = self.data.encharge_inventory + assert encharge_inventory is not None + return self.entity_description.value_fn(encharge_inventory[self._serial_number]) class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity): @@ -462,8 +466,6 @@ class EnvoyEnchargePowerEntity(EnvoyEnchargeEntity): @property def native_value(self) -> int | float | None: """Return the state of the power sensors.""" - envoy = self.coordinator.envoy - assert envoy.data is not None - assert envoy.data.encharge_power is not None - encharge = envoy.data.encharge_power[self._serial_number] - return self.entity_description.value_fn(encharge) + encharge_power = self.data.encharge_power + assert encharge_power is not None + return self.entity_description.value_fn(encharge_power[self._serial_number])