Skip to content

Commit

Permalink
Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvejsada committed Apr 6, 2024
1 parent 5c404db commit efe5776
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
4 changes: 3 additions & 1 deletion custom_components/cz_air_quality/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions custom_components/cz_air_quality/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
4 changes: 1 addition & 3 deletions custom_components/cz_air_quality/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Defining constants for the project.
"""


ICON_UPDATE = "mdi:update"
DOMAIN = "cz_air_quality"
CONF_STOP_SEL = "station_selector"
Expand Down Expand Up @@ -120,7 +119,6 @@
"Třinec-Kosmos",
"Věřňovice-Dolní Lutyně",
"Vrbno pod Pradědem",
"Zátor"
]
"Zátor"]


4 changes: 2 additions & 2 deletions custom_components/cz_air_quality/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
51 changes: 28 additions & 23 deletions custom_components/cz_air_quality/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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":
Expand All @@ -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
Expand Down Expand Up @@ -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):

Expand All @@ -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 . """
Expand Down

0 comments on commit efe5776

Please sign in to comment.