mqtt_smart_plug.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. """MQTT Smart Plug Service for subscribing to external MQTT topics and extracting power/energy data.
  2. This service enables integration with Shelly, Zigbee2MQTT, and other MQTT-based energy monitoring devices.
  3. """
  4. import asyncio
  5. import json
  6. import logging
  7. import threading
  8. from dataclasses import dataclass, field
  9. from datetime import datetime, timedelta, timezone
  10. from typing import Any
  11. import paho.mqtt.client as mqtt
  12. from backend.app.utils.paho_teardown import retire_paho_client
  13. logger = logging.getLogger(__name__)
  14. @dataclass
  15. class SmartPlugMQTTData:
  16. """Latest data received from an MQTT smart plug."""
  17. plug_id: int
  18. power: float | None = None # Current power in watts
  19. energy: float | None = None # Energy in kWh (today)
  20. energy_total: float | None = None # Lifetime kWh, never resets (#3582)
  21. state: str | None = None # "ON" or "OFF"
  22. # Aware, like the value a received message sets: is_reachable subtracts it
  23. # from an aware now, which raised TypeError before the first message.
  24. last_seen: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
  25. @dataclass
  26. class MQTTDataSourceConfig:
  27. """Configuration for a single MQTT data source (power, energy, or state)."""
  28. topic: str
  29. path: str
  30. multiplier: float = 1.0 # For power/energy
  31. on_value: str | None = None # For state (what value means "ON")
  32. class MQTTSmartPlugService:
  33. """Subscribes to MQTT topics for smart plug energy monitoring."""
  34. # Consider plug unreachable if no message received in this time
  35. REACHABLE_TIMEOUT_MINUTES = 5
  36. def __init__(self):
  37. self.client: mqtt.Client | None = None
  38. self.connected = False
  39. self._lock = threading.Lock()
  40. # topic -> list of (plug_id, data_type) where data_type is "power",
  41. # "energy", "energy_total" or "state"
  42. self.subscriptions: dict[str, list[tuple[int, str]]] = {}
  43. # plug_id -> {data_type: MQTTDataSourceConfig}
  44. self.plug_configs: dict[int, dict[str, MQTTDataSourceConfig]] = {}
  45. # plug_id -> latest data
  46. self.plug_data: dict[int, SmartPlugMQTTData] = {}
  47. self._disconnection_event: threading.Event | None = None
  48. self._configured = False
  49. self._broker = ""
  50. self._port = 1883
  51. self._username = ""
  52. self._password = ""
  53. self._use_tls = False
  54. def is_configured(self) -> bool:
  55. """Check if the MQTT service is configured and connected."""
  56. return self._configured and self.connected
  57. async def configure(self, settings: dict) -> bool:
  58. """Configure MQTT connection from settings.
  59. Uses the same broker settings as the MQTT relay service.
  60. Returns True if connection was successful or MQTT is disabled.
  61. """
  62. enabled = settings.get("mqtt_enabled", False)
  63. if not enabled:
  64. await self.disconnect()
  65. self._configured = False
  66. logger.debug("MQTT smart plug service disabled (MQTT relay not enabled)")
  67. return True
  68. broker = settings.get("mqtt_broker", "")
  69. port = settings.get("mqtt_port", 1883)
  70. username = settings.get("mqtt_username", "")
  71. password = settings.get("mqtt_password", "")
  72. use_tls = settings.get("mqtt_use_tls", False)
  73. if not broker:
  74. logger.warning("MQTT smart plug service: no broker configured")
  75. self._configured = False
  76. return False
  77. # Check if settings changed
  78. settings_changed = (
  79. self._broker != broker
  80. or self._port != port
  81. or self._username != username
  82. or self._password != password
  83. or self._use_tls != use_tls
  84. )
  85. self._broker = broker
  86. self._port = port
  87. self._username = username
  88. self._password = password
  89. self._use_tls = use_tls
  90. self._configured = True
  91. # Disconnect and reconnect if settings changed
  92. if settings_changed and self.client:
  93. await self.disconnect()
  94. # Connect if not already connected
  95. if not self.client or not self.connected:
  96. return await self._connect()
  97. return True
  98. async def _connect(self) -> bool:
  99. """Establish MQTT connection."""
  100. import asyncio
  101. import ssl
  102. try:
  103. # Create client with callback API version 2
  104. self.client = mqtt.Client(
  105. callback_api_version=mqtt.CallbackAPIVersion.VERSION2,
  106. client_id=f"bambuddy-smartplug-{id(self)}",
  107. protocol=mqtt.MQTTv311,
  108. )
  109. # Set up callbacks
  110. self.client.on_connect = self._on_connect
  111. self.client.on_disconnect = self._on_disconnect
  112. self.client.on_message = self._on_message
  113. # Configure authentication
  114. if self._username:
  115. self.client.username_pw_set(self._username, self._password)
  116. # Configure TLS
  117. if self._use_tls:
  118. self.client.tls_set(cert_reqs=ssl.CERT_NONE)
  119. self.client.tls_insecure_set(True)
  120. # Connect with timeout
  121. try:
  122. await asyncio.wait_for(
  123. asyncio.to_thread(self.client.connect_async, self._broker, self._port, 60),
  124. timeout=3.0,
  125. )
  126. except TimeoutError:
  127. logger.warning("MQTT smart plug connection to %s:%s timed out", self._broker, self._port)
  128. return False
  129. self.client.loop_start()
  130. # Wait briefly for connection
  131. await asyncio.sleep(1.0)
  132. if self.connected:
  133. logger.info("MQTT smart plug service connected to %s:%s", self._broker, self._port)
  134. # Resubscribe to all topics
  135. self._resubscribe_all()
  136. return True
  137. else:
  138. logger.warning("MQTT smart plug connection pending to %s:%s", self._broker, self._port)
  139. return True # Connection is async
  140. except Exception as e:
  141. logger.error("MQTT smart plug connection failed: %s", e)
  142. self.connected = False
  143. return False
  144. def _on_connect(
  145. self,
  146. client: mqtt.Client,
  147. userdata: Any,
  148. flags: dict,
  149. reason_code: int | mqtt.ReasonCode,
  150. properties: mqtt.Properties | None = None,
  151. ):
  152. """Callback when connected to broker."""
  153. rc = reason_code if isinstance(reason_code, int) else reason_code.value
  154. if rc == 0:
  155. self.connected = True
  156. logger.info("MQTT smart plug service connected successfully")
  157. # Resubscribe to all topics
  158. self._resubscribe_all()
  159. else:
  160. self.connected = False
  161. logger.error("MQTT smart plug connection failed: %s", reason_code)
  162. def _on_disconnect(
  163. self,
  164. client: mqtt.Client,
  165. userdata: Any,
  166. flags_or_rc: dict | int | mqtt.ReasonCode,
  167. reason_code: int | mqtt.ReasonCode | None = None,
  168. properties: mqtt.Properties | None = None,
  169. ):
  170. """Callback when disconnected from broker."""
  171. self.connected = False
  172. rc = reason_code if reason_code is not None else flags_or_rc
  173. rc_val = rc if isinstance(rc, int) else getattr(rc, "value", 0)
  174. if rc_val != 0:
  175. logger.warning("MQTT smart plug service disconnected: %s", rc)
  176. else:
  177. logger.info("MQTT smart plug service disconnected cleanly")
  178. if self._disconnection_event:
  179. self._disconnection_event.set()
  180. def _on_message(self, client: mqtt.Client, userdata: Any, msg: mqtt.MQTTMessage):
  181. """Handle incoming MQTT message, extract data using JSON path."""
  182. topic = msg.topic
  183. with self._lock:
  184. subscriptions = self.subscriptions.get(topic, [])
  185. if not subscriptions:
  186. return
  187. # Parse JSON payload (or treat as raw value)
  188. try:
  189. payload = json.loads(msg.payload.decode("utf-8"))
  190. is_json = True
  191. except (json.JSONDecodeError, UnicodeDecodeError):
  192. # Not JSON - treat the whole payload as a raw value
  193. payload = msg.payload.decode("utf-8").strip()
  194. is_json = False
  195. # Process for each subscribed (plug_id, data_type)
  196. for plug_id, data_type in subscriptions:
  197. configs = self.plug_configs.get(plug_id, {})
  198. config = configs.get(data_type)
  199. if not config:
  200. continue
  201. # Extract value using path (or use raw payload if no path)
  202. if is_json and config.path:
  203. raw_value = self._extract_json_path(payload, config.path)
  204. elif is_json and not config.path:
  205. # JSON but no path - if it's a simple value use it, otherwise skip
  206. if isinstance(payload, (int, float, str, bool)):
  207. raw_value = payload
  208. else:
  209. # Can't use a dict/list as a value
  210. logger.debug("MQTT plug %s: JSON payload is object/array but no path configured", plug_id)
  211. continue
  212. else:
  213. # Raw value (non-JSON)
  214. raw_value = payload
  215. if raw_value is None:
  216. continue
  217. # Initialize plug data if needed
  218. if plug_id not in self.plug_data:
  219. self.plug_data[plug_id] = SmartPlugMQTTData(plug_id=plug_id)
  220. data = self.plug_data[plug_id]
  221. data.last_seen = datetime.now(timezone.utc)
  222. # Process based on data type
  223. if data_type == "power":
  224. try:
  225. data.power = float(raw_value) * config.multiplier
  226. logger.debug("MQTT smart plug %s: power=%s", plug_id, data.power)
  227. except (ValueError, TypeError):
  228. pass # Ignore unparseable power reading from MQTT
  229. elif data_type == "energy":
  230. try:
  231. data.energy = float(raw_value) * config.multiplier
  232. logger.debug("MQTT smart plug %s: energy=%s", plug_id, data.energy)
  233. except (ValueError, TypeError):
  234. pass # Ignore unparseable energy reading from MQTT
  235. elif data_type == "energy_total":
  236. try:
  237. data.energy_total = float(raw_value) * config.multiplier
  238. logger.debug("MQTT smart plug %s: energy_total=%s", plug_id, data.energy_total)
  239. except (ValueError, TypeError):
  240. pass # Ignore unparseable energy reading from MQTT
  241. elif data_type == "state":
  242. state_str = str(raw_value)
  243. # Check against configured ON value if set
  244. if config.on_value:
  245. # Case-insensitive comparison
  246. if state_str.lower() == config.on_value.lower():
  247. data.state = "ON"
  248. else:
  249. data.state = "OFF"
  250. else:
  251. # Default behavior: normalize common values
  252. upper_state = state_str.upper()
  253. if upper_state in ("ON", "1", "TRUE"):
  254. data.state = "ON"
  255. elif upper_state in ("OFF", "0", "FALSE"):
  256. data.state = "OFF"
  257. else:
  258. data.state = state_str
  259. logger.debug("MQTT smart plug %s: state=%s", plug_id, data.state)
  260. def _extract_json_path(self, data: dict, path: str) -> Any:
  261. """Extract value using dot notation (e.g., 'power_l1' or 'data.power').
  262. Supports simple dot notation for nested objects.
  263. """
  264. if not path:
  265. return None
  266. parts = path.split(".")
  267. current = data
  268. for part in parts:
  269. if isinstance(current, dict) and part in current:
  270. current = current[part]
  271. else:
  272. return None
  273. return current
  274. def _resubscribe_all(self):
  275. """Resubscribe to all registered topics after reconnection."""
  276. if not self.client or not self.connected:
  277. return
  278. with self._lock:
  279. for topic in self.subscriptions:
  280. if self.subscriptions[topic]: # Only if there are subscribers
  281. try:
  282. self.client.subscribe(topic, qos=1)
  283. logger.debug("MQTT smart plug: resubscribed to %s", topic)
  284. except Exception as e:
  285. logger.error("MQTT smart plug: failed to resubscribe to %s: %s", topic, e)
  286. def subscribe(
  287. self,
  288. plug_id: int,
  289. # Power source
  290. power_topic: str | None = None,
  291. power_path: str | None = None,
  292. power_multiplier: float = 1.0,
  293. # Energy source
  294. energy_topic: str | None = None,
  295. energy_path: str | None = None,
  296. energy_multiplier: float = 1.0,
  297. # Lifetime energy counter, read from the energy topic (#3582)
  298. energy_total_path: str | None = None,
  299. energy_total_multiplier: float = 1.0,
  300. # State source
  301. state_topic: str | None = None,
  302. state_path: str | None = None,
  303. state_on_value: str | None = None,
  304. # Legacy: single topic/path/multiplier (for backward compatibility)
  305. topic: str | None = None,
  306. multiplier: float = 1.0,
  307. ):
  308. """Subscribe to MQTT topics for a plug.
  309. Each data type (power, energy, state) can have its own topic.
  310. For backward compatibility, if power_topic is not set but topic is,
  311. topic will be used for all data types that have paths configured.
  312. """
  313. with self._lock:
  314. # Initialize config for this plug
  315. self.plug_configs[plug_id] = {}
  316. # Determine topics (new fields take priority, fall back to legacy)
  317. effective_power_topic = power_topic or topic
  318. effective_energy_topic = energy_topic or topic
  319. effective_state_topic = state_topic or topic
  320. # Use new multipliers or fall back to legacy
  321. effective_power_mult = power_multiplier if power_multiplier != 1.0 else multiplier
  322. effective_energy_mult = energy_multiplier if energy_multiplier != 1.0 else multiplier
  323. # Configure power subscription (path is optional - empty means use raw payload)
  324. if effective_power_topic:
  325. config = MQTTDataSourceConfig(
  326. topic=effective_power_topic,
  327. path=power_path or "",
  328. multiplier=effective_power_mult,
  329. )
  330. self.plug_configs[plug_id]["power"] = config
  331. self._add_subscription(plug_id, effective_power_topic, "power")
  332. # Configure energy subscription (path is optional - empty means use raw payload)
  333. if effective_energy_topic:
  334. config = MQTTDataSourceConfig(
  335. topic=effective_energy_topic,
  336. path=energy_path or "",
  337. multiplier=effective_energy_mult,
  338. )
  339. self.plug_configs[plug_id]["energy"] = config
  340. self._add_subscription(plug_id, effective_energy_topic, "energy")
  341. # The lifetime counter sits beside the daily one in the same message
  342. # (Tasmota's ENERGY.Total next to ENERGY.Today), so it needs a path:
  343. # a raw payload can only be one of the two.
  344. if effective_energy_topic and energy_total_path:
  345. self.plug_configs[plug_id]["energy_total"] = MQTTDataSourceConfig(
  346. topic=effective_energy_topic,
  347. path=energy_total_path,
  348. multiplier=energy_total_multiplier,
  349. )
  350. self._add_subscription(plug_id, effective_energy_topic, "energy_total")
  351. # Configure state subscription (path is optional - empty means use raw payload)
  352. if effective_state_topic:
  353. config = MQTTDataSourceConfig(
  354. topic=effective_state_topic,
  355. path=state_path or "",
  356. on_value=state_on_value,
  357. )
  358. self.plug_configs[plug_id]["state"] = config
  359. self._add_subscription(plug_id, effective_state_topic, "state")
  360. # Initialize data entry
  361. if plug_id not in self.plug_data:
  362. self.plug_data[plug_id] = SmartPlugMQTTData(plug_id=plug_id)
  363. logger.info(
  364. f"MQTT smart plug {plug_id}: configured with "
  365. f"power={effective_power_topic if power_path else None}, "
  366. f"energy={effective_energy_topic if energy_path else None}, "
  367. f"energy_total={effective_energy_topic if energy_total_path else None}, "
  368. f"state={effective_state_topic if state_path else None}"
  369. )
  370. def _add_subscription(self, plug_id: int, topic: str, data_type: str):
  371. """Add a subscription for a plug/data_type to a topic."""
  372. if topic not in self.subscriptions:
  373. self.subscriptions[topic] = []
  374. # Actually subscribe if connected
  375. if self.client and self.connected:
  376. try:
  377. self.client.subscribe(topic, qos=1)
  378. logger.info("MQTT smart plug: subscribed to %s", topic)
  379. except Exception as e:
  380. logger.error("MQTT smart plug: failed to subscribe to %s: %s", topic, e)
  381. entry = (plug_id, data_type)
  382. if entry not in self.subscriptions[topic]:
  383. self.subscriptions[topic].append(entry)
  384. def unsubscribe(self, plug_id: int):
  385. """Unsubscribe when plug is deleted/updated."""
  386. with self._lock:
  387. # Get all configs for this plug
  388. configs = self.plug_configs.pop(plug_id, {})
  389. if not configs:
  390. # Still clean up any stray subscriptions
  391. pass
  392. # Collect all topics this plug was subscribed to
  393. topics_to_check = set()
  394. for _data_type, config in configs.items():
  395. topics_to_check.add(config.topic)
  396. # Also scan subscriptions to remove any entries for this plug
  397. for topic in list(self.subscriptions.keys()):
  398. # Remove all entries for this plug_id
  399. self.subscriptions[topic] = [(pid, dtype) for pid, dtype in self.subscriptions[topic] if pid != plug_id]
  400. topics_to_check.add(topic)
  401. # Unsubscribe from topics with no more subscribers
  402. for topic in topics_to_check:
  403. if topic in self.subscriptions and not self.subscriptions[topic]:
  404. del self.subscriptions[topic]
  405. if self.client and self.connected:
  406. try:
  407. self.client.unsubscribe(topic)
  408. logger.info("MQTT smart plug: unsubscribed from %s", topic)
  409. except Exception as e:
  410. logger.error("MQTT smart plug: failed to unsubscribe from %s: %s", topic, e)
  411. # Remove data
  412. self.plug_data.pop(plug_id, None)
  413. def get_plug_data(self, plug_id: int) -> SmartPlugMQTTData | None:
  414. """Get latest data for a plug (called by status endpoint)."""
  415. with self._lock:
  416. return self.plug_data.get(plug_id)
  417. def is_reachable(self, plug_id: int) -> bool:
  418. """Check if a plug has received data recently."""
  419. data = self.get_plug_data(plug_id)
  420. if not data:
  421. return False
  422. timeout = timedelta(minutes=self.REACHABLE_TIMEOUT_MINUTES)
  423. return datetime.now(timezone.utc) - data.last_seen < timeout
  424. async def disconnect(self, timeout: float = 0):
  425. """Disconnect from MQTT broker."""
  426. if self.client:
  427. try:
  428. self._disconnection_event = threading.Event()
  429. self.client.disconnect()
  430. await asyncio.to_thread(self._disconnection_event.wait, timeout=timeout)
  431. retire_paho_client(self.client, "smart-plugs")
  432. except Exception as e:
  433. logger.debug("MQTT smart plug disconnect error (ignored): %s", e)
  434. finally:
  435. self.client = None
  436. self.connected = False
  437. def subscribe_plug_to_mqtt(service: "MQTTSmartPlugService", plug: Any) -> list[str]:
  438. """Resolve per-type topic fields on a SmartPlug and register it with the service.
  439. The SmartPlug model carries both a legacy single `mqtt_topic` and newer
  440. per-type `mqtt_{power,energy,state}_topic` fields. Three code paths used
  441. to open-code this resolution (startup restore, create, update) and they
  442. drifted — the startup path skipped plugs that only had per-type topics
  443. set, leaving them unsubscribed after every restart (#1010). Funnelling
  444. all three through this helper keeps them in sync.
  445. Returns the list of topics subscribed (empty if nothing was configured).
  446. """
  447. power_topic = plug.mqtt_power_topic or plug.mqtt_topic
  448. energy_topic = plug.mqtt_energy_topic or plug.mqtt_topic
  449. state_topic = plug.mqtt_state_topic or plug.mqtt_topic
  450. if not (power_topic or energy_topic or state_topic):
  451. return []
  452. legacy_mult = plug.mqtt_multiplier or 1.0
  453. service.subscribe(
  454. plug_id=plug.id,
  455. power_topic=power_topic,
  456. power_path=plug.mqtt_power_path,
  457. power_multiplier=plug.mqtt_power_multiplier or legacy_mult,
  458. energy_topic=energy_topic,
  459. energy_path=plug.mqtt_energy_path,
  460. energy_multiplier=plug.mqtt_energy_multiplier or legacy_mult,
  461. energy_total_path=(plug.mqtt_energy_total_path or "").strip() or None,
  462. energy_total_multiplier=plug.mqtt_energy_total_multiplier or 1.0,
  463. state_topic=state_topic,
  464. state_path=plug.mqtt_state_path,
  465. state_on_value=plug.mqtt_state_on_value,
  466. )
  467. return [t for t in {power_topic, energy_topic, state_topic} if t]
  468. # Global instance
  469. mqtt_smart_plug_service = MQTTSmartPlugService()