smart_plugs.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. """API routes for smart plug management."""
  2. import logging
  3. from datetime import datetime, timedelta
  4. from fastapi import APIRouter, Body, Depends, HTTPException
  5. from pydantic import BaseModel
  6. from sqlalchemy import select
  7. from sqlalchemy.ext.asyncio import AsyncSession
  8. from backend.app.api.routes.settings import get_setting
  9. from backend.app.core.database import get_db
  10. from backend.app.models.printer import Printer
  11. from backend.app.models.smart_plug import SmartPlug
  12. from backend.app.schemas.smart_plug import (
  13. HAEntity,
  14. HASensorEntity,
  15. HATestConnectionRequest,
  16. HATestConnectionResponse,
  17. SmartPlugControl,
  18. SmartPlugCreate,
  19. SmartPlugEnergy,
  20. SmartPlugResponse,
  21. SmartPlugStatus,
  22. SmartPlugTestConnection,
  23. SmartPlugUpdate,
  24. )
  25. from backend.app.services.discovery import tasmota_scanner
  26. from backend.app.services.homeassistant import homeassistant_service
  27. from backend.app.services.mqtt_relay import mqtt_relay
  28. from backend.app.services.notification_service import notification_service
  29. from backend.app.services.printer_manager import printer_manager
  30. from backend.app.services.tasmota import tasmota_service
  31. logger = logging.getLogger(__name__)
  32. router = APIRouter(prefix="/smart-plugs", tags=["smart-plugs"])
  33. @router.get("/", response_model=list[SmartPlugResponse])
  34. async def list_smart_plugs(db: AsyncSession = Depends(get_db)):
  35. """List all smart plugs."""
  36. result = await db.execute(select(SmartPlug).order_by(SmartPlug.name))
  37. return list(result.scalars().all())
  38. @router.post("/", response_model=SmartPlugResponse)
  39. async def create_smart_plug(
  40. data: SmartPlugCreate,
  41. db: AsyncSession = Depends(get_db),
  42. ):
  43. """Create a new smart plug."""
  44. # Validate printer_id if provided
  45. if data.printer_id:
  46. result = await db.execute(select(Printer).where(Printer.id == data.printer_id))
  47. if not result.scalar_one_or_none():
  48. raise HTTPException(400, "Printer not found")
  49. # Check if printer already has a plug assigned
  50. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == data.printer_id))
  51. if result.scalar_one_or_none():
  52. raise HTTPException(400, "This printer already has a smart plug assigned")
  53. # For MQTT plugs, ensure MQTT broker is configured and service is connected
  54. if data.plug_type == "mqtt":
  55. # Try to configure the smart plug service if not already configured
  56. if not mqtt_relay.smart_plug_service.is_configured():
  57. # Get MQTT broker settings from database
  58. mqtt_broker = await get_setting(db, "mqtt_broker") or ""
  59. if not mqtt_broker:
  60. raise HTTPException(
  61. 400,
  62. "MQTT broker not configured. Please set MQTT broker address in Settings → Network → MQTT Publishing.",
  63. )
  64. # Configure the smart plug service with broker settings
  65. mqtt_settings = {
  66. "mqtt_enabled": True, # Enable for smart plug subscription
  67. "mqtt_broker": mqtt_broker,
  68. "mqtt_port": int(await get_setting(db, "mqtt_port") or "1883"),
  69. "mqtt_username": await get_setting(db, "mqtt_username") or "",
  70. "mqtt_password": await get_setting(db, "mqtt_password") or "",
  71. "mqtt_use_tls": (await get_setting(db, "mqtt_use_tls") or "false") == "true",
  72. }
  73. await mqtt_relay.smart_plug_service.configure(mqtt_settings)
  74. # Check if connection succeeded
  75. if not mqtt_relay.smart_plug_service.is_configured():
  76. raise HTTPException(
  77. 400,
  78. f"Failed to connect to MQTT broker at {mqtt_broker}. Please check your MQTT settings.",
  79. )
  80. plug = SmartPlug(**data.model_dump())
  81. db.add(plug)
  82. await db.commit()
  83. await db.refresh(plug)
  84. # Subscribe MQTT plugs to their topic
  85. if plug.plug_type == "mqtt" and plug.mqtt_topic:
  86. mqtt_relay.smart_plug_service.subscribe(
  87. plug_id=plug.id,
  88. topic=plug.mqtt_topic,
  89. power_path=plug.mqtt_power_path,
  90. energy_path=plug.mqtt_energy_path,
  91. state_path=plug.mqtt_state_path,
  92. multiplier=plug.mqtt_multiplier or 1.0,
  93. )
  94. logger.info(f"Created MQTT plug '{plug.name}' subscribed to {plug.mqtt_topic}")
  95. elif plug.plug_type == "homeassistant":
  96. logger.info(f"Created Home Assistant plug '{plug.name}' ({plug.ha_entity_id})")
  97. else:
  98. logger.info(f"Created Tasmota plug '{plug.name}' at {plug.ip_address}")
  99. return plug
  100. @router.get("/by-printer/{printer_id}", response_model=SmartPlugResponse | None)
  101. async def get_smart_plug_by_printer(printer_id: int, db: AsyncSession = Depends(get_db)):
  102. """Get the smart plug assigned to a printer."""
  103. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  104. plug = result.scalar_one_or_none()
  105. if not plug:
  106. return None
  107. return plug
  108. # Tasmota Discovery Endpoints
  109. # NOTE: These must be defined BEFORE /{plug_id} routes to avoid path conflicts
  110. class TasmotaScanRequest(BaseModel):
  111. """Request to scan for Tasmota devices."""
  112. from_ip: str | None = None # Starting IP (auto-detected if not provided)
  113. to_ip: str | None = None # Ending IP (auto-detected if not provided)
  114. timeout: float = 1.0 # Connection timeout per host
  115. def get_local_network_range() -> tuple[str, str]:
  116. """Auto-detect local network and return IP range to scan."""
  117. import socket
  118. try:
  119. # Get local IP by connecting to a public DNS (doesn't actually send data)
  120. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  121. s.connect(("8.8.8.8", 80))
  122. local_ip = s.getsockname()[0]
  123. s.close()
  124. # Parse IP and create range (assume /24 subnet)
  125. parts = local_ip.split(".")
  126. base = ".".join(parts[:3])
  127. from_ip = f"{base}.1"
  128. to_ip = f"{base}.254"
  129. logger.info(f"Auto-detected network: {from_ip} - {to_ip} (local IP: {local_ip})")
  130. return from_ip, to_ip
  131. except Exception as e:
  132. logger.error(f"Failed to detect local network: {e}")
  133. # Fallback to common home network
  134. return "192.168.1.1", "192.168.1.254"
  135. class TasmotaScanStatus(BaseModel):
  136. """Tasmota scan status response."""
  137. running: bool
  138. scanned: int
  139. total: int
  140. class DiscoveredTasmotaDevice(BaseModel):
  141. """Discovered Tasmota device."""
  142. ip_address: str
  143. name: str
  144. module: int | None = None
  145. state: str | None = None
  146. discovered_at: str | None = None
  147. @router.post("/discover/scan", response_model=TasmotaScanStatus)
  148. async def start_tasmota_scan(request: TasmotaScanRequest | None = Body(default=None)):
  149. """Start an IP range scan for Tasmota devices.
  150. Auto-detects local network if no IP range provided.
  151. """
  152. import asyncio
  153. # Auto-detect network
  154. from_ip, to_ip = get_local_network_range()
  155. timeout = request.timeout if request else 1.0
  156. # Start scan in background
  157. asyncio.create_task(tasmota_scanner.scan_range(from_ip, to_ip, timeout))
  158. # Return immediate status
  159. scanned, total = tasmota_scanner.progress
  160. return TasmotaScanStatus(
  161. running=tasmota_scanner.is_running,
  162. scanned=scanned,
  163. total=total,
  164. )
  165. @router.get("/discover/status", response_model=TasmotaScanStatus)
  166. async def get_tasmota_scan_status():
  167. """Get the current Tasmota scan status."""
  168. scanned, total = tasmota_scanner.progress
  169. return TasmotaScanStatus(
  170. running=tasmota_scanner.is_running,
  171. scanned=scanned,
  172. total=total,
  173. )
  174. @router.post("/discover/stop", response_model=TasmotaScanStatus)
  175. async def stop_tasmota_scan():
  176. """Stop the current Tasmota scan."""
  177. tasmota_scanner.stop()
  178. scanned, total = tasmota_scanner.progress
  179. return TasmotaScanStatus(
  180. running=tasmota_scanner.is_running,
  181. scanned=scanned,
  182. total=total,
  183. )
  184. @router.get("/discover/devices", response_model=list[DiscoveredTasmotaDevice])
  185. async def get_discovered_tasmota_devices():
  186. """Get list of discovered Tasmota devices."""
  187. return [
  188. DiscoveredTasmotaDevice(
  189. ip_address=d["ip_address"],
  190. name=d["name"],
  191. module=d.get("module"),
  192. state=d.get("state"),
  193. discovered_at=d.get("discovered_at"),
  194. )
  195. for d in tasmota_scanner.discovered_devices
  196. ]
  197. # Home Assistant Discovery Endpoints
  198. @router.post("/ha/test-connection", response_model=HATestConnectionResponse)
  199. async def test_ha_connection(request: HATestConnectionRequest):
  200. """Test connection to Home Assistant."""
  201. result = await homeassistant_service.test_connection(request.url, request.token)
  202. return HATestConnectionResponse(**result)
  203. @router.get("/ha/entities", response_model=list[HAEntity])
  204. async def list_ha_entities(
  205. db: AsyncSession = Depends(get_db),
  206. search: str | None = None,
  207. ):
  208. """List available Home Assistant entities.
  209. By default, returns switch/light/input_boolean entities.
  210. When search is provided, searches ALL entities by entity_id or friendly_name.
  211. Requires HA connection settings to be configured in Settings.
  212. """
  213. ha_url = await get_setting(db, "ha_url") or ""
  214. ha_token = await get_setting(db, "ha_token") or ""
  215. if not ha_url or not ha_token:
  216. raise HTTPException(
  217. 400, "Home Assistant not configured. Please set HA URL and token in Settings → Network → Home Assistant."
  218. )
  219. entities = await homeassistant_service.list_entities(ha_url, ha_token, search)
  220. return [HAEntity(**e) for e in entities]
  221. @router.get("/ha/sensors", response_model=list[HASensorEntity])
  222. async def list_ha_sensor_entities(db: AsyncSession = Depends(get_db)):
  223. """List available Home Assistant sensor entities for energy monitoring.
  224. Returns sensors with power/energy units (W, kW, kWh, Wh).
  225. Requires HA connection settings to be configured in Settings.
  226. """
  227. ha_url = await get_setting(db, "ha_url") or ""
  228. ha_token = await get_setting(db, "ha_token") or ""
  229. if not ha_url or not ha_token:
  230. raise HTTPException(
  231. 400, "Home Assistant not configured. Please set HA URL and token in Settings → Network → Home Assistant."
  232. )
  233. sensors = await homeassistant_service.list_sensor_entities(ha_url, ha_token)
  234. return [HASensorEntity(**s) for s in sensors]
  235. @router.get("/{plug_id}", response_model=SmartPlugResponse)
  236. async def get_smart_plug(plug_id: int, db: AsyncSession = Depends(get_db)):
  237. """Get a specific smart plug."""
  238. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  239. plug = result.scalar_one_or_none()
  240. if not plug:
  241. raise HTTPException(404, "Smart plug not found")
  242. return plug
  243. @router.patch("/{plug_id}", response_model=SmartPlugResponse)
  244. async def update_smart_plug(
  245. plug_id: int,
  246. data: SmartPlugUpdate,
  247. db: AsyncSession = Depends(get_db),
  248. ):
  249. """Update a smart plug."""
  250. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  251. plug = result.scalar_one_or_none()
  252. if not plug:
  253. raise HTTPException(404, "Smart plug not found")
  254. update_data = data.model_dump(exclude_unset=True)
  255. # Validate new printer_id if being changed
  256. if "printer_id" in update_data and update_data["printer_id"]:
  257. new_printer_id = update_data["printer_id"]
  258. # Check printer exists
  259. result = await db.execute(select(Printer).where(Printer.id == new_printer_id))
  260. if not result.scalar_one_or_none():
  261. raise HTTPException(400, "Printer not found")
  262. # Check if that printer already has a different plug assigned
  263. result = await db.execute(
  264. select(SmartPlug).where(
  265. SmartPlug.printer_id == new_printer_id,
  266. SmartPlug.id != plug_id,
  267. )
  268. )
  269. if result.scalar_one_or_none():
  270. raise HTTPException(400, "This printer already has a smart plug assigned")
  271. # Check if MQTT topic is changing - need to resubscribe
  272. old_topic = plug.mqtt_topic
  273. old_plug_type = plug.plug_type
  274. for field, value in update_data.items():
  275. setattr(plug, field, value)
  276. await db.commit()
  277. await db.refresh(plug)
  278. # Handle MQTT subscription changes
  279. if old_plug_type == "mqtt" and plug.plug_type != "mqtt":
  280. # Changed away from MQTT - unsubscribe
  281. mqtt_relay.smart_plug_service.unsubscribe(plug.id)
  282. elif plug.plug_type == "mqtt":
  283. # Is now MQTT - check if topic changed or newly MQTT
  284. if old_plug_type != "mqtt" or old_topic != plug.mqtt_topic:
  285. # Unsubscribe from old topic first
  286. if old_plug_type == "mqtt":
  287. mqtt_relay.smart_plug_service.unsubscribe(plug.id)
  288. # Subscribe to new topic
  289. if plug.mqtt_topic:
  290. mqtt_relay.smart_plug_service.subscribe(
  291. plug_id=plug.id,
  292. topic=plug.mqtt_topic,
  293. power_path=plug.mqtt_power_path,
  294. energy_path=plug.mqtt_energy_path,
  295. state_path=plug.mqtt_state_path,
  296. multiplier=plug.mqtt_multiplier or 1.0,
  297. )
  298. logger.info(f"Updated smart plug '{plug.name}'")
  299. return plug
  300. @router.delete("/{plug_id}")
  301. async def delete_smart_plug(plug_id: int, db: AsyncSession = Depends(get_db)):
  302. """Delete a smart plug."""
  303. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  304. plug = result.scalar_one_or_none()
  305. if not plug:
  306. raise HTTPException(404, "Smart plug not found")
  307. plug_name = plug.name
  308. plug_type = plug.plug_type
  309. # Unsubscribe MQTT plug before deletion
  310. if plug_type == "mqtt":
  311. mqtt_relay.smart_plug_service.unsubscribe(plug_id)
  312. await db.delete(plug)
  313. await db.commit()
  314. logger.info(f"Deleted smart plug '{plug_name}'")
  315. return {"message": "Smart plug deleted"}
  316. async def _get_service_for_plug(plug: SmartPlug, db: AsyncSession):
  317. """Get the appropriate service for the plug type.
  318. For HA plugs, configures the service with current settings from DB.
  319. """
  320. if plug.plug_type == "homeassistant":
  321. # Configure HA service with current settings
  322. ha_url = await get_setting(db, "ha_url") or ""
  323. ha_token = await get_setting(db, "ha_token") or ""
  324. homeassistant_service.configure(ha_url, ha_token)
  325. return homeassistant_service
  326. return tasmota_service
  327. @router.post("/{plug_id}/control")
  328. async def control_smart_plug(
  329. plug_id: int,
  330. control: SmartPlugControl,
  331. db: AsyncSession = Depends(get_db),
  332. ):
  333. """Manual control: on/off/toggle."""
  334. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  335. plug = result.scalar_one_or_none()
  336. if not plug:
  337. raise HTTPException(404, "Smart plug not found")
  338. # MQTT plugs are monitor-only - cannot control them
  339. if plug.plug_type == "mqtt":
  340. raise HTTPException(
  341. 400,
  342. "MQTT plugs are monitor-only. Use your MQTT broker or home automation system to control them.",
  343. )
  344. service = await _get_service_for_plug(plug, db)
  345. if control.action == "on":
  346. success = await service.turn_on(plug)
  347. expected_state = "ON"
  348. elif control.action == "off":
  349. success = await service.turn_off(plug)
  350. expected_state = "OFF"
  351. elif control.action == "toggle":
  352. success = await service.toggle(plug)
  353. expected_state = None # Unknown after toggle
  354. else:
  355. raise HTTPException(400, f"Invalid action: {control.action}")
  356. if not success:
  357. raise HTTPException(503, "Failed to communicate with device")
  358. # Update last state and reset auto_off_executed when turning on
  359. if expected_state:
  360. plug.last_state = expected_state
  361. if expected_state == "ON":
  362. plug.auto_off_executed = False # Reset flag when manually turning on
  363. elif expected_state == "OFF" and plug.printer_id:
  364. # Mark printer offline immediately for faster UI update
  365. printer_manager.mark_printer_offline(plug.printer_id)
  366. plug.last_checked = datetime.utcnow()
  367. await db.commit()
  368. # MQTT relay - publish smart plug state change
  369. if expected_state:
  370. try:
  371. from backend.app.services.mqtt_relay import mqtt_relay
  372. # Get printer name if linked
  373. printer_name = None
  374. if plug.printer_id:
  375. result = await db.execute(select(Printer).where(Printer.id == plug.printer_id))
  376. printer = result.scalar_one_or_none()
  377. printer_name = printer.name if printer else None
  378. await mqtt_relay.on_smart_plug_state(
  379. plug_id=plug.id,
  380. plug_name=plug.name,
  381. state="on" if expected_state == "ON" else "off",
  382. printer_id=plug.printer_id,
  383. printer_name=printer_name,
  384. )
  385. except Exception:
  386. pass # Don't fail if MQTT fails
  387. return {"success": True, "action": control.action}
  388. @router.get("/{plug_id}/status", response_model=SmartPlugStatus)
  389. async def get_plug_status(plug_id: int, db: AsyncSession = Depends(get_db)):
  390. """Get current plug status from device including energy data."""
  391. result = await db.execute(select(SmartPlug).where(SmartPlug.id == plug_id))
  392. plug = result.scalar_one_or_none()
  393. if not plug:
  394. raise HTTPException(404, "Smart plug not found")
  395. # Handle MQTT plugs - get data from subscription service
  396. if plug.plug_type == "mqtt":
  397. data = mqtt_relay.smart_plug_service.get_plug_data(plug_id)
  398. is_reachable = mqtt_relay.smart_plug_service.is_reachable(plug_id)
  399. if data:
  400. # Update last state in database
  401. if is_reachable and data.state:
  402. plug.last_state = data.state
  403. plug.last_checked = datetime.utcnow()
  404. await db.commit()
  405. energy_data = None
  406. if data.power is not None or data.energy is not None:
  407. energy_data = SmartPlugEnergy(
  408. power=data.power,
  409. today=data.energy,
  410. )
  411. # Check power alerts
  412. if data.power is not None:
  413. await check_power_alerts(plug, data.power, db)
  414. return SmartPlugStatus(
  415. state=data.state,
  416. reachable=is_reachable,
  417. device_name=None,
  418. energy=energy_data,
  419. )
  420. # No data received yet
  421. return SmartPlugStatus(
  422. state=None,
  423. reachable=False,
  424. device_name=None,
  425. energy=None,
  426. )
  427. # Handle Tasmota/HomeAssistant plugs
  428. service = await _get_service_for_plug(plug, db)
  429. status = await service.get_status(plug)
  430. # Update last state in database
  431. if status["reachable"]:
  432. plug.last_state = status["state"]
  433. plug.last_checked = datetime.utcnow()
  434. await db.commit()
  435. # Fetch energy data if device is reachable
  436. energy_data = None
  437. if status["reachable"]:
  438. energy = await service.get_energy(plug)
  439. if energy:
  440. energy_data = SmartPlugEnergy(**energy)
  441. # Check power alerts
  442. await check_power_alerts(plug, energy.get("power"), db)
  443. return SmartPlugStatus(
  444. state=status["state"],
  445. reachable=status["reachable"],
  446. device_name=status.get("device_name"),
  447. energy=energy_data,
  448. )
  449. async def check_power_alerts(plug: SmartPlug, current_power: float | None, db: AsyncSession):
  450. """Check if power crosses alert thresholds and send notifications."""
  451. if not plug.power_alert_enabled or current_power is None:
  452. return
  453. # Cooldown: don't alert more than once per 5 minutes
  454. cooldown_minutes = 5
  455. if plug.power_alert_last_triggered:
  456. time_since_last = datetime.utcnow() - plug.power_alert_last_triggered
  457. if time_since_last < timedelta(minutes=cooldown_minutes):
  458. return
  459. alert_triggered = False
  460. alert_type = None
  461. threshold = None
  462. # Check high threshold
  463. if plug.power_alert_high is not None and current_power > plug.power_alert_high:
  464. alert_triggered = True
  465. alert_type = "high"
  466. threshold = plug.power_alert_high
  467. # Check low threshold
  468. if plug.power_alert_low is not None and current_power < plug.power_alert_low:
  469. alert_triggered = True
  470. alert_type = "low"
  471. threshold = plug.power_alert_low
  472. if alert_triggered:
  473. plug.power_alert_last_triggered = datetime.utcnow()
  474. await db.commit()
  475. # Send notification
  476. title = f"Power Alert: {plug.name}"
  477. if alert_type == "high":
  478. message = f"Power consumption is {current_power:.1f}W, above threshold of {threshold:.1f}W"
  479. else:
  480. message = f"Power consumption is {current_power:.1f}W, below threshold of {threshold:.1f}W"
  481. logger.info(f"Power alert triggered for {plug.name}: {message}")
  482. # Use printer_error event type for power alerts (closest match)
  483. await notification_service.send_notification(
  484. event_type="printer_error",
  485. title=title,
  486. message=message,
  487. printer_id=plug.printer_id,
  488. printer_name=plug.name,
  489. context={
  490. "error_type": f"Power {alert_type.title()}",
  491. "error_detail": message,
  492. },
  493. )
  494. @router.post("/test-connection")
  495. async def test_connection(data: SmartPlugTestConnection):
  496. """Test connection to a Tasmota device."""
  497. result = await tasmota_service.test_connection(
  498. data.ip_address,
  499. data.username,
  500. data.password,
  501. )
  502. if not result["success"]:
  503. raise HTTPException(503, result.get("error", "Failed to connect to device"))
  504. return {
  505. "success": True,
  506. "state": result["state"],
  507. "device_name": result.get("device_name"),
  508. }