Skip to content

Commit 8a61bc1

Browse files
authored
Raise an exception when meter reading is not correct (#108)
1 parent cd4fa63 commit 8a61bc1

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

src/tadoasync/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
TadoBadRequestError,
66
TadoConnectionError,
77
TadoError,
8+
TadoReadingError,
89
TadoServerError,
910
)
1011
from .tadoasync import Tado
@@ -16,4 +17,5 @@
1617
"TadoAuthenticationError",
1718
"TadoServerError",
1819
"TadoBadRequestError",
20+
"TadoReadingError",
1921
]

src/tadoasync/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ class TadoBadRequestError(TadoError):
2323

2424
class TadoForbiddenError(TadoError):
2525
"""Tado forbidden exception."""
26+
27+
28+
class TadoReadingError(TadoError):
29+
"""Tado reading exception."""

src/tadoasync/tadoasync.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import asyncio
6-
import json
76
import time
87
from dataclasses import dataclass
98
from datetime import datetime, timezone
@@ -41,6 +40,7 @@
4140
TadoConnectionError,
4241
TadoError,
4342
TadoForbiddenError,
43+
TadoReadingError,
4444
)
4545
from tadoasync.models import (
4646
Capabilities,
@@ -385,17 +385,19 @@ async def set_child_lock(self, serial_no: str, *, child_lock: bool) -> None:
385385
)
386386

387387
async def set_meter_readings(
388-
self, date: str | None = None, reading: int = 0
389-
) -> str:
388+
self, reading: int, date: datetime | None = None
389+
) -> None:
390390
"""Set the meter readings."""
391391
if date is None:
392-
date = datetime.now(timezone.utc).isoformat()
392+
date = datetime.now(timezone.utc)
393393

394-
payload = {"date": date, "reading": reading}
394+
payload = {"date": date.strftime("%Y-%m-%d"), "reading": reading}
395395
response = await self._request(
396396
endpoint=EIQ_HOST_URL, data=payload, method=HttpMethod.POST
397397
)
398-
return json.dumps(response)
398+
data = orjson.loads(response)
399+
if "message" in data:
400+
raise TadoReadingError(f"Error setting meter reading: {data['message']}")
399401

400402
async def _request(
401403
self,

tests/__snapshots__/test_tado.ambr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
# serializer version: 1
2-
# name: test_add_meter_readings_duplicated
3-
'"{\\n \\"code\\": \\"duplicated_meter_reading\\",\\n \\"message\\": \\"reading already exists for date [2024-01-01]\\"\\n}\\n"'
4-
# ---
5-
# name: test_add_meter_readings_success
6-
'"{\\n \\"id\\": \\"12345a6b-7c8d-9e01-2fa3-4b5c67890def\\",\\n \\"homeId\\": 123456,\\n \\"date\\": \\"2024-01-01\\",\\n \\"reading\\": 1234\\n}\\n"'
7-
# ---
82
# name: test_geofencing_supported
93
dict({
104
'presence': 'HOME',

tests/test_tado.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
import os
55
import time
6+
from datetime import datetime, timezone
67
from typing import Any
78
from unittest.mock import AsyncMock, MagicMock, patch
89

@@ -18,6 +19,7 @@
1819
TadoBadRequestError,
1920
TadoConnectionError,
2021
TadoError,
22+
TadoReadingError,
2123
)
2224

2325
from syrupy import SnapshotAssertion
@@ -477,14 +479,14 @@ async def test_set_zone_overlay_success(
477479

478480

479481
async def test_add_meter_readings_success(
480-
python_tado: Tado, responses: aioresponses, snapshot: SnapshotAssertion
482+
python_tado: Tado, responses: aioresponses
481483
) -> None:
482484
"""Test adding meter readings."""
483485
responses.post(
484486
TADO_EIQ_URL,
485487
body=load_fixture(folder="meter", filename="add_reading_success.json"),
486488
)
487-
assert await python_tado.set_meter_readings(reading=5) == snapshot
489+
await python_tado.set_meter_readings(5)
488490

489491

490492
async def test_set_child_lock(python_tado: Tado, responses: aioresponses) -> None:
@@ -500,16 +502,21 @@ async def test_set_child_lock(python_tado: Tado, responses: aioresponses) -> Non
500502

501503

502504
async def test_add_meter_readings_duplicated(
503-
python_tado: Tado, responses: aioresponses, snapshot: SnapshotAssertion
505+
python_tado: Tado, responses: aioresponses
504506
) -> None:
505507
"""Test adding meter readings with duplicate."""
506-
date = "2021-01-01"
508+
date = datetime(2023, 10, 1, 12, 0, 0, tzinfo=timezone.utc)
507509
reading = 5
508510
responses.post(
509511
TADO_EIQ_URL,
510512
body=load_fixture(folder="meter", filename="add_reading_duplicate.json"),
511513
)
512-
assert await python_tado.set_meter_readings(date, reading) == snapshot
514+
with pytest.raises(
515+
TadoReadingError,
516+
match="Error setting meter reading: "
517+
"reading already exists for date \\[2024-01-01\\]",
518+
):
519+
await python_tado.set_meter_readings(reading, date)
513520

514521

515522
async def test_request_client_response_error(python_tado: Tado) -> None:

0 commit comments

Comments
 (0)