77 lines
2.4 KiB
Python
77 lines
2.4 KiB
Python
"""Tests for the countdown phase of the cycle."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
|
|
|
from custom_components.main_gate_controller.const import (
|
|
ATTR_DURATION,
|
|
ATTR_REMAINING_SECONDS,
|
|
ATTR_RUNNING,
|
|
DOMAIN,
|
|
GATE_STATE_OPEN,
|
|
)
|
|
from tests.conftest import make_entry_data
|
|
|
|
|
|
async def test_countdown_attributes_decrease(hass: HomeAssistant, service_calls):
|
|
"""remaining_seconds decreases over the open phase and finishes at zero."""
|
|
switch = "switch.fast_gate_relay"
|
|
hass.states.async_set(switch, "off")
|
|
|
|
entry = MockConfigEntry(
|
|
domain=DOMAIN,
|
|
title="Main Gate",
|
|
data=make_entry_data(
|
|
switch_entity_id=switch,
|
|
name="Main Gate",
|
|
open_duration=2,
|
|
pulse_duration=0.1,
|
|
notify_enabled=False,
|
|
notify_entities=[],
|
|
),
|
|
unique_id=switch,
|
|
)
|
|
entry.add_to_hass(hass)
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
|
|
controller = entry.runtime_data
|
|
|
|
# Manually step into the open phase and capture attribute changes.
|
|
assert controller.async_trigger() is True
|
|
|
|
# Wait until the cycle is in the ``open`` state.
|
|
while controller.status != GATE_STATE_OPEN:
|
|
await asyncio.sleep(0.05)
|
|
snapshot1 = controller.extra_state_attributes.copy()
|
|
assert snapshot1[ATTR_DURATION] == 2
|
|
assert snapshot1[ATTR_RUNNING] is True
|
|
first_remaining = snapshot1[ATTR_REMAINING_SECONDS]
|
|
assert first_remaining is not None and first_remaining >= 1
|
|
|
|
# Let the countdown progress for ~1 second before re-checking.
|
|
await asyncio.sleep(1.0)
|
|
snapshot2 = controller.extra_state_attributes.copy()
|
|
assert snapshot2[ATTR_REMAINING_SECONDS] < first_remaining
|
|
assert snapshot2[ATTR_RUNNING] is True
|
|
|
|
# Wait for completion.
|
|
await controller.async_wait_until_idle()
|
|
|
|
after = controller.extra_state_attributes
|
|
assert after[ATTR_RUNNING] is False
|
|
assert after[ATTR_REMAINING_SECONDS] is None
|
|
|
|
|
|
async def test_duration_reflects_configured_value(
|
|
hass: HomeAssistant, loaded_entry
|
|
):
|
|
"""The ``duration`` attribute follows the configuration value."""
|
|
controller = loaded_entry.runtime_data
|
|
attrs = controller.extra_state_attributes
|
|
assert attrs[ATTR_DURATION] == 20
|