|
| 1 | +"""Tests for handling of 'Undefined' variable values from the Control4 Director.""" |
| 2 | + |
| 3 | +import json |
| 4 | +from unittest.mock import AsyncMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from pyControl4.director import C4Director |
| 9 | +from pyControl4.light import C4Light |
| 10 | +from pyControl4.blind import C4Blind |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def director(): |
| 15 | + """Create a C4Director with a mocked session.""" |
| 16 | + return C4Director("192.168.1.1", "test-token") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.asyncio |
| 20 | +async def test_get_item_variable_value_undefined(director): |
| 21 | + """Test that getItemVariableValue normalizes 'Undefined' to None.""" |
| 22 | + response = json.dumps([{"id": 123, "varName": "HUMIDITY", "value": "Undefined"}]) |
| 23 | + with patch.object(director, "sendGetRequest", new=AsyncMock(return_value=response)): |
| 24 | + result = await director.getItemVariableValue(123, "HUMIDITY") |
| 25 | + assert result is None |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +async def test_get_all_item_variable_value_undefined(director): |
| 30 | + """Test that getAllItemVariableValue normalizes 'Undefined' to None in items.""" |
| 31 | + response = json.dumps( |
| 32 | + [ |
| 33 | + {"id": 100, "varName": "HUMIDITY", "value": "Undefined"}, |
| 34 | + {"id": 100, "varName": "TEMPERATURE_F", "value": 72.5}, |
| 35 | + {"id": 200, "varName": "HUMIDITY", "value": 45}, |
| 36 | + ] |
| 37 | + ) |
| 38 | + with patch.object(director, "sendGetRequest", new=AsyncMock(return_value=response)): |
| 39 | + result = await director.getAllItemVariableValue("HUMIDITY,TEMPERATURE_F") |
| 40 | + assert result[0]["value"] is None |
| 41 | + assert result[1]["value"] == 72.5 |
| 42 | + assert result[2]["value"] == 45 |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.asyncio |
| 46 | +async def test_light_get_level_undefined(director): |
| 47 | + """Test that int callers propagate None instead of crashing.""" |
| 48 | + light = C4Light(director, 100) |
| 49 | + response = json.dumps([{"id": 100, "varName": "LIGHT_LEVEL", "value": "Undefined"}]) |
| 50 | + with patch.object(director, "sendGetRequest", new=AsyncMock(return_value=response)): |
| 51 | + result = await light.getLevel() |
| 52 | + assert result is None |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_blind_get_fully_open_undefined(director): |
| 57 | + """Test that bool callers propagate None instead of a misleading value.""" |
| 58 | + blind = C4Blind(director, 200) |
| 59 | + response = json.dumps([{"id": 200, "varName": "Fully Open", "value": "Undefined"}]) |
| 60 | + with patch.object(director, "sendGetRequest", new=AsyncMock(return_value=response)): |
| 61 | + result = await blind.getFullyOpen() |
| 62 | + assert result is None |
0 commit comments