1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
"""Tests for the IOmeter package."""
import pytest
from aiohttp import ClientResponseError, ClientSession
from aioresponses import aioresponses
from iometer.client import IOmeterClient
from iometer.exceptions import (
IOmeterConnectionError,
IOmeterNoReadingsError,
IOmeterNoStatusError,
IOmeterTimeoutError,
)
from iometer.reading import Reading
from iometer.status import NullMeter, Status
HOST = "192.168.1.100"
@pytest.fixture(name="reading_json")
def reading_json_fixture():
"""Fixture reading response."""
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"},
{"obis": "01-00:02.08.00*ff", "value": 5432.1, "unit": "Wh"},
{"obis": "01-00:10.07.00*ff", "value": 100, "unit": "W"},
],
},
},
}
@pytest.fixture(name="reading_alt_obis_json")
def reading_alt_obis_json_fixture():
"""Fixture reading response with alternate current power OBIS only."""
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"},
{"obis": "01-00:02.08.00*ff", "value": 5432.1, "unit": "Wh"},
{"obis": "01-00:24.07.00*ff", "value": 100, "unit": "W"},
],
},
},
}
@pytest.fixture(name="reading_no_power_obis_json")
def reading_no_power_obis_json_fixture():
"""Fixture reading response without any current power OBIS registers."""
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"},
{"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"}
],
},
},
}
@pytest.fixture(name="status_json")
def status_json_fixture():
""" "Fixture status response"""
return {
"__typename": "iometer.status.v1",
"meter": {
"number": "1ISK0000000000",
},
"device": {
"bridge": {"rssi": -30, "version": "build-65"},
"id": "658c2b34-2017-45f2-a12b-731235f8bb97",
"core": {
"connectionStatus": "connected",
"rssi": -30,
"version": "build-58",
"powerStatus": "battery",
"batteryLevel": 100,
"attachmentStatus": "attached",
"pinStatus": "entered",
},
},
}
@pytest.fixture(name="status_wired_json")
def status_wired_json_fixture():
""" "Fixture status response with wired power"""
return {
"__typename": "iometer.status.v1",
"meter": {
"number": "1ISK0000000000",
},
"device": {
"bridge": {"rssi": -30, "version": "build-65"},
"id": "658c2b34-2017-45f2-a12b-731235f8bb97",
"core": {
"connectionStatus": "connected",
"rssi": -30,
"version": "build-58",
"powerStatus": "wired",
"attachmentStatus": "attached",
"pinStatus": "entered",
},
},
}
@pytest.fixture(name="status_detached_json")
def status_detached_json_fixture():
""" "Fixture status response with detached core"""
return {
"__typename": "iometer.status.v1",
"meter": {
"number": "1ISK0000000000",
},
"device": {
"bridge": {"rssi": -30, "version": "build-65"},
"id": "658c2b34-2017-45f2-a12b-731235f8bb97",
"core": {
"connectionStatus": "connected",
"rssi": -30,
"version": "build-58",
"powerStatus": "wired",
"attachmentStatus": "detached",
},
},
}
@pytest.fixture(name="status_disconnected_json")
def status_disconnected_json_fixture():
""" "Fixture status response with disconnected core"""
return {
"__typename": "iometer.status.v1",
"meter": {
"number": "1ISK0000000000",
},
"device": {
"bridge": {"rssi": -30, "version": "build-65"},
"id": "658c2b34-2017-45f2-a12b-731235f8bb97",
"core": {"connectionStatus": "disconnected"},
},
}
@pytest.fixture(name="status_no_meter_json")
def status_no_meter_json_fixture():
""" "Fixture status response"""
return {
"__typename": "iometer.status.v1",
"device": {
"bridge": {"rssi": -30, "version": "build-65"},
"id": "658c2b34-2017-45f2-a12b-731235f8bb97",
"core": {
"connectionStatus": "connected",
"rssi": -30,
"version": "build-58",
"powerStatus": "battery",
"batteryLevel": 100,
"attachmentStatus": "attached",
},
},
}
@pytest.fixture(name="mock_aioresponse")
def mock_aioresponse_fixture():
""" "Fixture mock session"""
with aioresponses() as m:
yield m
@pytest.fixture(name="client_iometer")
async def client_iometer_fixture():
"""Fixture IOmeter client"""
async with IOmeterClient(HOST) as client:
yield client
@pytest.mark.asyncio
async def test_client_initialization():
"""Test client initialization."""
client = IOmeterClient("test-host")
assert client.host == "test-host"
assert client.request_timeout == 5
assert client.session is None
@pytest.mark.asyncio
async def test_client_context_manager():
"""Test client as context manager."""
async with IOmeterClient("test-host") as client:
assert isinstance(client.session, ClientSession)
assert not client.session.closed # Session should not be closed
# After the context
assert client.session is None # Verify session is closed
@pytest.mark.asyncio
async def test_get_current_reading(client_iometer, mock_aioresponse, reading_json):
"""Test getting current reading."""
mock_endpoint = f"http://{HOST}/v1/reading"
mock_aioresponse.get(mock_endpoint, status=200, payload=reading_json)
reading = await client_iometer.get_current_reading()
assert isinstance(reading, Reading)
assert reading.meter.number == "1ISK0000000000"
assert reading.get_total_consumption() == 1234.5
assert reading.get_total_production() == 5432.1
assert reading.get_current_power() == 100
@pytest.mark.asyncio
async def test_get_current_reading_alt_obis(
client_iometer, mock_aioresponse, reading_alt_obis_json
):
"""Test current power using alternate OBIS when primary is missing."""
mock_endpoint = f"http://{HOST}/v1/reading"
mock_aioresponse.get(mock_endpoint, status=200, payload=reading_alt_obis_json)
reading = await client_iometer.get_current_reading()
assert isinstance(reading, Reading)
assert reading.get_current_power() == 100
@pytest.mark.asyncio
async def test_get_current_reading_no_power_obis(
client_iometer, mock_aioresponse, reading_no_power_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_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() == 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
async def test_get_current_status(client_iometer, mock_aioresponse, status_json):
"""Test getting device status."""
mock_endpoint = f"http://{HOST}/v1/status"
mock_aioresponse.get(mock_endpoint, status=200, payload=status_json)
status = await client_iometer.get_current_status()
assert isinstance(status, Status)
assert status.meter.number == "1ISK0000000000"
assert status.device.bridge.rssi == -30
assert status.device.bridge.version == "build-65"
assert status.device.id == "658c2b34-2017-45f2-a12b-731235f8bb97"
assert status.device.core.connection_status == "connected"
assert status.device.core.rssi == -30
assert status.device.core.version == "build-58"
assert status.device.core.power_status == "battery"
assert status.device.core.battery_level == 100
assert status.device.core.attachment_status == "attached"
assert status.device.core.pin_status == "entered"
@pytest.mark.asyncio
async def test_get_current_status_wired(
client_iometer, mock_aioresponse, status_wired_json
):
"""Test getting device status."""
mock_endpoint = f"http://{HOST}/v1/status"
mock_aioresponse.get(mock_endpoint, status=200, payload=status_wired_json)
status = await client_iometer.get_current_status()
assert isinstance(status, Status)
assert status.meter.number == "1ISK0000000000"
assert status.device.bridge.rssi == -30
assert status.device.bridge.version == "build-65"
assert status.device.id == "658c2b34-2017-45f2-a12b-731235f8bb97"
assert status.device.core.connection_status == "connected"
assert status.device.core.rssi == -30
assert status.device.core.version == "build-58"
assert status.device.core.power_status == "wired"
assert status.device.core.battery_level is None
assert status.device.core.attachment_status == "attached"
assert status.device.core.pin_status == "entered"
@pytest.mark.asyncio
async def test_get_current_status_detached(
client_iometer, mock_aioresponse, status_detached_json
):
"""Test getting device status."""
mock_endpoint = f"http://{HOST}/v1/status"
mock_aioresponse.get(mock_endpoint, status=200, payload=status_detached_json)
status = await client_iometer.get_current_status()
assert isinstance(status, Status)
assert status.meter.number == "1ISK0000000000"
assert status.device.bridge.rssi == -30
assert status.device.bridge.version == "build-65"
assert status.device.id == "658c2b34-2017-45f2-a12b-731235f8bb97"
assert status.device.core.connection_status == "connected"
assert status.device.core.rssi == -30
assert status.device.core.version == "build-58"
assert status.device.core.power_status == "wired"
assert status.device.core.battery_level is None
assert status.device.core.attachment_status == "detached"
assert status.device.core.pin_status is None
@pytest.mark.asyncio
async def test_get_current_status_disconnected(
client_iometer, mock_aioresponse, status_disconnected_json
):
"""Test getting device status."""
mock_endpoint = f"http://{HOST}/v1/status"
mock_aioresponse.get(mock_endpoint, status=200, payload=status_disconnected_json)
status = await client_iometer.get_current_status()
assert isinstance(status, Status)
assert status.meter.number == "1ISK0000000000"
assert status.device.bridge.rssi == -30
assert status.device.bridge.version == "build-65"
assert status.device.id == "658c2b34-2017-45f2-a12b-731235f8bb97"
assert status.device.core.connection_status == "disconnected"
assert status.device.core.rssi is None
assert status.device.core.version is None
assert status.device.core.power_status is None
assert status.device.core.battery_level is None
assert status.device.core.attachment_status is None
assert status.device.core.pin_status is None
@pytest.mark.asyncio
async def test_get_current_status_no_meter(
client_iometer, mock_aioresponse, status_no_meter_json
):
"""Test getting device status."""
mock_endpoint = f"http://{HOST}/v1/status"
mock_aioresponse.get(mock_endpoint, status=200, payload=status_no_meter_json)
status = await client_iometer.get_current_status()
assert isinstance(status, Status)
assert isinstance(status.meter, NullMeter)
assert status.meter.number is None
assert status.device.bridge.rssi == -30
assert status.device.bridge.version == "build-65"
assert status.device.id == "658c2b34-2017-45f2-a12b-731235f8bb97"
assert status.device.core.connection_status == "connected"
assert status.device.core.rssi == -30
assert status.device.core.version == "build-58"
assert status.device.core.power_status == "battery"
assert status.device.core.battery_level == 100
assert status.device.core.attachment_status == "attached"
assert status.device.core.pin_status is None
@pytest.mark.asyncio
async def test_timeout_error(client_iometer, mock_aioresponse):
"""Test handling of timeout errors."""
mock_endpoint = f"http://{HOST}/v1/reading"
mock_aioresponse.get(mock_endpoint, timeout=True)
with pytest.raises(IOmeterTimeoutError, match="Timeout while communicating"):
await client_iometer.get_current_reading()
@pytest.mark.asyncio
async def test_client_error(client_iometer, mock_aioresponse):
"""Test handling of client errors."""
mock_endpoint = f"http://{HOST}/v1/reading"
mock_aioresponse.get(mock_endpoint, exception=ClientResponseError)
with pytest.raises(IOmeterConnectionError, match="Error communicating"):
await client_iometer.get_current_reading()
@pytest.mark.asyncio
async def test_reading_not_found(client_iometer, mock_aioresponse):
"""Test that a 404 on the reading endpoint raises IOmeterNoReadingsError."""
mock_endpoint = f"http://{HOST}/v1/reading"
mock_aioresponse.get(mock_endpoint, status=404)
with pytest.raises(IOmeterNoReadingsError, match="No readings available"):
await client_iometer.get_current_reading()
@pytest.mark.asyncio
async def test_status_not_found(client_iometer, mock_aioresponse):
"""Test that a 404 on the status endpoint raises IOmeterNoStatusError."""
mock_endpoint = f"http://{HOST}/v1/status"
mock_aioresponse.get(mock_endpoint, status=404)
with pytest.raises(IOmeterNoStatusError, match="No status available"):
await client_iometer.get_current_status()
|