From f61332c9b42b96ec7d54c9379920615c2e732120 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Tue, 27 Jun 2023 13:19:10 +0200 Subject: [PATCH] Clean up forecast_solar const file (#95356) --- .../components/forecast_solar/const.py | 102 --------------- .../components/forecast_solar/models.py | 17 --- .../components/forecast_solar/sensor.py | 123 +++++++++++++++++- 3 files changed, 119 insertions(+), 123 deletions(-) delete mode 100644 homeassistant/components/forecast_solar/models.py diff --git a/homeassistant/components/forecast_solar/const.py b/homeassistant/components/forecast_solar/const.py index 0e47fa9701b..e566733413b 100644 --- a/homeassistant/components/forecast_solar/const.py +++ b/homeassistant/components/forecast_solar/const.py @@ -1,14 +1,8 @@ """Constants for the Forecast.Solar integration.""" from __future__ import annotations -from datetime import timedelta import logging -from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass -from homeassistant.const import UnitOfEnergy, UnitOfPower - -from .models import ForecastSolarSensorEntityDescription - DOMAIN = "forecast_solar" LOGGER = logging.getLogger(__package__) @@ -17,99 +11,3 @@ CONF_AZIMUTH = "azimuth" CONF_MODULES_POWER = "modules power" CONF_DAMPING = "damping" CONF_INVERTER_SIZE = "inverter_size" - -SENSORS: tuple[ForecastSolarSensorEntityDescription, ...] = ( - ForecastSolarSensorEntityDescription( - key="energy_production_today", - name="Estimated energy production - today", - state=lambda estimate: estimate.energy_production_today, - device_class=SensorDeviceClass.ENERGY, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - suggested_display_precision=1, - ), - ForecastSolarSensorEntityDescription( - key="energy_production_today_remaining", - name="Estimated energy production - remaining today", - state=lambda estimate: estimate.energy_production_today_remaining, - device_class=SensorDeviceClass.ENERGY, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - suggested_display_precision=1, - ), - ForecastSolarSensorEntityDescription( - key="energy_production_tomorrow", - name="Estimated energy production - tomorrow", - state=lambda estimate: estimate.energy_production_tomorrow, - device_class=SensorDeviceClass.ENERGY, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - suggested_display_precision=1, - ), - ForecastSolarSensorEntityDescription( - key="power_highest_peak_time_today", - name="Highest power peak time - today", - device_class=SensorDeviceClass.TIMESTAMP, - ), - ForecastSolarSensorEntityDescription( - key="power_highest_peak_time_tomorrow", - name="Highest power peak time - tomorrow", - device_class=SensorDeviceClass.TIMESTAMP, - ), - ForecastSolarSensorEntityDescription( - key="power_production_now", - name="Estimated power production - now", - device_class=SensorDeviceClass.POWER, - state=lambda estimate: estimate.power_production_now, - state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement=UnitOfPower.WATT, - ), - ForecastSolarSensorEntityDescription( - key="power_production_next_hour", - state=lambda estimate: estimate.power_production_at_time( - estimate.now() + timedelta(hours=1) - ), - name="Estimated power production - next hour", - device_class=SensorDeviceClass.POWER, - entity_registry_enabled_default=False, - native_unit_of_measurement=UnitOfPower.WATT, - ), - ForecastSolarSensorEntityDescription( - key="power_production_next_12hours", - state=lambda estimate: estimate.power_production_at_time( - estimate.now() + timedelta(hours=12) - ), - name="Estimated power production - next 12 hours", - device_class=SensorDeviceClass.POWER, - entity_registry_enabled_default=False, - native_unit_of_measurement=UnitOfPower.WATT, - ), - ForecastSolarSensorEntityDescription( - key="power_production_next_24hours", - state=lambda estimate: estimate.power_production_at_time( - estimate.now() + timedelta(hours=24) - ), - name="Estimated power production - next 24 hours", - device_class=SensorDeviceClass.POWER, - entity_registry_enabled_default=False, - native_unit_of_measurement=UnitOfPower.WATT, - ), - ForecastSolarSensorEntityDescription( - key="energy_current_hour", - name="Estimated energy production - this hour", - state=lambda estimate: estimate.energy_current_hour, - device_class=SensorDeviceClass.ENERGY, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - suggested_display_precision=1, - ), - ForecastSolarSensorEntityDescription( - key="energy_next_hour", - state=lambda estimate: estimate.sum_energy_production(1), - name="Estimated energy production - next hour", - device_class=SensorDeviceClass.ENERGY, - native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, - suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, - suggested_display_precision=1, - ), -) diff --git a/homeassistant/components/forecast_solar/models.py b/homeassistant/components/forecast_solar/models.py deleted file mode 100644 index af9b6125713..00000000000 --- a/homeassistant/components/forecast_solar/models.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Models for the Forecast.Solar integration.""" -from __future__ import annotations - -from collections.abc import Callable -from dataclasses import dataclass -from typing import Any - -from forecast_solar.models import Estimate - -from homeassistant.components.sensor import SensorEntityDescription - - -@dataclass -class ForecastSolarSensorEntityDescription(SensorEntityDescription): - """Describes a Forecast.Solar Sensor.""" - - state: Callable[[Estimate], Any] | None = None diff --git a/homeassistant/components/forecast_solar/sensor.py b/homeassistant/components/forecast_solar/sensor.py index 681e04f434f..2858bff098e 100644 --- a/homeassistant/components/forecast_solar/sensor.py +++ b/homeassistant/components/forecast_solar/sensor.py @@ -1,10 +1,22 @@ """Support for the Forecast.Solar sensor service.""" from __future__ import annotations -from datetime import datetime +from collections.abc import Callable +from dataclasses import dataclass +from datetime import datetime, timedelta +from typing import Any -from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorEntity +from forecast_solar.models import Estimate + +from homeassistant.components.sensor import ( + DOMAIN as SENSOR_DOMAIN, + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import UnitOfEnergy, UnitOfPower from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo @@ -12,9 +24,112 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import DOMAIN, SENSORS +from .const import DOMAIN from .coordinator import ForecastSolarDataUpdateCoordinator -from .models import ForecastSolarSensorEntityDescription + + +@dataclass +class ForecastSolarSensorEntityDescription(SensorEntityDescription): + """Describes a Forecast.Solar Sensor.""" + + state: Callable[[Estimate], Any] | None = None + + +SENSORS: tuple[ForecastSolarSensorEntityDescription, ...] = ( + ForecastSolarSensorEntityDescription( + key="energy_production_today", + name="Estimated energy production - today", + state=lambda estimate: estimate.energy_production_today, + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + suggested_display_precision=1, + ), + ForecastSolarSensorEntityDescription( + key="energy_production_today_remaining", + name="Estimated energy production - remaining today", + state=lambda estimate: estimate.energy_production_today_remaining, + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + suggested_display_precision=1, + ), + ForecastSolarSensorEntityDescription( + key="energy_production_tomorrow", + name="Estimated energy production - tomorrow", + state=lambda estimate: estimate.energy_production_tomorrow, + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + suggested_display_precision=1, + ), + ForecastSolarSensorEntityDescription( + key="power_highest_peak_time_today", + name="Highest power peak time - today", + device_class=SensorDeviceClass.TIMESTAMP, + ), + ForecastSolarSensorEntityDescription( + key="power_highest_peak_time_tomorrow", + name="Highest power peak time - tomorrow", + device_class=SensorDeviceClass.TIMESTAMP, + ), + ForecastSolarSensorEntityDescription( + key="power_production_now", + name="Estimated power production - now", + device_class=SensorDeviceClass.POWER, + state=lambda estimate: estimate.power_production_now, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfPower.WATT, + ), + ForecastSolarSensorEntityDescription( + key="power_production_next_hour", + state=lambda estimate: estimate.power_production_at_time( + estimate.now() + timedelta(hours=1) + ), + name="Estimated power production - next hour", + device_class=SensorDeviceClass.POWER, + entity_registry_enabled_default=False, + native_unit_of_measurement=UnitOfPower.WATT, + ), + ForecastSolarSensorEntityDescription( + key="power_production_next_12hours", + state=lambda estimate: estimate.power_production_at_time( + estimate.now() + timedelta(hours=12) + ), + name="Estimated power production - next 12 hours", + device_class=SensorDeviceClass.POWER, + entity_registry_enabled_default=False, + native_unit_of_measurement=UnitOfPower.WATT, + ), + ForecastSolarSensorEntityDescription( + key="power_production_next_24hours", + state=lambda estimate: estimate.power_production_at_time( + estimate.now() + timedelta(hours=24) + ), + name="Estimated power production - next 24 hours", + device_class=SensorDeviceClass.POWER, + entity_registry_enabled_default=False, + native_unit_of_measurement=UnitOfPower.WATT, + ), + ForecastSolarSensorEntityDescription( + key="energy_current_hour", + name="Estimated energy production - this hour", + state=lambda estimate: estimate.energy_current_hour, + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + suggested_display_precision=1, + ), + ForecastSolarSensorEntityDescription( + key="energy_next_hour", + state=lambda estimate: estimate.sum_energy_production(1), + name="Estimated energy production - next hour", + device_class=SensorDeviceClass.ENERGY, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + suggested_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, + suggested_display_precision=1, + ), +) async def async_setup_entry(