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

Commit 170579e

Browse files
authored
Merge pull request #103 from home-assistant/cleanup-after-use
Cleanup after use
2 parents a22fb53 + e5a6d0d commit 170579e

File tree

14 files changed

+63
-234
lines changed

14 files changed

+63
-234
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ cache:
44
directories:
55
- $HOME/.cache/pip
66
python:
7-
- 2.7
8-
- 3.4
7+
- 3.4.2
98
install:
109
- pip install -r requirements.txt
1110
- pip install pylint flake8

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# NetDisco
22

3-
NetDisco is a Python (2 and 3) library to discover local devices and services. It allows to scan on demand or offer a service that will scan the network in the background in a set interval.
3+
NetDisco is a Python 3 library to discover local devices and services. It allows to scan on demand or offer a service that will scan the network in the background in a set interval.
44

55
Current methods of scanning:
66

@@ -15,7 +15,7 @@ It is the library that powers the device discovery within [Home Assistant](https
1515

1616
## Installation
1717

18-
Netdisco is available on PyPi. Install using `pip3 install netdisco` (use `pip` if you're still on Python 2).
18+
Netdisco is available on PyPi. Install using `pip3 install netdisco`.
1919

2020
## Example
2121

netdisco/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""Command line tool to print discocvered devices or dump raw data."""
2-
from __future__ import print_function
32
import sys
43

54
from netdisco.discovery import NetworkDiscovery

netdisco/daikin.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Daikin device discovery."""
22
import socket
3-
import threading
43

54
# pylint: disable=unused-import, import-error, no-name-in-module
65
try:
@@ -27,12 +26,10 @@ class Daikin(object):
2726
def __init__(self):
2827
"""Initialize the Daikin discovery."""
2928
self.entries = []
30-
self._lock = threading.RLock()
3129

3230
def scan(self):
3331
"""Scan the network."""
34-
with self._lock:
35-
self.update()
32+
self.update()
3633

3734
def all(self):
3835
"""Scan and return all found entries."""

netdisco/discoverables/denonavr.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
"""Discover Denon AVR devices."""
2-
3-
try:
4-
# python 3
5-
from urllib.parse import urlparse
6-
except ImportError:
7-
# python 2
8-
from urlparse import urlparse
2+
from urllib.parse import urlparse
93

104
from . import SSDPDiscoverable
115

netdisco/discoverables/samsung_tv.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
"""Discover Samsung Smart TV services."""
2-
3-
try:
4-
# python 3
5-
from urllib.parse import urlparse
6-
except ImportError:
7-
# python 2
8-
from urlparse import urlparse
2+
from urllib.parse import urlparse
93

104
from . import SSDPDiscoverable
115

netdisco/discoverables/samsungac.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

netdisco/discovery.py

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""Combine all the different protocols into a simple interface."""
2-
from __future__ import print_function
32
import logging
43
import os
54
import importlib
6-
import threading
75

86
from .ssdp import SSDP
97
from .mdns import MDNS
@@ -36,54 +34,44 @@ class NetworkDiscovery(object):
3634
def __init__(self):
3735
"""Initialize the discovery."""
3836

39-
self.mdns = MDNS()
40-
self.ssdp = SSDP()
41-
self.gdm = GDM()
42-
self.lms = LMS()
43-
self.tellstick = Tellstick()
44-
self.daikin = Daikin()
45-
# self.samsungac = SamsungAC()
46-
self.phue = PHueNUPnPDiscovery()
47-
self.discoverables = {}
48-
49-
self._load_device_support()
37+
self.mdns = None
38+
self.ssdp = None
39+
self.gdm = None
40+
self.lms = None
41+
self.tellstick = None
42+
self.daikin = None
43+
self.phue = None
5044

5145
self.is_discovering = False
46+
self.discoverables = None
5247

5348
def scan(self):
5449
"""Start and tells scanners to scan."""
55-
if not self.is_discovering:
56-
self.mdns.start()
57-
self.is_discovering = True
50+
self.is_discovering = True
5851

59-
# Start all discovery processes in parallel
60-
ssdp_thread = threading.Thread(target=self.ssdp.scan)
61-
ssdp_thread.start()
52+
self.mdns = MDNS()
53+
self.mdns.start()
6254

63-
gdm_thread = threading.Thread(target=self.gdm.scan)
64-
gdm_thread.start()
55+
# Needs to be after MDNS
56+
self._load_device_support()
6557

66-
lms_thread = threading.Thread(target=self.lms.scan)
67-
lms_thread.start()
58+
self.ssdp = SSDP()
59+
self.ssdp.scan()
6860

69-
tellstick_thread = threading.Thread(target=self.tellstick.scan)
70-
tellstick_thread.start()
61+
self.gdm = GDM()
62+
self.gdm.scan()
7163

72-
daikin_thread = threading.Thread(target=self.daikin.scan)
73-
daikin_thread.start()
64+
self.lms = LMS()
65+
self.lms.scan()
7466

75-
# self.samsungac.scan()
67+
self.tellstick = Tellstick()
68+
self.tellstick.scan()
7669

77-
phue_thread = threading.Thread(target=self.phue.scan)
78-
phue_thread.start()
70+
self.daikin = Daikin()
71+
self.daikin.scan()
7972

80-
# Wait for all discovery processes to complete
81-
ssdp_thread.join()
82-
gdm_thread.join()
83-
lms_thread.join()
84-
tellstick_thread.join()
85-
daikin_thread.join()
86-
phue_thread.join()
73+
self.phue = PHueNUPnPDiscovery()
74+
self.phue.scan()
8775

8876
def stop(self):
8977
"""Turn discovery off."""
@@ -92,11 +80,20 @@ def stop(self):
9280

9381
self.mdns.stop()
9482

83+
# Not removing SSDP because it tracks state
84+
self.mdns = None
85+
self.gdm = None
86+
self.lms = None
87+
self.tellstick = None
88+
self.daikin = None
89+
self.phue = None
90+
self.discoverables = None
9591
self.is_discovering = False
9692

9793
def discover(self):
9894
"""Return a list of discovered devices and services."""
99-
self._check_enabled()
95+
if not self.is_discovering:
96+
raise RuntimeError("Needs to be called after start, before stop")
10097

10198
return [dis for dis, checker in self.discoverables.items()
10299
if checker.is_discovered()]
@@ -109,11 +106,6 @@ def get_entries(self, dis):
109106
"""Get a list with all info about a discovered type."""
110107
return self.discoverables[dis].get_entries()
111108

112-
def _check_enabled(self):
113-
"""Raise RuntimeError if discovery is disabled."""
114-
if not self.is_discovering:
115-
raise RuntimeError("NetworkDiscovery is disabled")
116-
117109
def _load_device_support(self):
118110
"""Load the devices and services that can be discovered."""
119111
self.discoverables = {}

netdisco/gdm.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
https://github.com/hippojay/script.plexbmc.helper/resources/lib/plexgdm.py
77
iBaa's PlexConnect: https://github.com/iBaa/PlexConnect/PlexAPI.py
88
"""
9-
import threading
109
import socket
1110

1211

@@ -16,12 +15,10 @@ class GDM(object):
1615
def __init__(self):
1716
self.entries = []
1817
self.last_scan = None
19-
self._lock = threading.RLock()
2018

2119
def scan(self):
2220
"""Scan the network."""
23-
with self._lock:
24-
self.update()
21+
self.update()
2522

2623
def all(self):
2724
"""Return all found entries.

netdisco/lms.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Squeezebox/Logitech Media server discovery."""
22
import socket
3-
import threading
43

54
from .const import ATTR_HOST, ATTR_PORT
65

@@ -15,12 +14,10 @@ def __init__(self):
1514
"""Initialize the Logitech discovery."""
1615
self.entries = []
1716
self.last_scan = None
18-
self._lock = threading.RLock()
1917

2018
def scan(self):
2119
"""Scan the network."""
22-
with self._lock:
23-
self.update()
20+
self.update()
2421

2522
def all(self):
2623
"""Scan and return all found entries."""

0 commit comments

Comments
 (0)