diff --git a/homeassistant/components/solaredge/config_flow.py b/homeassistant/components/solaredge/config_flow.py index 58e5577ccfa..8bdf6a4b4aa 100644 --- a/homeassistant/components/solaredge/config_flow.py +++ b/homeassistant/components/solaredge/config_flow.py @@ -23,7 +23,7 @@ class SolarEdgeConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Initialize the config flow.""" - self._errors = {} + self._errors: dict[str, str] = {} @callback def _async_current_site_ids(self) -> set[str]: diff --git a/homeassistant/components/solaredge/const.py b/homeassistant/components/solaredge/const.py index 916a01de9e9..fd989fc8a2a 100644 --- a/homeassistant/components/solaredge/const.py +++ b/homeassistant/components/solaredge/const.py @@ -75,6 +75,7 @@ SENSOR_TYPES = [ ), SolarEdgeSensorEntityDescription( key="site_details", + json_key="status", name="Site details", entity_registry_enabled_default=False, ), diff --git a/homeassistant/components/solaredge/coordinator.py b/homeassistant/components/solaredge/coordinator.py index fe8f2f86a8e..7d3e949fc10 100644 --- a/homeassistant/components/solaredge/coordinator.py +++ b/homeassistant/components/solaredge/coordinator.py @@ -3,6 +3,7 @@ from __future__ import annotations from abc import abstractmethod from datetime import date, datetime, timedelta +from typing import Any from solaredge import Solaredge from stringcase import snakecase @@ -23,16 +24,17 @@ from .const import ( class SolarEdgeDataService: """Get and update the latest data.""" + coordinator: DataUpdateCoordinator + def __init__(self, hass: HomeAssistant, api: Solaredge, site_id: str) -> None: """Initialize the data object.""" self.api = api self.site_id = site_id - self.data = {} - self.attributes = {} + self.data: dict[str, Any] = {} + self.attributes: dict[str, Any] = {} self.hass = hass - self.coordinator = None @callback def async_setup(self) -> None: @@ -105,12 +107,6 @@ class SolarEdgeOverviewDataService(SolarEdgeDataService): class SolarEdgeDetailsDataService(SolarEdgeDataService): """Get and update the latest details data.""" - def __init__(self, hass: HomeAssistant, api: Solaredge, site_id: str) -> None: - """Initialize the details data service.""" - super().__init__(hass, api, site_id) - - self.data = None - @property def update_interval(self) -> timedelta: """Update interval.""" @@ -125,7 +121,7 @@ class SolarEdgeDetailsDataService(SolarEdgeDataService): except KeyError as ex: raise UpdateFailed("Missing details data, skipping update") from ex - self.data = None + self.data = {} self.attributes = {} for key, value in details.items(): @@ -143,9 +139,13 @@ class SolarEdgeDetailsDataService(SolarEdgeDataService): ]: self.attributes[key] = value elif key == "status": - self.data = value + self.data["status"] = value - LOGGER.debug("Updated SolarEdge details: %s, %s", self.data, self.attributes) + LOGGER.debug( + "Updated SolarEdge details: %s, %s", + self.data.get("status"), + self.attributes, + ) class SolarEdgeInventoryDataService(SolarEdgeDataService): diff --git a/homeassistant/components/solaredge/models.py b/homeassistant/components/solaredge/models.py index ce24d854aac..57efb88023c 100644 --- a/homeassistant/components/solaredge/models.py +++ b/homeassistant/components/solaredge/models.py @@ -7,7 +7,14 @@ from homeassistant.components.sensor import SensorEntityDescription @dataclass -class SolarEdgeSensorEntityDescription(SensorEntityDescription): - """Sensor entity description for SolarEdge.""" +class SolarEdgeSensorEntityRequiredKeyMixin: + """Sensor entity description with json_key for SolarEdge.""" - json_key: str | None = None + json_key: str + + +@dataclass +class SolarEdgeSensorEntityDescription( + SensorEntityDescription, SolarEdgeSensorEntityRequiredKeyMixin +): + """Sensor entity description for SolarEdge.""" diff --git a/homeassistant/components/solaredge/sensor.py b/homeassistant/components/solaredge/sensor.py index a769a043442..a27c180e0b9 100644 --- a/homeassistant/components/solaredge/sensor.py +++ b/homeassistant/components/solaredge/sensor.py @@ -101,7 +101,7 @@ class SolarEdgeSensorFactory: def create_sensor( self, sensor_type: SolarEdgeSensorEntityDescription - ) -> SolarEdgeSensorEntityDescription: + ) -> SolarEdgeSensorEntity: """Create and return a sensor based on the sensor_key.""" sensor_class, service = self.services[sensor_type.key] @@ -155,7 +155,7 @@ class SolarEdgeDetailsSensor(SolarEdgeSensorEntity): @property def native_value(self) -> str | None: """Return the state of the sensor.""" - return self.data_service.data + return self.data_service.data.get(self.entity_description.json_key) @property def unique_id(self) -> str | None: @@ -169,7 +169,7 @@ class SolarEdgeInventorySensor(SolarEdgeSensorEntity): """Representation of an SolarEdge Monitoring API inventory sensor.""" @property - def extra_state_attributes(self) -> dict[str, Any]: + def extra_state_attributes(self) -> dict[str, Any] | None: """Return the state attributes.""" return self.data_service.attributes.get(self.entity_description.json_key) @@ -182,14 +182,19 @@ class SolarEdgeInventorySensor(SolarEdgeSensorEntity): class SolarEdgeEnergyDetailsSensor(SolarEdgeSensorEntity): """Representation of an SolarEdge Monitoring API power flow sensor.""" - def __init__(self, platform_name, sensor_type, data_service): + 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) self._attr_native_unit_of_measurement = data_service.unit @property - def extra_state_attributes(self) -> dict[str, Any]: + def extra_state_attributes(self) -> dict[str, Any] | None: """Return the state attributes.""" return self.data_service.attributes.get(self.entity_description.json_key) @@ -208,7 +213,7 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensorEntity): self, platform_name: str, description: SolarEdgeSensorEntityDescription, - data_service: SolarEdgeDataService, + data_service: SolarEdgePowerFlowDataService, ) -> None: """Initialize the power flow sensor.""" super().__init__(platform_name, description, data_service) @@ -216,7 +221,7 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensorEntity): self._attr_native_unit_of_measurement = data_service.unit @property - def extra_state_attributes(self) -> dict[str, Any]: + def extra_state_attributes(self) -> dict[str, Any] | None: """Return the state attributes.""" return self.data_service.attributes.get(self.entity_description.json_key) diff --git a/mypy.ini b/mypy.ini index 04c5b6f05e2..4975e893a38 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2750,15 +2750,6 @@ ignore_errors = true [mypy-homeassistant.components.profiler] ignore_errors = true -[mypy-homeassistant.components.solaredge.config_flow] -ignore_errors = true - -[mypy-homeassistant.components.solaredge.coordinator] -ignore_errors = true - -[mypy-homeassistant.components.solaredge.sensor] -ignore_errors = true - [mypy-homeassistant.components.sonos] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 9984eeeb405..d9a63aeb11f 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -51,9 +51,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.onvif.sensor", "homeassistant.components.plex.media_player", "homeassistant.components.profiler", - "homeassistant.components.solaredge.config_flow", - "homeassistant.components.solaredge.coordinator", - "homeassistant.components.solaredge.sensor", "homeassistant.components.sonos", "homeassistant.components.sonos.alarms", "homeassistant.components.sonos.binary_sensor",