326 lines
7.8 KiB
Python
326 lines
7.8 KiB
Python
import asyncio
|
|
import inspect
|
|
from types import SimpleNamespace
|
|
|
|
from app.auto.controller import AutoController
|
|
from app.my_dataclasses import MAX_TARGET_TEMP
|
|
|
|
|
|
class FakeTionController:
|
|
def __init__(self) -> None:
|
|
# Имитируем состояние до ручного изменения:
|
|
# AUTO ранее разрешил нагрев.
|
|
self.heater = True
|
|
self.target_temp = MAX_TARGET_TEMP
|
|
|
|
def heater_on(self) -> None:
|
|
self.heater = True
|
|
|
|
def heater_off(self) -> None:
|
|
self.heater = False
|
|
|
|
def set_target_temperature(
|
|
self,
|
|
temperature: int,
|
|
) -> None:
|
|
self.target_temp = temperature
|
|
|
|
|
|
class FakeTionState:
|
|
def __init__(self) -> None:
|
|
self.heater = True
|
|
self.in_temp = 18
|
|
|
|
|
|
class FakeTionService:
|
|
def __init__(self) -> None:
|
|
self.online = True
|
|
self.state = FakeTionState()
|
|
|
|
self.controller = FakeTionController()
|
|
|
|
async def execute(self, operation):
|
|
result = operation(self.controller)
|
|
|
|
if inspect.isawaitable(result):
|
|
await result
|
|
|
|
|
|
class FakeQingpingState:
|
|
def __init__(self) -> None:
|
|
self.temperature = 25.8
|
|
self.co2 = 900
|
|
|
|
|
|
class FakeQingpingService:
|
|
def __init__(self) -> None:
|
|
self.online = True
|
|
self.state = FakeQingpingState()
|
|
|
|
|
|
class FakeTemperaturePolicy:
|
|
def heater_required(
|
|
self,
|
|
temperature,
|
|
target_temp,
|
|
heater_on,
|
|
) -> bool:
|
|
return temperature < target_temp
|
|
|
|
|
|
class FakeSchedule:
|
|
def __init__(self) -> None:
|
|
self.paused = False
|
|
self.override_active = True
|
|
|
|
# Пользователь во время AUTO
|
|
# изменил только температуру:
|
|
#
|
|
# AUTO target = 25
|
|
# manual override target = 23
|
|
self.override_settings = SimpleNamespace(
|
|
target_temp=23,
|
|
heater=None,
|
|
)
|
|
|
|
def resolve(self, now=None):
|
|
# _process() вызывает resolve() ещё до
|
|
# проверки override_active.
|
|
#
|
|
# Для самого override содержимое resolution
|
|
# использоваться не должно.
|
|
return SimpleNamespace(
|
|
enabled=True,
|
|
auto_active=True,
|
|
auto_fallback_speed=2,
|
|
auto_target_temp=25,
|
|
)
|
|
|
|
|
|
def print_state(
|
|
title: str,
|
|
controller: AutoController,
|
|
tion: FakeTionService,
|
|
qingping: FakeQingpingService,
|
|
) -> None:
|
|
print()
|
|
print(title)
|
|
|
|
print(
|
|
{
|
|
"controller_state": controller._state,
|
|
"reason": controller._reason,
|
|
|
|
"qingping_online": qingping.online,
|
|
"qingping_temp": (
|
|
qingping.state.temperature
|
|
),
|
|
|
|
"manual_enabled": (
|
|
controller.manual_heater_enabled
|
|
),
|
|
"manual_target": (
|
|
controller.manual_target_temperature
|
|
),
|
|
"manual_heating": (
|
|
controller._manual_heating
|
|
),
|
|
|
|
"temperature": controller._temperature,
|
|
"temperature_source": (
|
|
controller._temperature_source
|
|
),
|
|
|
|
"tion_in_temp": tion.state.in_temp,
|
|
"tion_heater": tion.controller.heater,
|
|
"tion_target": (
|
|
tion.controller.target_temp
|
|
),
|
|
}
|
|
)
|
|
|
|
|
|
async def main() -> None:
|
|
schedule = FakeSchedule()
|
|
tion = FakeTionService()
|
|
qingping = FakeQingpingService()
|
|
|
|
controller = AutoController(
|
|
schedule_service=schedule,
|
|
qingping_service=qingping,
|
|
tion_service=tion,
|
|
temperature_policy=FakeTemperaturePolicy(),
|
|
)
|
|
|
|
# ==================================================
|
|
# 1. Регрессионный тест найденного бага.
|
|
#
|
|
# Было:
|
|
#
|
|
# AUTO target = 25
|
|
# Qingping = 25.8
|
|
# Tion heater = ON
|
|
#
|
|
# Пользователь вручную ставит 23.
|
|
#
|
|
# Несмотря на то, что встроенный датчик Tion
|
|
# показывает только 18°C, главным должен остаться
|
|
# Qingping:
|
|
#
|
|
# 25.8 > 23
|
|
# -> heater OFF.
|
|
# ==================================================
|
|
|
|
await controller._process()
|
|
|
|
print_state(
|
|
"1. Override target 23, Qingping 25.8:",
|
|
controller,
|
|
tion,
|
|
qingping,
|
|
)
|
|
|
|
assert controller._state == "suspended"
|
|
assert controller._reason == "manual_override"
|
|
|
|
assert (
|
|
controller.manual_target_temperature
|
|
== 23
|
|
)
|
|
|
|
assert (
|
|
controller.manual_heater_enabled
|
|
is True
|
|
)
|
|
|
|
assert (
|
|
controller._temperature_source
|
|
== "qingping"
|
|
)
|
|
|
|
assert controller._temperature == 25.8
|
|
|
|
assert controller._manual_heating is False
|
|
|
|
# КЛЮЧЕВАЯ ПРОВЕРКА БАГА:
|
|
assert tion.controller.heater is False
|
|
|
|
# Внешний регулятор активен,
|
|
# поэтому физическая уставка Tion остаётся MAX.
|
|
assert (
|
|
tion.controller.target_temp
|
|
== MAX_TARGET_TEMP
|
|
)
|
|
|
|
# ==================================================
|
|
# 2. Qingping пропадает во время того же override.
|
|
#
|
|
# Должны:
|
|
#
|
|
# target Tion -> 23
|
|
# heater -> ON
|
|
#
|
|
# И дальше встроенный термостат Tion решает,
|
|
# нужен ли фактический нагрев.
|
|
# ==================================================
|
|
|
|
qingping.online = False
|
|
qingping.state.temperature = None
|
|
|
|
await controller._process()
|
|
|
|
print_state(
|
|
"2. Same override, Qingping OFFLINE:",
|
|
controller,
|
|
tion,
|
|
qingping,
|
|
)
|
|
|
|
assert (
|
|
controller.manual_target_temperature
|
|
== 23
|
|
)
|
|
|
|
assert controller._manual_heating is None
|
|
|
|
assert tion.controller.target_temp == 23
|
|
assert tion.controller.heater is True
|
|
|
|
assert (
|
|
controller._temperature_source
|
|
== "tion"
|
|
)
|
|
|
|
assert controller._temperature == 18
|
|
|
|
# ==================================================
|
|
# 3. Qingping восстанавливается.
|
|
#
|
|
# Комната = 22°C, цель override = 23°C.
|
|
#
|
|
# Внешний регулятор автоматически должен
|
|
# снова забрать управление:
|
|
#
|
|
# target Tion -> MAX
|
|
# heater -> ON
|
|
# source -> Qingping
|
|
# ==================================================
|
|
|
|
qingping.online = True
|
|
qingping.state.temperature = 22.0
|
|
|
|
await controller._process()
|
|
|
|
print_state(
|
|
"3. Qingping recovered, room 22:",
|
|
controller,
|
|
tion,
|
|
qingping,
|
|
)
|
|
|
|
assert (
|
|
controller._temperature_source
|
|
== "qingping"
|
|
)
|
|
|
|
assert controller._temperature == 22.0
|
|
assert controller._manual_heating is True
|
|
|
|
assert tion.controller.heater is True
|
|
|
|
assert (
|
|
tion.controller.target_temp
|
|
== MAX_TARGET_TEMP
|
|
)
|
|
|
|
# ==================================================
|
|
# 4. Температура снова выше ручной цели.
|
|
#
|
|
# Проверяем, что heater снова отключается.
|
|
# ==================================================
|
|
|
|
qingping.state.temperature = 24.0
|
|
|
|
await controller._process()
|
|
|
|
print_state(
|
|
"4. Qingping 24, override target 23:",
|
|
controller,
|
|
tion,
|
|
qingping,
|
|
)
|
|
|
|
assert controller._manual_heating is False
|
|
assert tion.controller.heater is False
|
|
|
|
assert (
|
|
tion.controller.target_temp
|
|
== MAX_TARGET_TEMP
|
|
)
|
|
|
|
print()
|
|
print("TEST PASSED")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |