Deprecate weather.get_forecast (#102534)

* Deprecate weather.get_forecast

* Rename forecast to get_forecasts

* raise issue for use of deprecated service

* Add fix_flow

* Add service translation/yaml
This commit is contained in:
Kevin Stillhammer 2023-11-19 20:44:02 +01:00 committed by GitHub
parent 41224f1674
commit 173f4760bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 18726 additions and 76 deletions

View file

@ -32,8 +32,9 @@ from homeassistant.components.weather import (
ATTR_WEATHER_WIND_SPEED,
ATTR_WEATHER_WIND_SPEED_UNIT,
DOMAIN,
LEGACY_SERVICE_GET_FORECAST,
ROUNDING_PRECISION,
SERVICE_GET_FORECAST,
SERVICE_GET_FORECASTS,
Forecast,
WeatherEntity,
WeatherEntityFeature,
@ -959,6 +960,13 @@ async def test_forecast_twice_daily_missing_is_daytime(
assert msg["type"] == "result"
@pytest.mark.parametrize(
("service"),
[
SERVICE_GET_FORECASTS,
LEGACY_SERVICE_GET_FORECAST,
],
)
@pytest.mark.parametrize(
("forecast_type", "supported_features"),
[
@ -976,6 +984,7 @@ async def test_get_forecast(
forecast_type: str,
supported_features: int,
snapshot: SnapshotAssertion,
service: str,
) -> None:
"""Test get forecast service."""
@ -1006,7 +1015,7 @@ async def test_get_forecast(
response = await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
service,
{
"entity_id": entity0.entity_id,
"type": forecast_type,
@ -1017,9 +1026,30 @@ async def test_get_forecast(
assert response == snapshot
@pytest.mark.parametrize(
("service", "expected"),
[
(
SERVICE_GET_FORECASTS,
{
"weather.testing": {
"forecast": [],
}
},
),
(
LEGACY_SERVICE_GET_FORECAST,
{
"forecast": [],
},
),
],
)
async def test_get_forecast_no_forecast(
hass: HomeAssistant,
config_flow_fixture: None,
service: str,
expected: dict[str, list | dict[str, list]],
) -> None:
"""Test get forecast service."""
@ -1040,7 +1070,7 @@ async def test_get_forecast_no_forecast(
response = await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
service,
{
"entity_id": entity0.entity_id,
"type": "daily",
@ -1048,11 +1078,16 @@ async def test_get_forecast_no_forecast(
blocking=True,
return_response=True,
)
assert response == {
"forecast": [],
}
assert response == expected
@pytest.mark.parametrize(
("service"),
[
SERVICE_GET_FORECASTS,
LEGACY_SERVICE_GET_FORECAST,
],
)
@pytest.mark.parametrize(
("supported_features", "forecast_types"),
[
@ -1066,6 +1101,7 @@ async def test_get_forecast_unsupported(
config_flow_fixture: None,
forecast_types: list[str],
supported_features: int,
service: str,
) -> None:
"""Test get forecast service."""
@ -1095,7 +1131,7 @@ async def test_get_forecast_unsupported(
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
DOMAIN,
SERVICE_GET_FORECAST,
service,
{
"entity_id": weather_entity.entity_id,
"type": forecast_type,
@ -1255,3 +1291,52 @@ async def test_issue_forecast_deprecated_no_logging(
"custom_components.test_weather.weather::weather.test is using a forecast attribute on an instance of WeatherEntity"
not in caplog.text
)
async def test_issue_deprecated_service_weather_get_forecast(
hass: HomeAssistant,
issue_registry: ir.IssueRegistry,
config_flow_fixture: None,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the issue is raised on deprecated service weather.get_forecast."""
class MockWeatherMock(MockWeatherTest):
"""Mock weather class."""
async def async_forecast_daily(self) -> list[Forecast] | None:
"""Return the forecast_daily."""
return self.forecast_list
kwargs = {
"native_temperature": 38,
"native_temperature_unit": UnitOfTemperature.CELSIUS,
"supported_features": WeatherEntityFeature.FORECAST_DAILY,
}
entity0 = await create_entity(hass, MockWeatherMock, None, **kwargs)
_ = await hass.services.async_call(
DOMAIN,
LEGACY_SERVICE_GET_FORECAST,
{
"entity_id": entity0.entity_id,
"type": "daily",
},
blocking=True,
return_response=True,
)
issue = issue_registry.async_get_issue(
"weather", "deprecated_service_weather_get_forecast"
)
assert issue
assert issue.issue_domain == "test"
assert issue.issue_id == "deprecated_service_weather_get_forecast"
assert issue.translation_key == "deprecated_service_weather_get_forecast"
assert (
"Detected use of service 'weather.get_forecast'. "
"This is deprecated and will stop working in Home Assistant 2024.6. "
"Use 'weather.get_forecasts' instead which supports multiple entities"
) in caplog.text