From f395147f7c8db37f1f85524241689eb3b53e1581 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 24 Aug 2023 11:27:24 +0200 Subject: [PATCH] Move platform specifics out of Solaredge const (#98941) --- homeassistant/components/solaredge/const.py | 172 ---------------- homeassistant/components/solaredge/models.py | 20 -- homeassistant/components/solaredge/sensor.py | 192 +++++++++++++++++- .../components/solaredge/test_coordinator.py | 2 +- 4 files changed, 190 insertions(+), 196 deletions(-) delete mode 100644 homeassistant/components/solaredge/models.py diff --git a/homeassistant/components/solaredge/const.py b/homeassistant/components/solaredge/const.py index 6d95f8b6aec..aa6251ff433 100644 --- a/homeassistant/components/solaredge/const.py +++ b/homeassistant/components/solaredge/const.py @@ -2,11 +2,6 @@ from datetime import timedelta import logging -from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass -from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower - -from .models import SolarEdgeSensorEntityDescription - DOMAIN = "solaredge" LOGGER = logging.getLogger(__package__) @@ -24,170 +19,3 @@ POWER_FLOW_UPDATE_DELAY = timedelta(minutes=15) ENERGY_DETAILS_DELAY = timedelta(minutes=15) SCAN_INTERVAL = timedelta(minutes=15) - - -# Supported overview sensors -SENSOR_TYPES = [ - SolarEdgeSensorEntityDescription( - key="lifetime_energy", - json_key="lifeTimeData", - name="Lifetime energy", - icon="mdi:solar-power", - state_class=SensorStateClass.TOTAL, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="energy_this_year", - json_key="lastYearData", - name="Energy this year", - entity_registry_enabled_default=False, - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="energy_this_month", - json_key="lastMonthData", - name="Energy this month", - entity_registry_enabled_default=False, - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="energy_today", - json_key="lastDayData", - name="Energy today", - entity_registry_enabled_default=False, - icon="mdi:solar-power", - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="current_power", - json_key="currentPower", - name="Current Power", - icon="mdi:solar-power", - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfPower.WATT, - device_class=SensorDeviceClass.POWER, - ), - SolarEdgeSensorEntityDescription( - key="site_details", - json_key="status", - name="Site details", - entity_registry_enabled_default=False, - ), - SolarEdgeSensorEntityDescription( - key="meters", - json_key="meters", - name="Meters", - entity_registry_enabled_default=False, - ), - SolarEdgeSensorEntityDescription( - key="sensors", - json_key="sensors", - name="Sensors", - entity_registry_enabled_default=False, - ), - SolarEdgeSensorEntityDescription( - key="gateways", - json_key="gateways", - name="Gateways", - entity_registry_enabled_default=False, - ), - SolarEdgeSensorEntityDescription( - key="batteries", - json_key="batteries", - name="Batteries", - entity_registry_enabled_default=False, - ), - SolarEdgeSensorEntityDescription( - key="inverters", - json_key="inverters", - name="Inverters", - entity_registry_enabled_default=False, - ), - SolarEdgeSensorEntityDescription( - key="power_consumption", - json_key="LOAD", - name="Power Consumption", - entity_registry_enabled_default=False, - icon="mdi:flash", - ), - SolarEdgeSensorEntityDescription( - key="solar_power", - json_key="PV", - name="Solar Power", - entity_registry_enabled_default=False, - icon="mdi:solar-power", - ), - SolarEdgeSensorEntityDescription( - key="grid_power", - json_key="GRID", - name="Grid Power", - entity_registry_enabled_default=False, - icon="mdi:power-plug", - ), - SolarEdgeSensorEntityDescription( - key="storage_power", - json_key="STORAGE", - name="Storage Power", - entity_registry_enabled_default=False, - icon="mdi:car-battery", - ), - SolarEdgeSensorEntityDescription( - key="purchased_energy", - json_key="Purchased", - name="Imported Energy", - entity_registry_enabled_default=False, - state_class=SensorStateClass.TOTAL_INCREASING, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="production_energy", - json_key="Production", - name="Production Energy", - entity_registry_enabled_default=False, - state_class=SensorStateClass.TOTAL_INCREASING, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="consumption_energy", - json_key="Consumption", - name="Consumption Energy", - entity_registry_enabled_default=False, - state_class=SensorStateClass.TOTAL_INCREASING, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="selfconsumption_energy", - json_key="SelfConsumption", - name="SelfConsumption Energy", - entity_registry_enabled_default=False, - state_class=SensorStateClass.TOTAL_INCREASING, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="feedin_energy", - json_key="FeedIn", - name="Exported Energy", - entity_registry_enabled_default=False, - state_class=SensorStateClass.TOTAL_INCREASING, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - device_class=SensorDeviceClass.ENERGY, - ), - SolarEdgeSensorEntityDescription( - key="storage_level", - json_key="STORAGE", - name="Storage Level", - entity_registry_enabled_default=False, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=PERCENTAGE, - ), -] diff --git a/homeassistant/components/solaredge/models.py b/homeassistant/components/solaredge/models.py deleted file mode 100644 index 57efb88023c..00000000000 --- a/homeassistant/components/solaredge/models.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Models for the SolarEdge integration.""" -from __future__ import annotations - -from dataclasses import dataclass - -from homeassistant.components.sensor import SensorEntityDescription - - -@dataclass -class SolarEdgeSensorEntityRequiredKeyMixin: - """Sensor entity description with json_key for SolarEdge.""" - - 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 3a4b5ad90c2..e1ea7960086 100644 --- a/homeassistant/components/solaredge/sensor.py +++ b/homeassistant/components/solaredge/sensor.py @@ -1,12 +1,19 @@ """Support for SolarEdge Monitoring API.""" from __future__ import annotations +from dataclasses import dataclass from typing import Any from solaredge import Solaredge -from homeassistant.components.sensor import SensorDeviceClass, SensorEntity +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import ( @@ -14,7 +21,7 @@ from homeassistant.helpers.update_coordinator import ( DataUpdateCoordinator, ) -from .const import CONF_SITE_ID, DATA_API_CLIENT, DOMAIN, SENSOR_TYPES +from .const import CONF_SITE_ID, DATA_API_CLIENT, DOMAIN from .coordinator import ( SolarEdgeDataService, SolarEdgeDetailsDataService, @@ -23,7 +30,186 @@ from .coordinator import ( SolarEdgeOverviewDataService, SolarEdgePowerFlowDataService, ) -from .models import SolarEdgeSensorEntityDescription + + +@dataclass +class SolarEdgeSensorEntityRequiredKeyMixin: + """Sensor entity description with json_key for SolarEdge.""" + + json_key: str + + +@dataclass +class SolarEdgeSensorEntityDescription( + SensorEntityDescription, SolarEdgeSensorEntityRequiredKeyMixin +): + """Sensor entity description for SolarEdge.""" + + +SENSOR_TYPES = [ + SolarEdgeSensorEntityDescription( + key="lifetime_energy", + json_key="lifeTimeData", + name="Lifetime energy", + icon="mdi:solar-power", + state_class=SensorStateClass.TOTAL, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="energy_this_year", + json_key="lastYearData", + name="Energy this year", + entity_registry_enabled_default=False, + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="energy_this_month", + json_key="lastMonthData", + name="Energy this month", + entity_registry_enabled_default=False, + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="energy_today", + json_key="lastDayData", + name="Energy today", + entity_registry_enabled_default=False, + icon="mdi:solar-power", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="current_power", + json_key="currentPower", + name="Current Power", + icon="mdi:solar-power", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + ), + SolarEdgeSensorEntityDescription( + key="site_details", + json_key="status", + name="Site details", + entity_registry_enabled_default=False, + ), + SolarEdgeSensorEntityDescription( + key="meters", + json_key="meters", + name="Meters", + entity_registry_enabled_default=False, + ), + SolarEdgeSensorEntityDescription( + key="sensors", + json_key="sensors", + name="Sensors", + entity_registry_enabled_default=False, + ), + SolarEdgeSensorEntityDescription( + key="gateways", + json_key="gateways", + name="Gateways", + entity_registry_enabled_default=False, + ), + SolarEdgeSensorEntityDescription( + key="batteries", + json_key="batteries", + name="Batteries", + entity_registry_enabled_default=False, + ), + SolarEdgeSensorEntityDescription( + key="inverters", + json_key="inverters", + name="Inverters", + entity_registry_enabled_default=False, + ), + SolarEdgeSensorEntityDescription( + key="power_consumption", + json_key="LOAD", + name="Power Consumption", + entity_registry_enabled_default=False, + icon="mdi:flash", + ), + SolarEdgeSensorEntityDescription( + key="solar_power", + json_key="PV", + name="Solar Power", + entity_registry_enabled_default=False, + icon="mdi:solar-power", + ), + SolarEdgeSensorEntityDescription( + key="grid_power", + json_key="GRID", + name="Grid Power", + entity_registry_enabled_default=False, + icon="mdi:power-plug", + ), + SolarEdgeSensorEntityDescription( + key="storage_power", + json_key="STORAGE", + name="Storage Power", + entity_registry_enabled_default=False, + icon="mdi:car-battery", + ), + SolarEdgeSensorEntityDescription( + key="purchased_energy", + json_key="Purchased", + name="Imported Energy", + entity_registry_enabled_default=False, + state_class=SensorStateClass.TOTAL_INCREASING, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="production_energy", + json_key="Production", + name="Production Energy", + entity_registry_enabled_default=False, + state_class=SensorStateClass.TOTAL_INCREASING, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="consumption_energy", + json_key="Consumption", + name="Consumption Energy", + entity_registry_enabled_default=False, + state_class=SensorStateClass.TOTAL_INCREASING, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="selfconsumption_energy", + json_key="SelfConsumption", + name="SelfConsumption Energy", + entity_registry_enabled_default=False, + state_class=SensorStateClass.TOTAL_INCREASING, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="feedin_energy", + json_key="FeedIn", + name="Exported Energy", + entity_registry_enabled_default=False, + state_class=SensorStateClass.TOTAL_INCREASING, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + ), + SolarEdgeSensorEntityDescription( + key="storage_level", + json_key="STORAGE", + name="Storage Level", + entity_registry_enabled_default=False, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=PERCENTAGE, + ), +] async def async_setup_entry( diff --git a/tests/components/solaredge/test_coordinator.py b/tests/components/solaredge/test_coordinator.py index 5d9656b05d8..7b746a2ae05 100644 --- a/tests/components/solaredge/test_coordinator.py +++ b/tests/components/solaredge/test_coordinator.py @@ -6,8 +6,8 @@ from homeassistant.components.solaredge.const import ( DEFAULT_NAME, DOMAIN, OVERVIEW_UPDATE_DELAY, - SENSOR_TYPES, ) +from homeassistant.components.solaredge.sensor import SENSOR_TYPES from homeassistant.const import CONF_API_KEY, CONF_NAME, STATE_UNKNOWN from homeassistant.core import HomeAssistant import homeassistant.util.dt as dt_util