Skip to content

Commit

Permalink
Change zeroconf type (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shutgun committed Apr 25, 2023
1 parent 2cb9d8e commit fc95d21
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions devolo_home_control_api/backend/mprm.py
@@ -1,4 +1,4 @@
"""mPRM communication"""
"""mPRM communication."""
import contextlib
import socket
import sys
Expand All @@ -13,7 +13,8 @@
import requests
from zeroconf import ServiceBrowser, ServiceStateChange, Zeroconf

from ..exceptions.gateway import GatewayOfflineError
from devolo_home_control_api.exceptions.gateway import GatewayOfflineError

from .mprm_websocket import MprmWebsocket


Expand All @@ -24,6 +25,7 @@ class Mprm(MprmWebsocket, ABC):
"""

def __init__(self) -> None:
"""Initialize communication."""
self._zeroconf: Optional[Zeroconf]

super().__init__()
Expand Down Expand Up @@ -54,10 +56,10 @@ def detect_gateway_in_lan(self) -> str:
:return: Local IP of the gateway, if found
"""
zeroconf = self._zeroconf or Zeroconf()
browser = ServiceBrowser(zeroconf, "_http._tcp.local.", handlers=[self._on_service_state_change])
browser = ServiceBrowser(zeroconf, "_dvl-deviceapi._tcp.local.", handlers=[self._on_service_state_change])
self._logger.info("Searching for gateway in LAN.")
start_time = time.time()
while not time.time() > start_time + 3 and self._local_ip == "":
while not time.time() > start_time + 3 and not self._local_ip:
time.sleep(0.05)

Thread(target=browser.cancel, name=f"{self.__class__.__name__}.browser_cancel").start()
Expand Down Expand Up @@ -118,7 +120,7 @@ def _on_service_state_change(
"""Service handler for Zeroconf state changes."""
if state_change is ServiceStateChange.Added:
service_info = zeroconf.get_service_info(service_type, name)
if service_info and service_info.server.startswith("devolo-homecontrol"):
if service_info and service_info.server and service_info.server.startswith("devolo-homecontrol"):
with contextlib.suppress(requests.exceptions.ReadTimeout), contextlib.suppress(
requests.exceptions.ConnectTimeout
):
Expand Down

0 comments on commit fc95d21

Please sign in to comment.