128 lines
2.6 KiB
Python
128 lines
2.6 KiB
Python
from unittest.mock import MagicMock, patch
|
|
|
|
import app.api as api
|
|
|
|
|
|
class FakeTionState:
|
|
def __init__(
|
|
self,
|
|
heater: bool,
|
|
target_temp: int,
|
|
):
|
|
self.heater = heater
|
|
self.target_temp = target_temp
|
|
|
|
def to_dict(self) -> dict:
|
|
return {
|
|
"power": True,
|
|
"heater": self.heater,
|
|
"heating": True,
|
|
"mode": "outside",
|
|
"out_temp": 10,
|
|
"in_temp": 21,
|
|
"target_temp": self.target_temp,
|
|
"fan_speed": 3,
|
|
}
|
|
|
|
|
|
def main() -> None:
|
|
# --------------------------------------------------
|
|
# Физическое состояние Tion:
|
|
# heater ON
|
|
# target = 30°C
|
|
#
|
|
# Но пользователь в ручном режиме хочет 23°C.
|
|
# --------------------------------------------------
|
|
|
|
tion_state = FakeTionState(
|
|
heater=True,
|
|
target_temp=30,
|
|
)
|
|
|
|
fake_service = MagicMock()
|
|
fake_service.state = tion_state
|
|
fake_service.online = True
|
|
fake_service.running = True
|
|
fake_service.last_seen = None
|
|
fake_service.last_error = None
|
|
|
|
fake_schedule = MagicMock()
|
|
fake_schedule.paused = True
|
|
fake_schedule.override_active = False
|
|
|
|
fake_auto = MagicMock()
|
|
|
|
fake_auto.manual_heater_enabled = True
|
|
fake_auto.manual_target_temperature = 23
|
|
|
|
fake_auto.status.return_value = {
|
|
"manual_heater": {
|
|
"enabled": True,
|
|
"target_temp": 23,
|
|
"heating": None,
|
|
}
|
|
}
|
|
|
|
print("Physical Tion:")
|
|
print(tion_state.to_dict())
|
|
|
|
with (
|
|
patch.object(
|
|
api,
|
|
"service",
|
|
fake_service,
|
|
),
|
|
patch.object(
|
|
api,
|
|
"schedule_service",
|
|
fake_schedule,
|
|
),
|
|
patch.object(
|
|
api,
|
|
"auto_controller",
|
|
fake_auto,
|
|
),
|
|
):
|
|
status = api.get_status()
|
|
|
|
print()
|
|
print("Public API Tion:")
|
|
print(status["tion"])
|
|
|
|
print()
|
|
print("Auto:")
|
|
print(status["auto"])
|
|
|
|
# --------------------------------------------------
|
|
# Главная проверка.
|
|
# --------------------------------------------------
|
|
|
|
assert tion_state.target_temp == 30
|
|
|
|
assert (
|
|
status["tion"]["target_temp"]
|
|
== 23
|
|
)
|
|
|
|
assert (
|
|
status["tion"]["heater"]
|
|
is True
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"Physical target:",
|
|
tion_state.target_temp,
|
|
)
|
|
|
|
print(
|
|
"Public target:",
|
|
status["tion"]["target_temp"],
|
|
)
|
|
|
|
print()
|
|
print("TEST PASSED")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |