- Add Tasmota device discovery and switchbar quick access

- Features:
    - Tasmota device discovery: Auto-detect network and scan for Tasmota devices
      in Add Smart Plug modal. Supports devices with/without authentication.
    - Switchbar: Quick access widget in sidebar footer for controlling smart plugs
      from anywhere in the app. Shows real-time status and power consumption.
This commit is contained in:
maziggy
2025-12-28 15:02:42 +01:00
parent b3ef6ccb32
commit 0bcedd2c16
18 changed files with 9488 additions and 8725 deletions
@@ -25,11 +25,9 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_list_smart_plugs_with_data(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_list_smart_plugs_with_data(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify list returns existing plugs."""
plug = await smart_plug_factory(name="Test Plug 1")
await smart_plug_factory(name="Test Plug 1")
response = await async_client.get("/api/v1/smart-plugs/")
@@ -64,9 +62,7 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_create_smart_plug_with_printer(
self, async_client: AsyncClient, printer_factory, db_session
):
async def test_create_smart_plug_with_printer(self, async_client: AsyncClient, printer_factory, db_session):
"""Verify smart plug can be linked to a printer."""
printer = await printer_factory(name="Test Printer")
@@ -84,9 +80,7 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_create_plug_with_invalid_printer_id(
self, async_client: AsyncClient
):
async def test_create_plug_with_invalid_printer_id(self, async_client: AsyncClient):
"""Verify creating plug with non-existent printer fails."""
data = {
"name": "Test Plug",
@@ -105,9 +99,7 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_get_smart_plug(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_get_smart_plug(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify single plug can be retrieved."""
plug = await smart_plug_factory(name="Get Test Plug")
@@ -132,9 +124,7 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_update_auto_off_toggle(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_update_auto_off_toggle(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""CRITICAL: Verify auto_off toggle persists correctly.
This tests the regression scenario where toggling auto_off
@@ -149,10 +139,7 @@ class TestSmartPlugsAPI:
assert response.json()["auto_off"] is True
# Toggle auto_off to False
response = await async_client.patch(
f"/api/v1/smart-plugs/{plug.id}",
json={"auto_off": False}
)
response = await async_client.patch(f"/api/v1/smart-plugs/{plug.id}", json={"auto_off": False})
assert response.status_code == 200
assert response.json()["auto_off"] is False
@@ -163,16 +150,11 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_update_auto_on_toggle(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_update_auto_on_toggle(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify auto_on toggle persists correctly."""
plug = await smart_plug_factory(auto_on=True)
response = await async_client.patch(
f"/api/v1/smart-plugs/{plug.id}",
json={"auto_on": False}
)
response = await async_client.patch(f"/api/v1/smart-plugs/{plug.id}", json={"auto_on": False})
assert response.status_code == 200
assert response.json()["auto_on"] is False
@@ -183,31 +165,23 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_update_enabled_toggle(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_update_enabled_toggle(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify enabled toggle persists correctly."""
plug = await smart_plug_factory(enabled=True)
response = await async_client.patch(
f"/api/v1/smart-plugs/{plug.id}",
json={"enabled": False}
)
response = await async_client.patch(f"/api/v1/smart-plugs/{plug.id}", json={"enabled": False})
assert response.status_code == 200
assert response.json()["enabled"] is False
@pytest.mark.asyncio
@pytest.mark.integration
async def test_update_off_delay_mode(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_update_off_delay_mode(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify off_delay_mode can be changed."""
plug = await smart_plug_factory(off_delay_mode="time")
response = await async_client.patch(
f"/api/v1/smart-plugs/{plug.id}",
json={"off_delay_mode": "temperature", "off_temp_threshold": 50}
f"/api/v1/smart-plugs/{plug.id}", json={"off_delay_mode": "temperature", "off_temp_threshold": 50}
)
assert response.status_code == 200
@@ -217,9 +191,7 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_update_schedule_settings(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_update_schedule_settings(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify schedule settings can be updated."""
plug = await smart_plug_factory(schedule_enabled=False)
@@ -229,7 +201,7 @@ class TestSmartPlugsAPI:
"schedule_enabled": True,
"schedule_on_time": "08:00",
"schedule_off_time": "22:00",
}
},
)
assert response.status_code == 200
@@ -240,9 +212,7 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_update_multiple_fields(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_update_multiple_fields(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify multiple fields can be updated at once."""
plug = await smart_plug_factory(
name="Old Name",
@@ -256,7 +226,7 @@ class TestSmartPlugsAPI:
"name": "New Name",
"auto_on": False,
"auto_off": False,
}
},
)
assert response.status_code == 200
@@ -277,10 +247,7 @@ class TestSmartPlugsAPI:
"""Verify smart plug can be turned on."""
plug = await smart_plug_factory()
response = await async_client.post(
f"/api/v1/smart-plugs/{plug.id}/control",
json={"action": "on"}
)
response = await async_client.post(f"/api/v1/smart-plugs/{plug.id}/control", json={"action": "on"})
assert response.status_code == 200
result = response.json()
@@ -295,10 +262,7 @@ class TestSmartPlugsAPI:
"""Verify smart plug can be turned off."""
plug = await smart_plug_factory()
response = await async_client.post(
f"/api/v1/smart-plugs/{plug.id}/control",
json={"action": "off"}
)
response = await async_client.post(f"/api/v1/smart-plugs/{plug.id}/control", json={"action": "off"})
assert response.status_code == 200
result = response.json()
@@ -313,10 +277,7 @@ class TestSmartPlugsAPI:
"""Verify smart plug can be toggled."""
plug = await smart_plug_factory()
response = await async_client.post(
f"/api/v1/smart-plugs/{plug.id}/control",
json={"action": "toggle"}
)
response = await async_client.post(f"/api/v1/smart-plugs/{plug.id}/control", json={"action": "toggle"})
assert response.status_code == 200
result = response.json()
@@ -325,16 +286,11 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_control_invalid_action(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_control_invalid_action(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify invalid action returns error."""
plug = await smart_plug_factory()
response = await async_client.post(
f"/api/v1/smart-plugs/{plug.id}/control",
json={"action": "invalid"}
)
response = await async_client.post(f"/api/v1/smart-plugs/{plug.id}/control", json={"action": "invalid"})
# FastAPI returns 422 for pydantic validation errors
assert response.status_code == 422
@@ -364,9 +320,7 @@ class TestSmartPlugsAPI:
@pytest.mark.asyncio
@pytest.mark.integration
async def test_delete_smart_plug(
self, async_client: AsyncClient, smart_plug_factory, db_session
):
async def test_delete_smart_plug(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify smart plug can be deleted."""
plug = await smart_plug_factory()
plug_id = plug.id
@@ -386,3 +340,70 @@ class TestSmartPlugsAPI:
response = await async_client.delete("/api/v1/smart-plugs/9999")
assert response.status_code == 404
# ========================================================================
# Switchbar visibility
# ========================================================================
@pytest.mark.asyncio
@pytest.mark.integration
async def test_update_show_in_switchbar(self, async_client: AsyncClient, smart_plug_factory, db_session):
"""Verify show_in_switchbar toggle persists correctly."""
plug = await smart_plug_factory(show_in_switchbar=False)
response = await async_client.patch(f"/api/v1/smart-plugs/{plug.id}", json={"show_in_switchbar": True})
assert response.status_code == 200
assert response.json()["show_in_switchbar"] is True
# Verify persistence
response = await async_client.get(f"/api/v1/smart-plugs/{plug.id}")
assert response.json()["show_in_switchbar"] is True
# ========================================================================
# Tasmota Discovery endpoints
# ========================================================================
@pytest.mark.asyncio
@pytest.mark.integration
async def test_tasmota_discovery_scan(self, async_client: AsyncClient):
"""Verify Tasmota discovery scan can be started."""
response = await async_client.post("/api/v1/smart-plugs/discover/scan")
assert response.status_code == 200
data = response.json()
assert "running" in data
assert "scanned" in data
assert "total" in data
@pytest.mark.asyncio
@pytest.mark.integration
async def test_tasmota_discovery_status(self, async_client: AsyncClient):
"""Verify Tasmota discovery status endpoint works."""
response = await async_client.get("/api/v1/smart-plugs/discover/status")
assert response.status_code == 200
data = response.json()
assert "running" in data
assert "scanned" in data
assert "total" in data
@pytest.mark.asyncio
@pytest.mark.integration
async def test_tasmota_discovery_devices(self, async_client: AsyncClient):
"""Verify Tasmota discovered devices endpoint works."""
response = await async_client.get("/api/v1/smart-plugs/discover/devices")
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
@pytest.mark.asyncio
@pytest.mark.integration
async def test_tasmota_discovery_stop(self, async_client: AsyncClient):
"""Verify Tasmota discovery can be stopped."""
response = await async_client.post("/api/v1/smart-plugs/discover/stop")
assert response.status_code == 200
data = response.json()
assert "running" in data