M5: use DB-merged runtime settings for MQTT/discovery; add MQTT test button; move Expose panel into config accordion

This commit is contained in:
2026-06-22 20:11:54 +02:00
parent 75d685f04d
commit 35cdc7f22a
11 changed files with 502 additions and 59 deletions
+10 -11
View File
@@ -35,6 +35,7 @@ from sqlalchemy.orm import Session
from app.integrations.expose import ExposableEntity, build_catalog
from app.integrations.mqtt import mqtt_manager
from app.services.config_page import build_runtime_settings
logger = logging.getLogger(__name__)
@@ -44,12 +45,6 @@ logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
def _get_settings() -> Any:
"""Return the current runtime settings (cached singleton)."""
from app.config import get_settings
return get_settings()
def _should_publish(settings: Any) -> bool:
"""Return True only if MQTT and HA Discovery are both enabled and connected."""
return bool(
@@ -180,7 +175,8 @@ def publish_discovery(session: Session) -> None:
No-op if MQTT / discovery is not enabled or the client is not connected.
All MQTT errors are caught internally — this function never raises.
"""
settings = _get_settings()
from app.config import get_settings
settings = build_runtime_settings(session, get_settings())
if not _should_publish(settings):
logger.debug("publish_discovery: skipped (MQTT/Discovery not enabled or not connected)")
return
@@ -232,7 +228,8 @@ def publish_states(session: Session) -> None:
No-op if MQTT / discovery is not enabled or the client is not connected.
All errors are caught internally — this function never raises.
"""
settings = _get_settings()
from app.config import get_settings
settings = build_runtime_settings(session, get_settings())
if not _should_publish(settings):
logger.debug("publish_states: skipped (MQTT/Discovery not enabled or not connected)")
return
@@ -320,7 +317,8 @@ def publish_device_state(session: Session, device: Any) -> None:
No-op if MQTT / discovery is not enabled or the client is not connected.
All errors are caught internally — never raises.
"""
settings = _get_settings()
from app.config import get_settings
settings = build_runtime_settings(session, get_settings())
if not _should_publish(settings):
return
@@ -386,7 +384,7 @@ def publish_device_state(session: Session, device: Any) -> None:
)
def publish_device_offline(device_uuid: str) -> None:
def publish_device_offline(session: Session, device_uuid: str) -> None:
"""Publish an "offline" availability payload for *device_uuid*.
Called by ``modbus_poll.poll_device`` when a poll fails, to immediately
@@ -395,7 +393,8 @@ def publish_device_offline(device_uuid: str) -> None:
No-op if MQTT / discovery is not enabled or the client is not connected.
Never raises.
"""
settings = _get_settings()
from app.config import get_settings
settings = build_runtime_settings(session, get_settings())
if not _should_publish(settings):
return
+1 -1
View File
@@ -132,7 +132,7 @@ def poll_device(session: Session, device: ModbusDevice) -> ModbusReading | None:
# Best-effort: publish "offline" availability after failed poll.
try:
publish_device_offline(device.uuid)
publish_device_offline(session, device.uuid)
except Exception: # noqa: BLE001
logger.exception(
"poll_device: MQTT offline publish failed for device %r (id=%d)",