138 lines
2.6 KiB
Python
138 lines
2.6 KiB
Python
import asyncio
|
|
from types import SimpleNamespace
|
|
|
|
from app.auto.config import (
|
|
TemperatureConfig,
|
|
)
|
|
from app.auto.controller import (
|
|
AutoController,
|
|
)
|
|
from app.auto.temperature_policy import (
|
|
TemperaturePolicy,
|
|
)
|
|
|
|
|
|
class FakeQingpingService:
|
|
|
|
def __init__(self):
|
|
self.online = True
|
|
|
|
self.state = SimpleNamespace(
|
|
temperature=19.5,
|
|
)
|
|
|
|
|
|
class FakeTionController:
|
|
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
async def heater_on(self):
|
|
self.calls.append(
|
|
("heater_on", None)
|
|
)
|
|
|
|
async def heater_off(self):
|
|
self.calls.append(
|
|
("heater_off", None)
|
|
)
|
|
|
|
|
|
class FakeTionService:
|
|
|
|
def __init__(self):
|
|
self.online = True
|
|
|
|
self.state = SimpleNamespace(
|
|
in_temp=19,
|
|
)
|
|
|
|
self.controller = (
|
|
FakeTionController()
|
|
)
|
|
|
|
async def execute(
|
|
self,
|
|
operation,
|
|
):
|
|
return await operation(
|
|
self.controller
|
|
)
|
|
|
|
|
|
async def test_auto_uses_auto_target_temperature():
|
|
|
|
qingping = FakeQingpingService()
|
|
tion = FakeTionService()
|
|
|
|
policy = TemperaturePolicy(
|
|
TemperatureConfig(
|
|
hysteresis=0.5,
|
|
)
|
|
)
|
|
|
|
controller = AutoController(
|
|
schedule_service=None,
|
|
qingping_service=qingping,
|
|
tion_service=tion,
|
|
temperature_policy=policy,
|
|
)
|
|
|
|
resolution = SimpleNamespace(
|
|
# Последний SET хотел 23°C.
|
|
scheduled_settings=SimpleNamespace(
|
|
target_temp=23,
|
|
),
|
|
|
|
# Но текущий AUTO явно хочет 20°C.
|
|
auto_target_temp=20,
|
|
)
|
|
|
|
await controller._process_heater(
|
|
resolution
|
|
)
|
|
|
|
# При 19.5°C и target=20°C:
|
|
#
|
|
# heater был OFF,
|
|
# нижняя граница = 19.5°C.
|
|
#
|
|
# Поэтому heater должен остаться OFF.
|
|
#
|
|
# Если бы контроллер ошибочно использовал
|
|
# SET target_temp=23, он бы включил heater.
|
|
assert tion.controller.calls == [
|
|
("heater_off", None)
|
|
]
|
|
|
|
assert (
|
|
controller._auto_heater
|
|
is False
|
|
)
|
|
|
|
assert (
|
|
controller._temperature
|
|
== 19.5
|
|
)
|
|
|
|
assert (
|
|
controller._temperature_source
|
|
== "qingping"
|
|
)
|
|
|
|
|
|
async def main():
|
|
|
|
await test_auto_uses_auto_target_temperature()
|
|
|
|
print()
|
|
print("=" * 70)
|
|
print(
|
|
"ALL AUTO HEATER TARGET "
|
|
"TEMPERATURE TESTS PASSED"
|
|
)
|
|
print("=" * 70)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |