Migrate GIOS to new entity naming style (#75051)

Use new entity naming style
This commit is contained in:
Maciej Bieniek 2022-07-16 15:25:07 +02:00 committed by GitHub
parent 13cea26e74
commit 0f3cc4a4aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 85 deletions

View File

@ -4,15 +4,9 @@ from __future__ import annotations
from datetime import timedelta from datetime import timedelta
from typing import Final from typing import Final
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import CONCENTRATION_MICROGRAMS_PER_CUBIC_METER
from .model import GiosSensorEntityDescription
ATTRIBUTION: Final = "Data provided by GIOŚ" ATTRIBUTION: Final = "Data provided by GIOŚ"
CONF_STATION_ID: Final = "station_id" CONF_STATION_ID: Final = "station_id"
DEFAULT_NAME: Final = "GIOŚ"
# Term of service GIOŚ allow downloading data no more than twice an hour. # Term of service GIOŚ allow downloading data no more than twice an hour.
SCAN_INTERVAL: Final = timedelta(minutes=30) SCAN_INTERVAL: Final = timedelta(minutes=30)
DOMAIN: Final = "gios" DOMAIN: Final = "gios"
@ -33,61 +27,3 @@ ATTR_PM10: Final = "pm10"
ATTR_PM25: Final = "pm25" ATTR_PM25: Final = "pm25"
ATTR_SO2: Final = "so2" ATTR_SO2: Final = "so2"
ATTR_AQI: Final = "aqi" ATTR_AQI: Final = "aqi"
SENSOR_TYPES: Final[tuple[GiosSensorEntityDescription, ...]] = (
GiosSensorEntityDescription(
key=ATTR_AQI,
name="AQI",
device_class=SensorDeviceClass.AQI,
value=None,
),
GiosSensorEntityDescription(
key=ATTR_C6H6,
name="C6H6",
icon="mdi:molecule",
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_CO,
name="CO",
device_class=SensorDeviceClass.CO,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_NO2,
name="NO2",
device_class=SensorDeviceClass.NITROGEN_DIOXIDE,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_O3,
name="O3",
device_class=SensorDeviceClass.OZONE,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_PM10,
name="PM10",
device_class=SensorDeviceClass.PM10,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_PM25,
name="PM2.5",
device_class=SensorDeviceClass.PM25,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_SO2,
name="SO2",
device_class=SensorDeviceClass.SULPHUR_DIOXIDE,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
)

View File

@ -1,14 +0,0 @@
"""Type definitions for GIOS integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.sensor import SensorEntityDescription
@dataclass
class GiosSensorEntityDescription(SensorEntityDescription):
"""Class describing GIOS sensor entities."""
value: Callable | None = round

View File

@ -1,12 +1,25 @@
"""Support for the GIOS service.""" """Support for the GIOS service."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
import logging import logging
from typing import Any, cast from typing import Any, cast
from homeassistant.components.sensor import DOMAIN as PLATFORM, SensorEntity from homeassistant.components.sensor import (
DOMAIN as PLATFORM,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_NAME, CONF_NAME from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_NAME,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONF_NAME,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
@ -18,21 +31,90 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import GiosDataUpdateCoordinator from . import GiosDataUpdateCoordinator
from .const import ( from .const import (
ATTR_AQI, ATTR_AQI,
ATTR_C6H6,
ATTR_CO,
ATTR_INDEX, ATTR_INDEX,
ATTR_NO2,
ATTR_O3,
ATTR_PM10,
ATTR_PM25, ATTR_PM25,
ATTR_SO2,
ATTR_STATION, ATTR_STATION,
ATTRIBUTION, ATTRIBUTION,
DEFAULT_NAME,
DOMAIN, DOMAIN,
MANUFACTURER, MANUFACTURER,
SENSOR_TYPES,
URL, URL,
) )
from .model import GiosSensorEntityDescription
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@dataclass
class GiosSensorEntityDescription(SensorEntityDescription):
"""Class describing GIOS sensor entities."""
value: Callable | None = round
SENSOR_TYPES: tuple[GiosSensorEntityDescription, ...] = (
GiosSensorEntityDescription(
key=ATTR_AQI,
name="AQI",
device_class=SensorDeviceClass.AQI,
value=None,
),
GiosSensorEntityDescription(
key=ATTR_C6H6,
name="C6H6",
icon="mdi:molecule",
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_CO,
name="CO",
device_class=SensorDeviceClass.CO,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_NO2,
name="NO2",
device_class=SensorDeviceClass.NITROGEN_DIOXIDE,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_O3,
name="O3",
device_class=SensorDeviceClass.OZONE,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_PM10,
name="PM10",
device_class=SensorDeviceClass.PM10,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_PM25,
name="PM2.5",
device_class=SensorDeviceClass.PM25,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
GiosSensorEntityDescription(
key=ATTR_SO2,
name="SO2",
device_class=SensorDeviceClass.SULPHUR_DIOXIDE,
native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
state_class=SensorStateClass.MEASUREMENT,
),
)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
@ -72,6 +154,7 @@ async def async_setup_entry(
class GiosSensor(CoordinatorEntity[GiosDataUpdateCoordinator], SensorEntity): class GiosSensor(CoordinatorEntity[GiosDataUpdateCoordinator], SensorEntity):
"""Define an GIOS sensor.""" """Define an GIOS sensor."""
_attr_has_entity_name = True
entity_description: GiosSensorEntityDescription entity_description: GiosSensorEntityDescription
def __init__( def __init__(
@ -86,10 +169,9 @@ class GiosSensor(CoordinatorEntity[GiosDataUpdateCoordinator], SensorEntity):
entry_type=DeviceEntryType.SERVICE, entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, str(coordinator.gios.station_id))}, identifiers={(DOMAIN, str(coordinator.gios.station_id))},
manufacturer=MANUFACTURER, manufacturer=MANUFACTURER,
name=DEFAULT_NAME, name=name,
configuration_url=URL.format(station_id=coordinator.gios.station_id), configuration_url=URL.format(station_id=coordinator.gios.station_id),
) )
self._attr_name = f"{name} {description.name}"
self._attr_unique_id = f"{coordinator.gios.station_id}-{description.key}" self._attr_unique_id = f"{coordinator.gios.station_id}-{description.key}"
self._attrs: dict[str, Any] = { self._attrs: dict[str, Any] = {
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,