Skip to content

Commit

Permalink
Add Synology SRM device tracker (home-assistant#20320)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerialls authored and pvizeli committed Jan 29, 2019
1 parent 73a0c66 commit 6859d52
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ omit =
homeassistant/components/device_tracker/sky_hub.py
homeassistant/components/device_tracker/snmp.py
homeassistant/components/device_tracker/swisscom.py
homeassistant/components/device_tracker/synology_srm.py
homeassistant/components/device_tracker/tado.py
homeassistant/components/device_tracker/thomson.py
homeassistant/components/device_tracker/tile.py
Expand Down
100 changes: 100 additions & 0 deletions homeassistant/components/device_tracker/synology_srm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Device tracker for Synology SRM routers.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.synology_srm/
"""
import logging
import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
from homeassistant.const import (
CONF_HOST, CONF_USERNAME, CONF_PASSWORD,
CONF_PORT, CONF_SSL, CONF_VERIFY_SSL)

REQUIREMENTS = ['synology-srm==0.0.3']

_LOGGER = logging.getLogger(__name__)

DEFAULT_USERNAME = 'admin'
DEFAULT_PORT = 8001
DEFAULT_SSL = True
DEFAULT_VERIFY_SSL = False

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
})


def get_scanner(hass, config):
"""Validate the configuration and return Synology SRM scanner."""
scanner = SynologySrmDeviceScanner(config[DOMAIN])

return scanner if scanner.success_init else None


class SynologySrmDeviceScanner(DeviceScanner):
"""This class scans for devices connected to a Synology SRM router."""

def __init__(self, config):
"""Initialize the scanner."""
import synology_srm

self.client = synology_srm.Client(
host=config[CONF_HOST],
port=config[CONF_PORT],
username=config[CONF_USERNAME],
password=config[CONF_PASSWORD],
https=config[CONF_SSL]
)

if not config[CONF_VERIFY_SSL]:
self.client.http.disable_https_verify()

self.last_results = []
self.success_init = self._update_info()

_LOGGER.info("Synology SRM scanner initialized")

def scan_devices(self):
"""Scan for new devices and return a list with found device IDs."""
self._update_info()

return [device['mac'] for device in self.last_results]

def get_device_name(self, device):
"""Return the name of the given device or None if we don't know."""
filter_named = [result['hostname'] for result in self.last_results if
result['mac'] == device]

if filter_named:
return filter_named[0]

return None

def _update_info(self):
"""Check the router for connected devices."""
_LOGGER.debug("Scanning for connected devices")

devices = self.client.mesh.network_wifidevice()
last_results = []

for device in devices:
last_results.append({
'mac': device['mac'],
'hostname': device['hostname']
})

_LOGGER.debug(
"Found %d device(s) connected to the router",
len(devices)
)

self.last_results = last_results
return True
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,9 @@ suds-py3==1.3.3.0
# homeassistant.components.sensor.swiss_hydrological_data
swisshydrodata==0.0.3

# homeassistant.components.device_tracker.synology_srm
synology-srm==0.0.3

# homeassistant.components.tahoma
tahoma-api==0.0.14

Expand Down

0 comments on commit 6859d52

Please sign in to comment.