work: v1.5.0 добавил автоматику управления вытяжкой. Скорости сопоставляются в 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())
|
||||
@@ -0,0 +1,355 @@
|
||||
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):
|
||||
"""
|
||||
Используем отдельный MQTT client_id,
|
||||
чтобы тест не конфликтовал с обычным
|
||||
ClimatController.
|
||||
"""
|
||||
|
||||
async def start(self) -> None:
|
||||
if self._running:
|
||||
return
|
||||
|
||||
client_id = (
|
||||
f"climatcontroller-auto-elica-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.\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:
|
||||
# --------------------------------------------------
|
||||
# Реальный ElicaService + ElicaController.
|
||||
# --------------------------------------------------
|
||||
|
||||
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 initial.power:
|
||||
raise RuntimeError(
|
||||
"Elica must be OFF before this test. "
|
||||
"Turn it off manually and run again."
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Создаём настоящий Elica Auto Assist.
|
||||
# --------------------------------------------------
|
||||
|
||||
config = load_auto_config(
|
||||
AUTO_CONFIG
|
||||
)
|
||||
|
||||
assist = ElicaAutoAssistState(
|
||||
config.elica_assist
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Создаём настоящий AutoController.
|
||||
#
|
||||
# Schedule / Qingping / Tion здесь не нужны,
|
||||
# потому что мы НЕ запускаем его основной цикл,
|
||||
# а вызываем только _update_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
|
||||
#
|
||||
# Имитируем:
|
||||
#
|
||||
# Tion AUTO выбрал скорость 4.
|
||||
#
|
||||
# Именно реальный метод AutoController должен:
|
||||
#
|
||||
# target = 1
|
||||
# Elica OFF -> speed 1
|
||||
# mode -> OWNED
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"Calling AutoController "
|
||||
"_update_elica_assist(4)..."
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
4
|
||||
)
|
||||
|
||||
acquired = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica after Tion speed 4:",
|
||||
{
|
||||
"power": acquired.power,
|
||||
"speed": acquired.speed,
|
||||
"timer": acquired.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if not acquired.power:
|
||||
raise RuntimeError(
|
||||
"AutoController did not turn Elica on"
|
||||
)
|
||||
|
||||
if acquired.speed != 1:
|
||||
raise RuntimeError(
|
||||
f"Expected Elica speed 1, "
|
||||
f"got {acquired.speed}"
|
||||
)
|
||||
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Auto Assist did not enter OWNED"
|
||||
)
|
||||
|
||||
if assist.expected_speed != 1:
|
||||
raise RuntimeError(
|
||||
"expected_speed is not 1"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"ACQUIRE PASSED"
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 2
|
||||
#
|
||||
# Имитируем:
|
||||
#
|
||||
# Tion AUTO упал до скорости 3.
|
||||
#
|
||||
# Теперь реальный AutoController должен:
|
||||
#
|
||||
# target = None
|
||||
# проверить OWNED
|
||||
# выключить Elica
|
||||
# reset -> IDLE
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"Calling AutoController "
|
||||
"_update_elica_assist(3)..."
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
3
|
||||
)
|
||||
|
||||
released = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica after Tion speed 3:",
|
||||
{
|
||||
"power": released.power,
|
||||
"speed": released.speed,
|
||||
"timer": released.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if released.power:
|
||||
raise RuntimeError(
|
||||
"AutoController did not turn Elica off"
|
||||
)
|
||||
|
||||
if assist.mode.value != "idle":
|
||||
raise RuntimeError(
|
||||
"Auto Assist did not return to IDLE"
|
||||
)
|
||||
|
||||
if assist.expected_speed is not None:
|
||||
raise RuntimeError(
|
||||
"expected_speed was not cleared"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"RELEASE PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Real AutoController successfully performed:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion 4 -> Elica speed 1 -> OWNED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion 3 -> Elica OFF -> IDLE"
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,422 @@
|
||||
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
|
||||
MANUAL_CHANGE_TIMEOUT = 60.0
|
||||
|
||||
|
||||
class TestElicaService(ElicaService):
|
||||
async def start(self) -> None:
|
||||
if self._running:
|
||||
return
|
||||
|
||||
client_id = (
|
||||
f"climatcontroller-auto-exit-manual-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"
|
||||
)
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
async def wait_for_manual_change(
|
||||
service: ElicaService,
|
||||
*,
|
||||
power: bool,
|
||||
speed: int,
|
||||
timer: bool,
|
||||
):
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
deadline = (
|
||||
loop.time()
|
||||
+ MANUAL_CHANGE_TIMEOUT
|
||||
)
|
||||
|
||||
while True:
|
||||
state = service.state
|
||||
|
||||
ready = (
|
||||
state.mqtt_connected
|
||||
and state.device_online
|
||||
and service.feedback_ready
|
||||
)
|
||||
|
||||
if ready:
|
||||
logical = service.logical_state
|
||||
|
||||
if (
|
||||
logical.power != power
|
||||
or logical.speed != speed
|
||||
or logical.timer != timer
|
||||
):
|
||||
return logical
|
||||
|
||||
if loop.time() >= deadline:
|
||||
raise RuntimeError(
|
||||
"No manual Elica change detected "
|
||||
f"within {MANUAL_CHANGE_TIMEOUT} seconds"
|
||||
)
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
||||
if initial.power:
|
||||
raise RuntimeError(
|
||||
"Elica must be OFF before this test"
|
||||
)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# STEP 1
|
||||
#
|
||||
# AUTO активен:
|
||||
# Tion 4 -> Elica 1 -> OWNED
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print("STEP 1:")
|
||||
print(
|
||||
"Tion AUTO speed 4 "
|
||||
"-> Auto Assist acquires Elica"
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
4
|
||||
)
|
||||
|
||||
owned = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica:",
|
||||
{
|
||||
"power": owned.power,
|
||||
"speed": owned.speed,
|
||||
"timer": owned.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if (
|
||||
not owned.power
|
||||
or owned.speed != 1
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Auto Assist did not acquire "
|
||||
"Elica at speed 1"
|
||||
)
|
||||
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Assist did not enter OWNED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"ACQUIRE PASSED"
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# STEP 2
|
||||
#
|
||||
# Пользователь вручную меняет Elica.
|
||||
# Например 1 -> 2.
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print("STEP 2:")
|
||||
print(
|
||||
"Now MANUALLY change Elica."
|
||||
)
|
||||
|
||||
print(
|
||||
"For example change speed 1 -> 2."
|
||||
)
|
||||
|
||||
print(
|
||||
"The test is waiting..."
|
||||
)
|
||||
|
||||
changed = await wait_for_manual_change(
|
||||
service,
|
||||
power=owned.power,
|
||||
speed=owned.speed,
|
||||
timer=owned.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Manual Elica state:",
|
||||
{
|
||||
"power": changed.power,
|
||||
"speed": changed.speed,
|
||||
"timer": changed.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# STEP 3
|
||||
#
|
||||
# ВАЖНО:
|
||||
# AutoController ещё не успел сделать обычный
|
||||
# проход _update_elica_assist().
|
||||
#
|
||||
# Сразу имитируем выход из AUTO.
|
||||
#
|
||||
# _release_elica_assist() сам обязан заметить,
|
||||
# что actual != expected, и НЕ выключить Elica.
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print("STEP 3:")
|
||||
print(
|
||||
"Simulating immediate exit from AUTO..."
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica must NOT be switched off."
|
||||
)
|
||||
|
||||
await auto_controller._release_elica_assist()
|
||||
|
||||
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(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Elica должна остаться точно такой,
|
||||
# какой её сделал пользователь.
|
||||
# --------------------------------------------------
|
||||
|
||||
if (
|
||||
final_elica.power != changed.power
|
||||
or final_elica.speed != changed.speed
|
||||
or final_elica.timer != changed.timer
|
||||
):
|
||||
raise RuntimeError(
|
||||
"AutoController modified user's Elica "
|
||||
"during AUTO exit"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Сессия Auto Assist должна закончиться.
|
||||
# --------------------------------------------------
|
||||
|
||||
if assist.mode.value != "idle":
|
||||
raise RuntimeError(
|
||||
"Assist did not return to IDLE "
|
||||
"after AUTO exit"
|
||||
)
|
||||
|
||||
if assist.expected_speed is not None:
|
||||
raise RuntimeError(
|
||||
"expected_speed was not cleared"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"MANUAL TAKEOVER ON AUTO EXIT PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Verified sequence:"
|
||||
)
|
||||
|
||||
print(
|
||||
"AUTO -> Tion 4 "
|
||||
"-> Elica 1 "
|
||||
"-> OWNED"
|
||||
)
|
||||
|
||||
print(
|
||||
"User changes Elica"
|
||||
)
|
||||
|
||||
print(
|
||||
"AUTO exit "
|
||||
"-> Elica untouched "
|
||||
"-> IDLE"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Turn Elica off manually "
|
||||
"after the test."
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,555 @@
|
||||
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
|
||||
MANUAL_CHANGE_TIMEOUT = 60.0
|
||||
|
||||
|
||||
class TestElicaService(ElicaService):
|
||||
"""
|
||||
Отдельный MQTT client_id для live-теста.
|
||||
"""
|
||||
|
||||
async def start(self) -> None:
|
||||
if self._running:
|
||||
return
|
||||
|
||||
client_id = (
|
||||
f"climatcontroller-auto-elica-manual-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 wait_for_manual_change(
|
||||
service: ElicaService,
|
||||
*,
|
||||
power: bool,
|
||||
speed: int,
|
||||
timer: bool,
|
||||
):
|
||||
"""
|
||||
Ждём реального изменения Elica пользователем.
|
||||
|
||||
Потеря MQTT/feedback изменением
|
||||
не считается.
|
||||
"""
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
deadline = (
|
||||
loop.time()
|
||||
+ MANUAL_CHANGE_TIMEOUT
|
||||
)
|
||||
|
||||
while True:
|
||||
state = service.state
|
||||
|
||||
ready = (
|
||||
state.mqtt_connected
|
||||
and state.device_online
|
||||
and service.feedback_ready
|
||||
)
|
||||
|
||||
if ready:
|
||||
logical = service.logical_state
|
||||
|
||||
if (
|
||||
logical.power != power
|
||||
or logical.speed != speed
|
||||
or logical.timer != timer
|
||||
):
|
||||
return logical
|
||||
|
||||
if loop.time() >= deadline:
|
||||
raise RuntimeError(
|
||||
"No manual Elica change detected "
|
||||
f"within {MANUAL_CHANGE_TIMEOUT} seconds"
|
||||
)
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
||||
if initial.power:
|
||||
raise RuntimeError(
|
||||
"Elica must be OFF before this test. "
|
||||
"Turn it off manually and run again."
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Создаём настоящий Auto Assist.
|
||||
# --------------------------------------------------
|
||||
|
||||
config = load_auto_config(
|
||||
AUTO_CONFIG
|
||||
)
|
||||
|
||||
assist = ElicaAutoAssistState(
|
||||
config.elica_assist
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Настоящий AutoController.
|
||||
#
|
||||
# Основной цикл мы не запускаем.
|
||||
# Вызываем только _update_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
|
||||
#
|
||||
# Tion 4
|
||||
# -> AutoController включает Elica 1
|
||||
# -> OWNED
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"STEP 1:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion AUTO speed 4 "
|
||||
"-> Auto Assist should acquire Elica"
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
4
|
||||
)
|
||||
|
||||
owned = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica:",
|
||||
{
|
||||
"power": owned.power,
|
||||
"speed": owned.speed,
|
||||
"timer": owned.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if (
|
||||
not owned.power
|
||||
or owned.speed != 1
|
||||
):
|
||||
raise RuntimeError(
|
||||
"AutoController did not acquire "
|
||||
"Elica at speed 1"
|
||||
)
|
||||
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Assist did not enter OWNED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"ACQUIRE PASSED"
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 2
|
||||
#
|
||||
# Теперь пользователь физически меняет Elica.
|
||||
#
|
||||
# Например:
|
||||
#
|
||||
# speed 1 -> 2
|
||||
#
|
||||
# AutoController пока ничего не делает.
|
||||
# Просто ждём реальный MQTT feedback.
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"STEP 2:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Now MANUALLY change Elica."
|
||||
)
|
||||
|
||||
print(
|
||||
"For example change speed 1 -> 2."
|
||||
)
|
||||
|
||||
print(
|
||||
"The test is waiting..."
|
||||
)
|
||||
|
||||
changed = await wait_for_manual_change(
|
||||
service,
|
||||
power=owned.power,
|
||||
speed=owned.speed,
|
||||
timer=owned.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Manual Elica state:",
|
||||
{
|
||||
"power": changed.power,
|
||||
"speed": changed.speed,
|
||||
"timer": changed.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 3
|
||||
#
|
||||
# Tion всё ещё 4.
|
||||
#
|
||||
# Следующий проход AutoController должен увидеть:
|
||||
#
|
||||
# expected_speed = 1
|
||||
# actual speed = 2
|
||||
#
|
||||
# -> manual_intervention
|
||||
# -> SUPPRESSED
|
||||
#
|
||||
# И НИКАКИХ команд Elica.
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"STEP 3:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion is still speed 4."
|
||||
)
|
||||
|
||||
print(
|
||||
"AutoController should detect "
|
||||
"manual intervention..."
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
4
|
||||
)
|
||||
|
||||
after_takeover = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica after detection:",
|
||||
{
|
||||
"power": after_takeover.power,
|
||||
"speed": after_takeover.speed,
|
||||
"timer": after_takeover.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if assist.mode.value != "suppressed":
|
||||
raise RuntimeError(
|
||||
"Assist did not enter SUPPRESSED"
|
||||
)
|
||||
|
||||
if (
|
||||
assist.status()["reason"]
|
||||
!= "manual_intervention"
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Expected reason manual_intervention"
|
||||
)
|
||||
|
||||
# Elica должна остаться ровно такой,
|
||||
# какой её сделал пользователь.
|
||||
if (
|
||||
after_takeover.power
|
||||
!= changed.power
|
||||
or after_takeover.speed
|
||||
!= changed.speed
|
||||
or after_takeover.timer
|
||||
!= changed.timer
|
||||
):
|
||||
raise RuntimeError(
|
||||
"AutoController changed Elica "
|
||||
"after manual intervention"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"MANUAL TAKEOVER PASSED"
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 4
|
||||
#
|
||||
# Теперь Tion падает ниже порога:
|
||||
#
|
||||
# 4 -> 3
|
||||
#
|
||||
# Текущий автоматический цикл заканчивается.
|
||||
#
|
||||
# Но Elica уже пользовательская,
|
||||
# поэтому выключать её ЗАПРЕЩЕНО.
|
||||
#
|
||||
# SUPPRESSED -> IDLE
|
||||
# Elica остаётся включена.
|
||||
# ==================================================
|
||||
|
||||
print()
|
||||
print(
|
||||
"STEP 4:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion AUTO speed drops to 3."
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica must NOT be switched off."
|
||||
)
|
||||
|
||||
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
|
||||
!= changed.power
|
||||
or final_elica.speed
|
||||
!= changed.speed
|
||||
or final_elica.timer
|
||||
!= changed.timer
|
||||
):
|
||||
raise RuntimeError(
|
||||
"AutoController modified user's "
|
||||
"Elica when Tion dropped to speed 3"
|
||||
)
|
||||
|
||||
# Новый цикл должен быть готов начаться
|
||||
# в будущем.
|
||||
if assist.mode.value != "idle":
|
||||
raise RuntimeError(
|
||||
"Assist did not return to IDLE "
|
||||
"after the automatic cycle ended"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"RELEASE AFTER TAKEOVER PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Verified sequence:"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion 4"
|
||||
" -> Auto Elica 1"
|
||||
" -> OWNED"
|
||||
)
|
||||
|
||||
print(
|
||||
"User changes Elica"
|
||||
" -> SUPPRESSED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Tion 3"
|
||||
" -> Elica untouched"
|
||||
" -> IDLE"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Turn Elica off manually "
|
||||
"after the test."
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,314 @@
|
||||
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-exit-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"
|
||||
)
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
||||
if initial.power:
|
||||
raise RuntimeError(
|
||||
"Elica must be OFF before this test"
|
||||
)
|
||||
|
||||
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
|
||||
#
|
||||
# Имитируем рабочий AUTO:
|
||||
# Tion speed 4 -> Elica speed 1 -> OWNED
|
||||
# --------------------------------------------------
|
||||
|
||||
print()
|
||||
print("STEP 1:")
|
||||
print(
|
||||
"Tion AUTO speed 4 "
|
||||
"-> Auto Assist acquires Elica"
|
||||
)
|
||||
|
||||
await auto_controller._update_elica_assist(
|
||||
4
|
||||
)
|
||||
|
||||
owned = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica:",
|
||||
{
|
||||
"power": owned.power,
|
||||
"speed": owned.speed,
|
||||
"timer": owned.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if (
|
||||
not owned.power
|
||||
or owned.speed != 1
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Auto Assist did not acquire "
|
||||
"Elica at speed 1"
|
||||
)
|
||||
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Assist did not enter OWNED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"ACQUIRE PASSED"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# ЭТАП 2
|
||||
#
|
||||
# Теперь имитируем не Tion speed 3,
|
||||
# а полный ВЫХОД ИЗ AUTO.
|
||||
#
|
||||
# Именно _release_elica_assist()
|
||||
# должен выключить принадлежащую нам Elica.
|
||||
# --------------------------------------------------
|
||||
|
||||
print()
|
||||
print("STEP 2:")
|
||||
print(
|
||||
"Simulating exit from Tion AUTO..."
|
||||
)
|
||||
|
||||
await auto_controller._release_elica_assist()
|
||||
|
||||
released = elica_controller.state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica after AUTO exit:",
|
||||
{
|
||||
"power": released.power,
|
||||
"speed": released.speed,
|
||||
"timer": released.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist after AUTO exit:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# ПРОВЕРКИ
|
||||
# --------------------------------------------------
|
||||
|
||||
if released.power:
|
||||
raise RuntimeError(
|
||||
"Elica remained ON after AUTO exit"
|
||||
)
|
||||
|
||||
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(
|
||||
"AUTO EXIT RELEASE PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Verified sequence:"
|
||||
)
|
||||
|
||||
print(
|
||||
"AUTO active"
|
||||
" -> Tion 4"
|
||||
" -> Elica 1"
|
||||
" -> OWNED"
|
||||
)
|
||||
|
||||
print(
|
||||
"AUTO exit"
|
||||
" -> Elica OFF"
|
||||
" -> IDLE"
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,341 @@
|
||||
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"
|
||||
|
||||
# По нашей текущей таблице:
|
||||
# Tion 4 -> Elica 1
|
||||
TION_SPEED = 4
|
||||
|
||||
ELICA_READY_TIMEOUT = 15.0
|
||||
|
||||
|
||||
class TestElicaService(ElicaService):
|
||||
"""
|
||||
Отдельный MQTT client_id для live-теста,
|
||||
чтобы не конфликтовать с запущенным
|
||||
ClimatController.
|
||||
"""
|
||||
|
||||
async def start(self) -> None:
|
||||
if self._running:
|
||||
return
|
||||
|
||||
client_id = (
|
||||
f"climatcontroller-elica-acquire-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:
|
||||
"""
|
||||
Ждём подключения к MQTT, ESP32
|
||||
и полной обратной связи панели Elica.
|
||||
"""
|
||||
|
||||
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,
|
||||
)
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Перед началом теста Elica обязана быть OFF.
|
||||
# --------------------------------------------------
|
||||
|
||||
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. "
|
||||
"Turn it off manually and run the test again."
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Создаём Auto Assist.
|
||||
# --------------------------------------------------
|
||||
|
||||
config = load_auto_config(
|
||||
AUTO_CONFIG
|
||||
)
|
||||
|
||||
assist = ElicaAutoAssistState(
|
||||
config.elica_assist
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Имитируем решение AUTO Tion.
|
||||
# --------------------------------------------------
|
||||
|
||||
target_speed = (
|
||||
assist.set_tion_speed(
|
||||
TION_SPEED
|
||||
)
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Tion AUTO speed:",
|
||||
TION_SPEED,
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica target:",
|
||||
target_speed,
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist before observe:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Проверяем исходное состояние Elica.
|
||||
#
|
||||
# Она OFF, поэтому ассистент должен
|
||||
# остаться IDLE и разрешить захват.
|
||||
# --------------------------------------------------
|
||||
|
||||
assist.observe_elica(
|
||||
power=state.power,
|
||||
speed=state.speed,
|
||||
timer=state.timer,
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist after observe:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if assist.mode.value != "idle":
|
||||
raise RuntimeError(
|
||||
"Auto Assist did not remain IDLE "
|
||||
"with Elica switched off"
|
||||
)
|
||||
|
||||
if target_speed is None:
|
||||
raise RuntimeError(
|
||||
"No Elica target speed "
|
||||
"for current Tion speed"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"AUTO ASSIST IS NOW GOING TO "
|
||||
f"SET ELICA SPEED {target_speed}"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# ПЕРВАЯ РЕАЛЬНАЯ КОМАНДА.
|
||||
#
|
||||
# Из OFF вызов set_speed(1) нажмёт Speed
|
||||
# и дождётся подтверждения:
|
||||
#
|
||||
# power=True
|
||||
# speed=1
|
||||
# --------------------------------------------------
|
||||
|
||||
result = await controller.set_speed(
|
||||
target_speed
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Confirmed Elica:",
|
||||
{
|
||||
"power": result.power,
|
||||
"speed": result.speed,
|
||||
"timer": result.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Только после подтверждённой команды
|
||||
# объявляем Elica принадлежащей автоматике.
|
||||
# --------------------------------------------------
|
||||
|
||||
assist.mark_owned(
|
||||
result.speed
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist after mark_owned:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Передаём подтверждённое фактическое
|
||||
# состояние ещё раз.
|
||||
#
|
||||
# Оно совпадает с expected_speed,
|
||||
# поэтому OWNED должен сохраниться.
|
||||
# --------------------------------------------------
|
||||
|
||||
assist.observe_elica(
|
||||
power=result.power,
|
||||
speed=result.speed,
|
||||
timer=result.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Assist after confirmed feedback:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Auto Assist did not enter OWNED state"
|
||||
)
|
||||
|
||||
if result.speed != target_speed:
|
||||
raise RuntimeError(
|
||||
"Elica reached unexpected speed"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica was acquired by Auto Assist "
|
||||
f"at speed {result.speed}."
|
||||
)
|
||||
|
||||
print(
|
||||
"Turn Elica off manually after the test."
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,368 @@
|
||||
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,
|
||||
ElicaService,
|
||||
)
|
||||
|
||||
from app.my_dataclasses import (
|
||||
ELICA_MQTT_HOST,
|
||||
ELICA_MQTT_PORT,
|
||||
)
|
||||
|
||||
|
||||
AUTO_CONFIG = "config/auto.yaml"
|
||||
|
||||
# Имитируем ситуацию:
|
||||
# AUTO-контроллер Tion выбрал скорость 4.
|
||||
TION_SPEED = 4
|
||||
|
||||
ELICA_READY_TIMEOUT = 15.0
|
||||
ELICA_CHANGE_TIMEOUT = 60.0
|
||||
|
||||
|
||||
class TestElicaService(ElicaService):
|
||||
"""
|
||||
Тестовая версия ElicaService.
|
||||
|
||||
Отличие только одно:
|
||||
используется отдельный MQTT client_id,
|
||||
чтобы не конфликтовать с запущенным
|
||||
ClimatController.
|
||||
"""
|
||||
|
||||
async def start(self) -> None:
|
||||
if self._running:
|
||||
return
|
||||
|
||||
client_id = (
|
||||
f"climatcontroller-elica-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:
|
||||
"""
|
||||
Ждём полноценного подключения Elica:
|
||||
MQTT + ESP32 online + обратная связь.
|
||||
"""
|
||||
|
||||
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 wait_for_elica_change(
|
||||
service: ElicaService,
|
||||
*,
|
||||
power: bool,
|
||||
speed: int,
|
||||
timer: bool,
|
||||
):
|
||||
"""
|
||||
Ждём реального ручного изменения Elica.
|
||||
|
||||
ВАЖНО:
|
||||
потеря MQTT, offline ESP32 или потеря
|
||||
feedback НЕ считаются ручным изменением.
|
||||
"""
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
deadline = (
|
||||
loop.time()
|
||||
+ ELICA_CHANGE_TIMEOUT
|
||||
)
|
||||
|
||||
while True:
|
||||
state = service.state
|
||||
|
||||
# ----------------------------------------------
|
||||
# Если связь временно потеряна,
|
||||
# состояние logical_state использовать нельзя.
|
||||
#
|
||||
# Иначе OFF/0 после disconnect можно ошибочно
|
||||
# принять за ручное выключение Elica.
|
||||
# ----------------------------------------------
|
||||
|
||||
ready = (
|
||||
state.mqtt_connected
|
||||
and state.device_online
|
||||
and service.feedback_ready
|
||||
)
|
||||
|
||||
if ready:
|
||||
logical = service.logical_state
|
||||
|
||||
changed = (
|
||||
logical.power != power
|
||||
or logical.speed != speed
|
||||
or logical.timer != timer
|
||||
)
|
||||
|
||||
if changed:
|
||||
return logical
|
||||
|
||||
if loop.time() >= deadline:
|
||||
raise RuntimeError(
|
||||
"No real Elica change detected "
|
||||
f"within "
|
||||
f"{ELICA_CHANGE_TIMEOUT} seconds"
|
||||
)
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
# --------------------------------------------------
|
||||
# Запускаем только тестовый ElicaService.
|
||||
#
|
||||
# Tion, Qingping, FastAPI и Schedule
|
||||
# здесь не участвуют.
|
||||
# --------------------------------------------------
|
||||
|
||||
service = TestElicaService(
|
||||
ElicaConfig(
|
||||
mqtt_host=ELICA_MQTT_HOST,
|
||||
mqtt_port=ELICA_MQTT_PORT,
|
||||
)
|
||||
)
|
||||
|
||||
print(
|
||||
"Starting Elica service..."
|
||||
)
|
||||
|
||||
await service.start()
|
||||
|
||||
try:
|
||||
print(
|
||||
f"Connecting to MQTT "
|
||||
f"{ELICA_MQTT_HOST}:"
|
||||
f"{ELICA_MQTT_PORT}..."
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Ждём реальную ESP32 Elica.
|
||||
# --------------------------------------------------
|
||||
|
||||
await wait_for_elica(
|
||||
service
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica connected and "
|
||||
"feedback ready"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Читаем текущее состояние.
|
||||
# --------------------------------------------------
|
||||
|
||||
elica = service.logical_state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Real Elica:",
|
||||
{
|
||||
"power": elica.power,
|
||||
"speed": elica.speed,
|
||||
"timer": elica.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Загружаем Auto Assist.
|
||||
# --------------------------------------------------
|
||||
|
||||
config = load_auto_config(
|
||||
AUTO_CONFIG
|
||||
)
|
||||
|
||||
assist = ElicaAutoAssistState(
|
||||
config.elica_assist
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Имитируем:
|
||||
#
|
||||
# Tion AUTO выбрал скорость 4.
|
||||
#
|
||||
# По текущей таблице:
|
||||
# Tion 4 -> Elica 1.
|
||||
# --------------------------------------------------
|
||||
|
||||
target = assist.set_tion_speed(
|
||||
TION_SPEED
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Tion AUTO speed:",
|
||||
TION_SPEED,
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica target:",
|
||||
target,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Before observe:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Передаём исходное реальное состояние Elica.
|
||||
# --------------------------------------------------
|
||||
|
||||
assist.observe_elica(
|
||||
power=elica.power,
|
||||
speed=elica.speed,
|
||||
timer=elica.timer,
|
||||
)
|
||||
|
||||
print(
|
||||
"After observe:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Запоминаем именно подтверждённое состояние.
|
||||
# --------------------------------------------------
|
||||
|
||||
initial_power = elica.power
|
||||
initial_speed = elica.speed
|
||||
initial_timer = elica.timer
|
||||
|
||||
print()
|
||||
print(
|
||||
"Now change Elica manually."
|
||||
)
|
||||
|
||||
print(
|
||||
"Change speed, turn it on/off "
|
||||
"or enable Timer."
|
||||
)
|
||||
|
||||
print(
|
||||
"The test is waiting for "
|
||||
"REAL MQTT feedback..."
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Здесь тест ждёт физического изменения Elica.
|
||||
# Disconnect теперь изменение не имитирует.
|
||||
# --------------------------------------------------
|
||||
|
||||
changed = await wait_for_elica_change(
|
||||
service,
|
||||
power=initial_power,
|
||||
speed=initial_speed,
|
||||
timer=initial_timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica changed:",
|
||||
{
|
||||
"power": changed.power,
|
||||
"speed": changed.speed,
|
||||
"timer": changed.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Передаём ручное изменение Auto Assist.
|
||||
# --------------------------------------------------
|
||||
|
||||
assist.observe_elica(
|
||||
power=changed.power,
|
||||
speed=changed.speed,
|
||||
timer=changed.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"After manual change:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,335 @@
|
||||
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,
|
||||
ElicaService,
|
||||
)
|
||||
|
||||
from app.my_dataclasses import (
|
||||
ELICA_MQTT_HOST,
|
||||
ELICA_MQTT_PORT,
|
||||
)
|
||||
|
||||
|
||||
AUTO_CONFIG = "config/auto.yaml"
|
||||
|
||||
ELICA_READY_TIMEOUT = 15.0
|
||||
ELICA_CHANGE_TIMEOUT = 60.0
|
||||
|
||||
|
||||
class TestElicaService(ElicaService):
|
||||
async def start(self) -> None:
|
||||
if self._running:
|
||||
return
|
||||
|
||||
client_id = (
|
||||
f"climatcontroller-elica-owned-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 wait_for_elica_change(
|
||||
service: ElicaService,
|
||||
*,
|
||||
power: bool,
|
||||
speed: int,
|
||||
timer: bool,
|
||||
):
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
deadline = (
|
||||
loop.time()
|
||||
+ ELICA_CHANGE_TIMEOUT
|
||||
)
|
||||
|
||||
while True:
|
||||
state = service.state
|
||||
|
||||
ready = (
|
||||
state.mqtt_connected
|
||||
and state.device_online
|
||||
and service.feedback_ready
|
||||
)
|
||||
|
||||
if ready:
|
||||
logical = service.logical_state
|
||||
|
||||
if (
|
||||
logical.power != power
|
||||
or logical.speed != speed
|
||||
or logical.timer != timer
|
||||
):
|
||||
return logical
|
||||
|
||||
if loop.time() >= deadline:
|
||||
raise RuntimeError(
|
||||
"No real Elica change detected "
|
||||
f"within {ELICA_CHANGE_TIMEOUT} seconds"
|
||||
)
|
||||
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
service = TestElicaService(
|
||||
ElicaConfig(
|
||||
mqtt_host=ELICA_MQTT_HOST,
|
||||
mqtt_port=ELICA_MQTT_PORT,
|
||||
)
|
||||
)
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
elica = service.logical_state
|
||||
|
||||
print()
|
||||
print(
|
||||
"Current Elica:",
|
||||
{
|
||||
"power": elica.power,
|
||||
"speed": elica.speed,
|
||||
"timer": elica.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# Для этого теста Elica должна быть
|
||||
# уже включена на скорости 1, 2 или 3.
|
||||
if (
|
||||
not elica.power
|
||||
or elica.speed not in (1, 2, 3)
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Before starting this test, "
|
||||
"manually turn Elica on "
|
||||
"at speed 1, 2 or 3"
|
||||
)
|
||||
|
||||
config = load_auto_config(
|
||||
AUTO_CONFIG
|
||||
)
|
||||
|
||||
assist = ElicaAutoAssistState(
|
||||
config.elica_assist
|
||||
)
|
||||
|
||||
# Находим скорость Tion, которой
|
||||
# соответствует текущая скорость Elica.
|
||||
tion_speed = None
|
||||
|
||||
for (
|
||||
candidate_tion_speed,
|
||||
candidate_elica_speed,
|
||||
) in config.elica_assist.speed_map:
|
||||
if (
|
||||
candidate_elica_speed
|
||||
== elica.speed
|
||||
):
|
||||
tion_speed = (
|
||||
candidate_tion_speed
|
||||
)
|
||||
break
|
||||
|
||||
if tion_speed is None:
|
||||
raise RuntimeError(
|
||||
"Current Elica speed is not "
|
||||
"present in elica_assist.speed_map"
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Имитируем:
|
||||
#
|
||||
# Tion AUTO потребовал такую скорость Elica,
|
||||
# и автоматика якобы успешно её установила.
|
||||
# --------------------------------------------------
|
||||
|
||||
target = assist.set_tion_speed(
|
||||
tion_speed
|
||||
)
|
||||
|
||||
assist.mark_owned(
|
||||
elica.speed
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Tion AUTO speed:",
|
||||
tion_speed,
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica target:",
|
||||
target,
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist after mark_owned:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Передаём совпадающую обратную связь.
|
||||
#
|
||||
# OWNED должен сохраниться.
|
||||
# --------------------------------------------------
|
||||
|
||||
assist.observe_elica(
|
||||
power=elica.power,
|
||||
speed=elica.speed,
|
||||
timer=elica.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"After matching feedback:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
initial_power = elica.power
|
||||
initial_speed = elica.speed
|
||||
initial_timer = elica.timer
|
||||
|
||||
print()
|
||||
print(
|
||||
"Now change Elica manually."
|
||||
)
|
||||
|
||||
print(
|
||||
"For example, change its speed."
|
||||
)
|
||||
|
||||
print(
|
||||
"The test is waiting for "
|
||||
"REAL MQTT feedback..."
|
||||
)
|
||||
|
||||
changed = await wait_for_elica_change(
|
||||
service,
|
||||
power=initial_power,
|
||||
speed=initial_speed,
|
||||
timer=initial_timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica changed:",
|
||||
{
|
||||
"power": changed.power,
|
||||
"speed": changed.speed,
|
||||
"timer": changed.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# --------------------------------------------------
|
||||
# Теперь Auto Assist должен увидеть,
|
||||
# что фактическое состояние больше
|
||||
# не совпадает с expected_speed.
|
||||
# --------------------------------------------------
|
||||
|
||||
assist.observe_elica(
|
||||
power=changed.power,
|
||||
speed=changed.speed,
|
||||
timer=changed.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"After manual intervention:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -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())
|
||||
@@ -0,0 +1,316 @@
|
||||
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())
|
||||
@@ -0,0 +1,448 @@
|
||||
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-down-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 set_speed_with_trace(
|
||||
controller: ElicaController,
|
||||
service: ElicaService,
|
||||
target_speed: int,
|
||||
):
|
||||
"""
|
||||
Выполняет обычный controller.set_speed(),
|
||||
параллельно записывая фактические скорости Elica.
|
||||
|
||||
Это позволяет увидеть реальный цикл:
|
||||
2 -> 3 -> 4 -> 1
|
||||
"""
|
||||
|
||||
path: list[int] = []
|
||||
|
||||
initial = service.logical_state
|
||||
|
||||
if (
|
||||
initial.power
|
||||
and 1 <= initial.speed <= 4
|
||||
):
|
||||
path.append(initial.speed)
|
||||
|
||||
task = asyncio.create_task(
|
||||
controller.set_speed(
|
||||
target_speed
|
||||
)
|
||||
)
|
||||
|
||||
while not task.done():
|
||||
state = service.logical_state
|
||||
|
||||
if (
|
||||
state.power
|
||||
and 1 <= state.speed <= 4
|
||||
):
|
||||
if (
|
||||
not path
|
||||
or path[-1] != state.speed
|
||||
):
|
||||
path.append(
|
||||
state.speed
|
||||
)
|
||||
|
||||
await asyncio.sleep(0.02)
|
||||
|
||||
result = await task
|
||||
|
||||
# На всякий случай фиксируем конечное
|
||||
# подтверждённое состояние.
|
||||
if (
|
||||
result.power
|
||||
and 1 <= result.speed <= 4
|
||||
and (
|
||||
not path
|
||||
or path[-1] != result.speed
|
||||
)
|
||||
):
|
||||
path.append(
|
||||
result.speed
|
||||
)
|
||||
|
||||
return result, path
|
||||
|
||||
|
||||
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,
|
||||
},
|
||||
)
|
||||
|
||||
# Для чистоты начинаем с OFF.
|
||||
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 = 5
|
||||
# Elica target = 2
|
||||
#
|
||||
# Получаем OWNED на скорости 2.
|
||||
# ==================================================
|
||||
|
||||
target_2 = assist.set_tion_speed(
|
||||
5
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Tion AUTO speed:",
|
||||
5,
|
||||
)
|
||||
|
||||
print(
|
||||
"Elica target:",
|
||||
target_2,
|
||||
)
|
||||
|
||||
if target_2 != 2:
|
||||
raise RuntimeError(
|
||||
f"Expected Elica target 2, "
|
||||
f"got {target_2}"
|
||||
)
|
||||
|
||||
assist.observe_elica(
|
||||
power=state.power,
|
||||
speed=state.speed,
|
||||
timer=state.timer,
|
||||
)
|
||||
|
||||
result_2 = await controller.set_speed(
|
||||
target_2
|
||||
)
|
||||
|
||||
assist.mark_owned(
|
||||
result_2.speed
|
||||
)
|
||||
|
||||
assist.observe_elica(
|
||||
power=result_2.power,
|
||||
speed=result_2.speed,
|
||||
timer=result_2.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Elica acquired:",
|
||||
{
|
||||
"power": result_2.power,
|
||||
"speed": result_2.speed,
|
||||
"timer": result_2.timer,
|
||||
},
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if (
|
||||
assist.mode.value != "owned"
|
||||
or result_2.speed != 2
|
||||
):
|
||||
raise RuntimeError(
|
||||
"Failed to acquire Elica "
|
||||
"at speed 2"
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ЭТАП 2
|
||||
#
|
||||
# Tion AUTO падает:
|
||||
#
|
||||
# 5 -> 4
|
||||
#
|
||||
# Новая цель Elica:
|
||||
#
|
||||
# 2 -> 1
|
||||
#
|
||||
# Но физически Elica должна пройти:
|
||||
#
|
||||
# 2 -> 3 -> 4 -> 1
|
||||
# ==================================================
|
||||
|
||||
target_1 = assist.set_tion_speed(
|
||||
4
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Tion AUTO speed:",
|
||||
4,
|
||||
)
|
||||
|
||||
print(
|
||||
"New Elica target:",
|
||||
target_1,
|
||||
)
|
||||
|
||||
print(
|
||||
"Assist before speed change:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
if target_1 != 1:
|
||||
raise RuntimeError(
|
||||
f"Expected Elica target 1, "
|
||||
f"got {target_1}"
|
||||
)
|
||||
|
||||
# ВАЖНО:
|
||||
#
|
||||
# Здесь НЕ вызываем observe_elica()
|
||||
# во время промежуточных скоростей.
|
||||
#
|
||||
# 3 и 4 являются частью нашей
|
||||
# собственной команды set_speed(1),
|
||||
# а не действиями пользователя.
|
||||
|
||||
result_1, speed_path = (
|
||||
await set_speed_with_trace(
|
||||
controller,
|
||||
service,
|
||||
target_1,
|
||||
)
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Observed speed path:",
|
||||
speed_path,
|
||||
)
|
||||
|
||||
print(
|
||||
"Confirmed Elica:",
|
||||
{
|
||||
"power": result_1.power,
|
||||
"speed": result_1.speed,
|
||||
"timer": result_1.timer,
|
||||
},
|
||||
)
|
||||
|
||||
# Только ПОСЛЕ завершения всей нашей
|
||||
# последовательности обновляем владение.
|
||||
assist.mark_owned(
|
||||
result_1.speed
|
||||
)
|
||||
|
||||
assist.observe_elica(
|
||||
power=result_1.power,
|
||||
speed=result_1.speed,
|
||||
timer=result_1.timer,
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Assist after speed change:",
|
||||
assist.status(),
|
||||
)
|
||||
|
||||
# ==================================================
|
||||
# ПРОВЕРКИ
|
||||
# ==================================================
|
||||
|
||||
if speed_path != [2, 3, 4, 1]:
|
||||
raise RuntimeError(
|
||||
"Unexpected Elica speed path: "
|
||||
f"{speed_path}; "
|
||||
"expected [2, 3, 4, 1]"
|
||||
)
|
||||
|
||||
if result_1.speed != 1:
|
||||
raise RuntimeError(
|
||||
"Elica did not reach speed 1"
|
||||
)
|
||||
|
||||
if assist.mode.value != "owned":
|
||||
raise RuntimeError(
|
||||
"Auto Assist lost OWNED "
|
||||
"during automatic speed change"
|
||||
)
|
||||
|
||||
if assist.expected_speed != 1:
|
||||
raise RuntimeError(
|
||||
"Auto Assist expected_speed "
|
||||
"was not updated to 1"
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"TEST PASSED"
|
||||
)
|
||||
|
||||
print(
|
||||
"Automatic Elica transition:"
|
||||
)
|
||||
|
||||
print(
|
||||
"2 -> 3 -> 4 -> 1"
|
||||
)
|
||||
|
||||
print(
|
||||
"Auto Assist kept OWNED."
|
||||
)
|
||||
|
||||
print()
|
||||
print(
|
||||
"Turn Elica off manually "
|
||||
"after the test."
|
||||
)
|
||||
|
||||
finally:
|
||||
print()
|
||||
print(
|
||||
"Stopping Elica service..."
|
||||
)
|
||||
|
||||
await service.stop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user