Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit e8212c6

Browse files
committed
Merge pull request #19 from molobrakos/master
Add support for discovery of Tellstick Net devices
2 parents af1b6cc + 3e2c332 commit e8212c6

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

netdisco/discoverables/tellstick.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
""" Discovers Tellstick devices. """
2+
3+
from . import BaseDiscoverable
4+
5+
6+
class Discoverable(BaseDiscoverable):
7+
""" Adds support for discovering a Tellstick device. """
8+
9+
def __init__(self, netdis):
10+
self._netdis = netdis
11+
12+
def get_entries(self):
13+
return self._netdis.tellstick.entries

netdisco/discovery.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .mdns import MDNS
1111
from .gdm import GDM
1212
from .lms import LMS
13+
from .tellstick import Tellstick
1314

1415
_LOGGER = logging.getLogger(__name__)
1516

@@ -22,6 +23,7 @@ class NetworkDiscovery(object):
2223
SSDP scans in the foreground.
2324
GDM scans in the foreground.
2425
LMS scans in the foreground.
26+
Tellstick scans in the foreground
2527
2628
start: is ready to scan
2729
scan: scan the network
@@ -37,6 +39,7 @@ def __init__(self, limit_discovery=None):
3739
self.ssdp = SSDP()
3840
self.gdm = GDM()
3941
self.lms = LMS()
42+
self.tellstick = Tellstick()
4043
self.discoverables = {}
4144

4245
self._load_device_support()
@@ -52,6 +55,7 @@ def scan(self):
5255
self.ssdp.scan()
5356
self.gdm.scan()
5457
self.lms.scan()
58+
self.tellstick.scan()
5559

5660
def stop(self):
5761
""" Turn discovery off. """
@@ -124,3 +128,6 @@ def print_raw_data(self):
124128
print("")
125129
print("LMS")
126130
pprint(self.lms.entries)
131+
print("")
132+
print("Tellstick")
133+
pprint(self.tellstick.entries)

netdisco/tellstick.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
Tellstick device discovery.
3+
"""
4+
import socket
5+
import threading
6+
from datetime import timedelta
7+
8+
9+
DISCOVERY_PORT = 30303
10+
DISCOVERY_ADDRESS = '<broadcast>'
11+
DISCOVERY_PAYLOAD = b"D"
12+
DISCOVERY_TIMEOUT = timedelta(seconds=5)
13+
14+
15+
class Tellstick(object):
16+
"""Base class to discover Tellstick devices."""
17+
18+
def __init__(self):
19+
self.entries = []
20+
self._lock = threading.RLock()
21+
22+
def scan(self):
23+
"""Scan the network."""
24+
with self._lock:
25+
self.update()
26+
27+
def all(self):
28+
"""Scan and return all found entries."""
29+
self.scan()
30+
return self.entries
31+
32+
def update(self):
33+
"""Scan network for Tellstick devices."""
34+
entries = []
35+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
36+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
37+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
38+
sock.settimeout(DISCOVERY_TIMEOUT.seconds)
39+
sock.sendto(DISCOVERY_PAYLOAD, (DISCOVERY_ADDRESS, DISCOVERY_PORT))
40+
while True:
41+
try:
42+
data, (address, port) = sock.recvfrom(1024)
43+
entry = data.decode("ascii").split(":")
44+
if len(entry) != 4: # expecting product, mac, activation code, version
45+
continue
46+
entry = (address,) + tuple(entry)
47+
entries.append(entry)
48+
except socket.timeout:
49+
break
50+
51+
self.entries = entries
52+
53+
if __name__ == "__main__":
54+
from pprint import pprint
55+
tellstick = Tellstick()
56+
pprint("Scanning for Tellstick devices..")
57+
tellstick.update()
58+
pprint(tellstick.entries)

0 commit comments

Comments
 (0)