work: добавил автоматику управления вытяжкой. Скорости сопоставляются в auto.yaml. Сейчас 4 тион 1 элика, 5 - 2; 6 -3.
This commit is contained in:
@@ -0,0 +1,375 @@
|
||||
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-release-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,
|
||||
},
|
||||
)
|
||||
|
||||
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 target = 1
|
||||
# Получаем OWNED.
|
||||
# --------------------------------------------------
|
||||
|
||||
target = assist.set_tion_speed(
|
||||
4
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Tion AUTO speed:",
|
||||
4,
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica target:",
|
||||
target,
|
||||
)
|
||||
|
||||
if target != 1:
|
||||
raise RuntimeError(
|
||||
f"Expected target 1, got {target}"
|
||||
)
|
||||
|
||||
assist.observe_elica(
|
||||
power=state.power,
|
||||
speed=state.speed,
|
||||
timer=state.timer,
|
||||
)
|
||||
|
||||
result = await controller.set_speed(
|
||||
target
|
||||
)
|
||||
|
||||
assist.mark_owned(
|
||||
result.speed
|
||||
)
|
||||
|
||||
assist.observe_elica(
|
||||
power=result.power,
|
||||
speed=result.speed,
|
||||
timer=result.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica acquired:",
|
||||
{
|
||||
"power": result.power,
|
||||
"speed": result.speed,
|
||||
"timer": result.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Assist did not enter OWNED"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# ЭТАП 2
|
||||
#
|
||||
# Tion AUTO падает до 3.
|
||||
#
|
||||
# target_speed должен стать None.
|
||||
# --------------------------------------------------
|
||||
|
||||
target = assist.set_tion_speed(
|
||||
3
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Tion AUTO speed:",
|
||||
3,
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica target:",
|
||||
target,
|
||||
)
|
||||
|
||||
if target is not None:
|
||||
raise RuntimeError(
|
||||
"Expected no Elica target "
|
||||
"at Tion speed 3"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Перед выключением обязательно ещё раз
|
||||
# проверяем фактическое состояние.
|
||||
#
|
||||
# Если оно уже не совпадает с expected_speed,
|
||||
# значит пользователь вмешался.
|
||||
# --------------------------------------------------
|
||||
|
||||
current = controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica before release:",
|
||||
{
|
||||
"power": current.power,
|
||||
"speed": current.speed,
|
||||
"timer": current.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist before release:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
assist.observe_elica(
|
||||
power=current.power,
|
||||
speed=current.speed,
|
||||
timer=current.timer,
|
||||
)
|
||||
|
||||
# После проверки мы всё ещё должны
|
||||
# владеть Elica.
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Auto Assist lost ownership "
|
||||
"before release. "
|
||||
"Elica will NOT be switched off."
|
||||
)
|
||||
|
||||
expected = assist.expected_speed
|
||||
|
||||
if (
|
||||
not current.power
|
||||
or current.speed != expected
|
||||
or current.timer
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Actual Elica state does not match "
|
||||
"the state owned by Auto Assist"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# ПЕРВОЕ АВТОМАТИЧЕСКОЕ ВЫКЛЮЧЕНИЕ.
|
||||
# --------------------------------------------------
|
||||
|
||||
print()
|
||||
print(
|
||||
"AUTO ASSIST IS NOW GOING TO "
|
||||
"TURN ELICA OFF"
|
||||
)
|
||||
|
||||
result_off = await controller.power_off()
|
||||
|
||||
print()
|
||||
print(
|
||||
"Confirmed Elica:",
|
||||
{
|
||||
"power": result_off.power,
|
||||
"speed": result_off.speed,
|
||||
"timer": result_off.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# После успешного выключения
|
||||
# текущий автоматический цикл завершён.
|
||||
assist.reset()
|
||||
|
||||
print(
|
||||
"Assist after release:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# ПРОВЕРКИ
|
||||
# --------------------------------------------------
|
||||
|
||||
if result_off.power:
|
||||
raise RuntimeError(
|
||||
"Elica did not switch off"
|
||||
)
|
||||
|
||||
if assist.mode.value != "idle":
|
||||
raise RuntimeError(
|
||||
"Assist did not return to IDLE"
|
||||
)
|
||||
|
||||
if assist.expected_speed is not None:
|
||||
raise RuntimeError(
|
||||
"expected_speed was not cleared"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Auto Assist acquired Elica, "
|
||||
"then released and switched it off "
|
||||
"when Tion dropped to speed 3."
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user