Add action enums to forecast_solar (#190295)

This commit is contained in:
epenet
2026-11-04 08:03:22 +01:00
committed by GitHub
parent 96c3c2c1d7
commit 2eae4ce479
2 changed files with 57 additions and 42 deletions
@@ -1,6 +1,7 @@
"""Services for the Forecast.Solar integration."""
from datetime import datetime
from enum import StrEnum
import probatio
@@ -20,26 +21,35 @@ from homeassistant.util.json import JsonValueType
from .const import DOMAIN
from .coordinator import ForecastSolarConfigEntry, energy_by_period_start
ATTR_CONFIG_ENTRY = "config_entry"
ATTR_START = "start"
ATTR_END = "end"
ATTR_RESOLUTION = "resolution"
RESOLUTION_RAW = "raw"
RESOLUTION_HOURLY = "hourly"
SERVICE_GET_FORECAST = "get_forecast"
class ForecastSolarService(StrEnum):
"""Store keys for Forecast.Solar services."""
GET_FORECAST = "get_forecast"
class ForecastSolarServiceArgument(StrEnum):
"""Store keys for Forecast.Solar service arguments."""
CONFIG_ENTRY = "config_entry"
START = "start"
END = "end"
RESOLUTION = "resolution"
GET_FORECAST_SCHEMA = probatio.Schema(
{
probatio.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector(
{"integration": DOMAIN}
),
probatio.Optional(ATTR_START): cv.datetime,
probatio.Optional(ATTR_END): cv.datetime,
probatio.Optional(ATTR_RESOLUTION, default=RESOLUTION_RAW): probatio.In(
(RESOLUTION_RAW, RESOLUTION_HOURLY)
),
probatio.Required(
ForecastSolarServiceArgument.CONFIG_ENTRY
): ConfigEntrySelector({"integration": DOMAIN}),
probatio.Optional(ForecastSolarServiceArgument.START): cv.datetime,
probatio.Optional(ForecastSolarServiceArgument.END): cv.datetime,
probatio.Optional(
ForecastSolarServiceArgument.RESOLUTION, default=RESOLUTION_RAW
): probatio.In((RESOLUTION_RAW, RESOLUTION_HOURLY)),
}
)
@@ -87,20 +97,20 @@ def async_setup_services(hass: HomeAssistant) -> None:
``+10:00``), not UTC.
"""
entry: ForecastSolarConfigEntry = service.async_get_config_entry(
hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY]
hass, DOMAIN, call.data[ForecastSolarServiceArgument.CONFIG_ENTRY]
)
estimate = entry.runtime_data.data
wh_period = energy_by_period_start(estimate.wh_period)
watts: dict[datetime, float]
if call.data[ATTR_RESOLUTION] == RESOLUTION_HOURLY:
if call.data[ForecastSolarServiceArgument.RESOLUTION] == RESOLUTION_HOURLY:
watts, wh_period = _aggregate_hourly(dict(estimate.watts), wh_period)
else:
watts = dict(estimate.watts)
start: datetime | None = call.data.get(ATTR_START)
end: datetime | None = call.data.get(ATTR_END)
start: datetime | None = call.data.get(ForecastSolarServiceArgument.START)
end: datetime | None = call.data.get(ForecastSolarServiceArgument.END)
# Interpret naive inputs in the forecast's zone; use the async
# helper to avoid blocking tz-data I/O on first use.
@@ -137,7 +147,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
hass.services.async_register(
DOMAIN,
SERVICE_GET_FORECAST,
ForecastSolarService.GET_FORECAST,
async_get_forecast,
schema=GET_FORECAST_SCHEMA,
supports_response=SupportsResponse.ONLY,
@@ -9,13 +9,10 @@ from pytest_homeassistant.common import MockConfigEntry
from homeassistant.components.forecast_solar.const import DOMAIN
from homeassistant.components.forecast_solar.services import (
ATTR_CONFIG_ENTRY,
ATTR_END,
ATTR_RESOLUTION,
ATTR_START,
RESOLUTION_HOURLY,
RESOLUTION_RAW,
SERVICE_GET_FORECAST,
ForecastSolarService,
ForecastSolarServiceArgument,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
@@ -49,10 +46,10 @@ async def test_get_forecast_raw(
response = await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
ForecastSolarService.GET_FORECAST,
{
ATTR_CONFIG_ENTRY: init_integration.entry_id,
ATTR_RESOLUTION: RESOLUTION_RAW,
ForecastSolarServiceArgument.CONFIG_ENTRY: init_integration.entry_id,
ForecastSolarServiceArgument.RESOLUTION: RESOLUTION_RAW,
},
blocking=True,
return_response=True,
@@ -108,10 +105,10 @@ async def test_get_forecast_hourly_aggregation(
response = await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
ForecastSolarService.GET_FORECAST,
{
ATTR_CONFIG_ENTRY: init_integration.entry_id,
ATTR_RESOLUTION: RESOLUTION_HOURLY,
ForecastSolarServiceArgument.CONFIG_ENTRY: init_integration.entry_id,
ForecastSolarServiceArgument.RESOLUTION: RESOLUTION_HOURLY,
},
blocking=True,
return_response=True,
@@ -144,11 +141,15 @@ async def test_get_forecast_filters_by_time_range(
response = await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
ForecastSolarService.GET_FORECAST,
{
ATTR_CONFIG_ENTRY: init_integration.entry_id,
ATTR_START: datetime(2026, 6, 19, 9, 0, tzinfo=api_tz),
ATTR_END: datetime(2026, 6, 19, 18, 0, tzinfo=api_tz),
ForecastSolarServiceArgument.CONFIG_ENTRY: init_integration.entry_id,
ForecastSolarServiceArgument.START: datetime(
2026, 6, 19, 9, 0, tzinfo=api_tz
),
ForecastSolarServiceArgument.END: datetime(
2026, 6, 19, 18, 0, tzinfo=api_tz
),
},
blocking=True,
return_response=True,
@@ -191,12 +192,12 @@ async def test_get_forecast_accepts_naive_datetimes(
response = await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
ForecastSolarService.GET_FORECAST,
{
ATTR_CONFIG_ENTRY: init_integration.entry_id,
ForecastSolarServiceArgument.CONFIG_ENTRY: init_integration.entry_id,
# Naive datetimes, as produced by ``cv.datetime`` from the UI.
ATTR_START: datetime(2026, 6, 19, 9, 0),
ATTR_END: datetime(2026, 6, 19, 18, 0),
ForecastSolarServiceArgument.START: datetime(2026, 6, 19, 9, 0),
ForecastSolarServiceArgument.END: datetime(2026, 6, 19, 18, 0),
},
blocking=True,
return_response=True,
@@ -219,11 +220,15 @@ async def test_get_forecast_rejects_end_before_start(
with pytest.raises(ServiceValidationError):
await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
ForecastSolarService.GET_FORECAST,
{
ATTR_CONFIG_ENTRY: init_integration.entry_id,
ATTR_START: datetime(2026, 6, 19, 18, 0, tzinfo=tz),
ATTR_END: datetime(2026, 6, 19, 9, 0, tzinfo=tz),
ForecastSolarServiceArgument.CONFIG_ENTRY: init_integration.entry_id,
ForecastSolarServiceArgument.START: datetime(
2026, 6, 19, 18, 0, tzinfo=tz
),
ForecastSolarServiceArgument.END: datetime(
2026, 6, 19, 9, 0, tzinfo=tz
),
},
blocking=True,
return_response=True,