All tests Window Feature Manager ok.

This commit is contained in:
Jean-Marc Collin
2024-12-27 11:53:58 +00:00
parent 18248aee9e
commit c83c772901
4 changed files with 727 additions and 254 deletions

View File

@@ -2,7 +2,7 @@
""" Test the Window management """
import asyncio
import logging
from unittest.mock import patch, call, PropertyMock, AsyncMock, MagicMock
from unittest.mock import patch, call, PropertyMock
from datetime import datetime, timedelta
from custom_components.versatile_thermostat.base_thermostat import BaseThermostat
@@ -10,234 +10,10 @@ from custom_components.versatile_thermostat.thermostat_climate import (
ThermostatOverClimate,
)
from custom_components.versatile_thermostat.feature_window_manager import (
FeatureWindowManager,
)
from .commons import * # pylint: disable=wildcard-import, unused-wildcard-import
logging.getLogger().setLevel(logging.DEBUG)
async def test_window_feature_manager_create(
hass: HomeAssistant,
):
"""Test the FeatureMotionManager class direclty"""
fake_vtherm = MagicMock(spec=BaseThermostat)
type(fake_vtherm).name = PropertyMock(return_value="the name")
# 1. creation
window_manager = FeatureWindowManager(fake_vtherm, hass)
assert window_manager is not None
assert window_manager.is_configured is False
assert window_manager.is_window_auto_configured is False
assert window_manager.is_window_detected is False
assert window_manager.window_state == STATE_UNAVAILABLE
assert window_manager.name == "the name"
assert len(window_manager._active_listener) == 0
custom_attributes = {}
window_manager.add_custom_attributes(custom_attributes)
assert custom_attributes["window_sensor_entity_id"] is None
assert custom_attributes["window_state"] == STATE_UNAVAILABLE
assert custom_attributes["window_auto_state"] == STATE_UNAVAILABLE
assert custom_attributes["is_window_configured"] is False
assert custom_attributes["is_window_auto_configured"] is False
assert custom_attributes["window_delay_sec"] == 0
assert custom_attributes["window_auto_open_threshold"] == 0
assert custom_attributes["window_auto_close_threshold"] == 0
assert custom_attributes["window_auto_max_duration"] == 0
assert custom_attributes["window_action"] is None
@pytest.mark.parametrize(
"use_window_feature, window_sensor_entity_id, window_delay_sec, window_auto_open_threshold, window_auto_close_threshold, window_auto_max_duration, window_action, is_configured, is_auto_configured, window_state, window_auto_state",
[
# fmt: off
( True, "sensor.the_window_sensor", 10, None, None, None, CONF_WINDOW_TURN_OFF, True, False, STATE_UNKNOWN, STATE_UNAVAILABLE ),
( False, "sensor.the_window_sensor", 10, None, None, None, CONF_WINDOW_TURN_OFF, False, False, STATE_UNAVAILABLE, STATE_UNAVAILABLE ),
( True, "sensor.the_window_sensor", 10, None, None, None, CONF_WINDOW_TURN_OFF, True, False, STATE_UNKNOWN, STATE_UNAVAILABLE ),
# delay is missing
( True, "sensor.the_window_sensor", None, None, None, None, CONF_WINDOW_TURN_OFF, False, False, STATE_UNAVAILABLE, STATE_UNAVAILABLE ),
# action is missing -> defaults to TURN_OFF
( True, "sensor.the_window_sensor", 10, None, None, None, None, True, False, STATE_UNKNOWN, STATE_UNAVAILABLE ),
# With Window auto config complete
( True, None, None, 1, 2, 3, CONF_WINDOW_TURN_OFF, True, True, STATE_UNKNOWN, STATE_UNKNOWN ),
# With Window auto config not complete -> missing open threshold but defaults to 0
( True, None, None, None, 2, 3, CONF_WINDOW_TURN_OFF, False, False, STATE_UNAVAILABLE, STATE_UNAVAILABLE ),
# With Window auto config not complete -> missing close threshold
( True, None, None, 1, None, 3, CONF_WINDOW_TURN_OFF, False, False, STATE_UNAVAILABLE, STATE_UNAVAILABLE ),
# With Window auto config not complete -> missing max duration threshold but defaults to 0
( True, None, None, 1, 2, None, CONF_WINDOW_TURN_OFF, False, False, STATE_UNAVAILABLE, STATE_UNAVAILABLE ),
# fmt: on
],
)
async def test_window_feature_manager_post_init(
hass: HomeAssistant,
use_window_feature,
window_sensor_entity_id,
window_delay_sec,
window_auto_open_threshold,
window_auto_close_threshold,
window_auto_max_duration,
window_action,
is_configured,
is_auto_configured,
window_state,
window_auto_state,
):
"""Test the FeatureMotionManager class direclty"""
fake_vtherm = MagicMock(spec=BaseThermostat)
type(fake_vtherm).name = PropertyMock(return_value="the name")
# 1. creation
window_manager = FeatureWindowManager(fake_vtherm, hass)
assert window_manager is not None
# 2. post_init
window_manager.post_init(
{
CONF_USE_WINDOW_FEATURE: use_window_feature,
CONF_WINDOW_SENSOR: window_sensor_entity_id,
CONF_WINDOW_DELAY: window_delay_sec,
CONF_WINDOW_AUTO_OPEN_THRESHOLD: window_auto_open_threshold,
CONF_WINDOW_AUTO_CLOSE_THRESHOLD: window_auto_close_threshold,
CONF_WINDOW_AUTO_MAX_DURATION: window_auto_max_duration,
CONF_WINDOW_ACTION: window_action,
}
)
assert window_manager.is_configured is is_configured
assert window_manager.is_window_auto_configured == is_auto_configured
assert window_manager.window_sensor_entity_id == window_sensor_entity_id
assert window_manager.window_state == window_state
assert window_manager.window_auto_state == window_auto_state
assert window_manager.window_delay_sec == window_delay_sec
assert window_manager.window_auto_open_threshold == window_auto_open_threshold
assert window_manager.window_auto_close_threshold == window_auto_close_threshold
custom_attributes = {}
window_manager.add_custom_attributes(custom_attributes)
assert custom_attributes["window_sensor_entity_id"] == window_sensor_entity_id
assert custom_attributes["window_state"] == window_state
assert custom_attributes["window_auto_state"] == window_auto_state
assert custom_attributes["is_window_bypass"] is False
assert custom_attributes["is_window_configured"] is is_configured
assert custom_attributes["is_window_auto_configured"] is is_auto_configured
assert custom_attributes["is_window_bypass"] is False
assert custom_attributes["window_delay_sec"] is window_delay_sec
assert custom_attributes["window_auto_open_threshold"] is window_auto_open_threshold
assert (
custom_attributes["window_auto_close_threshold"] is window_auto_close_threshold
)
assert custom_attributes["window_auto_max_duration"] is window_auto_max_duration
@pytest.mark.parametrize(
"current_state, new_state, nb_call, window_state, is_window_detected, changed",
[
(STATE_OFF, STATE_ON, 1, STATE_ON, True, True),
(STATE_OFF, STATE_OFF, 0, STATE_OFF, False, False),
],
)
async def test_window_feature_manager_refresh_sensor(
hass: HomeAssistant,
current_state,
new_state, # new state of motion event
nb_call,
window_state,
is_window_detected,
changed,
):
"""Test the FeatureMotionManager class direclty"""
fake_vtherm = MagicMock(spec=BaseThermostat)
type(fake_vtherm).name = PropertyMock(return_value="the name")
type(fake_vtherm).preset_mode = PropertyMock(return_value=PRESET_COMFORT)
# 1. creation
window_manager = FeatureWindowManager(fake_vtherm, hass)
# 2. post_init
window_manager.post_init(
{
CONF_WINDOW_SENSOR: "sensor.the_window_sensor",
CONF_USE_WINDOW_FEATURE: True,
CONF_WINDOW_DELAY: 10,
}
)
# 3. start listening
window_manager.start_listening()
assert window_manager.is_configured is True
assert window_manager.window_state == STATE_UNKNOWN
assert window_manager.window_auto_state == STATE_UNAVAILABLE
assert len(window_manager._active_listener) == 1
# 4. test refresh with the parametrized
# fmt:off
with patch("homeassistant.core.StateMachine.get", return_value=State("sensor.the_motion_sensor", new_state)) as mock_get_state:
# fmt:on
# Configurer les méthodes mockées
fake_vtherm.async_set_hvac_mode = AsyncMock()
fake_vtherm.set_hvac_off_reason = MagicMock()
# force old state for the test
window_manager._window_state = current_state
ret = await window_manager.refresh_state()
assert ret == changed
assert window_manager.is_configured is True
# in the refresh there is no delay
assert window_manager.window_state == new_state
assert mock_get_state.call_count == 1
assert mock_get_state.call_count == 1
assert fake_vtherm.async_set_hvac_mode.call_count == nb_call
assert fake_vtherm.set_hvac_off_reason.call_count == nb_call
if nb_call == 1:
fake_vtherm.async_set_hvac_mode.assert_has_calls(
[
call.async_set_hvac_mode(HVACMode.OFF),
]
)
fake_vtherm.set_hvac_off_reason.assert_has_calls(
[
call.set_hvac_off_reason(HVAC_OFF_REASON_WINDOW_DETECTION),
]
)
fake_vtherm.reset_mock()
# 5. Check custom_attributes
custom_attributes = {}
window_manager.add_custom_attributes(custom_attributes)
assert custom_attributes["window_sensor_entity_id"] == "sensor.the_window_sensor"
assert custom_attributes["window_state"] == new_state
assert custom_attributes["window_auto_state"] == STATE_UNAVAILABLE
assert custom_attributes["is_window_bypass"] is False
assert custom_attributes["is_window_configured"] is True
assert custom_attributes["is_window_auto_configured"] is False
assert custom_attributes["is_window_bypass"] is False
assert custom_attributes["window_delay_sec"] is 10
assert custom_attributes["window_auto_open_threshold"] is None
assert (
custom_attributes["window_auto_close_threshold"] is None
)
assert custom_attributes["window_auto_max_duration"] is None
window_manager.stop_listening()
await hass.async_block_till_done()
@pytest.mark.parametrize("expected_lingering_tasks", [True])
@pytest.mark.parametrize("expected_lingering_timers", [True])
async def test_window_management_time_not_enough(