From 5da3196668d3415159902240c25fbfd6f90b0e20 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Tue, 26 May 2020 19:08:09 +0300 Subject: [PATCH] Fix sonoff camera random port --- custom_components/sonoff/sonoff_camera.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/custom_components/sonoff/sonoff_camera.py b/custom_components/sonoff/sonoff_camera.py index 6895dfce..da769286 100644 --- a/custom_components/sonoff/sonoff_camera.py +++ b/custom_components/sonoff/sonoff_camera.py @@ -73,12 +73,6 @@ class EWeLinkCameras(Thread): def __init__(self): super().__init__(name="Sonoff_CAM", daemon=True) - self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - # TODO: random port - self.sock.bind(('', 50000)) - self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) - def datagram_received(self, data: bytes, addr: tuple): # _LOGGER.debug(f"<= {addr[0]:15} {data[:80].hex()}") @@ -130,13 +124,21 @@ def sendto(self, data: Union[bytes, str], device: Camera): # _LOGGER.debug(f"=> {device.addr[0]:15} {data[:60].hex()}") self.sock.sendto(data, device.addr) + def start(self): + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) + self.sock.bind(('', 0)) + + super().start() + async def send(self, deviceid: str, command: str): device = self.devices.get(deviceid) if not device or time.time() - device.last_time > 9: # start Thread if first time if not self.is_alive(): - super().start() + self.start() if not device: # create new device, we want wait for it