work: Низкий уровень работы с Элика завершен. Добавил логику для отслеживания состояния вытяжки.

This commit is contained in:
2026-09-22 22:01:57 +03:00
parent 8ece9ba131
commit 4169b0c3e5
4 changed files with 139 additions and 3 deletions
+13 -1
View File
@@ -19,6 +19,17 @@ class ElicaConfig:
light_relay: int = 3 light_relay: int = 3
timer_relay: int = 4 timer_relay: int = 4
# Индикаторы Elica -> IN
speed_1_input: int = 1
speed_2_input: int = 2
speed_3_input: int = 3
speed_4_input: int = 4
power_input: int = 5
# Определение мигания индикатора скорости.
blink_max_off_s: float = 1.5
blink_hold_s: float = 2.5
@property @property
def status_topic(self) -> str: def status_topic(self) -> str:
""" """
@@ -66,4 +77,5 @@ class ElicaConfig:
@property @property
def input_state_wildcard(self) -> str: def input_state_wildcard(self) -> str:
return f"{self.topic}/input/+/state" return f"{self.topic}/input/+/state"
+19
View File
@@ -37,3 +37,22 @@ class ElicaState:
return result return result
@dataclass(frozen=True, slots=True)
class ElicaPanelState:
speed_leds: tuple[bool, bool, bool, bool] = (
False, False, False, False
)
power_led: bool = False
def to_dict(self) -> dict:
return asdict(self)
@dataclass(frozen=True, slots=True)
class ElicaLogicalState:
power: bool = False
speed: int = 0
timer: bool = False
def to_dict(self) -> dict:
return asdict(self)
View File
+107 -2
View File
@@ -5,7 +5,7 @@ import paho.mqtt.client as mqtt
from dataclasses import replace from dataclasses import replace
from datetime import datetime, timezone from datetime import datetime, timezone
from .config import ElicaConfig from .config import ElicaConfig
from .models import ElicaState from .models import ElicaState, ElicaPanelState, ElicaLogicalState
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -28,6 +28,9 @@ class ElicaService:
self._state = ElicaState() self._state = ElicaState()
self._running = False self._running = False
self._client: mqtt.Client | None = None self._client: mqtt.Client | None = None
self._last_speed = 0
self._speed_led_off_at: float | None = None
self._last_blink_at: float | None = None
@property @property
@@ -290,6 +293,7 @@ class ElicaService:
input_states=tuple(input_states), input_states=tuple(input_states),
last_message_at=datetime.now(timezone.utc), last_message_at=datetime.now(timezone.utc),
) )
self._update_panel_tracker()
def input_state(self, input_number: int) -> bool: def input_state(self, input_number: int) -> bool:
"""Возвращает последнее известное состояние IN1-IN8.""" """Возвращает последнее известное состояние IN1-IN8."""
@@ -298,6 +302,89 @@ class ElicaService:
return self._state.input_states[input_number - 1] return self._state.input_states[input_number - 1]
# ------------------------------------------------------------------
# Elica state tracking
# ------------------------------------------------------------------
def _update_panel_tracker(self) -> None:
"""Отслеживает скорость и мигание индикатора таймера."""
panel = self.panel_state
now = time.monotonic()
if not panel.power_led:
self._last_speed = 0
self._speed_led_off_at = None
self._last_blink_at = None
return
active_speeds = [
index + 1
for index, enabled in enumerate(panel.speed_leds)
if enabled
]
if active_speeds:
speed = active_speeds[0]
if speed != self._last_speed:
self._last_speed = speed
self._speed_led_off_at = None
self._last_blink_at = None
return
if self._speed_led_off_at is not None:
off_time = now - self._speed_led_off_at
if off_time <= self._config.blink_max_off_s:
self._last_blink_at = now
self._speed_led_off_at = None
return
if self._last_speed > 0 and self._speed_led_off_at is None:
self._speed_led_off_at = now
@property
def logical_state(self) -> ElicaLogicalState:
"""Возвращает интерпретированное состояние вытяжки Elica."""
panel = self.panel_state
now = time.monotonic()
if not panel.power_led:
return ElicaLogicalState()
active_speeds = [
index + 1
for index, enabled in enumerate(panel.speed_leds)
if enabled
]
if active_speeds:
speed = active_speeds[0]
elif (
self._last_speed > 0
and self._speed_led_off_at is not None
and now - self._speed_led_off_at <= self._config.blink_max_off_s
):
speed = self._last_speed
else:
speed = 0
timer = (
speed > 0
and self._last_blink_at is not None
and now - self._last_blink_at <= self._config.blink_hold_s
)
return ElicaLogicalState(
power=True,
speed=speed,
timer=timer,
)
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# MQTT publish # MQTT publish
# ------------------------------------------------------------------ # ------------------------------------------------------------------
@@ -368,6 +455,8 @@ class ElicaService:
"running": self._running, "running": self._running,
"topic": self._config.topic, "topic": self._config.topic,
"pulse_ms": self._config.pulse_ms, "pulse_ms": self._config.pulse_ms,
"panel": self.panel_state.to_dict(),
"elica": self.logical_state.to_dict(),
**self._state.to_dict(), **self._state.to_dict(),
} }
@@ -428,4 +517,20 @@ class ElicaService:
async def press_timer(self) -> None: async def press_timer(self) -> None:
"""Эмулирует нажатие кнопки Timer.""" """Эмулирует нажатие кнопки Timer."""
await self.pulse_relay(self._config.timer_relay) await self.pulse_relay(self._config.timer_relay)
# ------------------------------------------------------------------
# Elica panel feedback
# ------------------------------------------------------------------
@property
def panel_state(self) -> ElicaPanelState:
"""Возвращает текущее физическое состояние индикаторов Elica."""
return ElicaPanelState(
speed_leds=(
self.input_state(self._config.speed_1_input),
self.input_state(self._config.speed_2_input),
self.input_state(self._config.speed_3_input),
self.input_state(self._config.speed_4_input),
),
power_led=self.input_state(self._config.power_input),
)