Migrate WAQI to has entity name (#101203)

This commit is contained in:
Joost Lekkerkerker 2023-10-01 23:47:32 +02:00 committed by GitHub
parent cabfbc245d
commit 4c24ff6847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -24,6 +24,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv 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.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -154,12 +155,18 @@ class WaqiSensor(CoordinatorEntity[WAQIDataUpdateCoordinator], SensorEntity):
_attr_icon = ATTR_ICON _attr_icon = ATTR_ICON
_attr_device_class = SensorDeviceClass.AQI _attr_device_class = SensorDeviceClass.AQI
_attr_state_class = SensorStateClass.MEASUREMENT _attr_state_class = SensorStateClass.MEASUREMENT
_attr_has_entity_name = True
_attr_name = None
def __init__(self, coordinator: WAQIDataUpdateCoordinator) -> None: def __init__(self, coordinator: WAQIDataUpdateCoordinator) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(coordinator) 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_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 @property
def native_value(self) -> int | None: def native_value(self) -> int | None:

View File

@ -72,7 +72,7 @@ async def test_legacy_migration_already_imported(
assert await async_setup_component(hass, DOMAIN, {}) assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done() 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" assert state.state == "29"
hass.async_create_task( hass.async_create_task(
@ -116,7 +116,7 @@ async def test_sensor_id_migration(
) )
assert len(entities) == 1 assert len(entities) == 1
assert hass.states.get("sensor.waqi_4584") 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" assert entities[0].unique_id == "4584_air_quality"
@ -132,7 +132,7 @@ async def test_sensor(hass: HomeAssistant, mock_config_entry: MockConfigEntry) -
assert await async_setup_component(hass, DOMAIN, {}) assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done() 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" assert state.state == "29"