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
+47 -2
View File
@@ -367,7 +367,7 @@ class TestPostRepublish:
mock_settings.ha_discovery_enabled = True
mock_settings.ha_discovery_prefix = "homeassistant"
with patch("app.api.routes.api.expose.get_settings", return_value=mock_settings):
with patch("app.api.routes.api.expose.build_runtime_settings", return_value=mock_settings):
with patch("app.api.routes.api.expose.mqtt_manager") as mock_mqtt:
mock_mqtt.is_configured.return_value = True
mock_mqtt.is_connected = True
@@ -406,7 +406,7 @@ class TestPostRepublish:
mock_settings.mqtt_enabled = True
mock_settings.ha_discovery_enabled = True
with patch("app.api.routes.api.expose.get_settings", return_value=mock_settings):
with patch("app.api.routes.api.expose.build_runtime_settings", return_value=mock_settings):
with patch("app.api.routes.api.expose.mqtt_manager") as mock_mqtt:
mock_mqtt.is_connected = False
resp = client.post(
@@ -475,3 +475,48 @@ class TestCatalogGrouping:
components = {e["entity"]["component"] for e in catalog}
assert "binary_sensor" in components
assert "sensor" in components
# ---------------------------------------------------------------------------
# M5-fix2: DB-config → mqtt_status runtime assertion (Issue 2)
# ---------------------------------------------------------------------------
class TestMqttStatusReadsFromDB:
def test_get_expose_mqtt_configured_reflects_db_value(self, expose_client):
"""GET /api/expose must return mqtt_configured=True when MQTT is enabled+configured
in the DB (app_config), even if the bootstrap env says disabled.
This verifies that _get_mqtt_status uses build_runtime_settings (DB-merged)
rather than bare get_settings() (bootstrap-only).
"""
client, engine = expose_client
_login(client)
# Write MQTT_ENABLED=true and MQTT_BROKER_HOST into app_config directly.
# The lifespan seeds all config keys on startup, so we must UPDATE existing rows.
from datetime import UTC, datetime
from app.models.config import AppConfigEntry
now = datetime.now(UTC)
with Session(engine) as session:
for key, value in [("MQTT_ENABLED", "true"), ("MQTT_BROKER_HOST", "broker.test.local")]:
row = session.query(AppConfigEntry).filter(AppConfigEntry.key == key).first()
if row is None:
session.add(AppConfigEntry(key=key, value=value, updated_at=now))
else:
row.value = value
row.updated_at = now
session.commit()
# The bootstrap env does NOT have MQTT enabled (default False in Settings).
# But GET /api/expose should still report mqtt_configured=True because it reads DB.
with patch("app.integrations.expose._REGISTRY", []):
resp = client.get("/api/expose")
assert resp.status_code == 200
mqtt_status = resp.json()["mqtt_status"]
assert mqtt_status["mqtt_configured"] is True, (
f"Expected mqtt_configured=True from DB settings, "
f"got {mqtt_status['mqtt_configured']!r}. Full status: {mqtt_status}"
)