async def async_step_user(self, user_input=None): """Handle a flow initialized by the user.""" errors = {} if user_input is not None: try: await self.async_set_unique_id(user_input[CONF_STATION_ID], raise_on_progress=False) self._abort_if_unique_id_configured() websession = async_get_clientsession(self.hass) with timeout(30): gios = Gios(user_input[CONF_STATION_ID], websession) await gios.update() return self.async_create_entry( title=user_input[CONF_STATION_ID], data=user_input, ) except (ApiError, ClientConnectorError, asyncio.TimeoutError): errors["base"] = "cannot_connect" except NoStationError: errors[CONF_STATION_ID] = "wrong_station_id" except InvalidSensorsData: errors[CONF_STATION_ID] = "invalid_sensors_data" return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA, errors=errors)
async def test_no_indexes_data(): """Test with valid data.""" with open("tests/data/stations.json") as file: stations = json.load(file) with open("tests/data/station.json") as file: station = json.load(file) with open("tests/data/sensors_valid_first_values.json") as file: sensors = json.load(file) indexes = {} with patch("gios.Gios._get_stations", return_value=stations), patch( "gios.Gios._get_station", return_value=station), patch( "gios.Gios._get_all_sensors", return_value=sensors), patch( "gios.Gios._get_indexes", return_value=indexes), pytest.raises( InvalidSensorsData) as error: async with ClientSession() as websession: gios = Gios(VALID_STATION_ID, websession) await gios.update() assert str(error.value) == "Invalid index data from GIOS API" assert gios.available is False
def __init__( self, hass: HomeAssistant, session: ClientSession, station_id: int ) -> None: """Class to manage fetching GIOS data API.""" self.gios = Gios(station_id, session) super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
async def _test_sensors_data(self, client, station_id): """Return true if sensors data is valid.""" with timeout(30): gios = Gios(station_id, client) await gios.update() if gios.available: return True return False
def __init__(self, hass, session, station_id): """Class to manage fetching GIOS data API.""" self.gios = Gios(station_id, session) super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
def __init__(self, hass, session, station_id, scan_interval): """Initialize.""" self.gios = Gios(station_id, session) super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=scan_interval)
async def _test_station_id(self, client, station_id): """Return true if station_id is valid.""" try: with timeout(30): gios = Gios(station_id, client) await gios.update() except NoStationError: return False return True
def __init__(self, opp: OpenPeerPower, session: ClientSession, station_id: int) -> None: """Class to manage fetching GIOS data API.""" self.gios = Gios(station_id, session) super().__init__(opp, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
def __init__(self, session, station_id): """Initialize.""" self._gios = Gios(station_id, session) self.station_id = station_id self.sensors = {} self.latitude = None self.longitude = None self.station_name = None self.available = True
async def test_api_error(): """Test GIOS API error.""" with patch("gios.Gios._async_get", side_effect=ApiError(404)), pytest.raises(ApiError) as error: async with ClientSession() as websession: gios = Gios(VALID_STATION_ID, websession) await gios.update() assert str(error.value) == "404" assert gios.available is False
async def test_invalid_sensor_data_2(): """Test with invalid sensor data.""" with open("tests/fixtures/stations.json") as file: stations = json.load(file) with open("tests/fixtures/station.json") as file: station = json.load(file) session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload=stations, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/station/sensors/{VALID_STATION_ID}", payload=station, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/672", payload=None, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/658", payload=None, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/660", payload=None, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/665", payload=None, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/667", payload=None, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/670", payload=None, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/14395", payload=None, ) gios = Gios(VALID_STATION_ID, session) try: await gios.async_update() except InvalidSensorsData as error: assert str(error.status) == "Invalid sensor data from GIOS API" await session.close()
def __init__(self, session, station_id, **kwargs): """Initialize.""" self._gios = Gios(station_id, session) self.station_id = station_id self.sensors = {} self.latitude = None self.longitude = None self.station_name = None self.available = True self.async_update = Throttle(kwargs[CONF_SCAN_INTERVAL])( self._async_update)
async def test_no_stations_data(): """Test with no stations data.""" stations = {} with patch("gios.Gios._async_get", return_value=stations), pytest.raises(ApiError) as error: async with ClientSession() as websession: gios = Gios(VALID_STATION_ID, websession) await gios.update() assert str(error.value) == "Invalid measuring stations list from GIOS API" assert gios.available is False
async def test_invalid_station_id(): """Test with invalid station_id.""" with open("tests/data/stations.json") as file: stations = json.load(file) with patch("gios.Gios._get_stations", return_value=stations), pytest.raises(NoStationError) as error: async with ClientSession() as websession: gios = Gios(INVALID_STATION_ID, websession) await gios.update() assert str(error.value) == "0 is not a valid measuring station ID" assert gios.available is False
async def test_no_station_data(): """Test with no station data.""" with open("tests/data/stations.json") as file: stations = json.load(file) station = {} with patch("gios.Gios._get_stations", return_value=stations), patch( "gios.Gios._get_station", return_value=station), pytest.raises(InvalidSensorsData) as error: async with ClientSession() as websession: gios = Gios(VALID_STATION_ID, websession) await gios.update() assert str(error.value) == "Invalid measuring station data from GIOS API"
async def test_api_error(): """Test GIOS API error.""" session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", status=404, ) gios = Gios(VALID_STATION_ID, session) try: await gios.async_update() except ApiError as error: assert str(error.status) == "404" await session.close()
async def test_valid_data_second_value(): """Test with valid data and valid second sensor's value.""" with open("tests/data/stations.json") as file: stations = json.load(file) with open("tests/data/station.json") as file: station = json.load(file) with open("tests/data/sensors_valid_second_values.json") as file: sensors = json.load(file) with open("tests/data/indexes.json") as file: indexes = json.load(file) with patch("gios.Gios._get_stations", return_value=stations), patch("gios.Gios._get_station", return_value=station), patch( "gios.Gios._get_all_sensors", return_value=sensors), patch( "gios.Gios._get_indexes", return_value=indexes): async with ClientSession() as websession: gios = Gios(VALID_STATION_ID, websession) await gios.update() assert gios.station_name == VALID_STATION_NAME assert gios.station_id == VALID_STATION_ID assert gios.latitude == VALID_LATITUDE assert gios.longitude == VALID_LONGITUDE assert len(gios.data) == 8 assert gios.available is True assert gios.data["SO2"]["value"] == 4.25478 assert gios.data["SO2"]["index"] == "very good" assert gios.data["C6H6"]["value"] == 0.22789 assert gios.data["C6H6"]["index"] == "very good" assert gios.data["CO"]["value"] == 250.874 assert gios.data["CO"]["index"] == "very good" assert gios.data["NO2"]["value"] == 7.33411 assert gios.data["NO2"]["index"] == "very good" assert gios.data["O3"]["value"] == 93.7768 assert gios.data["O3"]["index"] == "good" assert gios.data["PM2.5"]["value"] == 4.21464 assert gios.data["PM2.5"]["index"] == "very good" assert gios.data["PM10"]["value"] == 17.8344 assert gios.data["PM10"]["index"] == "very good" assert gios.data["AQI"]["value"] == "good"
async def main(): try: async with ClientSession() as websession: gios = Gios(GIOS_STATION_ID, websession) data = await gios.async_update() except (ApiError, NoStationError, InvalidSensorsData, ClientError) as error: print(f"{error}") return latitude = gios.latitude longitude = gios.longitude station_name = gios.station_name print(f"Longitude: {longitude}") print(f"Latitude: {latitude}") print(f"Station name: {station_name}") print(data)
async def test_no_stations_data(): """Test with no stations data.""" session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload={}, ) gios = Gios(VALID_STATION_ID, session) try: await gios.async_update() except ApiError as error: assert str(error.status ) == "Invalid measuring stations list from GIOS API" await session.close()
async def async_step_user(self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Handle a flow initialized by the user.""" errors = {} if user_input is not None: try: await self.async_set_unique_id(str( user_input[CONF_STATION_ID]), raise_on_progress=False) self._abort_if_unique_id_configured() websession = async_get_clientsession(self.hass) with timeout(API_TIMEOUT): gios = Gios(user_input[CONF_STATION_ID], websession) await gios.async_update() assert gios.station_name is not None return self.async_create_entry( title=gios.station_name, data=user_input, ) except (ApiError, ClientConnectorError, asyncio.TimeoutError): errors["base"] = "cannot_connect" except NoStationError: errors[CONF_STATION_ID] = "wrong_station_id" except InvalidSensorsData: errors[CONF_STATION_ID] = "invalid_sensors_data" return self.async_show_form( step_id="user", data_schema=vol.Schema( { vol.Required(CONF_STATION_ID): int, vol.Optional(CONF_NAME, default=self.hass.config.location_name): str, }), errors=errors, )
async def test_invalid_station_id(): """Test with invalid station_id.""" with open("tests/fixtures/stations.json") as file: stations = json.load(file) session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload=stations, ) gios = Gios(INVALID_STATION_ID, session) try: await gios.async_update() except NoStationError as error: assert str(error.status) == "0 is not a valid measuring station ID" await session.close()
async def test_no_station_data(): """Test with no station data.""" with open("tests/fixtures/stations.json") as file: stations = json.load(file) session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload=stations, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/station/sensors/{VALID_STATION_ID}", payload={}, ) gios = Gios(VALID_STATION_ID, session) try: await gios.async_update() except InvalidSensorsData as error: assert str( error.status) == "Invalid measuring station data from GIOS API" await session.close()
async def test_valid_data_second_value(): # pylint:disable=too-many-statements """Test with valid data and valid second sensor's value.""" with open("tests/data/stations.json") as file: stations = json.load(file) with open("tests/data/station.json") as file: station = json.load(file) with open("tests/data/sensor_658.json") as file: sensor_658 = json.load(file) with open("tests/data/sensor_660.json") as file: sensor_660 = json.load(file) with open("tests/data/sensor_665.json") as file: sensor_665 = json.load(file) with open("tests/data/sensor_667.json") as file: sensor_667 = json.load(file) with open("tests/data/sensor_670.json") as file: sensor_670 = json.load(file) with open("tests/data/sensor_672.json") as file: sensor_672 = json.load(file) with open("tests/data/sensor_14395.json") as file: sensor_14395 = json.load(file) with open("tests/data/indexes.json") as file: indexes = json.load(file) sensor_658["values"][0]["value"] = None sensor_660["values"][0]["value"] = None sensor_665["values"][0]["value"] = None sensor_667["values"][0]["value"] = None sensor_670["values"][0]["value"] = None sensor_672["values"][0]["value"] = None sensor_14395["values"][0]["value"] = None session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload=stations, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/station/sensors/{VALID_STATION_ID}", payload=station, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/672", payload=sensor_672, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/658", payload=sensor_658, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/660", payload=sensor_660, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/665", payload=sensor_665, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/667", payload=sensor_667, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/670", payload=sensor_670, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/14395", payload=sensor_14395, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/aqindex/getIndex/{VALID_STATION_ID}", payload=indexes, ) gios = Gios(VALID_STATION_ID, session) await gios.update() await session.close() assert gios.station_name == VALID_STATION_NAME assert gios.station_id == VALID_STATION_ID assert gios.latitude == VALID_LATITUDE assert gios.longitude == VALID_LONGITUDE assert len(gios.data) == 8 assert gios.data["SO2"]["value"] == 11.501 assert gios.data["SO2"]["index"] == "very good" assert gios.data["C6H6"]["value"] == 3.24432 assert gios.data["C6H6"]["index"] == "very good" assert gios.data["CO"]["value"] == 1041.74 assert gios.data["CO"]["index"] == "very good" assert gios.data["NO2"]["value"] == 52.6198 assert gios.data["NO2"]["index"] == "very good" assert gios.data["O3"]["value"] == 4.93778 assert gios.data["O3"]["index"] == "good" assert gios.data["PM2.5"]["value"] == 72.0243 assert gios.data["PM2.5"]["index"] == "very good" assert gios.data["PM10"]["value"] == 115.559 assert gios.data["PM10"]["index"] == "very good" assert gios.data["AQI"]["value"] == "good"
async def test_no_sensor_data_1(): # pylint:disable=too-many-statements """Test with no sensor data.""" with open("tests/fixtures/stations.json") as file: stations = json.load(file) with open("tests/fixtures/station.json") as file: station = json.load(file) with open("tests/fixtures/sensor_658.json") as file: sensor_658 = json.load(file) with open("tests/fixtures/sensor_660.json") as file: sensor_660 = json.load(file) with open("tests/fixtures/sensor_665.json") as file: sensor_665 = json.load(file) with open("tests/fixtures/sensor_667.json") as file: sensor_667 = json.load(file) with open("tests/fixtures/sensor_670.json") as file: sensor_670 = json.load(file) with open("tests/fixtures/sensor_672.json") as file: sensor_672 = json.load(file) with open("tests/fixtures/sensor_14395.json") as file: sensor_14395 = json.load(file) with open("tests/fixtures/indexes.json") as file: indexes = json.load(file) sensor_658["values"][0]["value"] = None sensor_658["values"][1]["value"] = None sensor_660["values"][0]["value"] = None sensor_660["values"][1]["value"] = None sensor_665["values"][0]["value"] = None sensor_665["values"][1]["value"] = None sensor_667["values"][0]["value"] = None sensor_667["values"][1]["value"] = None sensor_670["values"][0]["value"] = None sensor_670["values"][1]["value"] = None sensor_672["values"][0]["value"] = None sensor_672["values"][1]["value"] = None sensor_14395["values"][0]["value"] = None sensor_14395["values"][1]["value"] = None session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload=stations, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/station/sensors/{VALID_STATION_ID}", payload=station, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/672", payload=sensor_672, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/658", payload=sensor_658, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/660", payload=sensor_660, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/665", payload=sensor_665, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/667", payload=sensor_667, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/670", payload=sensor_670, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/14395", payload=sensor_14395, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/aqindex/getIndex/{VALID_STATION_ID}", payload=indexes, ) gios = Gios(VALID_STATION_ID, session) try: await gios.async_update() except InvalidSensorsData as error: assert str(error.status) == "Invalid sensor data from GIOS API" await session.close()
async def test_no_indexes_data(): """Test with valid data.""" with open("tests/fixtures/stations.json") as file: stations = json.load(file) with open("tests/fixtures/station.json") as file: station = json.load(file) with open("tests/fixtures/sensor_658.json") as file: sensor_658 = json.load(file) with open("tests/fixtures/sensor_660.json") as file: sensor_660 = json.load(file) with open("tests/fixtures/sensor_665.json") as file: sensor_665 = json.load(file) with open("tests/fixtures/sensor_667.json") as file: sensor_667 = json.load(file) with open("tests/fixtures/sensor_670.json") as file: sensor_670 = json.load(file) with open("tests/fixtures/sensor_672.json") as file: sensor_672 = json.load(file) with open("tests/fixtures/sensor_14395.json") as file: sensor_14395 = json.load(file) session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload=stations, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/station/sensors/{VALID_STATION_ID}", payload=station, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/672", payload=sensor_672, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/658", payload=sensor_658, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/660", payload=sensor_660, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/665", payload=sensor_665, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/667", payload=sensor_667, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/670", payload=sensor_670, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/14395", payload=sensor_14395, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/aqindex/getIndex/{VALID_STATION_ID}", payload={}, ) gios = Gios(VALID_STATION_ID, session) try: await gios.async_update() except InvalidSensorsData as error: assert str(error.status) == "Invalid index data from GIOS API" await session.close()
async def test_valid_data_first_value(): # pylint:disable=too-many-statements """Test with valid data and valid first sensor's value.""" with open("tests/fixtures/stations.json") as file: stations = json.load(file) with open("tests/fixtures/station.json") as file: station = json.load(file) with open("tests/fixtures/sensor_658.json") as file: sensor_658 = json.load(file) with open("tests/fixtures/sensor_660.json") as file: sensor_660 = json.load(file) with open("tests/fixtures/sensor_665.json") as file: sensor_665 = json.load(file) with open("tests/fixtures/sensor_667.json") as file: sensor_667 = json.load(file) with open("tests/fixtures/sensor_670.json") as file: sensor_670 = json.load(file) with open("tests/fixtures/sensor_672.json") as file: sensor_672 = json.load(file) with open("tests/fixtures/sensor_14395.json") as file: sensor_14395 = json.load(file) with open("tests/fixtures/indexes.json") as file: indexes = json.load(file) session = aiohttp.ClientSession() with aioresponses() as session_mock: session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/station/findAll", payload=stations, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/station/sensors/{VALID_STATION_ID}", payload=station, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/672", payload=sensor_672, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/658", payload=sensor_658, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/660", payload=sensor_660, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/665", payload=sensor_665, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/667", payload=sensor_667, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/670", payload=sensor_670, ) session_mock.get( "http://api.gios.gov.pl/pjp-api/rest/data/getData/14395", payload=sensor_14395, ) session_mock.get( f"http://api.gios.gov.pl/pjp-api/rest/aqindex/getIndex/{VALID_STATION_ID}", payload=indexes, ) gios = Gios(VALID_STATION_ID, session) data = await gios.async_update() await session.close() assert gios.station_name == VALID_STATION_NAME assert gios.station_id == VALID_STATION_ID assert gios.latitude == VALID_LATITUDE assert gios.longitude == VALID_LONGITUDE assert len(data) == 8 assert data["so2"]["value"] == 11.6502 assert data["so2"]["index"] == "very good" assert data["c6h6"]["value"] == 2.57148 assert data["c6h6"]["index"] == "very good" assert data["co"]["value"] == 786.702 assert data["co"]["index"] == "very good" assert data["no2"]["value"] == 59.9545 assert data["no2"]["index"] == "very good" assert data["o3"]["value"] == 8.63111 assert data["o3"]["index"] == "good" assert data["pm2.5"]["value"] == 59.9428 assert data["pm2.5"]["index"] == "very good" assert data["pm10"]["value"] == 123.879 assert data["pm10"]["index"] == "very good" assert data["aqi"]["value"] == "good"