316 lines
6.7 KiB
Python
316 lines
6.7 KiB
Python
import asyncio
|
|
import os
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
from app.auto.config import load_auto_config
|
|
from app.auto.elica_assist import ElicaAutoAssistState
|
|
|
|
from app.elica import (
|
|
ElicaConfig,
|
|
ElicaController,
|
|
ElicaService,
|
|
)
|
|
|
|
from app.my_dataclasses import (
|
|
ELICA_MQTT_HOST,
|
|
ELICA_MQTT_PORT,
|
|
)
|
|
|
|
|
|
AUTO_CONFIG = "config/auto.yaml"
|
|
|
|
ELICA_READY_TIMEOUT = 15.0
|
|
|
|
|
|
class TestElicaService(ElicaService):
|
|
async def start(self) -> None:
|
|
if self._running:
|
|
return
|
|
|
|
client_id = (
|
|
f"climatcontroller-elica-speed-test-{os.getpid()}"
|
|
)
|
|
|
|
print(
|
|
"Test MQTT client_id:",
|
|
client_id,
|
|
)
|
|
|
|
client = mqtt.Client(
|
|
callback_api_version=(
|
|
mqtt.CallbackAPIVersion.VERSION2
|
|
),
|
|
client_id=client_id,
|
|
)
|
|
|
|
client.on_connect = self._on_connect
|
|
client.on_disconnect = self._on_disconnect
|
|
client.on_message = self._on_message
|
|
|
|
client.reconnect_delay_set(
|
|
min_delay=1,
|
|
max_delay=30,
|
|
)
|
|
|
|
self._client = client
|
|
self._running = True
|
|
|
|
client.connect_async(
|
|
self._config.mqtt_host,
|
|
self._config.mqtt_port,
|
|
keepalive=60,
|
|
)
|
|
|
|
client.loop_start()
|
|
|
|
|
|
async def wait_for_elica(
|
|
service: ElicaService,
|
|
) -> None:
|
|
loop = asyncio.get_running_loop()
|
|
|
|
deadline = (
|
|
loop.time()
|
|
+ ELICA_READY_TIMEOUT
|
|
)
|
|
|
|
while True:
|
|
state = service.state
|
|
|
|
if (
|
|
state.mqtt_connected
|
|
and state.device_online
|
|
and service.feedback_ready
|
|
):
|
|
return
|
|
|
|
if loop.time() >= deadline:
|
|
raise RuntimeError(
|
|
"Elica did not become ready"
|
|
)
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
|
async def main() -> None:
|
|
service = TestElicaService(
|
|
ElicaConfig(
|
|
mqtt_host=ELICA_MQTT_HOST,
|
|
mqtt_port=ELICA_MQTT_PORT,
|
|
)
|
|
)
|
|
|
|
controller = ElicaController(
|
|
service
|
|
)
|
|
|
|
print(
|
|
"Starting Elica service..."
|
|
)
|
|
|
|
await service.start()
|
|
|
|
try:
|
|
print(
|
|
f"Connecting to MQTT "
|
|
f"{ELICA_MQTT_HOST}:"
|
|
f"{ELICA_MQTT_PORT}..."
|
|
)
|
|
|
|
await wait_for_elica(
|
|
service
|
|
)
|
|
|
|
print(
|
|
"Elica connected and "
|
|
"feedback ready"
|
|
)
|
|
|
|
state = controller.state
|
|
|
|
print()
|
|
print(
|
|
"Initial Elica:",
|
|
{
|
|
"power": state.power,
|
|
"speed": state.speed,
|
|
"timer": state.timer,
|
|
},
|
|
)
|
|
|
|
# Для этого теста Elica должна быть выключена.
|
|
if state.power:
|
|
raise RuntimeError(
|
|
"Elica must be OFF before this test"
|
|
)
|
|
|
|
config = load_auto_config(
|
|
AUTO_CONFIG
|
|
)
|
|
|
|
assist = ElicaAutoAssistState(
|
|
config.elica_assist
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# Этап 1:
|
|
# Tion AUTO = 4 -> Elica должна стать 1
|
|
# --------------------------------------------------
|
|
|
|
target_1 = assist.set_tion_speed(4)
|
|
|
|
print()
|
|
print(
|
|
"Tion AUTO speed:",
|
|
4,
|
|
)
|
|
|
|
print(
|
|
"Elica target:",
|
|
target_1,
|
|
)
|
|
|
|
if target_1 != 1:
|
|
raise RuntimeError(
|
|
f"Expected Elica target 1, got {target_1}"
|
|
)
|
|
|
|
assist.observe_elica(
|
|
power=state.power,
|
|
speed=state.speed,
|
|
timer=state.timer,
|
|
)
|
|
|
|
result_1 = await controller.set_speed(
|
|
target_1
|
|
)
|
|
|
|
assist.mark_owned(
|
|
result_1.speed
|
|
)
|
|
|
|
assist.observe_elica(
|
|
power=result_1.power,
|
|
speed=result_1.speed,
|
|
timer=result_1.timer,
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"After first acquire:",
|
|
{
|
|
"power": result_1.power,
|
|
"speed": result_1.speed,
|
|
"timer": result_1.timer,
|
|
},
|
|
)
|
|
|
|
print(
|
|
"Assist:",
|
|
assist.status(),
|
|
)
|
|
|
|
if assist.mode.value != "owned":
|
|
raise RuntimeError(
|
|
"Assist did not enter OWNED"
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# Этап 2:
|
|
# Tion AUTO меняется 4 -> 5
|
|
# Значит Elica 1 -> 2
|
|
# --------------------------------------------------
|
|
|
|
target_2 = assist.set_tion_speed(5)
|
|
|
|
print()
|
|
print(
|
|
"Tion AUTO speed:",
|
|
5,
|
|
)
|
|
|
|
print(
|
|
"New Elica target:",
|
|
target_2,
|
|
)
|
|
|
|
if target_2 != 2:
|
|
raise RuntimeError(
|
|
f"Expected Elica target 2, got {target_2}"
|
|
)
|
|
|
|
# --------------------------------------------------
|
|
# Пока владельцем остаётся автоматика,
|
|
# поэтому ей разрешено изменить скорость.
|
|
# --------------------------------------------------
|
|
|
|
result_2 = await controller.set_speed(
|
|
target_2
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"Confirmed Elica after speed change:",
|
|
{
|
|
"power": result_2.power,
|
|
"speed": result_2.speed,
|
|
"timer": result_2.timer,
|
|
},
|
|
)
|
|
|
|
# Обновляем expected_speed только после
|
|
# подтверждённой команды.
|
|
assist.mark_owned(
|
|
result_2.speed
|
|
)
|
|
|
|
assist.observe_elica(
|
|
power=result_2.power,
|
|
speed=result_2.speed,
|
|
timer=result_2.timer,
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"Assist after speed change:",
|
|
assist.status(),
|
|
)
|
|
|
|
if assist.mode.value != "owned":
|
|
raise RuntimeError(
|
|
"Assist lost OWNED after "
|
|
"automatic speed change"
|
|
)
|
|
|
|
if result_2.speed != 2:
|
|
raise RuntimeError(
|
|
"Elica did not reach speed 2"
|
|
)
|
|
|
|
print()
|
|
print(
|
|
"TEST PASSED"
|
|
)
|
|
|
|
print(
|
|
"Auto Assist changed Elica "
|
|
"from speed 1 to speed 2 "
|
|
"and kept OWNED."
|
|
)
|
|
|
|
print(
|
|
"Turn Elica off manually after the test."
|
|
)
|
|
|
|
finally:
|
|
print()
|
|
print(
|
|
"Stopping Elica service..."
|
|
)
|
|
|
|
await service.stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |