1 """Support for the GIOS service."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
9 from gios.model
import GiosSensors
15 SensorEntityDescription,
26 from .
import GiosConfigEntry
41 from .coordinator
import GiosDataUpdateCoordinator
43 _LOGGER = logging.getLogger(__name__)
46 @dataclass(frozen=True, kw_only=True)
48 """Class describing GIOS sensor entities."""
50 value: Callable[[GiosSensors], StateType]
51 subkey: str |
None =
None
54 SENSOR_TYPES: tuple[GiosSensorEntityDescription, ...] = (
57 value=
lambda sensors: sensors.aqi.value
if sensors.aqi
else None,
58 device_class=SensorDeviceClass.ENUM,
59 options=[
"very_bad",
"bad",
"sufficient",
"moderate",
"good",
"very_good"],
60 translation_key=
"aqi",
64 value=
lambda sensors: sensors.c6h6.value
if sensors.c6h6
else None,
65 suggested_display_precision=0,
66 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
67 state_class=SensorStateClass.MEASUREMENT,
68 translation_key=
"c6h6",
72 value=
lambda sensors: sensors.co.value
if sensors.co
else None,
73 suggested_display_precision=0,
74 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
75 state_class=SensorStateClass.MEASUREMENT,
80 value=
lambda sensors: sensors.no2.value
if sensors.no2
else None,
81 suggested_display_precision=0,
82 device_class=SensorDeviceClass.NITROGEN_DIOXIDE,
83 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
84 state_class=SensorStateClass.MEASUREMENT,
89 value=
lambda sensors: sensors.no2.index
if sensors.no2
else None,
90 device_class=SensorDeviceClass.ENUM,
91 options=[
"very_bad",
"bad",
"sufficient",
"moderate",
"good",
"very_good"],
92 translation_key=
"no2_index",
96 value=
lambda sensors: sensors.o3.value
if sensors.o3
else None,
97 suggested_display_precision=0,
98 device_class=SensorDeviceClass.OZONE,
99 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
100 state_class=SensorStateClass.MEASUREMENT,
105 value=
lambda sensors: sensors.o3.index
if sensors.o3
else None,
106 device_class=SensorDeviceClass.ENUM,
107 options=[
"very_bad",
"bad",
"sufficient",
"moderate",
"good",
"very_good"],
108 translation_key=
"o3_index",
112 value=
lambda sensors: sensors.pm10.value
if sensors.pm10
else None,
113 suggested_display_precision=0,
114 device_class=SensorDeviceClass.PM10,
115 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
116 state_class=SensorStateClass.MEASUREMENT,
121 value=
lambda sensors: sensors.pm10.index
if sensors.pm10
else None,
122 device_class=SensorDeviceClass.ENUM,
123 options=[
"very_bad",
"bad",
"sufficient",
"moderate",
"good",
"very_good"],
124 translation_key=
"pm10_index",
128 value=
lambda sensors: sensors.pm25.value
if sensors.pm25
else None,
129 suggested_display_precision=0,
130 device_class=SensorDeviceClass.PM25,
131 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
132 state_class=SensorStateClass.MEASUREMENT,
137 value=
lambda sensors: sensors.pm25.index
if sensors.pm25
else None,
138 device_class=SensorDeviceClass.ENUM,
139 options=[
"very_bad",
"bad",
"sufficient",
"moderate",
"good",
"very_good"],
140 translation_key=
"pm25_index",
144 value=
lambda sensors: sensors.so2.value
if sensors.so2
else None,
145 suggested_display_precision=0,
146 device_class=SensorDeviceClass.SULPHUR_DIOXIDE,
147 native_unit_of_measurement=CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
148 state_class=SensorStateClass.MEASUREMENT,
153 value=
lambda sensors: sensors.so2.index
if sensors.so2
else None,
154 device_class=SensorDeviceClass.ENUM,
155 options=[
"very_bad",
"bad",
"sufficient",
"moderate",
"good",
"very_good"],
156 translation_key=
"so2_index",
162 hass: HomeAssistant, entry: GiosConfigEntry, async_add_entities: AddEntitiesCallback
164 """Add a GIOS entities from a config_entry."""
165 name = entry.data[CONF_NAME]
167 coordinator = entry.runtime_data.coordinator
170 entity_registry = er.async_get(hass)
171 old_unique_id = f
"{coordinator.gios.station_id}-pm2.5"
172 if entity_id := entity_registry.async_get_entity_id(
173 PLATFORM, DOMAIN, old_unique_id
175 new_unique_id = f
"{coordinator.gios.station_id}-{ATTR_PM25}"
177 "Migrating entity %s from old unique ID '%s' to new unique ID '%s'",
182 entity_registry.async_update_entity(entity_id, new_unique_id=new_unique_id)
184 sensors: list[GiosSensor] = []
186 for description
in SENSOR_TYPES:
187 if getattr(coordinator.data, description.key)
is None:
189 sensors.append(
GiosSensor(name, coordinator, description))
195 """Define an GIOS sensor."""
197 _attr_attribution = ATTRIBUTION
198 _attr_has_entity_name =
True
199 entity_description: GiosSensorEntityDescription
204 coordinator: GiosDataUpdateCoordinator,
205 description: GiosSensorEntityDescription,
210 entry_type=DeviceEntryType.SERVICE,
211 identifiers={(DOMAIN,
str(coordinator.gios.station_id))},
212 manufacturer=MANUFACTURER,
214 configuration_url=URL.format(station_id=coordinator.gios.station_id),
216 if description.subkey:
218 f
"{coordinator.gios.station_id}-{description.key}-{description.subkey}"
221 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.gios.station_id}-{description.key}"
226 """Return the state."""
231 """Return if entity is available."""
232 sensor_data = getattr(self.coordinator.data, self.
entity_descriptionentity_description.key)
233 available = super().available
and bool(sensor_data)
237 return available
and bool(sensor_data.index)
None __init__(self, str name, GiosDataUpdateCoordinator coordinator, GiosSensorEntityDescription description)
StateType native_value(self)
None async_setup_entry(HomeAssistant hass, GiosConfigEntry entry, AddEntitiesCallback async_add_entities)