diff --git a/homeassistant/components/gios/config_flow.py b/homeassistant/components/gios/config_flow.py index 9b242a8cc99..a4bbfdeff6f 100644 --- a/homeassistant/components/gios/config_flow.py +++ b/homeassistant/components/gios/config_flow.py @@ -54,7 +54,11 @@ class GiosFlowHandler(ConfigFlow, domain=DOMAIN): return self.async_create_entry( title=gios.station_name, - data=user_input, + # CONF_NAME is still used, but its value is preserved + # primarily for backward compatibility. This allows older + # versions of the software to read the entry data without + # raising errors. + data={**user_input, CONF_NAME: gios.station_name}, ) except (ApiError, ClientConnectorError, TimeoutError): errors["base"] = "cannot_connect" @@ -79,8 +83,7 @@ class GiosFlowHandler(ConfigFlow, domain=DOMAIN): sort=True, mode=SelectSelectorMode.DROPDOWN, ), - ), - vol.Optional(CONF_NAME, default=self.hass.config.location_name): str, + ) } ) diff --git a/homeassistant/components/gios/coordinator.py b/homeassistant/components/gios/coordinator.py index eb0dd82eb67..c80557da55f 100644 --- a/homeassistant/components/gios/coordinator.py +++ b/homeassistant/components/gios/coordinator.py @@ -5,6 +5,7 @@ from __future__ import annotations import asyncio from dataclasses import dataclass import logging +from typing import TYPE_CHECKING from aiohttp.client_exceptions import ClientConnectorError from gios import Gios @@ -12,10 +13,12 @@ from gios.exceptions import GiosError from gios.model import GiosSensors from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from .const import API_TIMEOUT, DOMAIN, SCAN_INTERVAL +from .const import API_TIMEOUT, DOMAIN, MANUFACTURER, SCAN_INTERVAL, URL _LOGGER = logging.getLogger(__name__) @@ -51,6 +54,21 @@ class GiosDataUpdateCoordinator(DataUpdateCoordinator[GiosSensors]): update_interval=SCAN_INTERVAL, ) + station_id = gios.station_id + if TYPE_CHECKING: + # Station ID is Optional in the library, but here we know it is set for sure + # so we can safely assert it is not None for type checking purposes + # Gios instance is created only with a valid station ID in the async_setup_entry. + assert station_id is not None + + self.device_info = DeviceInfo( + entry_type=DeviceEntryType.SERVICE, + identifiers={(DOMAIN, str(station_id))}, + manufacturer=MANUFACTURER, + name=config_entry.data[CONF_NAME], + configuration_url=URL.format(station_id=station_id), + ) + async def _async_update_data(self) -> GiosSensors: """Update data via library.""" try: diff --git a/homeassistant/components/gios/sensor.py b/homeassistant/components/gios/sensor.py index a0511e84536..7fb6fcf431c 100644 --- a/homeassistant/components/gios/sensor.py +++ b/homeassistant/components/gios/sensor.py @@ -15,10 +15,9 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONF_NAME +from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -36,8 +35,6 @@ from .const import ( ATTR_SO2, ATTRIBUTION, DOMAIN, - MANUFACTURER, - URL, ) from .coordinator import GiosConfigEntry, GiosDataUpdateCoordinator @@ -184,8 +181,6 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Add a GIOS entities from a config_entry.""" - name = entry.data[CONF_NAME] - coordinator = entry.runtime_data.coordinator # Due to the change of the attribute name of one sensor, it is necessary to migrate # the unique_id to the new name. @@ -208,7 +203,7 @@ async def async_setup_entry( for description in SENSOR_TYPES: if getattr(coordinator.data, description.key) is None: continue - sensors.append(GiosSensor(name, coordinator, description)) + sensors.append(GiosSensor(coordinator, description)) async_add_entities(sensors) @@ -222,19 +217,13 @@ class GiosSensor(CoordinatorEntity[GiosDataUpdateCoordinator], SensorEntity): def __init__( self, - name: str, coordinator: GiosDataUpdateCoordinator, description: GiosSensorEntityDescription, ) -> None: """Initialize.""" super().__init__(coordinator) - self._attr_device_info = DeviceInfo( - entry_type=DeviceEntryType.SERVICE, - identifiers={(DOMAIN, str(coordinator.gios.station_id))}, - manufacturer=MANUFACTURER, - name=name, - configuration_url=URL.format(station_id=coordinator.gios.station_id), - ) + + self._attr_device_info = coordinator.device_info if description.subkey: self._attr_unique_id = ( f"{coordinator.gios.station_id}-{description.key}-{description.subkey}" diff --git a/homeassistant/components/gios/strings.json b/homeassistant/components/gios/strings.json index f7c82929c1c..da9c246600a 100644 --- a/homeassistant/components/gios/strings.json +++ b/homeassistant/components/gios/strings.json @@ -11,11 +11,9 @@ "step": { "user": { "data": { - "name": "[%key:common::config_flow::data::name%]", "station_id": "Measuring station" }, "data_description": { - "name": "Config entry name, by default, this is the name of your Home Assistant instance.", "station_id": "The name of the measuring station where the environmental data is collected." }, "title": "GIO\u015a (Polish Chief Inspectorate Of Environmental Protection)" diff --git a/tests/components/gios/conftest.py b/tests/components/gios/conftest.py index 3ab1a70ed79..fd54beae3b6 100644 --- a/tests/components/gios/conftest.py +++ b/tests/components/gios/conftest.py @@ -6,7 +6,8 @@ from unittest.mock import AsyncMock, MagicMock, patch from gios.model import GiosSensors, GiosStation, Sensor as GiosSensor import pytest -from homeassistant.components.gios.const import DOMAIN +from homeassistant.components.gios.const import CONF_STATION_ID, DOMAIN +from homeassistant.const import CONF_NAME from homeassistant.core import HomeAssistant from . import setup_integration @@ -21,7 +22,10 @@ def mock_config_entry() -> MockConfigEntry: domain=DOMAIN, title="Home", unique_id="123", - data={"station_id": 123, "name": "Home"}, + data={ + CONF_STATION_ID: 123, + CONF_NAME: "Home", + }, entry_id="86129426118ae32020417a53712d6eef", ) @@ -49,7 +53,7 @@ def mock_gios_sensors() -> GiosSensors: def mock_gios_stations() -> dict[int, GiosStation]: """Return the default mocked gios stations.""" return { - 123: GiosStation(id=123, name="Test Name 1", latitude=99.99, longitude=88.88), + 123: GiosStation(id=123, name="Home", latitude=99.99, longitude=88.88), 321: GiosStation(id=321, name="Test Name 2", latitude=77.77, longitude=66.66), } diff --git a/tests/components/gios/test_config_flow.py b/tests/components/gios/test_config_flow.py index 715e15318f8..b7229c621be 100644 --- a/tests/components/gios/test_config_flow.py +++ b/tests/components/gios/test_config_flow.py @@ -12,7 +12,6 @@ from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType CONFIG = { - CONF_NAME: "Foo", CONF_STATION_ID: "123", } @@ -68,12 +67,13 @@ async def test_form_submission_errors( assert result["type"] is FlowResultType.FORM assert result["errors"] == errors + mock_gios.async_update.side_effect = None result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=CONFIG ) assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "Test Name 1" + assert result["title"] == "Home" async def test_create_entry(hass: HomeAssistant) -> None: @@ -87,7 +87,10 @@ async def test_create_entry(hass: HomeAssistant) -> None: ) assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == "Test Name 1" - assert result["data"][CONF_STATION_ID] == 123 + assert result["title"] == "Home" + assert result["data"] == { + CONF_STATION_ID: 123, + CONF_NAME: "Home", + } assert result["result"].unique_id == "123"