From e20c1cb1ac3922613e8b6efcb39c3b9c0476a28a Mon Sep 17 00:00:00 2001 From: Duco Sebel <74970928+DCSBL@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:42:26 +0100 Subject: [PATCH] Reapply hotfix 'invalid tariff' --- homewizard_energy/models.py | 11 +++++++++++ tests/v1/test_v1_models.py | 8 ++++++++ tests/v2/test_v2_models.py | 8 ++++++++ 3 files changed, 27 insertions(+) diff --git a/homewizard_energy/models.py b/homewizard_energy/models.py index 7ba32c2..e0738c3 100644 --- a/homewizard_energy/models.py +++ b/homewizard_energy/models.py @@ -432,6 +432,17 @@ def __pre_deserialize__(cls, d: dict[Any, Any]) -> dict[Any, Any]: return d + @classmethod + def __post_deserialize__(cls, obj: Measurement) -> Measurement: + """Post deserialize hook for Measurement object.""" + _ = cls # Unused + + # Some smart meters report a tariff other than 1, 2, 3 or 4, which is invalid + if obj.tariff not in (1, 2, 3, 4): + obj.tariff = None + + return obj + @dataclass(kw_only=True) class ExternalDevice(BaseModel): diff --git a/tests/v1/test_v1_models.py b/tests/v1/test_v1_models.py index 293c6c7..43f0eea 100644 --- a/tests/v1/test_v1_models.py +++ b/tests/v1/test_v1_models.py @@ -118,6 +118,14 @@ async def test_data(model: str, fixtures: str, snapshot: SnapshotAssertion): assert snapshot == data +async def test_data_ignores_invalid_tariff(): + """Test Data model ignores invalid tariff.""" + + measurement = Measurement.from_dict({"active_tariff": 5432}) + assert measurement + assert measurement.tariff is None + + @pytest.mark.parametrize( ("model", "fixtures"), [ diff --git a/tests/v2/test_v2_models.py b/tests/v2/test_v2_models.py index c49a827..3178a18 100644 --- a/tests/v2/test_v2_models.py +++ b/tests/v2/test_v2_models.py @@ -59,6 +59,14 @@ async def test_measurement(model: str, fixtures: str, snapshot: SnapshotAssertio assert snapshot == data +async def test_measurement_ignores_invalid_tariff(): + """Test Measurement model ignores invalid tariff.""" + + measurement = Measurement.from_dict({"tariff": 5432}) + assert measurement + assert measurement.tariff is None + + @pytest.mark.parametrize( ("model", "fixtures"), [