Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/tadoasync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
TadoBadRequestError,
TadoConnectionError,
TadoError,
TadoReadingError,
TadoServerError,
)
from .tadoasync import Tado
Expand All @@ -16,4 +17,5 @@
"TadoAuthenticationError",
"TadoServerError",
"TadoBadRequestError",
"TadoReadingError",
]
4 changes: 4 additions & 0 deletions src/tadoasync/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ class TadoBadRequestError(TadoError):

class TadoForbiddenError(TadoError):
"""Tado forbidden exception."""


class TadoReadingError(TadoError):
"""Tado reading exception."""
14 changes: 8 additions & 6 deletions src/tadoasync/tadoasync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from __future__ import annotations

import asyncio
import json
import time
from dataclasses import dataclass
from datetime import datetime, timezone
Expand Down Expand Up @@ -41,6 +40,7 @@
TadoConnectionError,
TadoError,
TadoForbiddenError,
TadoReadingError,
)
from tadoasync.models import (
Capabilities,
Expand Down Expand Up @@ -389,17 +389,19 @@ async def set_child_lock(self, serial_no: str, child_lock: bool | None) -> None:
)

async def set_meter_readings(
self, date: str | None = None, reading: int = 0
) -> str:
self, reading: int, date: datetime | None = None
) -> None:
"""Set the meter readings."""
if date is None:
date = datetime.now(timezone.utc).isoformat()
date = datetime.now(timezone.utc)

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

async def _request(
self,
Expand Down
6 changes: 0 additions & 6 deletions tests/__snapshots__/test_tado.ambr
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# serializer version: 1
# name: test_add_meter_readings_duplicated
'"{\\n \\"code\\": \\"duplicated_meter_reading\\",\\n \\"message\\": \\"reading already exists for date [2024-01-01]\\"\\n}\\n"'
# ---
# name: test_add_meter_readings_success
'"{\\n \\"id\\": \\"12345a6b-7c8d-9e01-2fa3-4b5c67890def\\",\\n \\"homeId\\": 123456,\\n \\"date\\": \\"2024-01-01\\",\\n \\"reading\\": 1234\\n}\\n"'
# ---
# name: test_geofencing_supported
dict({
'presence': 'HOME',
Expand Down
17 changes: 12 additions & 5 deletions tests/test_tado.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asyncio
import os
import time
from datetime import datetime, timezone
from typing import Any
from unittest.mock import AsyncMock, MagicMock, patch

Expand All @@ -18,6 +19,7 @@
TadoBadRequestError,
TadoConnectionError,
TadoError,
TadoReadingError,
)

from syrupy import SnapshotAssertion
Expand Down Expand Up @@ -477,14 +479,14 @@ async def test_set_zone_overlay_success(


async def test_add_meter_readings_success(
python_tado: Tado, responses: aioresponses, snapshot: SnapshotAssertion
python_tado: Tado, responses: aioresponses
) -> None:
"""Test adding meter readings."""
responses.post(
TADO_EIQ_URL,
body=load_fixture(folder="meter", filename="add_reading_success.json"),
)
assert await python_tado.set_meter_readings(reading=5) == snapshot
await python_tado.set_meter_readings(5)


async def test_set_child_lock(python_tado: Tado, responses: aioresponses) -> None:
Expand All @@ -511,16 +513,21 @@ async def test_set_child_lock(python_tado: Tado, responses: aioresponses) -> Non


async def test_add_meter_readings_duplicated(
python_tado: Tado, responses: aioresponses, snapshot: SnapshotAssertion
python_tado: Tado, responses: aioresponses
) -> None:
"""Test adding meter readings with duplicate."""
date = "2021-01-01"
date = datetime(2023, 10, 1, 12, 0, 0, tzinfo=timezone.utc)
reading = 5
responses.post(
TADO_EIQ_URL,
body=load_fixture(folder="meter", filename="add_reading_duplicate.json"),
)
assert await python_tado.set_meter_readings(date, reading) == snapshot
with pytest.raises(
TadoReadingError,
match="Error setting meter reading: "
"reading already exists for date \\[2024-01-01\\]",
):
await python_tado.set_meter_readings(reading, date)


async def test_request_client_response_error(python_tado: Tado) -> None:
Expand Down
Loading