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
13 changes: 8 additions & 5 deletions iometer/reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ def to_json(self) -> str:
}
)

def get_total_consumption(self) -> float:
def get_total_consumption(self) -> float | None:
"""Get total consumption in Wh."""
register = self.meter.reading.get_register_by_obis(self.TOTAL_CONSUMPTION_OBIS)
return register.value if register else 0
return register.value if register else None

def get_total_production(self) -> float:
"""Get total production in Wh."""
def get_total_production(self) -> float | None:
"""Get total production in Wh.

Returns None if OBIS is not found, otherwise the value in Wh as float.
"""
register = self.meter.reading.get_register_by_obis(self.TOTAL_PRODUCTION_OBIS)
return register.value if register else 0
return register.value if register else None

def get_current_power(self) -> float | None:
"""Get current power consumption in W.
Expand Down
50 changes: 44 additions & 6 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def reading_alt_obis_json_fixture():
"reading": {
"time": "2024-11-11T11:11:11Z",
"registers": [
{"obis": "01-00:01.08.00*ff", "value": 1111.1, "unit": "Wh"},
{"obis": "01-00:02.08.00*ff", "value": 2222.2, "unit": "Wh"},
{"obis": "01-00:24.07.00*ff", "value": 200, "unit": "W"},
{"obis": "01-00:01.08.00*ff", "value": 1234.5, "unit": "Wh"},
{"obis": "01-00:02.08.00*ff", "value": 5432.1, "unit": "Wh"},
{"obis": "01-00:24.07.00*ff", "value": 100, "unit": "W"},
],
},
},
Expand All @@ -60,8 +60,27 @@ def reading_no_power_obis_json_fixture():
"reading": {
"time": "2024-11-11T11:11:11Z",
"registers": [
{"obis": "01-00:01.08.00*ff", "value": 3333.3, "unit": "Wh"},
{"obis": "01-00:02.08.00*ff", "value": 4444.4, "unit": "Wh"},
{"obis": "01-00:01.08.00*ff", "value": 1234.5, "unit": "Wh"},
{"obis": "01-00:02.08.00*ff", "value": 5432.1, "unit": "Wh"},
],
},
},
}


@pytest.fixture(name="reading_no_power_no_production_obis_json")
def reading_no_power_no_production_obis_json_fixture():
"""Fixture reading response without any current power OBIS
and production OBIS register.
"""
return {
"__typename": "iometer.reading.v1",
"meter": {
"number": "1ISK0000000000",
"reading": {
"time": "2024-11-11T11:11:11Z",
"registers": [
{"obis": "01-00:01.08.00*ff", "value": 1234.5, "unit": "Wh"}
],
},
},
Expand Down Expand Up @@ -231,7 +250,7 @@ async def test_get_current_reading_alt_obis(

reading = await client_iometer.get_current_reading()
assert isinstance(reading, Reading)
assert reading.get_current_power() == 200
assert reading.get_current_power() == 100


@pytest.mark.asyncio
Expand All @@ -245,6 +264,25 @@ async def test_get_current_reading_no_power_obis(
reading = await client_iometer.get_current_reading()
assert isinstance(reading, Reading)
assert reading.get_current_power() is None
assert reading.get_total_production() == 5432.1
assert reading.get_total_consumption() == 1234.5


@pytest.mark.asyncio
async def test_get_current_reading_no_power_no_production_obis(
client_iometer, mock_aioresponse, reading_no_power_no_production_obis_json
):
"""Test current power returns None when no power OBIS registers are present."""
mock_endpoint = f"http://{HOST}/v1/reading"
mock_aioresponse.get(
mock_endpoint, status=200, payload=reading_no_power_no_production_obis_json
)

reading = await client_iometer.get_current_reading()
assert isinstance(reading, Reading)
assert reading.get_current_power() is None
assert reading.get_total_production() is None
assert reading.get_total_consumption() == 1234.5


@pytest.mark.asyncio
Expand Down