diff --git a/imgw_pib/__init__.py b/imgw_pib/__init__.py index 58c64e1..7291c49 100644 --- a/imgw_pib/__init__.py +++ b/imgw_pib/__init__.py @@ -1,6 +1,7 @@ """Python wrapper for IMGW-PIB API.""" import logging +from datetime import UTC, datetime from http import HTTPStatus from typing import Any, Self @@ -248,16 +249,25 @@ def _parse_hydrological_data(self: Self, data: dict[str, Any]) -> HydrologicalDa if self._alarm_water_level is not None else None, ) - water_temperature = data[ApiNames.WATER_TEMPERATURE] + + water_temperature_measurement_date = get_datetime( + data[ApiNames.WATER_TEMPERATURE_MEASUREMENT_DATE], + "%Y-%m-%d %H:%M:%S", + ) + if ( + water_temperature_measurement_date is not None + and water_temperature_measurement_date.date() == datetime.now(tz=UTC).date() + ): + water_temperature = data[ApiNames.WATER_TEMPERATURE] + else: + water_temperature_measurement_date = None + water_temperature = None + water_temperature_sensor = SensorData( name="Water Temperature", value=float(water_temperature) if water_temperature is not None else None, unit=Units.CELSIUS.value if water_temperature is not None else None, ) - water_temperature_measurement_date = get_datetime( - data[ApiNames.WATER_TEMPERATURE_MEASUREMENT_DATE], - "%Y-%m-%d %H:%M:%S", - ) return HydrologicalData( water_level=water_level_sensor, diff --git a/requirements-test.txt b/requirements-test.txt index 442d3a0..7ce7205 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,5 +1,6 @@ aioresponses==0.7.6 coverage==7.5.3 +freezegun==1.5.1 mypy==1.10.0 pytest-asyncio==0.23.7 pytest-cov==5.0.0 diff --git a/setup.py b/setup.py index 6e03df2..78a48d9 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ PROJECT_DIR = Path(__file__).parent.resolve() README_FILE = PROJECT_DIR / "README.md" -VERSION = "1.0.2" +VERSION = "1.0.3" setup( name="imgw_pib", diff --git a/tests/snapshots/test_init.ambr b/tests/snapshots/test_init.ambr index eaef270..3dd7ca5 100644 --- a/tests/snapshots/test_init.ambr +++ b/tests/snapshots/test_init.ambr @@ -1,6 +1,6 @@ # serializer version: 1 # name: test_hydrological_station - HydrologicalData(water_level=SensorData(name='Water Level', value=568.0, unit='cm'), flood_alarm_level=SensorData(name='Flood Alarm Level', value=630.0, unit='cm'), flood_warning_level=SensorData(name='Flood Warning Level', value=590.0, unit='cm'), water_temperature=SensorData(name='Water Temperature', value=8.1, unit='°C'), river='Zalew Wiślany', station_id='154190050', station='Nowe Batorowo', water_level_measurement_date=datetime.datetime(2024, 4, 22, 10, 0, tzinfo=datetime.timezone.utc), water_temperature_measurement_date=datetime.datetime(2024, 4, 22, 10, 10, tzinfo=datetime.timezone.utc), flood_alarm=False, flood_warning=False) + HydrologicalData(water_level=SensorData(name='Water Level', value=568.0, unit='cm'), flood_alarm_level=SensorData(name='Flood Alarm Level', value=630.0, unit='cm'), flood_warning_level=SensorData(name='Flood Warning Level', value=590.0, unit='cm'), water_temperature=SensorData(name='Water Temperature', value=8.1, unit='°C'), river='Zalew Wiślany', station_id='154190050', station='Nowe Batorowo', water_level_measurement_date=FakeDatetime(2024, 4, 22, 10, 0, tzinfo=datetime.timezone.utc), water_temperature_measurement_date=FakeDatetime(2024, 4, 22, 10, 10, tzinfo=datetime.timezone.utc), flood_alarm=False, flood_warning=False) # --- # name: test_hydrological_stations dict({ diff --git a/tests/test_init.py b/tests/test_init.py index 0fb7d46..3c24229 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -1,11 +1,13 @@ """Tests for imgw-pib package.""" +from datetime import UTC, datetime from http import HTTPStatus from typing import Any import aiohttp import pytest from aioresponses import aioresponses +from freezegun import freeze_time from syrupy import SnapshotAssertion from imgw_pib import ImgwPib @@ -17,6 +19,8 @@ from imgw_pib.exceptions import ApiError from imgw_pib.model import ApiNames +TEST_TIME = datetime(2024, 4, 22, 11, 10, 32, tzinfo=UTC) + @pytest.mark.asyncio() async def test_weather_stations( @@ -109,7 +113,7 @@ async def test_hydrological_station( """Test weather station.""" session = aiohttp.ClientSession() - with aioresponses() as session_mock: + with aioresponses() as session_mock, freeze_time(TEST_TIME): session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations) session_mock.get( f"{API_HYDROLOGICAL_ENDPOINT}/id/154190050", payload=hydrological_station @@ -369,3 +373,35 @@ async def test_hydrological_details_is_none( await session.close() assert str(exc_info.value) == "Invalid hydrological details format" + + +@pytest.mark.asyncio() +async def test_water_temperature_not_current( + hydrological_stations: list[dict[str, Any]], + hydrological_station: dict[str, Any], + hydrological_details: dict[str, Any], +) -> None: + """Test water_temperature is not current.""" + session = aiohttp.ClientSession() + + hydrological_station["temperatura_wody_data_pomiaru"] = "2002-01-01 12:00:01" + + with aioresponses() as session_mock, freeze_time(TEST_TIME): + session_mock.get(API_HYDROLOGICAL_ENDPOINT, payload=hydrological_stations) + session_mock.get( + f"{API_HYDROLOGICAL_ENDPOINT}/id/154190050", payload=hydrological_station + ) + session_mock.get( + API_HYDROLOGICAL_DETAILS_ENDPOINT.format( + hydrological_station_id="154190050" + ), + payload=hydrological_details, + ) + + imgwpib = await ImgwPib.create(session, hydrological_station_id="154190050") + result = await imgwpib.get_hydrological_data() + + await session.close() + + assert result.water_temperature.value is None + assert result.water_temperature_measurement_date is None