diff --git a/custom_components/cz_air_quality/__init__.py b/custom_components/cz_air_quality/__init__.py index 7f6fb54..92ea316 100644 --- a/custom_components/cz_air_quality/__init__.py +++ b/custom_components/cz_air_quality/__init__.py @@ -5,13 +5,15 @@ from homeassistant.core import HomeAssistant from . import hub from .const import DOMAIN, CONF_STOP_SEL +from .air_quality_data import CHMUAirQuality PLATFORMS: list[str] = ["sensor"] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Air Quality station from a config entry flow.""" - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = hub.AirQuality(hass, entry.data[CONF_STOP_SEL]) + initial_data = await hass.async_add_executor_job(CHMUAirQuality.update_info, entry.data[CONF_STOP_SEL]) + hass.data.setdefault(DOMAIN, {})[entry.entry_id] = hub.AirQuality(hass, entry.data[CONF_STOP_SEL], initial_data) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True diff --git a/custom_components/cz_air_quality/config_flow.py b/custom_components/cz_air_quality/config_flow.py index b79b958..183c3e0 100644 --- a/custom_components/cz_air_quality/config_flow.py +++ b/custom_components/cz_air_quality/config_flow.py @@ -3,8 +3,7 @@ import logging from typing import Any, Tuple, Dict -from .const import DOMAIN, CONF_STOP_SEL -from homeassistant.const import STATION_LIST +from .const import DOMAIN, CONF_STOP_SEL, STATION_LIST from homeassistant import config_entries, exceptions from homeassistant.core import HomeAssistant from homeassistant.helpers.selector import selector @@ -44,8 +43,7 @@ async def async_step_user(self, user_input=None): # Steps to take if user input is received if user_input is not None: try: - info, data = await validate_input(self.hass, user_input) - return self.async_create_entry(title=user_input[CONF_STOP_SEL]) + return self.async_create_entry(title=user_input[CONF_STOP_SEL], data=user_input) except CannotConnect: _LOGGER.exception("Cannot download data, check your internet connection.") diff --git a/custom_components/cz_air_quality/const.py b/custom_components/cz_air_quality/const.py index 8f5ddfd..17092d6 100644 --- a/custom_components/cz_air_quality/const.py +++ b/custom_components/cz_air_quality/const.py @@ -2,7 +2,6 @@ Defining constants for the project. """ - ICON_UPDATE = "mdi:update" DOMAIN = "cz_air_quality" CONF_STOP_SEL = "station_selector" @@ -120,7 +119,6 @@ "Třinec-Kosmos", "Věřňovice-Dolní Lutyně", "Vrbno pod Pradědem", - "Zátor" - ] + "Zátor"] diff --git a/custom_components/cz_air_quality/hub.py b/custom_components/cz_air_quality/hub.py index 8067513..dd7b196 100644 --- a/custom_components/cz_air_quality/hub.py +++ b/custom_components/cz_air_quality/hub.py @@ -9,12 +9,12 @@ class AirQuality: """Setting Air Quality Station as device.""" - def __init__(self, hass: HomeAssistant, station: str) -> None: + def __init__(self, hass: HomeAssistant, station: str, data) -> None: """Initialize departure board.""" self._hass: HomeAssistant = hass self._station: str = station self._callbacks = set() - self.data: dict = {} + self.data: dict = data @property def device_info(self): diff --git a/custom_components/cz_air_quality/sensor.py b/custom_components/cz_air_quality/sensor.py index c91096b..b64ee79 100644 --- a/custom_components/cz_air_quality/sensor.py +++ b/custom_components/cz_air_quality/sensor.py @@ -19,7 +19,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): # Set entities for air quality and measurements new_entities.append(AirQualitySensor(air_quality_station)) - for i in range(len(air_quality_station.measuments)): + for i in range(len(air_quality_station.measurements)): new_entities.append(MeasurementSensor(i, air_quality_station)) # Set diagnostic entities @@ -76,12 +76,7 @@ def extra_state_attributes(self) -> dict: @property def name(self) -> str: """Returns entity name""" - return f"AQ Index {self._aq_station.name}" - - @property - def device_class(self) -> str: - """Returns device class""" - return SensorDeviceClass.AQI + return f"AQ Index" async def async_added_to_hass(self): """Run when this Entity has been added to HA.""" @@ -111,25 +106,22 @@ def device_info(self) -> str: return self._aq_station.device_info @property - def native_value(self) -> str: + def native_value(self): """ Returns data as state if available.""" - if self._aq_station.measurements[self._measurement]["Flag"] == "no_meas": - return STATE_UNAVAILABLE - elif self._aq_station.measurements[self._measurement]["Flag"] == "no_data": - return STATE_UNKNOWN - elif self._aq_station.measurements[self._measurement]["Flag"] == "ok": + if self._aq_station.measurements[self._measurement]["Flag"] == "ok": if self._aq_station.measurements[self._measurement]["Val"] != "": - return STATE_UNKNOWN + if "," in self._aq_station.measurements[self._measurement]["Val"]: + value: str = self._aq_station.measurements[self._measurement]["Val"] + return float(value.replace(",", ".")) + else: + return float(self._aq_station.measurements[self._measurement]["Val"]) else: - return self._aq_station.measurements[self._measurement]["Val"] + return None + else: + return None @property - def name(self) -> str: - """Returns entity name""" - return self._aq_station.measurements[self._measurement]["Code"] - - @property - def device_class(self) -> str: + def device_class(self) -> SensorDeviceClass | None: """Returns device class""" match self._aq_station.measurements[self._measurement]["Code"]: case "SO2": @@ -143,10 +135,24 @@ def device_class(self) -> str: case "PM2_5": return SensorDeviceClass.PM25 + @property + def name(self) -> str: + """Returns entity name""" + return self._aq_station.measurements[self._measurement]["Code"] + @property def native_unit_of_measurement(self): return "µg/m³" + @property + def extra_state_attributes(self) -> dict | None: + if self._aq_station.measurements[self._measurement]["Flag"] == "no_meas": + return {"info": "Veličina se na uvedené stanici neměří"} + elif self._aq_station.measurements[self._measurement]["Flag"] == "no_data": + return {"info": "Neúplná data"} + else: + return None + async def async_added_to_hass(self): """Run when this Entity has been added to HA.""" # Sensors should also register callbacks to HA when their state changes @@ -193,7 +199,6 @@ class UpdateSensor(SensorEntity): _attr_has_entity_name = True _attr_entity_category = EntityCategory.DIAGNOSTIC _attr_icon = ICON_UPDATE - _attr_device_class = SensorDeviceClass.TIMESTAMP def __init__(self, aq_station): @@ -212,7 +217,7 @@ def name(self) -> str: @property def native_value(self): - return self._aq_station.updated + return self._aq_station.data_updated async def async_update(self): """ Calls regular update of data . """