Skip to content

Commit

Permalink
Rework _ZeroconfListener to correct typing
Browse files Browse the repository at this point in the history
_zeroconf.get_service_info might return None on timeout, and services
without any service info are no good to us, so reject those and ensure
that we only have the right types in our dict.

Also, inherit from ServiceListener, to show that we're of the right type.
  • Loading branch information
glance- committed Jan 21, 2020
1 parent 429a5e5 commit 8f6b8a6
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions homeassistant_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@
_LOGGING = logging.getLogger(__name__)


class _ZeroconfListener:
class _ZeroconfListener(zeroconf.ServiceListener):
def __init__(self) -> None:
self.services = {} # type: Dict[str, zeroconf.ServiceInfo]

def remove_service(
self, _zeroconf: zeroconf.Zeroconf, _type: str, name: str
) -> None:
"""Remove service."""
self.services[name] = None
del self.services[name]

def add_service(
self, _zeroconf: zeroconf.Zeroconf, _type: str, name: str
) -> None:
"""Add service."""
self.services[name] = _zeroconf.get_service_info(_type, name)
service = _zeroconf.get_service_info(_type, name)
# Reject services which time out
if service:
self.services[name] = service


def _locate_ha() -> Optional[str]:
Expand Down

0 comments on commit 8f6b8a6

Please sign in to comment.