Skip to content

Commit

Permalink
Migrate WAQI to has entity name (home-assistant#101203)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Oct 1, 2023
1 parent cabfbc2 commit 4c24ff6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion homeassistant/components/waqi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
Expand Down Expand Up @@ -154,12 +155,18 @@ class WaqiSensor(CoordinatorEntity[WAQIDataUpdateCoordinator], SensorEntity):
_attr_icon = ATTR_ICON
_attr_device_class = SensorDeviceClass.AQI
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_has_entity_name = True
_attr_name = None

def __init__(self, coordinator: WAQIDataUpdateCoordinator) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._attr_name = f"WAQI {self.coordinator.data.city.name}"
self._attr_unique_id = f"{coordinator.data.station_id}_air_quality"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, str(coordinator.data.station_id))},
name=coordinator.data.city.name,
entry_type=DeviceEntryType.SERVICE,
)

@property
def native_value(self) -> int | None:
Expand Down
6 changes: 3 additions & 3 deletions tests/components/waqi/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async def test_legacy_migration_already_imported(
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()

state = hass.states.get("sensor.waqi_de_jongweg_utrecht")
state = hass.states.get("sensor.de_jongweg_utrecht")
assert state.state == "29"

hass.async_create_task(
Expand Down Expand Up @@ -116,7 +116,7 @@ async def test_sensor_id_migration(
)
assert len(entities) == 1
assert hass.states.get("sensor.waqi_4584")
assert hass.states.get("sensor.waqi_de_jongweg_utrecht") is None
assert hass.states.get("sensor.de_jongweg_utrecht") is None
assert entities[0].unique_id == "4584_air_quality"


Expand All @@ -132,7 +132,7 @@ async def test_sensor(hass: HomeAssistant, mock_config_entry: MockConfigEntry) -
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()

state = hass.states.get("sensor.waqi_de_jongweg_utrecht")
state = hass.states.get("sensor.de_jongweg_utrecht")
assert state.state == "29"


Expand Down

0 comments on commit 4c24ff6

Please sign in to comment.