diff --git a/custom_components/cz_air_quality/air_quality_data.py b/custom_components/cz_air_quality/air_quality_data.py index e42acea..e9dc9b8 100644 --- a/custom_components/cz_air_quality/air_quality_data.py +++ b/custom_components/cz_air_quality/air_quality_data.py @@ -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: @@ -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 diff --git a/custom_components/cz_air_quality/config_flow.py b/custom_components/cz_air_quality/config_flow.py index 183c3e0..2a2019e 100644 --- a/custom_components/cz_air_quality/config_flow.py +++ b/custom_components/cz_air_quality/config_flow.py @@ -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): @@ -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: diff --git a/custom_components/cz_air_quality/manifest.json b/custom_components/cz_air_quality/manifest.json index d784b96..43a7c0b 100644 --- a/custom_components/cz_air_quality/manifest.json +++ b/custom_components/cz_air_quality/manifest.json @@ -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" }