Skip to content

Commit 2c987d1

Browse files
committed
Add docstrings
1 parent dffe576 commit 2c987d1

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

dingz/discovery.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Discover Dingz devices in a network."""
12
import asyncio
23
import logging
34
from typing import Optional, List
@@ -6,6 +7,7 @@
67

78

89
class DiscoveredDevice(object):
10+
"""Representation of discovered device."""
911
mac: str
1012
type: int
1113
is_child: bool
@@ -15,9 +17,10 @@ class DiscoveredDevice(object):
1517

1618
@staticmethod
1719
def create_from_announce_msg(raw_addr, announce_msg):
18-
_LOGGER.debug("received announce msg '%s' from %s ", announce_msg, raw_addr)
20+
"""Create announce message."""
21+
_LOGGER.debug("Received announce message '%s' from %s ", announce_msg, raw_addr)
1922
if len(announce_msg) != 8:
20-
raise RuntimeError("unexpected announcement, '%s'" % announce_msg)
23+
raise RuntimeError("Unexpected announcement, '%s'" % announce_msg)
2124

2225
device = DiscoveredDevice(host=raw_addr[0], mac=announce_msg[0:6].hex(":"))
2326
device.type = announce_msg[6]
@@ -31,43 +34,54 @@ def create_from_announce_msg(raw_addr, announce_msg):
3134
return device
3235

3336
def __init__(self, host, mac):
37+
"""Initialize the """
3438
self.host = host
3539
self.mac = mac
3640

3741

3842
class DeviceRegistry(object):
43+
"""Representation of the device registry."""
44+
3945
def __init__(self):
46+
"""Initialize the device registry."""
4047
self.devices_by_mac = {}
4148

4249
def register(self, device):
50+
"""Register a device."""
4351
self.devices_by_mac[device.mac] = device
4452

4553
def devices(self):
54+
"""Get all present devices"""
4655
return list(self.devices_by_mac.values())
4756

4857

4958
class DiscoveryProtocol(asyncio.DatagramProtocol):
59+
"""Representation of the discovery protocol."""
5060
def __init__(self, registry: DeviceRegistry):
61+
""""Initialize the discovery protocol."""
5162
super().__init__()
5263
self.registry = registry
5364

5465
def connection_made(self, transport):
55-
_LOGGER.debug("starting up udp listener")
66+
"""Create an UDP listener."""
67+
_LOGGER.debug("Starting up UDP listener")
5668
self.transport = transport
5769

5870
def datagram_received(self, data, addr):
71+
"""Handle a datagram."""
5972
device = DiscoveredDevice.create_from_announce_msg(addr, data)
6073
self.registry.register(device)
6174

6275
def connection_lost(self, exc: Optional[Exception]) -> None:
63-
_LOGGER.debug("shutting down udp listener")
76+
"""Stop if connection is lost."""
77+
_LOGGER.debug("Shutting down UDP listener")
6478
super().connection_lost(exc)
6579

6680

6781
async def discover_dingz_devices(timeout: int = 7) -> List[DiscoveredDevice]:
6882
"""
6983
Try to discover all local dingz instances. All dingz instances
70-
report their presence every ~5 seconds in a UDP broadcast to port 7979.
84+
report their presence every ~5 seconds in an UDP broadcast to port 7979.
7185
7286
:param timeout: timeout in seconds for discover.
7387
:return: list of discovered devices
@@ -77,16 +91,16 @@ async def discover_dingz_devices(timeout: int = 7) -> List[DiscoveredDevice]:
7791
(transport, protocol) = await loop.create_datagram_endpoint(
7892
lambda: DiscoveryProtocol(registry), local_addr=("0.0.0.0", 7979)
7993
)
80-
# server runs in the background, meanwhile wait until timeout expires
94+
# Server runs in the background, meanwhile wait until timeout expires
8195
await asyncio.sleep(timeout)
8296

83-
# shutdown server
97+
# Shutdown server
8498
transport.close()
8599

86100
devices = registry.devices()
87101
for device in devices:
88102
_LOGGER.debug(
89-
"discovered dingz %s (%s) with mac %s", device.host, device.type, device.mac
103+
"Discovered dingz %s (%s) with mac %s", device.host, device.type, device.mac
90104
)
91105
return devices
92106

0 commit comments

Comments
 (0)