1 """Sensor for checking the air quality around Norway."""
3 from __future__
import annotations
5 from datetime
import timedelta
8 from niluclient
import (
20 create_location_client,
21 create_station_client,
22 lookup_stations_in_area,
24 import voluptuous
as vol
27 PLATFORM_SCHEMA
as AIR_QUALITY_PLATFORM_SCHEMA,
42 _LOGGER = logging.getLogger(__name__)
45 ATTR_POLLUTION_INDEX =
"nilu_pollution_index"
48 CONF_STATION =
"stations"
54 CONF_ALLOWED_AREAS = [
92 PLATFORM_SCHEMA = AIR_QUALITY_PLATFORM_SCHEMA.extend(
95 CONF_LATITUDE,
"coordinates",
"Latitude and longitude must exist together"
98 CONF_LONGITUDE,
"coordinates",
"Latitude and longitude must exist together"
102 "station_collection",
104 "Can only configure one specific station or "
105 "stations in a specific area pr sensor. "
106 "Please only configure station or area."
108 ): vol.All(cv.string, vol.In(CONF_ALLOWED_AREAS)),
111 "station_collection",
113 "Can only configure one specific station or "
114 "stations in a specific area pr sensor. "
115 "Please only configure station or area."
117 ): vol.All(cv.ensure_list, [cv.string]),
118 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
119 vol.Optional(CONF_SHOW_ON_MAP, default=
False): cv.boolean,
127 add_entities: AddEntitiesCallback,
128 discovery_info: DiscoveryInfoType |
None =
None,
130 """Set up the NILU air quality sensor."""
131 name: str = config[CONF_NAME]
132 area: str |
None = config.get(CONF_AREA)
133 stations: list[str] |
None = config.get(CONF_STATION)
134 show_on_map: bool = config[CONF_SHOW_ON_MAP]
139 stations = lookup_stations_in_area(area)
141 latitude = config.get(CONF_LATITUDE, hass.config.latitude)
142 longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
143 location_client = create_location_client(latitude, longitude)
144 stations = location_client.station_names
146 assert stations
is not None
147 for station
in stations:
148 client =
NiluData(create_station_client(station))
150 if client.data.sensors:
151 sensors.append(
NiluSensor(client, name, show_on_map))
153 _LOGGER.warning(
"%s didn't give any sensors results", station)
159 """Class for handling the data retrieval."""
162 """Initialize the data object."""
167 """Get data cached in client."""
168 return self.
apiapi.data
170 @Throttle(SCAN_INTERVAL)
172 """Get the latest data from nilu API."""
177 """Single nilu station air sensor."""
179 _attr_attribution =
"Data provided by luftkvalitet.info and nilu.no"
181 def __init__(self, api_data: NiluData, name: str, show_on_map: bool) ->
None:
182 """Initialize the sensor."""
184 self.
_name_name = f
"{name} {api_data.data.name}"
189 self.
_attrs_attrs[CONF_LATITUDE] = api_data.data.latitude
190 self.
_attrs_attrs[CONF_LONGITUDE] = api_data.data.longitude
194 """Return other details about the sensor state."""
199 """Return the name of the sensor."""
200 return self.
_name_name
204 """Return the Air Quality Index (AQI)."""
209 """Return the CO (carbon monoxide) level."""
214 """Return the CO2 (carbon dioxide) level."""
219 """Return the N2O (nitrogen oxide) level."""
224 """Return the NO (nitrogen monoxide) level."""
229 """Return the NO2 (nitrogen dioxide) level."""
234 """Return the O3 (ozone) level."""
239 """Return the particulate matter 2.5 level."""
244 """Return the particulate matter 10 level."""
249 """Return the particulate matter 0.1 level."""
254 """Return the SO2 (sulphur dioxide) level."""
258 """Return formatted value of specified component."""
259 if component_name
in self.
_api_api.data.sensors:
260 sensor = self.
_api_api.data.sensors[component_name]
265 """Update the sensor."""
268 sensors = self.
_api_api.data.sensors.values()
270 max_index =
max(s.pollution_index
for s
in sensors)
272 self.
_attrs_attrs[ATTR_POLLUTION_INDEX] = POLLUTION_INDEX[self.
_max_aqi_max_aqi]
274 self.
_attrs_attrs[ATTR_AREA] = self.
_api_api.data.area
str|None nitrogen_oxide(self)
str|None sulphur_dioxide(self)
str|None air_quality_index(self)
str|None particulate_matter_2_5(self)
str|None get_component_state(self, str component_name)
str|None carbon_dioxide(self)
str|None nitrogen_monoxide(self)
str|None carbon_monoxide(self)
dict extra_state_attributes(self)
str|None particulate_matter_0_1(self)
str|None particulate_matter_10(self)
str|None nitrogen_dioxide(self)
None __init__(self, NiluData api_data, str name, bool show_on_map)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)