From 21fa2a20bf6935bc7896432add75a971ef7c3f39 Mon Sep 17 00:00:00 2001 From: Felipe Martins Diel Date: Wed, 27 Jan 2021 16:39:54 -0300 Subject: [PATCH] Add a function to send ping packets (#526) Rename 'host' attribute to 'address' (ping) (#528) --- broadlink/__init__.py | 2 +- broadlink/device.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/broadlink/__init__.py b/broadlink/__init__.py index 1117251..1b2f0ab 100644 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -6,7 +6,7 @@ from typing import Generator, List, Union, Tuple from .alarm import S1C from .climate import hysen from .cover import dooya -from .device import device, scan +from .device import device, ping, scan from .exceptions import exception from .light import lb1 from .remote import rm, rm4 diff --git a/broadlink/device.py b/broadlink/device.py index 9509278..970ee7f 100644 --- a/broadlink/device.py +++ b/broadlink/device.py @@ -70,6 +70,20 @@ def scan( conn.close() +def ping(address: str, port: int = 80) -> None: + """Send a ping packet to an address. + + This packet feeds the watchdog timer of firmwares >= v53. + Useful to prevent reboots when the cloud cannot be reached. + It must be sent every 2 minutes in such cases. + """ + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as conn: + conn.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) + packet = bytearray(0x30) + packet[0x26] = 1 + conn.sendto(packet, (address, port)) + + class device: """Controls a Broadlink device.""" @@ -182,6 +196,15 @@ class device: self.is_locked = is_locked return True + def ping(self) -> None: + """Ping the device. + + This packet feeds the watchdog timer of firmwares >= v53. + Useful to prevent reboots when the cloud cannot be reached. + It must be sent every 2 minutes in such cases. + """ + ping(self.host[0], port=self.host[1]) + def get_fwversion(self) -> int: """Get firmware version.""" packet = bytearray([0x68])