Skip to content

Commit

Permalink
Reapply hotfix 'invalid tariff'
Browse files Browse the repository at this point in the history
  • Loading branch information
DCSBL committed Jan 7, 2025
1 parent 8c597e3 commit e20c1cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions homewizard_energy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions tests/v1/test_v1_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down
8 changes: 8 additions & 0 deletions tests/v2/test_v2_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down

0 comments on commit e20c1cb

Please sign in to comment.