Skip to content
4 changes: 1 addition & 3 deletions homeassistant/components/gios/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_NAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.selector import (
SelectOptionDict,
Expand Down Expand Up @@ -79,8 +78,7 @@ async def async_step_user(
sort=True,
mode=SelectSelectorMode.DROPDOWN,
),
),
vol.Optional(CONF_NAME, default=self.hass.config.location_name): str,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name was removed here, but not removed from the translations file (string.json), which still needs to be cleaned up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Removed.

)
}
)

Expand Down
16 changes: 15 additions & 1 deletion homeassistant/components/gios/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
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 +53,18 @@ def __init__(
update_interval=SCAN_INTERVAL,
)

station_id = gios.station_id
assert station_id is not None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be using assert in production code. In general the use means we either have a logic error (or could improve logic) or there might be a typing issue/improvement that could result the need for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this check here because the API client can be initialized with an optional station_id parameter, but in our case, the client is always initialized with a specific station ID. The client without a station ID is used in config_flow to retrieve the list of available stations.
https://github.com/bieniu/gios/blob/871ea652a27216f87645a0161b823f500cd7a944/gios/__init__.py#L51
Here is Gios client initialization code:

gios = await Gios.create(websession, station_id)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added if TYPE_CHECKING to avoid this code in production code.

station_name = gios.measurement_stations[station_id].name

self.device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, str(gios.station_id))},
manufacturer=MANUFACTURER,
name=config_entry.data.get(CONF_NAME) or station_name,
configuration_url=URL.format(station_id=gios.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: 1 addition & 1 deletion tests/components/gios/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def mock_config_entry() -> MockConfigEntry:
domain=DOMAIN,
title="Home",
unique_id="123",
data={"station_id": 123, "name": "Home"},
data={"station_id": 123},
entry_id="86129426118ae32020417a53712d6eef",
)

Expand Down
1 change: 0 additions & 1 deletion tests/components/gios/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
dict({
'config_entry': dict({
'data': dict({
'name': 'Home',
'station_id': 123,
}),
'disabled_by': None,
Expand Down
Loading
Loading