Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discovery for python-zeroconf 0.28+ #898

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions miio/discovery.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import codecs
import inspect
import ipaddress
import logging
from functools import partial
from ipaddress import ip_address
from typing import Callable, Dict, Optional, Union # noqa: F401

import zeroconf
Expand Down Expand Up @@ -205,9 +205,19 @@ def pretty_token(token):
return codecs.encode(token, "hex").decode()


def get_addr_from_info(info):
addrs = info.addresses
if len(addrs) > 1:
_LOGGER.warning(
"More than single IP address in the advertisement, using the first one"
)

return str(ip_address(addrs[0]))


def other_package_info(info, desc):
"""Return information about another package supporting the device."""
return "%s @ %s, check %s" % (info.name, ipaddress.ip_address(info.address), desc)
return "Found %s at %s, check %s" % (info.name, get_addr_from_info(info), desc)


def create_device(name: str, addr: str, device_cls: partial) -> Device:
Expand Down Expand Up @@ -261,7 +271,8 @@ def check_and_create_device(self, info, addr) -> Optional[Device]:

def add_service(self, zeroconf, type, name):
info = zeroconf.get_service_info(type, name)
addr = str(ipaddress.ip_address(info.address))
addr = get_addr_from_info(info)

if addr not in self.found_devices:
dev = self.check_and_create_device(info, addr)
self.found_devices[addr] = dev
Expand Down