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

Commit

Permalink
Merge pull request #103 from home-assistant/cleanup-after-use
Browse files Browse the repository at this point in the history
Cleanup after use
  • Loading branch information
balloob authored Mar 1, 2017
2 parents a22fb53 + e5a6d0d commit 170579e
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 234 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ cache:
directories:
- $HOME/.cache/pip
python:
- 2.7
- 3.4
- 3.4.2
install:
- pip install -r requirements.txt
- pip install pylint flake8
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NetDisco

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.
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.

Current methods of scanning:

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

## Installation

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

## Example

Expand Down
1 change: 0 additions & 1 deletion netdisco/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Command line tool to print discocvered devices or dump raw data."""
from __future__ import print_function
import sys

from netdisco.discovery import NetworkDiscovery
Expand Down
5 changes: 1 addition & 4 deletions netdisco/daikin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Daikin device discovery."""
import socket
import threading

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

def scan(self):
"""Scan the network."""
with self._lock:
self.update()
self.update()

def all(self):
"""Scan and return all found entries."""
Expand Down
8 changes: 1 addition & 7 deletions netdisco/discoverables/denonavr.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
"""Discover Denon AVR devices."""

try:
# python 3
from urllib.parse import urlparse
except ImportError:
# python 2
from urlparse import urlparse
from urllib.parse import urlparse

from . import SSDPDiscoverable

Expand Down
8 changes: 1 addition & 7 deletions netdisco/discoverables/samsung_tv.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
"""Discover Samsung Smart TV services."""

try:
# python 3
from urllib.parse import urlparse
except ImportError:
# python 2
from urlparse import urlparse
from urllib.parse import urlparse

from . import SSDPDiscoverable

Expand Down
15 changes: 0 additions & 15 deletions netdisco/discoverables/samsungac.py

This file was deleted.

78 changes: 35 additions & 43 deletions netdisco/discovery.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""Combine all the different protocols into a simple interface."""
from __future__ import print_function
import logging
import os
import importlib
import threading

from .ssdp import SSDP
from .mdns import MDNS
Expand Down Expand Up @@ -36,54 +34,44 @@ class NetworkDiscovery(object):
def __init__(self):
"""Initialize the discovery."""

self.mdns = MDNS()
self.ssdp = SSDP()
self.gdm = GDM()
self.lms = LMS()
self.tellstick = Tellstick()
self.daikin = Daikin()
# self.samsungac = SamsungAC()
self.phue = PHueNUPnPDiscovery()
self.discoverables = {}

self._load_device_support()
self.mdns = None
self.ssdp = None
self.gdm = None
self.lms = None
self.tellstick = None
self.daikin = None
self.phue = None

self.is_discovering = False
self.discoverables = None

def scan(self):
"""Start and tells scanners to scan."""
if not self.is_discovering:
self.mdns.start()
self.is_discovering = True
self.is_discovering = True

# Start all discovery processes in parallel
ssdp_thread = threading.Thread(target=self.ssdp.scan)
ssdp_thread.start()
self.mdns = MDNS()
self.mdns.start()

gdm_thread = threading.Thread(target=self.gdm.scan)
gdm_thread.start()
# Needs to be after MDNS
self._load_device_support()

lms_thread = threading.Thread(target=self.lms.scan)
lms_thread.start()
self.ssdp = SSDP()
self.ssdp.scan()

tellstick_thread = threading.Thread(target=self.tellstick.scan)
tellstick_thread.start()
self.gdm = GDM()
self.gdm.scan()

daikin_thread = threading.Thread(target=self.daikin.scan)
daikin_thread.start()
self.lms = LMS()
self.lms.scan()

# self.samsungac.scan()
self.tellstick = Tellstick()
self.tellstick.scan()

phue_thread = threading.Thread(target=self.phue.scan)
phue_thread.start()
self.daikin = Daikin()
self.daikin.scan()

# Wait for all discovery processes to complete
ssdp_thread.join()
gdm_thread.join()
lms_thread.join()
tellstick_thread.join()
daikin_thread.join()
phue_thread.join()
self.phue = PHueNUPnPDiscovery()
self.phue.scan()

def stop(self):
"""Turn discovery off."""
Expand All @@ -92,11 +80,20 @@ def stop(self):

self.mdns.stop()

# Not removing SSDP because it tracks state
self.mdns = None
self.gdm = None
self.lms = None
self.tellstick = None
self.daikin = None
self.phue = None
self.discoverables = None
self.is_discovering = False

def discover(self):
"""Return a list of discovered devices and services."""
self._check_enabled()
if not self.is_discovering:
raise RuntimeError("Needs to be called after start, before stop")

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

def _check_enabled(self):
"""Raise RuntimeError if discovery is disabled."""
if not self.is_discovering:
raise RuntimeError("NetworkDiscovery is disabled")

def _load_device_support(self):
"""Load the devices and services that can be discovered."""
self.discoverables = {}
Expand Down
5 changes: 1 addition & 4 deletions netdisco/gdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
https://github.com/hippojay/script.plexbmc.helper/resources/lib/plexgdm.py
iBaa's PlexConnect: https://github.com/iBaa/PlexConnect/PlexAPI.py
"""
import threading
import socket


Expand All @@ -16,12 +15,10 @@ class GDM(object):
def __init__(self):
self.entries = []
self.last_scan = None
self._lock = threading.RLock()

def scan(self):
"""Scan the network."""
with self._lock:
self.update()
self.update()

def all(self):
"""Return all found entries.
Expand Down
5 changes: 1 addition & 4 deletions netdisco/lms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Squeezebox/Logitech Media server discovery."""
import socket
import threading

from .const import ATTR_HOST, ATTR_PORT

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

def scan(self):
"""Scan the network."""
with self._lock:
self.update()
self.update()

def all(self):
"""Scan and return all found entries."""
Expand Down
114 changes: 0 additions & 114 deletions netdisco/samsungac.py

This file was deleted.

Loading

0 comments on commit 170579e

Please sign in to comment.