work: добавил автоматику управления вытяжкой. Скорости сопоставляются в auto.yaml. Сейчас 4 тион 1 элика, 5 - 2; 6 -3.
This commit is contained in:
@@ -0,0 +1,425 @@
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
from app.auto.config import load_auto_config
|
||||
from app.auto.controller import AutoController
|
||||
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-auto-elica-already-on-test-"
|
||||
f"{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.\n"
|
||||
f"mqtt_connected="
|
||||
f"{state.mqtt_connected}\n"
|
||||
f"device_online="
|
||||
f"{state.device_online}\n"
|
||||
f"feedback_ready="
|
||||
f"{service.feedback_ready}\n"
|
||||
f"missing_feedback_inputs="
|
||||
f"{service.missing_feedback_inputs}"
|
||||
)
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
service = TestElicaService(
|
||||
ElicaConfig(
|
||||
mqtt_host=ELICA_MQTT_HOST,
|
||||
mqtt_port=ELICA_MQTT_PORT,
|
||||
)
|
||||
)
|
||||
|
||||
elica_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"
|
||||
)
|
||||
|
||||
initial = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Initial Elica:",
|
||||
{
|
||||
"power": initial.power,
|
||||
"speed": initial.speed,
|
||||
"timer": initial.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Для этого теста Elica должна быть заранее
|
||||
# включена пользователем.
|
||||
# --------------------------------------------------
|
||||
|
||||
if not initial.power:
|
||||
raise RuntimeError(
|
||||
"Before starting this test, "
|
||||
"manually turn Elica ON "
|
||||
"at any speed."
|
||||
)
|
||||
|
||||
initial_power = initial.power
|
||||
initial_speed = initial.speed
|
||||
initial_timer = initial.timer
|
||||
|
||||
config = load_auto_config(
|
||||
AUTO_CONFIG
|
||||
)
|
||||
|
||||
assist = ElicaAutoAssistState(
|
||||
config.elica_assist
|
||||
)
|
||||
|
||||
auto_controller = AutoController(
|
||||
schedule_service=object(),
|
||||
qingping_service=object(),
|
||||
tion_service=object(),
|
||||
elica_controller=elica_controller,
|
||||
policy=None,
|
||||
temperature_policy=None,
|
||||
elica_assist=assist,
|
||||
interval=1.0,
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 1
|
||||
#
|
||||
# Пользовательская Elica уже включена.
|
||||
#
|
||||
# Tion AUTO поднимается до 4.
|
||||
#
|
||||
# AutoController должен:
|
||||
#
|
||||
# увидеть Elica ON
|
||||
# -> SUPPRESSED
|
||||
# -> reason = elica_already_on
|
||||
# -> ничего не переключать
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"STEP 1:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion AUTO speed 4 while "
|
||||
"Elica is already user-controlled."
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
4
|
||||
)
|
||||
|
||||
after_speed_4 = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica after Tion speed 4:",
|
||||
{
|
||||
"power": after_speed_4.power,
|
||||
"speed": after_speed_4.speed,
|
||||
"timer": after_speed_4.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if assist.mode.value != "suppressed":
|
||||
raise RuntimeError(
|
||||
"Assist did not enter SUPPRESSED"
|
||||
)
|
||||
|
||||
if (
|
||||
assist.status()["reason"]
|
||||
!= "elica_already_on"
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Expected reason elica_already_on"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Elica обязана остаться полностью неизменной.
|
||||
# --------------------------------------------------
|
||||
|
||||
if (
|
||||
after_speed_4.power != initial_power
|
||||
or after_speed_4.speed != initial_speed
|
||||
or after_speed_4.timer != initial_timer
|
||||
):
|
||||
raise RuntimeError(
|
||||
"AutoController modified "
|
||||
"already-running user Elica"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"SUPPRESSION PASSED"
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 2
|
||||
#
|
||||
# Пока Tion остаётся в диапазоне,
|
||||
# например поднимается 4 -> 5.
|
||||
#
|
||||
# Elica всё равно трогать нельзя.
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"STEP 2:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion AUTO speed changes 4 -> 5."
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica must remain untouched."
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
5
|
||||
)
|
||||
|
||||
after_speed_5 = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica after Tion speed 5:",
|
||||
{
|
||||
"power": after_speed_5.power,
|
||||
"speed": after_speed_5.speed,
|
||||
"timer": after_speed_5.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if assist.mode.value != "suppressed":
|
||||
raise RuntimeError(
|
||||
"Assist left SUPPRESSED "
|
||||
"during the same cycle"
|
||||
)
|
||||
|
||||
if (
|
||||
after_speed_5.power != initial_power
|
||||
or after_speed_5.speed != initial_speed
|
||||
or after_speed_5.timer != initial_timer
|
||||
):
|
||||
raise RuntimeError(
|
||||
"AutoController modified Elica "
|
||||
"while SUPPRESSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"SUPPRESSED HOLD PASSED"
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 3
|
||||
#
|
||||
# Tion падает до 3.
|
||||
#
|
||||
# Текущий повышенный AUTO-цикл закончен.
|
||||
#
|
||||
# Elica должна остаться пользовательской,
|
||||
# но Assist может вернуться в IDLE.
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print("STEP 3:")
|
||||
|
||||
print("Tion AUTO speed drops to 3.")
|
||||
|
||||
print(
|
||||
"Elica must remain ON and unchanged."
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
3
|
||||
)
|
||||
|
||||
final_elica = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Final Elica:",
|
||||
{
|
||||
"power": final_elica.power,
|
||||
"speed": final_elica.speed,
|
||||
"timer": final_elica.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Final Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if (
|
||||
final_elica.power != initial_power
|
||||
or final_elica.speed != initial_speed
|
||||
or final_elica.timer != initial_timer
|
||||
):
|
||||
raise RuntimeError(
|
||||
"AutoController changed user Elica "
|
||||
"when the Tion cycle ended"
|
||||
)
|
||||
|
||||
if assist.mode.value != "idle":
|
||||
raise RuntimeError(
|
||||
"Assist did not return to IDLE "
|
||||
"after Tion dropped below threshold"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"CYCLE END PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print("Verified sequence:")
|
||||
|
||||
print(
|
||||
"User Elica already ON"
|
||||
" -> Tion 4"
|
||||
" -> SUPPRESSED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion 5"
|
||||
" -> Elica untouched"
|
||||
" -> still SUPPRESSED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion 3"
|
||||
" -> Elica untouched"
|
||||
" -> IDLE"
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print("Stopping Elica service...")
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user