mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Migrate SolarEdge to has entity name (#98944)
This commit is contained in:
parent
409afddeb5
commit
0b5218ec96
@ -15,6 +15,7 @@ from homeassistant.components.sensor import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower
|
from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
@ -221,9 +222,7 @@ async def async_setup_entry(
|
|||||||
# Add the needed sensors to hass
|
# Add the needed sensors to hass
|
||||||
api: Solaredge = hass.data[DOMAIN][entry.entry_id][DATA_API_CLIENT]
|
api: Solaredge = hass.data[DOMAIN][entry.entry_id][DATA_API_CLIENT]
|
||||||
|
|
||||||
sensor_factory = SolarEdgeSensorFactory(
|
sensor_factory = SolarEdgeSensorFactory(hass, entry.data[CONF_SITE_ID], api)
|
||||||
hass, entry.title, entry.data[CONF_SITE_ID], api
|
|
||||||
)
|
|
||||||
for service in sensor_factory.all_services:
|
for service in sensor_factory.all_services:
|
||||||
service.async_setup()
|
service.async_setup()
|
||||||
await service.coordinator.async_refresh()
|
await service.coordinator.async_refresh()
|
||||||
@ -239,11 +238,8 @@ async def async_setup_entry(
|
|||||||
class SolarEdgeSensorFactory:
|
class SolarEdgeSensorFactory:
|
||||||
"""Factory which creates sensors based on the sensor_key."""
|
"""Factory which creates sensors based on the sensor_key."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, hass: HomeAssistant, site_id: str, api: Solaredge) -> None:
|
||||||
self, hass: HomeAssistant, platform_name: str, site_id: str, api: Solaredge
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the factory."""
|
"""Initialize the factory."""
|
||||||
self.platform_name = platform_name
|
|
||||||
|
|
||||||
details = SolarEdgeDetailsDataService(hass, api, site_id)
|
details = SolarEdgeDetailsDataService(hass, api, site_id)
|
||||||
overview = SolarEdgeOverviewDataService(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."""
|
"""Create and return a sensor based on the sensor_key."""
|
||||||
sensor_class, service = self.services[sensor_type.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(
|
class SolarEdgeSensorEntity(
|
||||||
@ -302,21 +298,22 @@ class SolarEdgeSensorEntity(
|
|||||||
):
|
):
|
||||||
"""Abstract class for a solaredge sensor."""
|
"""Abstract class for a solaredge sensor."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
entity_description: SolarEdgeSensorEntityDescription
|
entity_description: SolarEdgeSensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
platform_name: str,
|
|
||||||
description: SolarEdgeSensorEntityDescription,
|
description: SolarEdgeSensorEntityDescription,
|
||||||
data_service: SolarEdgeDataService,
|
data_service: SolarEdgeDataService,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(data_service.coordinator)
|
super().__init__(data_service.coordinator)
|
||||||
self.platform_name = platform_name
|
|
||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self.data_service = data_service
|
self.data_service = data_service
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
self._attr_name = f"{platform_name} ({description.name})"
|
identifiers={(DOMAIN, data_service.site_id)}, manufacturer="SolarEdge"
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str | None:
|
def unique_id(self) -> str | None:
|
||||||
@ -375,12 +372,11 @@ class SolarEdgeEnergyDetailsSensor(SolarEdgeSensorEntity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
platform_name: str,
|
|
||||||
sensor_type: SolarEdgeSensorEntityDescription,
|
sensor_type: SolarEdgeSensorEntityDescription,
|
||||||
data_service: SolarEdgeEnergyDetailsService,
|
data_service: SolarEdgeEnergyDetailsService,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the power flow sensor."""
|
"""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
|
self._attr_native_unit_of_measurement = data_service.unit
|
||||||
|
|
||||||
@ -402,12 +398,11 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensorEntity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
platform_name: str,
|
|
||||||
description: SolarEdgeSensorEntityDescription,
|
description: SolarEdgeSensorEntityDescription,
|
||||||
data_service: SolarEdgePowerFlowDataService,
|
data_service: SolarEdgePowerFlowDataService,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the power flow sensor."""
|
"""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
|
self._attr_native_unit_of_measurement = data_service.unit
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user