1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-21 22:51:41 +01:00

Add a function to send ping packets (#526)

Rename 'host' attribute to 'address' (ping) (#528)
This commit is contained in:
Felipe Martins Diel 2021-01-27 16:39:54 -03:00
parent 9af3a3c56c
commit 21fa2a20bf
2 changed files with 24 additions and 1 deletions

View File

@ -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

View File

@ -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])