handler.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. """Real MQTT handler implementation."""
  2. import os
  3. import threading
  4. import time
  5. import json
  6. from typing import Dict, Callable
  7. import paho.mqtt.client as mqtt
  8. import logging
  9. import asyncio
  10. from .base import BaseMQTTHandler
  11. from modules.core.state import state
  12. from modules.core.pattern_manager import list_theta_rho_files
  13. from modules.core.playlist_manager import list_all_playlists
  14. logger = logging.getLogger(__name__)
  15. class MQTTHandler(BaseMQTTHandler):
  16. """Real implementation of MQTT handler."""
  17. def __init__(self, callback_registry: Dict[str, Callable]):
  18. # MQTT Configuration - prioritize state config over environment variables
  19. # This allows UI configuration to override .env settings
  20. self.broker = state.mqtt_broker if state.mqtt_broker else os.getenv('MQTT_BROKER')
  21. self.port = state.mqtt_port if state.mqtt_port else int(os.getenv('MQTT_PORT', '1883'))
  22. self.username = state.mqtt_username if state.mqtt_username else os.getenv('MQTT_USERNAME')
  23. self.password = state.mqtt_password if state.mqtt_password else os.getenv('MQTT_PASSWORD')
  24. self.client_id = state.mqtt_client_id if state.mqtt_client_id else os.getenv('MQTT_CLIENT_ID', 'dune_weaver')
  25. self.status_topic = os.getenv('MQTT_STATUS_TOPIC', 'dune_weaver/status')
  26. self.command_topic = os.getenv('MQTT_COMMAND_TOPIC', 'dune_weaver/command')
  27. self.status_interval = int(os.getenv('MQTT_STATUS_INTERVAL', '30'))
  28. # Store callback registry
  29. self.callback_registry = callback_registry
  30. # Threading control
  31. self.running = False
  32. self.status_thread = None
  33. # Home Assistant MQTT Discovery settings - prioritize state config
  34. self.discovery_prefix = state.mqtt_discovery_prefix if state.mqtt_discovery_prefix else os.getenv('MQTT_DISCOVERY_PREFIX', 'homeassistant')
  35. self.device_name = state.mqtt_device_name if state.mqtt_device_name else os.getenv('HA_DEVICE_NAME', 'Dune Weaver')
  36. self.device_id = state.mqtt_device_id if state.mqtt_device_id else os.getenv('HA_DEVICE_ID', 'dune_weaver')
  37. # Additional topics for state
  38. self.running_state_topic = f"{self.device_id}/state/running"
  39. self.serial_state_topic = f"{self.device_id}/state/serial"
  40. self.pattern_select_topic = f"{self.device_id}/pattern/set"
  41. self.playlist_select_topic = f"{self.device_id}/playlist/set"
  42. self.speed_topic = f"{self.device_id}/speed/set"
  43. self.completion_topic = f"{self.device_id}/state/completion"
  44. self.time_remaining_topic = f"{self.device_id}/state/time_remaining"
  45. # LED control topics
  46. self.led_power_topic = f"{self.device_id}/led/power/set"
  47. self.led_brightness_topic = f"{self.device_id}/led/brightness/set"
  48. self.led_effect_topic = f"{self.device_id}/led/effect/set"
  49. self.led_speed_topic = f"{self.device_id}/led/speed/set"
  50. self.led_intensity_topic = f"{self.device_id}/led/intensity/set"
  51. self.led_color_topic = f"{self.device_id}/led/color/set"
  52. # Store current state
  53. self.current_file = ""
  54. self.is_running_state = False
  55. self.serial_state = ""
  56. self.patterns = []
  57. self.playlists = []
  58. # Track connection state
  59. self._connected = False
  60. # Initialize MQTT client if broker is configured
  61. if self.broker:
  62. self.client = mqtt.Client(client_id=self.client_id)
  63. self.client.on_connect = self.on_connect
  64. self.client.on_disconnect = self.on_disconnect
  65. self.client.on_message = self.on_message
  66. if self.username and self.password:
  67. self.client.username_pw_set(self.username, self.password)
  68. self.state = state
  69. self.state.mqtt_handler = self # Set reference to self in state, needed so that state setters can update the state
  70. # Store the main event loop during initialization
  71. self.main_loop = asyncio.get_event_loop()
  72. def setup_ha_discovery(self):
  73. """Publish Home Assistant MQTT discovery configurations."""
  74. if not self.is_enabled:
  75. return
  76. base_device = {
  77. "identifiers": [self.device_id],
  78. "name": self.device_name,
  79. "model": "Dune Weaver",
  80. "manufacturer": "DIY"
  81. }
  82. # Serial State Sensor
  83. serial_config = {
  84. "name": f"{self.device_name} Serial State",
  85. "unique_id": f"{self.device_id}_serial_state",
  86. "state_topic": self.serial_state_topic,
  87. "device": base_device,
  88. "icon": "mdi:serial-port",
  89. "entity_category": "diagnostic"
  90. }
  91. self._publish_discovery("sensor", "serial_state", serial_config)
  92. # Running State Sensor
  93. running_config = {
  94. "name": f"{self.device_name} Running State",
  95. "unique_id": f"{self.device_id}_running_state",
  96. "state_topic": self.running_state_topic,
  97. "device": base_device,
  98. "icon": "mdi:machine",
  99. "entity_category": "diagnostic"
  100. }
  101. self._publish_discovery("sensor", "running_state", running_config)
  102. # Stop Button
  103. stop_config = {
  104. "name": "Stop pattern execution",
  105. "unique_id": f"{self.device_id}_stop",
  106. "command_topic": f"{self.device_id}/command/stop",
  107. "device": base_device,
  108. "icon": "mdi:stop",
  109. "entity_category": "config"
  110. }
  111. self._publish_discovery("button", "stop", stop_config)
  112. # Pause Button
  113. pause_config = {
  114. "name": "Pause pattern execution",
  115. "unique_id": f"{self.device_id}_pause",
  116. "command_topic": f"{self.device_id}/command/pause",
  117. "state_topic": f"{self.device_id}/command/pause/state",
  118. "device": base_device,
  119. "icon": "mdi:pause",
  120. "entity_category": "config",
  121. "enabled_by_default": True,
  122. "availability": {
  123. "topic": f"{self.device_id}/command/pause/available",
  124. "payload_available": "true",
  125. "payload_not_available": "false"
  126. }
  127. }
  128. self._publish_discovery("button", "pause", pause_config)
  129. # Play Button
  130. play_config = {
  131. "name": "Resume pattern execution",
  132. "unique_id": f"{self.device_id}_play",
  133. "command_topic": f"{self.device_id}/command/play",
  134. "state_topic": f"{self.device_id}/command/play/state",
  135. "device": base_device,
  136. "icon": "mdi:play",
  137. "entity_category": "config",
  138. "enabled_by_default": True,
  139. "availability": {
  140. "topic": f"{self.device_id}/command/play/available",
  141. "payload_available": "true",
  142. "payload_not_available": "false"
  143. }
  144. }
  145. self._publish_discovery("button", "play", play_config)
  146. # Speed Control
  147. speed_config = {
  148. "name": f"{self.device_name} Speed",
  149. "unique_id": f"{self.device_id}_speed",
  150. "command_topic": self.speed_topic,
  151. "state_topic": f"{self.speed_topic}/state",
  152. "device": base_device,
  153. "icon": "mdi:speedometer",
  154. "mode": "box",
  155. "min": 50,
  156. "max": 2000,
  157. "step": 50
  158. }
  159. self._publish_discovery("number", "speed", speed_config)
  160. # Pattern Select
  161. pattern_config = {
  162. "name": f"{self.device_name} Pattern",
  163. "unique_id": f"{self.device_id}_pattern",
  164. "command_topic": self.pattern_select_topic,
  165. "state_topic": f"{self.pattern_select_topic}/state",
  166. "options": self.patterns,
  167. "device": base_device,
  168. "icon": "mdi:draw"
  169. }
  170. self._publish_discovery("select", "pattern", pattern_config)
  171. # Playlist Select
  172. playlist_config = {
  173. "name": f"{self.device_name} Playlist",
  174. "unique_id": f"{self.device_id}_playlist",
  175. "command_topic": self.playlist_select_topic,
  176. "state_topic": f"{self.playlist_select_topic}/state",
  177. "options": self.playlists,
  178. "device": base_device,
  179. "icon": "mdi:playlist-play"
  180. }
  181. self._publish_discovery("select", "playlist", playlist_config)
  182. # Playlist Run Mode Select
  183. playlist_mode_config = {
  184. "name": f"{self.device_name} Playlist Mode",
  185. "unique_id": f"{self.device_id}_playlist_mode",
  186. "command_topic": f"{self.device_id}/playlist/mode/set",
  187. "state_topic": f"{self.device_id}/playlist/mode/state",
  188. "options": ["single", "loop"],
  189. "device": base_device,
  190. "icon": "mdi:repeat",
  191. "entity_category": "config"
  192. }
  193. self._publish_discovery("select", "playlist_mode", playlist_mode_config)
  194. # Playlist Pause Time Number Input
  195. pause_time_config = {
  196. "name": f"{self.device_name} Playlist Pause Time",
  197. "unique_id": f"{self.device_id}_pause_time",
  198. "command_topic": f"{self.device_id}/playlist/pause_time/set",
  199. "state_topic": f"{self.device_id}/playlist/pause_time/state",
  200. "device": base_device,
  201. "icon": "mdi:timer",
  202. "entity_category": "config",
  203. "mode": "box",
  204. "unit_of_measurement": "seconds",
  205. "min": 0,
  206. "max": 86400,
  207. }
  208. self._publish_discovery("number", "pause_time", pause_time_config)
  209. # Clear Pattern Select
  210. clear_pattern_config = {
  211. "name": f"{self.device_name} Clear Pattern",
  212. "unique_id": f"{self.device_id}_clear_pattern",
  213. "command_topic": f"{self.device_id}/playlist/clear_pattern/set",
  214. "state_topic": f"{self.device_id}/playlist/clear_pattern/state",
  215. "options": ["none", "random", "adaptive", "clear_from_in", "clear_from_out", "clear_sideway"],
  216. "device": base_device,
  217. "icon": "mdi:eraser",
  218. "entity_category": "config"
  219. }
  220. self._publish_discovery("select", "clear_pattern", clear_pattern_config)
  221. # Shuffle Switch
  222. shuffle_config = {
  223. "name": f"{self.device_name} Shuffle",
  224. "unique_id": f"{self.device_id}_shuffle",
  225. "command_topic": f"{self.device_id}/playlist/shuffle/set",
  226. "state_topic": f"{self.device_id}/playlist/shuffle/state",
  227. "payload_on": "ON",
  228. "payload_off": "OFF",
  229. "device": base_device,
  230. "icon": "mdi:shuffle-variant",
  231. "entity_category": "config"
  232. }
  233. self._publish_discovery("switch", "shuffle", shuffle_config)
  234. # Completion Percentage Sensor
  235. completion_config = {
  236. "name": f"{self.device_name} Completion",
  237. "unique_id": f"{self.device_id}_completion",
  238. "state_topic": self.completion_topic,
  239. "device": base_device,
  240. "icon": "mdi:progress-clock",
  241. "unit_of_measurement": "%",
  242. "state_class": "measurement",
  243. "entity_category": "diagnostic"
  244. }
  245. self._publish_discovery("sensor", "completion", completion_config)
  246. # Time Remaining Sensor
  247. time_remaining_config = {
  248. "name": f"{self.device_name} Time Remaining",
  249. "unique_id": f"{self.device_id}_time_remaining",
  250. "state_topic": self.time_remaining_topic,
  251. "device": base_device,
  252. "icon": "mdi:timer-sand",
  253. "unit_of_measurement": "s",
  254. "device_class": "duration",
  255. "state_class": "measurement",
  256. "entity_category": "diagnostic"
  257. }
  258. self._publish_discovery("sensor", "time_remaining", time_remaining_config)
  259. # LED Control Entities (only for DW LEDs - WLED has its own MQTT integration)
  260. if state.led_provider == "dw_leds":
  261. # LED Power Switch
  262. led_power_config = {
  263. "name": f"{self.device_name} LED Power",
  264. "unique_id": f"{self.device_id}_led_power",
  265. "command_topic": self.led_power_topic,
  266. "state_topic": f"{self.device_id}/led/power/state",
  267. "payload_on": "ON",
  268. "payload_off": "OFF",
  269. "device": base_device,
  270. "icon": "mdi:lightbulb",
  271. "optimistic": False
  272. }
  273. self._publish_discovery("switch", "led_power", led_power_config)
  274. # LED Brightness Control
  275. led_brightness_config = {
  276. "name": f"{self.device_name} LED Brightness",
  277. "unique_id": f"{self.device_id}_led_brightness",
  278. "command_topic": self.led_brightness_topic,
  279. "state_topic": f"{self.device_id}/led/brightness/state",
  280. "device": base_device,
  281. "icon": "mdi:brightness-6",
  282. "min": 0,
  283. "max": 100,
  284. "mode": "slider"
  285. }
  286. self._publish_discovery("number", "led_brightness", led_brightness_config)
  287. # LED Effect Selector
  288. led_effect_options = [
  289. "Static", "Blink", "Breathe", "Wipe", "Fade", "Scan", "Dual Scan",
  290. "Rainbow Cycle", "Rainbow", "Theater Chase", "Running Lights",
  291. "Random Color", "Dynamic", "Twinkle", "Sparkle", "Strobe", "Fire",
  292. "Comet", "Chase", "Police", "Lightning", "Fireworks", "Ripple", "Flow",
  293. "Colorloop", "Palette Flow", "Gradient", "Multi Strobe", "Waves", "BPM",
  294. "Juggle", "Meteor", "Pride", "Pacifica", "Plasma", "Dissolve", "Glitter",
  295. "Confetti", "Sinelon", "Candle", "Aurora", "Rain", "Halloween", "Noise",
  296. "Funky Plank"
  297. ]
  298. led_effect_config = {
  299. "name": f"{self.device_name} LED Effect",
  300. "unique_id": f"{self.device_id}_led_effect",
  301. "command_topic": self.led_effect_topic,
  302. "state_topic": f"{self.device_id}/led/effect/state",
  303. "options": led_effect_options,
  304. "device": base_device,
  305. "icon": "mdi:palette"
  306. }
  307. self._publish_discovery("select", "led_effect", led_effect_config)
  308. # LED Speed Control
  309. led_speed_config = {
  310. "name": f"{self.device_name} LED Speed",
  311. "unique_id": f"{self.device_id}_led_speed",
  312. "command_topic": self.led_speed_topic,
  313. "state_topic": f"{self.device_id}/led/speed/state",
  314. "device": base_device,
  315. "icon": "mdi:speedometer",
  316. "min": 0,
  317. "max": 255,
  318. "mode": "slider"
  319. }
  320. self._publish_discovery("number", "led_speed", led_speed_config)
  321. # LED Intensity Control
  322. led_intensity_config = {
  323. "name": f"{self.device_name} LED Intensity",
  324. "unique_id": f"{self.device_id}_led_intensity",
  325. "command_topic": self.led_intensity_topic,
  326. "state_topic": f"{self.device_id}/led/intensity/state",
  327. "device": base_device,
  328. "icon": "mdi:brightness-7",
  329. "min": 0,
  330. "max": 255,
  331. "mode": "slider"
  332. }
  333. self._publish_discovery("number", "led_intensity", led_intensity_config)
  334. # LED RGB Color Control
  335. led_color_config = {
  336. "name": f"{self.device_name} LED Color",
  337. "unique_id": f"{self.device_id}_led_color",
  338. "command_topic": self.led_color_topic,
  339. "state_topic": f"{self.device_id}/led/color/state",
  340. "rgb_command_topic": self.led_color_topic,
  341. "rgb_state_topic": f"{self.device_id}/led/color/state",
  342. "device": base_device,
  343. "icon": "mdi:palette-swatch",
  344. "schema": "json",
  345. "rgb": True
  346. }
  347. self._publish_discovery("light", "led_color", led_color_config)
  348. def _publish_discovery(self, component: str, config_type: str, config: dict):
  349. """Helper method to publish HA discovery configs."""
  350. if not self.is_enabled:
  351. return
  352. discovery_topic = f"{self.discovery_prefix}/{component}/{self.device_id}/{config_type}/config"
  353. self.client.publish(discovery_topic, json.dumps(config), retain=True)
  354. def _publish_running_state(self, running_state=None):
  355. """Helper to publish running state and button availability."""
  356. if running_state is None:
  357. if not self.state.current_playing_file:
  358. running_state = "idle"
  359. elif self.state.pause_requested:
  360. running_state = "paused"
  361. else:
  362. running_state = "running"
  363. self.client.publish(self.running_state_topic, running_state, retain=True)
  364. # Update button availability based on state
  365. self.client.publish(f"{self.device_id}/command/pause/available",
  366. "true" if running_state == "running" else "false",
  367. retain=True)
  368. self.client.publish(f"{self.device_id}/command/play/available",
  369. "true" if running_state == "paused" else "false",
  370. retain=True)
  371. def _publish_pattern_state(self, current_file=None):
  372. """Helper to publish pattern state."""
  373. if current_file is None:
  374. current_file = self.state.current_playing_file
  375. if current_file:
  376. if current_file.startswith('./patterns/'):
  377. current_file = current_file[len('./patterns/'):]
  378. else:
  379. current_file = current_file.split("/")[-1].split("\\")[-1]
  380. self.client.publish(f"{self.pattern_select_topic}/state", current_file, retain=True)
  381. else:
  382. # Clear the pattern selection
  383. self.client.publish(f"{self.pattern_select_topic}/state", "None", retain=True)
  384. def _publish_playlist_state(self, playlist_name=None):
  385. """Helper to publish playlist state."""
  386. if playlist_name is None:
  387. playlist_name = self.state.current_playlist_name
  388. if playlist_name:
  389. self.client.publish(f"{self.playlist_select_topic}/state", playlist_name, retain=True)
  390. else:
  391. # Clear the playlist selection
  392. self.client.publish(f"{self.playlist_select_topic}/state", "None", retain=True)
  393. def _publish_serial_state(self):
  394. """Helper to publish serial state."""
  395. serial_connected = (state.conn.is_connected() if state.conn else False)
  396. serial_port = state.port if serial_connected else None
  397. serial_status = f"connected to {serial_port}" if serial_connected else "disconnected"
  398. self.client.publish(self.serial_state_topic, serial_status, retain=True)
  399. def _publish_progress_state(self):
  400. """Helper to publish completion percentage and time remaining."""
  401. if state.execution_progress:
  402. current, total, remaining_time, elapsed_time = state.execution_progress
  403. completion_percentage = (current / total * 100) if total > 0 else 0
  404. # Publish completion percentage (rounded to 1 decimal place)
  405. self.client.publish(self.completion_topic, round(completion_percentage, 1), retain=True)
  406. # Publish time remaining (rounded to nearest second, defaulting to 0 if None)
  407. time_remaining_seconds = round(remaining_time) if remaining_time is not None else 0
  408. self.client.publish(self.time_remaining_topic, max(0, time_remaining_seconds), retain=True)
  409. else:
  410. # No pattern running, publish zeros
  411. self.client.publish(self.completion_topic, 0, retain=True)
  412. self.client.publish(self.time_remaining_topic, 0, retain=True)
  413. def _publish_playlist_settings_state(self):
  414. """Helper to publish playlist settings state (mode, pause_time, clear_pattern, shuffle)."""
  415. self.client.publish(f"{self.device_id}/playlist/mode/state", state.playlist_mode, retain=True)
  416. self.client.publish(f"{self.device_id}/playlist/pause_time/state", state.pause_time, retain=True)
  417. self.client.publish(f"{self.device_id}/playlist/clear_pattern/state", state.clear_pattern, retain=True)
  418. shuffle_state = "ON" if state.shuffle else "OFF"
  419. self.client.publish(f"{self.device_id}/playlist/shuffle/state", shuffle_state, retain=True)
  420. def _publish_led_state(self):
  421. """Helper to publish LED state to MQTT (DW LEDs only - WLED has its own MQTT)."""
  422. if not state.led_controller or state.led_provider != "dw_leds":
  423. return
  424. try:
  425. status = state.led_controller.check_status()
  426. if not status.get("connected", False):
  427. return
  428. # Publish power state (check both "power" for WLED compatibility and "power_on" for DW LEDs)
  429. is_powered = status.get("power_on", status.get("power", False))
  430. power_state = "ON" if is_powered else "OFF"
  431. self.client.publish(f"{self.device_id}/led/power/state", power_state, retain=True)
  432. # Publish brightness (convert from 0-1 to 0-100)
  433. if "brightness" in status:
  434. brightness = int(status["brightness"] * 100)
  435. self.client.publish(f"{self.device_id}/led/brightness/state", brightness, retain=True)
  436. # Publish effect
  437. if "effect_id" in status:
  438. effect_map = {
  439. 0: "Static", 1: "Blink", 2: "Breathe", 3: "Wipe", 4: "Fade",
  440. 5: "Scan", 6: "Dual Scan", 7: "Rainbow Cycle", 8: "Rainbow",
  441. 9: "Theater Chase", 10: "Running Lights", 11: "Random Color",
  442. 12: "Dynamic", 13: "Twinkle", 14: "Sparkle", 15: "Strobe",
  443. 16: "Fire", 17: "Comet", 18: "Chase", 19: "Police", 20: "Lightning",
  444. 21: "Fireworks", 22: "Ripple", 23: "Flow", 24: "Colorloop",
  445. 25: "Palette Flow", 26: "Gradient", 27: "Multi Strobe", 28: "Waves",
  446. 29: "BPM", 30: "Juggle", 31: "Meteor", 32: "Pride", 33: "Pacifica",
  447. 34: "Plasma", 35: "Dissolve", 36: "Glitter", 37: "Confetti",
  448. 38: "Sinelon", 39: "Candle", 40: "Aurora", 41: "Rain",
  449. 42: "Halloween", 43: "Noise", 44: "Funky Plank"
  450. }
  451. effect_name = effect_map.get(status["effect_id"], "Static")
  452. self.client.publish(f"{self.device_id}/led/effect/state", effect_name, retain=True)
  453. # Publish speed
  454. if "speed" in status:
  455. self.client.publish(f"{self.device_id}/led/speed/state", status["speed"], retain=True)
  456. # Publish intensity
  457. if "intensity" in status:
  458. self.client.publish(f"{self.device_id}/led/intensity/state", status["intensity"], retain=True)
  459. # Publish color (RGB)
  460. if "colors" in status and len(status["colors"]) > 0:
  461. # colors is array of hex strings like ["#ff0000", "#00ff00", "#0000ff"]
  462. # Convert first color to RGB dict
  463. color_hex = status["colors"][0]
  464. if color_hex and color_hex.startswith('#') and len(color_hex) == 7:
  465. r = int(color_hex[1:3], 16)
  466. g = int(color_hex[3:5], 16)
  467. b = int(color_hex[5:7], 16)
  468. self.client.publish(f"{self.device_id}/led/color/state",
  469. json.dumps({"r": r, "g": g, "b": b}), retain=True)
  470. except Exception as e:
  471. logger.error(f"Error publishing LED state: {e}")
  472. def update_state(self, current_file=None, is_running=None, playlist=None, playlist_name=None):
  473. """Update state in Home Assistant. Only publishes the attributes that are explicitly passed."""
  474. if not self.is_enabled:
  475. return
  476. # Update pattern state if current_file is provided
  477. if current_file is not None:
  478. self._publish_pattern_state(current_file)
  479. # Update running state and button availability if is_running is provided
  480. if is_running is not None:
  481. running_state = "running" if is_running else "paused" if self.state.current_playing_file else "idle"
  482. self._publish_running_state(running_state)
  483. # Update playlist state if playlist info is provided
  484. if playlist_name is not None:
  485. self._publish_playlist_state(playlist_name)
  486. def on_connect(self, client, userdata, flags, rc):
  487. """Callback when connected to MQTT broker."""
  488. if rc == 0:
  489. self._connected = True
  490. logger.info("MQTT Connection Accepted.")
  491. # Subscribe to command topics
  492. client.subscribe([
  493. (self.command_topic, 0),
  494. (self.pattern_select_topic, 0),
  495. (self.playlist_select_topic, 0),
  496. (self.speed_topic, 0),
  497. (f"{self.device_id}/command/stop", 0),
  498. (f"{self.device_id}/command/pause", 0),
  499. (f"{self.device_id}/command/play", 0),
  500. (f"{self.device_id}/playlist/mode/set", 0),
  501. (f"{self.device_id}/playlist/pause_time/set", 0),
  502. (f"{self.device_id}/playlist/clear_pattern/set", 0),
  503. (f"{self.device_id}/playlist/shuffle/set", 0),
  504. (self.led_power_topic, 0),
  505. (self.led_brightness_topic, 0),
  506. (self.led_effect_topic, 0),
  507. (self.led_speed_topic, 0),
  508. (self.led_intensity_topic, 0),
  509. (self.led_color_topic, 0),
  510. ])
  511. # Publish discovery configurations
  512. self.setup_ha_discovery()
  513. else:
  514. self._connected = False
  515. error_messages = {
  516. 1: "Protocol level not supported",
  517. 2: "The client-identifier is not allowed by the server",
  518. 3: "The MQTT service is not available",
  519. 4: "The data in the username or password is malformed",
  520. 5: "The client is not authorized to connect"
  521. }
  522. error_msg = error_messages.get(rc, f"Unknown error code: {rc}")
  523. logger.error(f"MQTT Connection Refused. {error_msg}")
  524. def on_disconnect(self, client, userdata, rc):
  525. """Callback when disconnected from MQTT broker."""
  526. self._connected = False
  527. if rc == 0:
  528. logger.info("MQTT disconnected cleanly")
  529. else:
  530. logger.warning(f"MQTT disconnected unexpectedly with code: {rc}")
  531. def on_message(self, client, userdata, msg):
  532. """Callback when message is received."""
  533. try:
  534. if msg.topic == self.pattern_select_topic:
  535. from modules.core.pattern_manager import THETA_RHO_DIR
  536. # Handle pattern selection
  537. pattern_name = msg.payload.decode()
  538. if pattern_name in self.patterns:
  539. # Schedule the coroutine to run in the main event loop
  540. asyncio.run_coroutine_threadsafe(
  541. self.callback_registry['run_pattern'](file_path=f"{THETA_RHO_DIR}/{pattern_name}"),
  542. self.main_loop
  543. ).add_done_callback(
  544. lambda _: self._publish_pattern_state(None) # Clear pattern after execution
  545. )
  546. self.client.publish(f"{self.pattern_select_topic}/state", pattern_name, retain=True)
  547. elif msg.topic == self.playlist_select_topic:
  548. # Handle playlist selection
  549. playlist_name = msg.payload.decode()
  550. if playlist_name in self.playlists:
  551. # Schedule the coroutine to run in the main event loop
  552. asyncio.run_coroutine_threadsafe(
  553. self.callback_registry['run_playlist'](
  554. playlist_name=playlist_name,
  555. run_mode=self.state.playlist_mode,
  556. pause_time=self.state.pause_time,
  557. clear_pattern=self.state.clear_pattern,
  558. shuffle=self.state.shuffle
  559. ),
  560. self.main_loop
  561. ).add_done_callback(
  562. lambda _: self._publish_playlist_state(None) # Clear playlist after execution
  563. )
  564. self.client.publish(f"{self.playlist_select_topic}/state", playlist_name, retain=True)
  565. elif msg.topic == self.speed_topic:
  566. speed = int(msg.payload.decode())
  567. self.callback_registry['set_speed'](speed)
  568. elif msg.topic == f"{self.device_id}/command/stop":
  569. # Handle stop command
  570. callback = self.callback_registry['stop']
  571. if asyncio.iscoroutinefunction(callback):
  572. asyncio.run_coroutine_threadsafe(callback(), self.main_loop)
  573. else:
  574. callback()
  575. # Clear both pattern and playlist selections
  576. self._publish_pattern_state(None)
  577. self._publish_playlist_state(None)
  578. elif msg.topic == f"{self.device_id}/command/pause":
  579. # Handle pause command - only if in running state
  580. if bool(self.state.current_playing_file) and not self.state.pause_requested:
  581. # Check if callback is async or sync
  582. callback = self.callback_registry['pause']
  583. if asyncio.iscoroutinefunction(callback):
  584. asyncio.run_coroutine_threadsafe(callback(), self.main_loop)
  585. else:
  586. callback()
  587. elif msg.topic == f"{self.device_id}/command/play":
  588. # Handle play command - only if in paused state
  589. if bool(self.state.current_playing_file) and self.state.pause_requested:
  590. # Check if callback is async or sync
  591. callback = self.callback_registry['resume']
  592. if asyncio.iscoroutinefunction(callback):
  593. asyncio.run_coroutine_threadsafe(callback(), self.main_loop)
  594. else:
  595. callback()
  596. elif msg.topic == f"{self.device_id}/playlist/mode/set":
  597. mode = msg.payload.decode()
  598. if mode in ["single", "loop"]:
  599. state.playlist_mode = mode
  600. self.client.publish(f"{self.device_id}/playlist/mode/state", mode, retain=True)
  601. elif msg.topic == f"{self.device_id}/playlist/pause_time/set":
  602. pause_time = float(msg.payload.decode())
  603. if 0 <= pause_time <= 60:
  604. state.pause_time = pause_time
  605. self.client.publish(f"{self.device_id}/playlist/pause_time/state", pause_time, retain=True)
  606. elif msg.topic == f"{self.device_id}/playlist/clear_pattern/set":
  607. clear_pattern = msg.payload.decode()
  608. if clear_pattern in ["none", "random", "adaptive", "clear_from_in", "clear_from_out", "clear_sideway"]:
  609. state.clear_pattern = clear_pattern
  610. self.client.publish(f"{self.device_id}/playlist/clear_pattern/state", clear_pattern, retain=True)
  611. elif msg.topic == f"{self.device_id}/playlist/shuffle/set":
  612. payload = msg.payload.decode()
  613. shuffle_value = payload == "ON"
  614. state.shuffle = shuffle_value
  615. self.client.publish(f"{self.device_id}/playlist/shuffle/state", payload, retain=True)
  616. elif msg.topic == self.led_power_topic:
  617. # Handle LED power command (DW LEDs only)
  618. payload = msg.payload.decode()
  619. if state.led_controller and state.led_provider == "dw_leds":
  620. power_state = 1 if payload == "ON" else 0
  621. state.led_controller.set_power(power_state)
  622. # Reset idle timeout when LEDs are manually powered on via MQTT (only if idle timeout is enabled)
  623. if payload == "ON" and state.dw_led_idle_timeout_enabled:
  624. state.dw_led_last_activity_time = time.time()
  625. logger.debug("LED activity time reset due to MQTT power on")
  626. self.client.publish(f"{self.device_id}/led/power/state", payload, retain=True)
  627. elif msg.topic == self.led_brightness_topic:
  628. # Handle LED brightness command (DW LEDs only)
  629. brightness = int(msg.payload.decode())
  630. if 0 <= brightness <= 100 and state.led_controller and state.led_provider == "dw_leds":
  631. controller = state.led_controller.get_controller()
  632. if controller and hasattr(controller, 'set_brightness'):
  633. # DW LED controller expects 0-100, converts internally to 0.0-1.0
  634. controller.set_brightness(brightness)
  635. self.client.publish(f"{self.device_id}/led/brightness/state", brightness, retain=True)
  636. elif msg.topic == self.led_effect_topic:
  637. # Handle LED effect command (DW LEDs only)
  638. effect_name = msg.payload.decode()
  639. if state.led_controller and state.led_provider == "dw_leds":
  640. # Map effect name to ID
  641. effect_map = {
  642. "Static": 0, "Blink": 1, "Breathe": 2, "Wipe": 3, "Fade": 4,
  643. "Scan": 5, "Dual Scan": 6, "Rainbow Cycle": 7, "Rainbow": 8,
  644. "Theater Chase": 9, "Running Lights": 10, "Random Color": 11,
  645. "Dynamic": 12, "Twinkle": 13, "Sparkle": 14, "Strobe": 15,
  646. "Fire": 16, "Comet": 17, "Chase": 18, "Police": 19, "Lightning": 20,
  647. "Fireworks": 21, "Ripple": 22, "Flow": 23, "Colorloop": 24,
  648. "Palette Flow": 25, "Gradient": 26, "Multi Strobe": 27, "Waves": 28,
  649. "BPM": 29, "Juggle": 30, "Meteor": 31, "Pride": 32, "Pacifica": 33,
  650. "Plasma": 34, "Dissolve": 35, "Glitter": 36, "Confetti": 37,
  651. "Sinelon": 38, "Candle": 39, "Aurora": 40, "Rain": 41,
  652. "Halloween": 42, "Noise": 43, "Funky Plank": 44
  653. }
  654. effect_id = effect_map.get(effect_name)
  655. if effect_id is not None:
  656. controller = state.led_controller.get_controller()
  657. if controller and hasattr(controller, 'set_effect'):
  658. controller.set_effect(effect_id)
  659. self.client.publish(f"{self.device_id}/led/effect/state", effect_name, retain=True)
  660. elif msg.topic == self.led_speed_topic:
  661. # Handle LED speed command (DW LEDs only)
  662. speed = int(msg.payload.decode())
  663. if 0 <= speed <= 255 and state.led_controller and state.led_provider == "dw_leds":
  664. controller = state.led_controller.get_controller()
  665. if controller and hasattr(controller, 'set_speed'):
  666. controller.set_speed(speed)
  667. self.client.publish(f"{self.device_id}/led/speed/state", speed, retain=True)
  668. elif msg.topic == self.led_intensity_topic:
  669. # Handle LED intensity command (DW LEDs only)
  670. intensity = int(msg.payload.decode())
  671. if 0 <= intensity <= 255 and state.led_controller and state.led_provider == "dw_leds":
  672. controller = state.led_controller.get_controller()
  673. if controller and hasattr(controller, 'set_intensity'):
  674. controller.set_intensity(intensity)
  675. self.client.publish(f"{self.device_id}/led/intensity/state", intensity, retain=True)
  676. elif msg.topic == self.led_color_topic:
  677. # Handle LED color command (RGB) (DW LEDs only)
  678. try:
  679. color_data = json.loads(msg.payload.decode())
  680. if state.led_controller and state.led_provider == "dw_leds" and 'r' in color_data and 'g' in color_data and 'b' in color_data:
  681. controller = state.led_controller.get_controller()
  682. if controller and hasattr(controller, 'set_color'):
  683. r, g, b = color_data['r'], color_data['g'], color_data['b']
  684. controller.set_color(r, g, b)
  685. self.client.publish(f"{self.device_id}/led/color/state",
  686. json.dumps({"r": r, "g": g, "b": b}), retain=True)
  687. except json.JSONDecodeError:
  688. logger.error(f"Invalid JSON for color command: {msg.payload}")
  689. else:
  690. # Handle other commands
  691. payload = json.loads(msg.payload.decode())
  692. command = payload.get('command')
  693. params = payload.get('params', {})
  694. if command in self.callback_registry:
  695. self.callback_registry[command](**params)
  696. else:
  697. logger.error(f"Unknown command received: {command}")
  698. except json.JSONDecodeError:
  699. logger.error(f"Invalid JSON payload received: {msg.payload}")
  700. except Exception as e:
  701. logger.error(f"Error processing MQTT message: {e}")
  702. def publish_status(self):
  703. """Publish status updates periodically."""
  704. while self.running:
  705. try:
  706. # Update all states
  707. self._publish_running_state()
  708. self._publish_pattern_state()
  709. self._publish_playlist_state()
  710. self._publish_serial_state()
  711. self._publish_progress_state()
  712. # Update speed state
  713. self.client.publish(f"{self.speed_topic}/state", self.state.speed, retain=True)
  714. # Update LED state
  715. self._publish_led_state()
  716. # Publish keepalive status
  717. status = {
  718. "timestamp": time.time(),
  719. "client_id": self.client_id
  720. }
  721. self.client.publish(self.status_topic, json.dumps(status))
  722. # Wait for next interval
  723. time.sleep(self.status_interval)
  724. except Exception as e:
  725. logger.error(f"Error publishing status: {e}")
  726. time.sleep(5) # Wait before retry
  727. def start(self) -> None:
  728. """Start the MQTT handler."""
  729. if not self.is_enabled:
  730. return
  731. try:
  732. self.client.connect(self.broker, self.port)
  733. self.client.loop_start()
  734. # Start status publishing thread
  735. self.running = True
  736. self.status_thread = threading.Thread(target=self.publish_status, daemon=True)
  737. self.status_thread.start()
  738. # Get initial pattern and playlist lists
  739. self.patterns = list_theta_rho_files()
  740. self.playlists = list_all_playlists()
  741. # Wait a bit for MQTT connection to establish
  742. time.sleep(1)
  743. # Publish initial states
  744. self._publish_running_state()
  745. self._publish_pattern_state()
  746. self._publish_playlist_state()
  747. self._publish_serial_state()
  748. self._publish_progress_state()
  749. self._publish_playlist_settings_state()
  750. self._publish_led_state()
  751. # Setup Home Assistant discovery
  752. self.setup_ha_discovery()
  753. logger.info("MQTT Handler started successfully")
  754. except Exception as e:
  755. logger.error(f"Failed to start MQTT Handler: {e}")
  756. def stop(self) -> None:
  757. """Stop the MQTT handler."""
  758. if not self.is_enabled:
  759. return
  760. # First stop the running flag to prevent new iterations
  761. self.running = False
  762. # Clean up status thread
  763. local_status_thread = self.status_thread # Keep a local reference
  764. if local_status_thread and local_status_thread.is_alive():
  765. try:
  766. local_status_thread.join(timeout=5)
  767. if local_status_thread.is_alive():
  768. logger.warning("MQTT status thread did not terminate cleanly")
  769. except Exception as e:
  770. logger.error(f"Error joining status thread: {e}")
  771. self.status_thread = None
  772. # Clean up MQTT client
  773. try:
  774. if hasattr(self, 'client'):
  775. self.client.loop_stop()
  776. self.client.disconnect()
  777. except Exception as e:
  778. logger.error(f"Error disconnecting MQTT client: {e}")
  779. # Clean up main loop reference
  780. self.main_loop = None
  781. logger.info("MQTT handler stopped")
  782. @property
  783. def is_enabled(self) -> bool:
  784. """Return whether MQTT functionality is enabled.
  785. MQTT is enabled if:
  786. 1. A broker address is configured (either via state or env var), AND
  787. 2. Either state.mqtt_enabled is True, OR no UI config exists (env-only mode)
  788. """
  789. # If no broker configured, MQTT is disabled
  790. if not self.broker:
  791. return False
  792. # If state has mqtt_enabled explicitly set (UI was used), respect that setting
  793. # If mqtt_broker is set in state, user configured via UI - use mqtt_enabled
  794. if state.mqtt_broker:
  795. return state.mqtt_enabled
  796. # Otherwise, broker came from env vars - enable if broker exists
  797. return True
  798. @property
  799. def is_connected(self) -> bool:
  800. """Return whether MQTT client is currently connected to the broker."""
  801. return self._connected and self.is_enabled