Remove deprecated zamg YAML config (#86113)

This commit is contained in:
Daniel Gangl 2023-01-17 21:58:20 +01:00 committed by GitHub
parent 1c2510bfb3
commit cc74fcbda7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 268 deletions

View file

@ -5,13 +5,11 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from zamg import ZamgData from zamg import ZamgData
from zamg.exceptions import ZamgApiError, ZamgNoDataError, ZamgStationNotFoundError from zamg.exceptions import ZamgApiError, ZamgNoDataError
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from .const import CONF_STATION_ID, DOMAIN, LOGGER from .const import CONF_STATION_ID, DOMAIN, LOGGER
@ -73,56 +71,3 @@ class ZamgConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
title=self._client.get_station_name, title=self._client.get_station_name,
data={CONF_STATION_ID: station_id}, data={CONF_STATION_ID: station_id},
) )
async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
"""Handle ZAMG configuration import."""
station_id = config.get(CONF_STATION_ID)
# create issue every time after restart
# parameter is_persistent seems not working
async_create_issue(
self.hass,
DOMAIN,
"deprecated_yaml",
breaks_in_ha_version="2023.1.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
)
if self._client is None:
self._client = ZamgData()
self._client.session = async_get_clientsession(self.hass)
try:
if station_id not in await self._client.zamg_stations():
LOGGER.warning(
(
"Configured station_id %s could not be found at zamg, trying to"
" add nearest weather station instead"
),
station_id,
)
latitude = config.get(CONF_LATITUDE) or self.hass.config.latitude
longitude = config.get(CONF_LONGITUDE) or self.hass.config.longitude
station_id = await self._client.closest_station(latitude, longitude)
# Check if already configured
await self.async_set_unique_id(station_id)
self._abort_if_unique_id_configured()
LOGGER.debug(
"importing zamg station from configuration.yaml: station_id = %s",
station_id,
)
except (ZamgApiError) as err:
LOGGER.error("Config_flow import: Received error from ZAMG: %s", err)
return self.async_abort(reason="cannot_connect")
except (ZamgStationNotFoundError) as err:
LOGGER.error("Config_flow import: Received error from ZAMG: %s", err)
return self.async_abort(reason="station_not_found")
return await self.async_step_user(
user_input={
CONF_STATION_ID: station_id,
}
)

View file

@ -5,20 +5,14 @@ from collections.abc import Mapping
from dataclasses import dataclass from dataclasses import dataclass
from typing import Union from typing import Union
import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_MONITORED_CONDITIONS,
CONF_NAME,
DEGREE, DEGREE,
PERCENTAGE, PERCENTAGE,
UnitOfPrecipitationDepth, UnitOfPrecipitationDepth,
@ -28,11 +22,10 @@ from homeassistant.const import (
UnitOfTime, UnitOfTime,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ( from .const import (
@ -40,7 +33,6 @@ from .const import (
ATTR_UPDATED, ATTR_UPDATED,
ATTRIBUTION, ATTRIBUTION,
CONF_STATION_ID, CONF_STATION_ID,
DEFAULT_NAME,
DOMAIN, DOMAIN,
MANUFACTURER_URL, MANUFACTURER_URL,
) )
@ -192,39 +184,6 @@ SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
API_FIELDS: list[str] = [desc.para_name for desc in SENSOR_TYPES] API_FIELDS: list[str] = [desc.para_name for desc in SENSOR_TYPES]
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_MONITORED_CONDITIONS, default=["temperature"]): vol.All(
cv.ensure_list, [vol.In(SENSOR_KEYS)]
),
vol.Optional(CONF_STATION_ID): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Inclusive(
CONF_LATITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.latitude,
vol.Inclusive(
CONF_LONGITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.longitude,
}
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the ZAMG sensor platform."""
# trigger import flow
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=config,
)
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback

View file

@ -1,60 +1,23 @@
"""Sensor for zamg the Austrian "Zentralanstalt für Meteorologie und Geodynamik" integration.""" """Sensor for zamg the Austrian "Zentralanstalt für Meteorologie und Geodynamik" integration."""
from __future__ import annotations from __future__ import annotations
import voluptuous as vol from homeassistant.components.weather import WeatherEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.components.weather import PLATFORM_SCHEMA, WeatherEntity
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_LATITUDE,
CONF_LONGITUDE,
CONF_NAME,
UnitOfPrecipitationDepth, UnitOfPrecipitationDepth,
UnitOfPressure, UnitOfPressure,
UnitOfSpeed, UnitOfSpeed,
UnitOfTemperature, UnitOfTemperature,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import ATTRIBUTION, CONF_STATION_ID, DOMAIN, MANUFACTURER_URL from .const import ATTRIBUTION, CONF_STATION_ID, DOMAIN, MANUFACTURER_URL
from .coordinator import ZamgDataUpdateCoordinator from .coordinator import ZamgDataUpdateCoordinator
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_STATION_ID): cv.string,
vol.Inclusive(
CONF_LATITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.latitude,
vol.Inclusive(
CONF_LONGITUDE, "coordinates", "Latitude and longitude must exist together"
): cv.longitude,
}
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the ZAMG weather platform."""
# trigger import flow
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=config,
)
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback

View file

@ -1,15 +1,14 @@
"""Tests for the Zamg config flow.""" """Tests for the Zamg config flow."""
from unittest.mock import MagicMock from unittest.mock import MagicMock
from zamg.exceptions import ZamgApiError, ZamgStationNotFoundError from zamg.exceptions import ZamgApiError
from homeassistant.components.zamg.const import CONF_STATION_ID, DOMAIN, LOGGER from homeassistant.components.zamg.const import CONF_STATION_ID, DOMAIN, LOGGER
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from .conftest import TEST_STATION_ID, TEST_STATION_NAME from .conftest import TEST_STATION_ID
async def test_full_user_flow_implementation( async def test_full_user_flow_implementation(
@ -75,21 +74,6 @@ async def test_error_update(
assert result.get("reason") == "cannot_connect" assert result.get("reason") == "cannot_connect"
async def test_full_import_flow_implementation(
hass: HomeAssistant,
mock_zamg: MagicMock,
mock_setup_entry: None,
) -> None:
"""Test the full import flow from start to finish."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_STATION_ID: TEST_STATION_ID, CONF_NAME: TEST_STATION_NAME},
)
assert result.get("type") == FlowResultType.CREATE_ENTRY
assert result.get("data") == {CONF_STATION_ID: TEST_STATION_ID}
async def test_user_flow_duplicate( async def test_user_flow_duplicate(
hass: HomeAssistant, hass: HomeAssistant,
mock_zamg: MagicMock, mock_zamg: MagicMock,
@ -125,114 +109,3 @@ async def test_user_flow_duplicate(
) )
assert result.get("type") == FlowResultType.ABORT assert result.get("type") == FlowResultType.ABORT
assert result.get("reason") == "already_configured" assert result.get("reason") == "already_configured"
async def test_import_flow_duplicate(
hass: HomeAssistant,
mock_zamg: MagicMock,
mock_setup_entry: None,
) -> None:
"""Test import flow with duplicate entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
)
assert result.get("step_id") == "user"
assert result.get("type") == FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_STATION_ID: TEST_STATION_ID},
)
assert result.get("type") == FlowResultType.CREATE_ENTRY
assert "data" in result
assert result["data"][CONF_STATION_ID] == TEST_STATION_ID
assert "result" in result
assert result["result"].unique_id == TEST_STATION_ID
# try to add another instance
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_STATION_ID: TEST_STATION_ID, CONF_NAME: TEST_STATION_NAME},
)
assert result.get("type") == FlowResultType.ABORT
assert result.get("reason") == "already_configured"
async def test_import_flow_duplicate_after_position(
hass: HomeAssistant,
mock_zamg: MagicMock,
mock_setup_entry: None,
) -> None:
"""Test import flow with duplicate entry."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
)
assert result.get("step_id") == "user"
assert result.get("type") == FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={CONF_STATION_ID: TEST_STATION_ID},
)
assert result.get("type") == FlowResultType.CREATE_ENTRY
assert "data" in result
assert result["data"][CONF_STATION_ID] == TEST_STATION_ID
assert "result" in result
assert result["result"].unique_id == TEST_STATION_ID
# try to add another instance
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_STATION_ID: "123", CONF_NAME: TEST_STATION_NAME},
)
assert result.get("type") == FlowResultType.ABORT
assert result.get("reason") == "already_configured"
async def test_import_flow_no_name(
hass: HomeAssistant,
mock_zamg: MagicMock,
mock_setup_entry: None,
) -> None:
"""Test import flow without any name."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_STATION_ID: TEST_STATION_ID},
)
assert result.get("type") == FlowResultType.CREATE_ENTRY
assert result.get("data") == {CONF_STATION_ID: TEST_STATION_ID}
async def test_import_flow_invalid_station(
hass: HomeAssistant,
mock_zamg: MagicMock,
mock_setup_entry: None,
) -> None:
"""Test import flow with invalid station."""
mock_zamg.closest_station.side_effect = ZamgStationNotFoundError
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_STATION_ID: ""},
)
assert result.get("type") == FlowResultType.ABORT
assert result.get("reason") == "station_not_found"
async def test_import_flow_zamg_error(
hass: HomeAssistant,
mock_zamg: MagicMock,
mock_setup_entry: None,
) -> None:
"""Test import flow with error on getting zamg stations."""
mock_zamg.zamg_stations.side_effect = ZamgApiError
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_STATION_ID: ""},
)
assert result.get("type") == FlowResultType.ABORT
assert result.get("reason") == "cannot_connect"