discovery.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. """
  2. Bambu Lab printer discovery service using SSDP and subnet scanning.
  3. Bambu Lab printers advertise themselves via SSDP (Simple Service Discovery Protocol)
  4. on the local network. This service listens for these advertisements and provides
  5. a list of discovered printers.
  6. For Docker environments where SSDP multicast doesn't work, subnet scanning is
  7. available as an alternative discovery method.
  8. """
  9. import asyncio
  10. import ipaddress
  11. import logging
  12. import os
  13. import re
  14. import socket
  15. import struct
  16. from dataclasses import dataclass
  17. from datetime import datetime
  18. from pathlib import Path
  19. logger = logging.getLogger(__name__)
  20. def is_running_in_docker() -> bool:
  21. """Detect if we're running inside a Docker container."""
  22. # Check for .dockerenv file
  23. if Path("/.dockerenv").exists():
  24. return True
  25. # Check cgroup for docker/containerd
  26. try:
  27. with open("/proc/1/cgroup") as f:
  28. content = f.read()
  29. if "docker" in content or "containerd" in content or "kubepods" in content:
  30. return True
  31. except (FileNotFoundError, PermissionError):
  32. pass
  33. # Check for container environment variable
  34. return bool(os.environ.get("CONTAINER") or os.environ.get("DOCKER_CONTAINER"))
  35. # SSDP multicast address - Bambu uses port 2021, not standard 1900
  36. SSDP_ADDR = "239.255.255.250"
  37. SSDP_PORT = 2021 # Bambu Lab uses non-standard port
  38. # Bambu Lab SSDP search target
  39. BAMBU_SEARCH_TARGET = "urn:bambulab-com:device:3dprinter:1"
  40. # Virtual printer serial to exclude from discovery (Bambuddy's own virtual printer)
  41. VIRTUAL_PRINTER_SERIAL = "00M09A391800001"
  42. # SSDP M-SEARCH message
  43. SSDP_MSEARCH = (
  44. "M-SEARCH * HTTP/1.1\r\n"
  45. f"HOST: {SSDP_ADDR}:{SSDP_PORT}\r\n"
  46. 'MAN: "ssdp:discover"\r\n'
  47. "MX: 3\r\n"
  48. f"ST: {BAMBU_SEARCH_TARGET}\r\n"
  49. "\r\n"
  50. )
  51. @dataclass
  52. class DiscoveredPrinter:
  53. """Represents a discovered Bambu Lab printer."""
  54. serial: str
  55. name: str
  56. ip_address: str
  57. model: str | None = None
  58. discovered_at: str | None = None
  59. def to_dict(self) -> dict:
  60. return {
  61. "serial": self.serial,
  62. "name": self.name,
  63. "ip_address": self.ip_address,
  64. "model": self.model,
  65. "discovered_at": self.discovered_at,
  66. }
  67. class PrinterDiscoveryService:
  68. """Service for discovering Bambu Lab printers on the network."""
  69. def __init__(self):
  70. self._discovered: dict[str, DiscoveredPrinter] = {}
  71. self._running = False
  72. self._task: asyncio.Task | None = None
  73. @property
  74. def is_running(self) -> bool:
  75. return self._running
  76. @property
  77. def discovered_printers(self) -> list[DiscoveredPrinter]:
  78. return list(self._discovered.values())
  79. def clear(self):
  80. """Clear discovered printers."""
  81. self._discovered.clear()
  82. async def start(self, duration: float = 10.0):
  83. """Start discovery for a specified duration."""
  84. if self._running:
  85. return
  86. self._running = True
  87. self._discovered.clear()
  88. self._task = asyncio.create_task(self._discover(duration))
  89. async def stop(self):
  90. """Stop discovery."""
  91. self._running = False
  92. if self._task and not self._task.done():
  93. self._task.cancel()
  94. try:
  95. await self._task
  96. except asyncio.CancelledError:
  97. pass
  98. self._task = None
  99. async def _discover(self, duration: float):
  100. """Run discovery for the specified duration.
  101. Bambu printers broadcast NOTIFY messages periodically on port 2021.
  102. We need to bind to that port and listen for broadcasts.
  103. """
  104. sock = None
  105. try:
  106. # Create UDP socket for SSDP
  107. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  108. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  109. # Try to set SO_REUSEPORT if available (Linux/macOS)
  110. try:
  111. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  112. except (AttributeError, OSError):
  113. pass
  114. # Set non-blocking mode
  115. sock.setblocking(False)
  116. # Bind to the SSDP port to receive NOTIFY broadcasts from printers
  117. sock.bind(("", SSDP_PORT))
  118. # Join multicast group to receive multicast messages
  119. mreq = struct.pack("4sl", socket.inet_aton(SSDP_ADDR), socket.INADDR_ANY)
  120. sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
  121. # Enable broadcast
  122. sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  123. logger.info(f"Starting SSDP discovery on port {SSDP_PORT} for Bambu Lab printers...")
  124. # Send initial M-SEARCH request to trigger responses
  125. try:
  126. sock.sendto(SSDP_MSEARCH.encode(), (SSDP_ADDR, SSDP_PORT))
  127. except Exception as e:
  128. logger.debug(f"M-SEARCH send error: {e}")
  129. start_time = asyncio.get_event_loop().time()
  130. last_send = start_time
  131. while self._running and (asyncio.get_event_loop().time() - start_time) < duration:
  132. # Try to receive data
  133. try:
  134. data, addr = sock.recvfrom(4096)
  135. message = data.decode("utf-8", errors="ignore")
  136. logger.debug(f"Received from {addr[0]}: {message[:100]}...")
  137. self._handle_response(message, addr[0])
  138. except BlockingIOError:
  139. # No data available, that's fine
  140. pass
  141. except Exception as e:
  142. logger.debug(f"SSDP receive error: {e}")
  143. # Re-send M-SEARCH every 3 seconds
  144. now = asyncio.get_event_loop().time()
  145. if now - last_send >= 3.0:
  146. try:
  147. sock.sendto(SSDP_MSEARCH.encode(), (SSDP_ADDR, SSDP_PORT))
  148. last_send = now
  149. except Exception as e:
  150. logger.debug(f"SSDP send error: {e}")
  151. await asyncio.sleep(0.1)
  152. logger.info(f"Discovery complete. Found {len(self._discovered)} printers.")
  153. except OSError as e:
  154. if e.errno == 98: # Address already in use
  155. logger.warning(f"Port {SSDP_PORT} is in use, trying alternative discovery...")
  156. await self._discover_alternative(duration)
  157. else:
  158. logger.error(f"Discovery error: {e}")
  159. except Exception as e:
  160. logger.error(f"Discovery error: {e}")
  161. finally:
  162. self._running = False
  163. if sock:
  164. try:
  165. sock.close()
  166. except Exception:
  167. pass
  168. async def _discover_alternative(self, duration: float):
  169. """Alternative discovery using a random port (less reliable)."""
  170. sock = None
  171. try:
  172. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  173. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  174. sock.setblocking(False)
  175. sock.bind(("", 0))
  176. # Join multicast group
  177. mreq = struct.pack("4sl", socket.inet_aton(SSDP_ADDR), socket.INADDR_ANY)
  178. sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
  179. sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
  180. logger.info("Using alternative discovery method...")
  181. start_time = asyncio.get_event_loop().time()
  182. last_send = start_time
  183. while self._running and (asyncio.get_event_loop().time() - start_time) < duration:
  184. try:
  185. data, addr = sock.recvfrom(4096)
  186. self._handle_response(data.decode("utf-8", errors="ignore"), addr[0])
  187. except BlockingIOError:
  188. pass
  189. except Exception as e:
  190. logger.debug(f"SSDP receive error: {e}")
  191. now = asyncio.get_event_loop().time()
  192. if now - last_send >= 2.0:
  193. try:
  194. sock.sendto(SSDP_MSEARCH.encode(), (SSDP_ADDR, SSDP_PORT))
  195. last_send = now
  196. except Exception:
  197. pass
  198. await asyncio.sleep(0.1)
  199. logger.info(f"Alternative discovery complete. Found {len(self._discovered)} printers.")
  200. except Exception as e:
  201. logger.error(f"Alternative discovery error: {e}")
  202. finally:
  203. if sock:
  204. try:
  205. sock.close()
  206. except Exception:
  207. pass
  208. def _handle_response(self, response: str, ip_address: str):
  209. """Parse SSDP response and extract printer info."""
  210. # Check if it's a Bambu Lab printer response
  211. if BAMBU_SEARCH_TARGET not in response and "bambulab" not in response.lower():
  212. logger.debug(f"Ignoring non-Bambu response from {ip_address}")
  213. return
  214. # Extract USN (Unique Service Name) which contains the serial
  215. # Bambu format is just "USN: SERIALNUMBER" (no uuid: prefix)
  216. usn_match = re.search(r"USN:\s*(?:uuid:)?([^\s\r\n]+)", response, re.IGNORECASE)
  217. if not usn_match:
  218. logger.debug(f"No USN found in response from {ip_address}")
  219. return
  220. serial = usn_match.group(1).strip()
  221. # Skip Bambuddy's own virtual printer
  222. if serial == VIRTUAL_PRINTER_SERIAL:
  223. logger.debug(f"Ignoring Bambuddy virtual printer at {ip_address}")
  224. return
  225. # Extract device name from LOCATION or DevName header
  226. name = serial # Default to serial if no name found
  227. name_match = re.search(r"DevName\.bambu\.com:\s*(.+?)(?:\r\n|\n|$)", response, re.IGNORECASE)
  228. if name_match:
  229. name = name_match.group(1).strip()
  230. # Try to extract model from DevModel header
  231. model = None
  232. model_match = re.search(r"DevModel\.bambu\.com:\s*(.+?)(?:\r\n|\n|$)", response, re.IGNORECASE)
  233. if model_match:
  234. model = model_match.group(1).strip()
  235. # Also try NT header for model
  236. if not model:
  237. nt_match = re.search(r"NT:\s*urn:bambulab-com:device:([^:]+)", response, re.IGNORECASE)
  238. if nt_match:
  239. model = nt_match.group(1).strip()
  240. # Skip if already discovered
  241. if serial in self._discovered:
  242. return
  243. printer = DiscoveredPrinter(
  244. serial=serial,
  245. name=name,
  246. ip_address=ip_address,
  247. model=model,
  248. discovered_at=datetime.now().isoformat(),
  249. )
  250. self._discovered[serial] = printer
  251. logger.info(f"Discovered printer: {name} ({serial}) at {ip_address}")
  252. class SubnetScanner:
  253. """Scanner for discovering Bambu printers by probing IP addresses."""
  254. # Bambu printer ports
  255. MQTT_PORT = 8883
  256. FTP_PORT = 990
  257. def __init__(self):
  258. self._discovered: dict[str, DiscoveredPrinter] = {}
  259. self._running = False
  260. self._scanned = 0
  261. self._total = 0
  262. @property
  263. def is_running(self) -> bool:
  264. return self._running
  265. @property
  266. def discovered_printers(self) -> list[DiscoveredPrinter]:
  267. return list(self._discovered.values())
  268. @property
  269. def progress(self) -> tuple[int, int]:
  270. """Return (scanned, total) counts."""
  271. return self._scanned, self._total
  272. async def scan_subnet(self, subnet: str, timeout: float = 1.0) -> list[DiscoveredPrinter]:
  273. """Scan a subnet for Bambu printers.
  274. Args:
  275. subnet: CIDR notation subnet (e.g., "192.168.1.0/24")
  276. timeout: Connection timeout per host in seconds
  277. Returns:
  278. List of discovered printers
  279. """
  280. if self._running:
  281. return []
  282. self._running = True
  283. self._discovered.clear()
  284. self._scanned = 0
  285. try:
  286. network = ipaddress.ip_network(subnet, strict=False)
  287. hosts = list(network.hosts())
  288. self._total = len(hosts)
  289. if self._total > 1024:
  290. logger.warning(f"Subnet {subnet} has {self._total} hosts, limiting to /22 (1024 hosts)")
  291. self._total = 1024
  292. hosts = hosts[:1024]
  293. logger.info(f"Starting subnet scan of {subnet} ({self._total} hosts)")
  294. # Scan in batches to avoid overwhelming the network
  295. batch_size = 50
  296. for i in range(0, len(hosts), batch_size):
  297. if not self._running:
  298. break
  299. batch = hosts[i : i + batch_size]
  300. tasks = [self._probe_host(str(ip), timeout) for ip in batch]
  301. await asyncio.gather(*tasks, return_exceptions=True)
  302. self._scanned = min(i + batch_size, len(hosts))
  303. logger.info(f"Subnet scan complete. Found {len(self._discovered)} printers.")
  304. return self.discovered_printers
  305. except ValueError as e:
  306. logger.error(f"Invalid subnet format: {e}")
  307. return []
  308. finally:
  309. self._running = False
  310. async def _probe_host(self, ip: str, timeout: float):
  311. """Probe a single host for Bambu printer ports."""
  312. # Check FTP port (990) - more reliable indicator
  313. ftp_open = await self._check_port(ip, self.FTP_PORT, timeout)
  314. if not ftp_open:
  315. return
  316. # Also check MQTT port (8883) for confirmation
  317. mqtt_open = await self._check_port(ip, self.MQTT_PORT, timeout)
  318. if not mqtt_open:
  319. return
  320. # Both ports open - likely a Bambu printer
  321. logger.info(f"Found potential Bambu printer at {ip}")
  322. # Try to get printer info via SSDP unicast
  323. serial, name, model = await self._get_printer_info_ssdp(ip, timeout)
  324. # Skip Bambuddy's own virtual printer
  325. if serial == VIRTUAL_PRINTER_SERIAL:
  326. logger.debug(f"Ignoring Bambuddy virtual printer at {ip}")
  327. return
  328. printer = DiscoveredPrinter(
  329. serial=serial or f"unknown-{ip.replace('.', '-')}",
  330. name=name or f"Printer at {ip}",
  331. ip_address=ip,
  332. model=model,
  333. discovered_at=datetime.now().isoformat(),
  334. )
  335. self._discovered[ip] = printer
  336. async def _get_printer_info_ssdp(self, ip: str, timeout: float) -> tuple[str | None, str | None, str | None]:
  337. """Try to get printer info via SSDP unicast query."""
  338. loop = asyncio.get_event_loop()
  339. def _query():
  340. try:
  341. sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  342. sock.settimeout(timeout)
  343. sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  344. # Send M-SEARCH directly to the printer
  345. msearch = (
  346. "M-SEARCH * HTTP/1.1\r\n"
  347. f"HOST: {ip}:{SSDP_PORT}\r\n"
  348. 'MAN: "ssdp:discover"\r\n'
  349. "MX: 1\r\n"
  350. f"ST: {BAMBU_SEARCH_TARGET}\r\n"
  351. "\r\n"
  352. )
  353. sock.sendto(msearch.encode(), (ip, SSDP_PORT))
  354. # Wait for response
  355. data, _ = sock.recvfrom(4096)
  356. response = data.decode("utf-8", errors="ignore")
  357. sock.close()
  358. # Parse response
  359. serial = None
  360. name = None
  361. model = None
  362. usn_match = re.search(r"USN:\s*(?:uuid:)?([^\s\r\n]+)", response, re.IGNORECASE)
  363. if usn_match:
  364. serial = usn_match.group(1).strip()
  365. name_match = re.search(r"DevName\.bambu\.com:\s*(.+?)(?:\r\n|\n|$)", response, re.IGNORECASE)
  366. if name_match:
  367. name = name_match.group(1).strip()
  368. model_match = re.search(r"DevModel\.bambu\.com:\s*(.+?)(?:\r\n|\n|$)", response, re.IGNORECASE)
  369. if model_match:
  370. model = model_match.group(1).strip()
  371. logger.debug(f"SSDP info from {ip}: serial={serial}, name={name}, model={model}")
  372. return serial, name, model
  373. except Exception as e:
  374. logger.debug(f"SSDP query to {ip} failed: {e}")
  375. return None, None, None
  376. return await loop.run_in_executor(None, _query)
  377. async def _check_port(self, ip: str, port: int, timeout: float) -> bool:
  378. """Check if a port is open on the given IP."""
  379. try:
  380. _, writer = await asyncio.wait_for(asyncio.open_connection(ip, port), timeout=timeout)
  381. writer.close()
  382. await writer.wait_closed()
  383. logger.debug(f"Port {port} open on {ip}")
  384. return True
  385. except TimeoutError:
  386. return False
  387. except ConnectionRefusedError:
  388. return False
  389. except OSError as e:
  390. # Log first few errors to help debug network issues
  391. if self._scanned < 5:
  392. logger.debug(f"OSError checking {ip}:{port}: {e}")
  393. return False
  394. def stop(self):
  395. """Stop the current scan."""
  396. self._running = False
  397. class TasmotaScanner:
  398. """Scanner for discovering Tasmota devices by probing IP addresses."""
  399. HTTP_PORT = 80
  400. def __init__(self):
  401. self._discovered: dict[str, dict] = {}
  402. self._running = False
  403. self._scanned = 0
  404. self._total = 0
  405. @property
  406. def is_running(self) -> bool:
  407. return self._running
  408. @property
  409. def discovered_devices(self) -> list[dict]:
  410. return list(self._discovered.values())
  411. @property
  412. def progress(self) -> tuple[int, int]:
  413. """Return (scanned, total) counts."""
  414. return self._scanned, self._total
  415. async def scan_range(self, from_ip: str, to_ip: str, timeout: float = 1.0) -> list[dict]:
  416. """Scan an IP range for Tasmota devices.
  417. Args:
  418. from_ip: Starting IP address (e.g., "192.168.1.1")
  419. to_ip: Ending IP address (e.g., "192.168.1.254")
  420. timeout: Connection timeout per host in seconds
  421. Returns:
  422. List of discovered Tasmota devices
  423. """
  424. if self._running:
  425. return []
  426. self._running = True
  427. self._discovered.clear()
  428. self._scanned = 0
  429. try:
  430. start = ipaddress.ip_address(from_ip)
  431. end = ipaddress.ip_address(to_ip)
  432. # Generate list of IPs in range
  433. hosts = []
  434. current = start
  435. while current <= end:
  436. hosts.append(str(current))
  437. current = ipaddress.ip_address(int(current) + 1)
  438. self._total = len(hosts)
  439. if self._total > 1024:
  440. logger.warning(f"IP range has {self._total} hosts, limiting to 1024")
  441. self._total = 1024
  442. hosts = hosts[:1024]
  443. logger.info(f"Starting Tasmota scan from {from_ip} to {to_ip} ({self._total} hosts)")
  444. # Scan in batches to avoid overwhelming the network
  445. batch_size = 50
  446. for i in range(0, len(hosts), batch_size):
  447. if not self._running:
  448. logger.info("Tasmota scan stopped by user")
  449. break
  450. batch = hosts[i : i + batch_size]
  451. tasks = [self._probe_host(ip) for ip in batch]
  452. try:
  453. await asyncio.gather(*tasks, return_exceptions=True)
  454. except Exception as e:
  455. logger.warning(f"Batch {i // batch_size} error: {e}")
  456. self._scanned = min(i + batch_size, len(hosts))
  457. logger.info(f"Tasmota scan complete. Found {len(self._discovered)} devices.")
  458. return self.discovered_devices
  459. except ValueError as e:
  460. logger.error(f"Invalid IP address format: {e}")
  461. return []
  462. finally:
  463. self._running = False
  464. async def _probe_host(self, ip: str):
  465. """Probe a single host for Tasmota HTTP API."""
  466. try:
  467. # Hard timeout of 5 seconds max per host
  468. await asyncio.wait_for(self._do_probe(ip), timeout=5.0)
  469. except TimeoutError:
  470. pass
  471. except Exception:
  472. pass
  473. async def _do_probe(self, ip: str):
  474. """Actually probe the host."""
  475. import httpx
  476. try:
  477. # Reasonable timeouts for network scanning
  478. client_timeout = httpx.Timeout(3.0, connect=1.0)
  479. async with httpx.AsyncClient(timeout=client_timeout, follow_redirects=False) as client:
  480. # First try simple Power command - most reliable indicator of Tasmota
  481. power_url = f"http://{ip}/cm?cmnd=Power"
  482. try:
  483. power_response = await client.get(power_url)
  484. if power_response.status_code == 401:
  485. # Device requires auth - still a Tasmota device!
  486. logger.info(f"Discovered Tasmota at {ip} (requires auth - 401)")
  487. device = {
  488. "ip_address": ip,
  489. "name": f"Tasmota ({ip})",
  490. "module": None,
  491. "state": "UNKNOWN",
  492. "discovered_at": datetime.now().isoformat(),
  493. }
  494. self._discovered[ip] = device
  495. return
  496. if power_response.status_code != 200:
  497. return
  498. power_data = power_response.json()
  499. # Check for Tasmota auth warning (returns 200 with WARNING)
  500. if "WARNING" in power_data:
  501. logger.info(f"Discovered Tasmota at {ip} (requires auth)")
  502. device = {
  503. "ip_address": ip,
  504. "name": f"Tasmota ({ip})",
  505. "module": None,
  506. "state": "UNKNOWN",
  507. "discovered_at": datetime.now().isoformat(),
  508. }
  509. self._discovered[ip] = device
  510. return
  511. # Check if response looks like Tasmota (has POWER or POWER1 key)
  512. power_state = power_data.get("POWER") or power_data.get("POWER1")
  513. if power_state is None:
  514. return
  515. except Exception as e:
  516. logger.debug(f"Error probing {ip}: {e}")
  517. return
  518. # It's a Tasmota device! Now get more info
  519. device_name = f"Tasmota ({ip})"
  520. module = None
  521. # Try to get device name from Status 0
  522. try:
  523. status_url = f"http://{ip}/cm?cmnd=Status%200"
  524. status_response = await client.get(status_url)
  525. if status_response.status_code == 200:
  526. status_data = status_response.json()
  527. if "Status" in status_data:
  528. status = status_data["Status"]
  529. device_name = status.get("DeviceName") or device_name
  530. if not device_name or device_name == f"Tasmota ({ip})":
  531. # Try FriendlyName
  532. friendly = status.get("FriendlyName")
  533. if friendly and isinstance(friendly, list) and friendly[0]:
  534. device_name = friendly[0]
  535. module = status.get("Module")
  536. except Exception:
  537. pass
  538. device = {
  539. "ip_address": ip,
  540. "name": device_name,
  541. "module": module,
  542. "state": power_state,
  543. "discovered_at": datetime.now().isoformat(),
  544. }
  545. self._discovered[ip] = device
  546. logger.info(f"Discovered Tasmota device: {device_name} at {ip}")
  547. except httpx.TimeoutException:
  548. pass
  549. except httpx.ConnectError:
  550. pass
  551. except Exception:
  552. pass
  553. def stop(self):
  554. """Stop the current scan."""
  555. self._running = False
  556. # Global instances
  557. discovery_service = PrinterDiscoveryService()
  558. subnet_scanner = SubnetScanner()
  559. tasmota_scanner = TasmotaScanner()