Clean up forecast_solar const file (#95356)

This commit is contained in:
Joost Lekkerkerker 2023-06-27 13:19:10 +02:00 committed by GitHub
parent d8e73b6a6b
commit f61332c9b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 123 deletions

View File

@ -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,
),
)

View File

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

View File

@ -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(