Skip to content

Commit bc87516

Browse files
authored
Handle missing production and consumption OBIS codes
This PR adds logic for handling missing production and consumption OBIS codes. Currently the client returns 0 if the registers: "01-00:01.08.00*ff" "01-00:02.08.00*ff" are not present. This behavior is correct, as a user might think that the value is 0, although it is not present. Therefore we add a return value of None, when missing. Additionally tests are added.
1 parent 337c9a9 commit bc87516

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

iometer/reading.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,18 @@ def to_json(self) -> str:
9595
}
9696
)
9797

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

103-
def get_total_production(self) -> float:
104-
"""Get total production in Wh."""
103+
def get_total_production(self) -> float | None:
104+
"""Get total production in Wh.
105+
106+
Returns None if OBIS is not found, otherwise the value in Wh as float.
107+
"""
105108
register = self.meter.reading.get_register_by_obis(self.TOTAL_PRODUCTION_OBIS)
106-
return register.value if register else 0
109+
return register.value if register else None
107110

108111
def get_current_power(self) -> float | None:
109112
"""Get current power consumption in W.

tests/test.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ def reading_alt_obis_json_fixture():
4141
"reading": {
4242
"time": "2024-11-11T11:11:11Z",
4343
"registers": [
44-
{"obis": "01-00:01.08.00*ff", "value": 1111.1, "unit": "Wh"},
45-
{"obis": "01-00:02.08.00*ff", "value": 2222.2, "unit": "Wh"},
46-
{"obis": "01-00:24.07.00*ff", "value": 200, "unit": "W"},
44+
{"obis": "01-00:01.08.00*ff", "value": 1234.5, "unit": "Wh"},
45+
{"obis": "01-00:02.08.00*ff", "value": 5432.1, "unit": "Wh"},
46+
{"obis": "01-00:24.07.00*ff", "value": 100, "unit": "W"},
4747
],
4848
},
4949
},
@@ -60,8 +60,27 @@ def reading_no_power_obis_json_fixture():
6060
"reading": {
6161
"time": "2024-11-11T11:11:11Z",
6262
"registers": [
63-
{"obis": "01-00:01.08.00*ff", "value": 3333.3, "unit": "Wh"},
64-
{"obis": "01-00:02.08.00*ff", "value": 4444.4, "unit": "Wh"},
63+
{"obis": "01-00:01.08.00*ff", "value": 1234.5, "unit": "Wh"},
64+
{"obis": "01-00:02.08.00*ff", "value": 5432.1, "unit": "Wh"},
65+
],
66+
},
67+
},
68+
}
69+
70+
71+
@pytest.fixture(name="reading_no_power_no_production_obis_json")
72+
def reading_no_power_no_production_obis_json_fixture():
73+
"""Fixture reading response without any current power OBIS
74+
and production OBIS register.
75+
"""
76+
return {
77+
"__typename": "iometer.reading.v1",
78+
"meter": {
79+
"number": "1ISK0000000000",
80+
"reading": {
81+
"time": "2024-11-11T11:11:11Z",
82+
"registers": [
83+
{"obis": "01-00:01.08.00*ff", "value": 1234.5, "unit": "Wh"}
6584
],
6685
},
6786
},
@@ -231,7 +250,7 @@ async def test_get_current_reading_alt_obis(
231250

232251
reading = await client_iometer.get_current_reading()
233252
assert isinstance(reading, Reading)
234-
assert reading.get_current_power() == 200
253+
assert reading.get_current_power() == 100
235254

236255

237256
@pytest.mark.asyncio
@@ -245,6 +264,25 @@ async def test_get_current_reading_no_power_obis(
245264
reading = await client_iometer.get_current_reading()
246265
assert isinstance(reading, Reading)
247266
assert reading.get_current_power() is None
267+
assert reading.get_total_production() == 5432.1
268+
assert reading.get_total_consumption() == 1234.5
269+
270+
271+
@pytest.mark.asyncio
272+
async def test_get_current_reading_no_power_no_production_obis(
273+
client_iometer, mock_aioresponse, reading_no_power_no_production_obis_json
274+
):
275+
"""Test current power returns None when no power OBIS registers are present."""
276+
mock_endpoint = f"http://{HOST}/v1/reading"
277+
mock_aioresponse.get(
278+
mock_endpoint, status=200, payload=reading_no_power_no_production_obis_json
279+
)
280+
281+
reading = await client_iometer.get_current_reading()
282+
assert isinstance(reading, Reading)
283+
assert reading.get_current_power() is None
284+
assert reading.get_total_production() is None
285+
assert reading.get_total_consumption() == 1234.5
248286

249287

250288
@pytest.mark.asyncio

0 commit comments

Comments
 (0)