Migrate SolarEdge to has entity name (#98944)

This commit is contained in:
Joost Lekkerkerker 2023-10-22 19:30:05 +02:00 committed by GitHub
parent 409afddeb5
commit 0b5218ec96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,7 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
@ -221,9 +222,7 @@ async def async_setup_entry(
# Add the needed sensors to hass
api: Solaredge = hass.data[DOMAIN][entry.entry_id][DATA_API_CLIENT]
sensor_factory = SolarEdgeSensorFactory(
hass, entry.title, entry.data[CONF_SITE_ID], api
)
sensor_factory = SolarEdgeSensorFactory(hass, entry.data[CONF_SITE_ID], api)
for service in sensor_factory.all_services:
service.async_setup()
await service.coordinator.async_refresh()
@ -239,11 +238,8 @@ async def async_setup_entry(
class SolarEdgeSensorFactory:
"""Factory which creates sensors based on the sensor_key."""
def __init__(
self, hass: HomeAssistant, platform_name: str, site_id: str, api: Solaredge
) -> None:
def __init__(self, hass: HomeAssistant, site_id: str, api: Solaredge) -> None:
"""Initialize the factory."""
self.platform_name = platform_name
details = SolarEdgeDetailsDataService(hass, api, site_id)
overview = SolarEdgeOverviewDataService(hass, api, site_id)
@ -294,7 +290,7 @@ class SolarEdgeSensorFactory:
"""Create and return a sensor based on the sensor_key."""
sensor_class, service = self.services[sensor_type.key]
return sensor_class(self.platform_name, sensor_type, service)
return sensor_class(sensor_type, service)
class SolarEdgeSensorEntity(
@ -302,21 +298,22 @@ class SolarEdgeSensorEntity(
):
"""Abstract class for a solaredge sensor."""
_attr_has_entity_name = True
entity_description: SolarEdgeSensorEntityDescription
def __init__(
self,
platform_name: str,
description: SolarEdgeSensorEntityDescription,
data_service: SolarEdgeDataService,
) -> None:
"""Initialize the sensor."""
super().__init__(data_service.coordinator)
self.platform_name = platform_name
self.entity_description = description
self.data_service = data_service
self._attr_name = f"{platform_name} ({description.name})"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, data_service.site_id)}, manufacturer="SolarEdge"
)
@property
def unique_id(self) -> str | None:
@ -375,12 +372,11 @@ class SolarEdgeEnergyDetailsSensor(SolarEdgeSensorEntity):
def __init__(
self,
platform_name: str,
sensor_type: SolarEdgeSensorEntityDescription,
data_service: SolarEdgeEnergyDetailsService,
) -> None:
"""Initialize the power flow sensor."""
super().__init__(platform_name, sensor_type, data_service)
super().__init__(sensor_type, data_service)
self._attr_native_unit_of_measurement = data_service.unit
@ -402,12 +398,11 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensorEntity):
def __init__(
self,
platform_name: str,
description: SolarEdgeSensorEntityDescription,
data_service: SolarEdgePowerFlowDataService,
) -> None:
"""Initialize the power flow sensor."""
super().__init__(platform_name, description, data_service)
super().__init__(description, data_service)
self._attr_native_unit_of_measurement = data_service.unit