|
9 | 9 | # To run: pytest custom_components/enpal_webparser/tests/test_entity_factory.py |
10 | 10 | # |
11 | 11 |
|
| 12 | +import pytest |
| 13 | +from datetime import timedelta |
| 14 | + |
| 15 | +from unittest.mock import AsyncMock |
| 16 | + |
12 | 17 | from homeassistant.helpers.update_coordinator import DataUpdateCoordinator |
| 18 | +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator |
| 19 | +from homeassistant.core import HomeAssistant |
13 | 20 | from custom_components.enpal_webparser.entity_factory import ( |
14 | 21 | build_sensor_entity, |
15 | 22 | EnpalBaseSensor, |
@@ -78,3 +85,50 @@ def test_sensor_device_info(): |
78 | 85 | assert device_info.get("manufacturer") == "Enpal" |
79 | 86 | assert device_info.get("model") == "Webparser" |
80 | 87 |
|
| 88 | +@pytest.fixture |
| 89 | +def hass(): |
| 90 | + """Return a mocked hass instance.""" |
| 91 | + hass = AsyncMock(spec=HomeAssistant) |
| 92 | + return hass |
| 93 | + |
| 94 | +@pytest.fixture |
| 95 | +def mock_sensor_dict(): |
| 96 | + return { |
| 97 | + "name": "Test Sensor", |
| 98 | + "value": 123.45, |
| 99 | + "unit": "kWh", |
| 100 | + "device_class": "energy", |
| 101 | + "enpal_last_update": "2024-06-01T12:00:00", |
| 102 | + } |
| 103 | + |
| 104 | +@pytest.fixture |
| 105 | +def hass_coordinator(hass: HomeAssistant): |
| 106 | + async def _update_method(): |
| 107 | + return [{ |
| 108 | + "name": "Test Sensor", |
| 109 | + "value": 987.65, |
| 110 | + "unit": "kWh", |
| 111 | + "device_class": "energy", |
| 112 | + "enpal_last_update": "2024-06-01T12:30:00", |
| 113 | + }] |
| 114 | + coordinator = DataUpdateCoordinator( |
| 115 | + hass, |
| 116 | + logger=None, |
| 117 | + name="test", |
| 118 | + update_method=_update_method, |
| 119 | + update_interval=timedelta(seconds=30), |
| 120 | + ) |
| 121 | + return coordinator |
| 122 | + |
| 123 | +@pytest.mark.asyncio |
| 124 | +async def test_build_energy_sensor_full(hass: HomeAssistant, mock_sensor_dict, hass_coordinator): |
| 125 | + sensor = build_sensor_entity(mock_sensor_dict, hass_coordinator) |
| 126 | + assert isinstance(sensor, EnpalEnergySensor) |
| 127 | + assert sensor.name == "Test Sensor" |
| 128 | + assert sensor.native_value == 123.45 |
| 129 | + assert sensor.native_unit_of_measurement == "kWh" |
| 130 | + assert sensor.device_class == "energy" |
| 131 | + assert sensor.state_class == "total_increasing" |
| 132 | + assert "enpal_last_update" in sensor.extra_state_attributes |
| 133 | + |
| 134 | + |
0 commit comments