Skip to content

Commit

Permalink
Added validation for config entry.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvejsada committed Apr 7, 2024
1 parent d16abe9 commit 51d779d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
23 changes: 18 additions & 5 deletions custom_components/cz_air_quality/air_quality_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
class CHMUAirQuality:

@staticmethod
def update_info(station):
def get_data():
""" Gets new data from website"""
response = requests.get(URL)
res = response.json()
data = CHMUAirQuality.parse_data(res, station)
data = response.json()
return data

@staticmethod
def parse_data(data, station) -> dict:
"""Returns data for relevant station"""
def update_info(station) -> dict:
""" Parse data to get specific station."""
data = CHMUAirQuality.get_data()
res = None

# Loops over the station to get data for specific station.
for i in range(len(data["States"][0]['Regions'])):
for sub in data["States"][0]['Regions'][i]["Stations"]:
if sub['Name'] == station:
Expand All @@ -28,4 +30,15 @@ def parse_data(data, station) -> dict:
if res is not None:
break

# Return the data for specific station only
return {"updated": data["Actualized"], "station_data": res}

@staticmethod
def validate_station(station):
""" Validate the station is in the list."""
data = CHMUAirQuality.get_data()
for i in range(len(data["States"][0]['Regions'])):
for sub in data["States"][0]['Regions'][i]["Stations"]:
if sub['Name'] == station:
return None
return False
9 changes: 7 additions & 2 deletions custom_components/cz_air_quality/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
from homeassistant import config_entries, exceptions
from homeassistant.core import HomeAssistant
from homeassistant.helpers.selector import selector
from.air_quality_data import CHMUAirQuality


_LOGGER = logging.getLogger(__name__)


async def validate_input(hass: HomeAssistant, data: dict) -> tuple[dict[str, str], dict]:
def validate_input(data: dict) -> None:
"""Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
pass
if CHMUAirQuality.validate_station(data[CONF_STOP_SEL]):
return None
else:
raise StationNotFound


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
Expand All @@ -43,6 +47,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:
await self.hass.async_add_executor_job(validate_input,user_input)
return self.async_create_entry(title=user_input[CONF_STOP_SEL], data=user_input)

except CannotConnect:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/cz_air_quality/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/dvejsada/Air_Quality_CZ/issues",
"requirements": [],
"version": "0.0.1"
"version": "0.1.1"
}

0 comments on commit 51d779d

Please sign in to comment.