Skip to content

Commit 7ce3989

Browse files
committed
Enhance test setup for LED brightness control by updating dependencies and adding unit tests for state management and API interactions
1 parent b4f4727 commit 7ce3989

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- uses: actions/setup-python@v5
2626
with:
2727
python-version: "3.12"
28-
- name: Install test runner
29-
run: python -m pip install --upgrade pip pytest
28+
- name: Install test dependencies
29+
run: python -m pip install --upgrade pip && python -m pip install -r requirements_test.txt
3030
- name: Run unit tests
3131
run: pytest

requirements_test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pytest
22
pytest-homeassistant-custom-component
3+
websockets
4+
aiofiles
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import logging
2+
from dataclasses import dataclass
3+
from unittest.mock import AsyncMock
4+
5+
import pytest
6+
7+
from pytest_homeassistant_custom_component.common import MockConfigEntry
8+
9+
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
10+
11+
from custom_components.hypervolt_charger.const import DOMAIN
12+
from custom_components.hypervolt_charger.light import HypervoltLedBrightnessLight
13+
14+
_LOGGER = logging.getLogger(__name__)
15+
16+
17+
@dataclass
18+
class _State:
19+
charger_id: str
20+
led_brightness: float | None
21+
22+
23+
class _Api:
24+
def __init__(self) -> None:
25+
self.websocket_sync = object()
26+
self.set_led_brightness = AsyncMock()
27+
28+
29+
@pytest.mark.asyncio
30+
async def test_async_turn_on_calls_api_with_percent_mapping(hass):
31+
entry = MockConfigEntry(domain=DOMAIN, data={"charger_id": "ABC123"})
32+
entry.add_to_hass(hass)
33+
34+
coordinator: DataUpdateCoordinator[_State] = DataUpdateCoordinator(
35+
hass,
36+
_LOGGER,
37+
name="hypervolt_test",
38+
update_method=AsyncMock(),
39+
config_entry=entry,
40+
)
41+
coordinator.data = _State(charger_id="ABC123", led_brightness=0.0)
42+
coordinator.api = _Api()
43+
44+
entity = HypervoltLedBrightnessLight(coordinator)
45+
entity.hass = hass
46+
entity.async_get_last_state = AsyncMock(return_value=None)
47+
48+
await entity.async_added_to_hass()
49+
50+
# brightness 128/255 -> round(128/255*100)=50%
51+
await entity.async_turn_on(brightness=128)
52+
53+
coordinator.api.set_led_brightness.assert_awaited_once_with(50.0)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import logging
2+
from dataclasses import dataclass
3+
from unittest.mock import AsyncMock, Mock
4+
5+
import pytest
6+
7+
from pytest_homeassistant_custom_component.common import MockConfigEntry
8+
9+
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
10+
11+
from custom_components.hypervolt_charger.const import DOMAIN
12+
from custom_components.hypervolt_charger.light import HypervoltLedBrightnessLight
13+
14+
_LOGGER = logging.getLogger(__name__)
15+
16+
17+
@dataclass
18+
class _State:
19+
charger_id: str
20+
led_brightness: float | None
21+
22+
23+
class _Api:
24+
def __init__(self) -> None:
25+
self.websocket_sync = object()
26+
self.set_led_brightness = AsyncMock()
27+
28+
29+
@pytest.mark.asyncio
30+
async def test_coordinator_push_update_triggers_entity_state_write(hass):
31+
entry = MockConfigEntry(domain=DOMAIN, data={"charger_id": "ABC123"})
32+
entry.add_to_hass(hass)
33+
34+
coordinator: DataUpdateCoordinator[_State] = DataUpdateCoordinator(
35+
hass,
36+
_LOGGER,
37+
name="hypervolt_test",
38+
update_method=AsyncMock(),
39+
config_entry=entry,
40+
)
41+
coordinator.data = _State(charger_id="ABC123", led_brightness=0.0)
42+
coordinator.api = _Api()
43+
44+
entity = HypervoltLedBrightnessLight(coordinator)
45+
entity.hass = hass
46+
entity.async_get_last_state = AsyncMock(return_value=None)
47+
48+
write_mock = Mock()
49+
entity.async_write_ha_state = write_mock
50+
51+
await entity.async_added_to_hass()
52+
53+
# Simulate websocket push -> coordinator updated data
54+
coordinator.async_set_updated_data(_State(charger_id="ABC123", led_brightness=0.5))
55+
56+
assert write_mock.call_count == 1
57+
assert entity.brightness == round(0.5 * 255)
58+
assert entity.is_on is True

0 commit comments

Comments
 (0)