Move platform specifics out of Solaredge const (#98941)

This commit is contained in:
Joost Lekkerkerker 2023-08-24 11:27:24 +02:00 committed by GitHub
parent f44215d286
commit f395147f7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 190 additions and 196 deletions

View File

@ -2,11 +2,6 @@
from datetime import timedelta from datetime import timedelta
import logging import logging
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower
from .models import SolarEdgeSensorEntityDescription
DOMAIN = "solaredge" DOMAIN = "solaredge"
LOGGER = logging.getLogger(__package__) LOGGER = logging.getLogger(__package__)
@ -24,170 +19,3 @@ POWER_FLOW_UPDATE_DELAY = timedelta(minutes=15)
ENERGY_DETAILS_DELAY = timedelta(minutes=15) ENERGY_DETAILS_DELAY = timedelta(minutes=15)
SCAN_INTERVAL = 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,
),
]

View File

@ -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."""

View File

@ -1,12 +1,19 @@
"""Support for SolarEdge Monitoring API.""" """Support for SolarEdge Monitoring API."""
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
from typing import Any from typing import Any
from solaredge import Solaredge 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.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
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 (
@ -14,7 +21,7 @@ from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator, 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 ( from .coordinator import (
SolarEdgeDataService, SolarEdgeDataService,
SolarEdgeDetailsDataService, SolarEdgeDetailsDataService,
@ -23,7 +30,186 @@ from .coordinator import (
SolarEdgeOverviewDataService, SolarEdgeOverviewDataService,
SolarEdgePowerFlowDataService, 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( async def async_setup_entry(

View File

@ -6,8 +6,8 @@ from homeassistant.components.solaredge.const import (
DEFAULT_NAME, DEFAULT_NAME,
DOMAIN, DOMAIN,
OVERVIEW_UPDATE_DELAY, 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.const import CONF_API_KEY, CONF_NAME, STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util