Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions homeassistant/components/gios/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ async def async_step_user(

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},
Comment thread
bieniu marked this conversation as resolved.
)
except (ApiError, ClientConnectorError, TimeoutError):
errors["base"] = "cannot_connect"
Expand All @@ -79,8 +83,7 @@ async def async_step_user(
sort=True,
mode=SelectSelectorMode.DROPDOWN,
),
),
vol.Optional(CONF_NAME, default=self.hass.config.location_name): str,
Comment thread
bieniu marked this conversation as resolved.
)
}
)

Expand Down
20 changes: 19 additions & 1 deletion homeassistant/components/gios/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
import asyncio
from dataclasses import dataclass
import logging
from typing import TYPE_CHECKING

from aiohttp.client_exceptions import ClientConnectorError
from gios import Gios
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__)

Expand Down Expand Up @@ -51,6 +54,21 @@ def __init__(
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:
Expand Down
19 changes: 4 additions & 15 deletions homeassistant/components/gios/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
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
Expand All @@ -36,8 +35,6 @@
ATTR_SO2,
ATTRIBUTION,
DOMAIN,
MANUFACTURER,
URL,
)
from .coordinator import GiosConfigEntry, GiosDataUpdateCoordinator

Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand All @@ -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}"
Expand Down
2 changes: 0 additions & 2 deletions homeassistant/components/gios/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
10 changes: 7 additions & 3 deletions tests/components/gios/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
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
Expand All @@ -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",
)

Expand Down Expand Up @@ -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),
}

Expand Down
11 changes: 7 additions & 4 deletions tests/components/gios/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from homeassistant.data_entry_flow import FlowResultType

CONFIG = {
CONF_NAME: "Foo",
CONF_STATION_ID: "123",
}

Expand Down Expand Up @@ -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:
Expand All @@ -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"
Loading