forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Synology SRM device tracker (home-assistant#20320)
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
homeassistant/components/device_tracker/synology_srm.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters