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.
Xfinity Gateway device_tracker platform (home-assistant#21026)
* initial commit * updated .coveragerc, CODEOWNERS, generated requirements_all.txt * fixed lines exceeding 79 characters * pylint fixes * shorten docstring and simplify get_scanner * extract initialization into get_scanner * bump pypi version * name change
- Loading branch information
1 parent
efe4ce9
commit 0e78054
Showing
4 changed files
with
64 additions
and
1 deletion.
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
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
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,58 @@ | ||
"""Support for device tracking via Xfinity Gateways.""" | ||
import logging | ||
|
||
from requests.exceptions import RequestException | ||
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 | ||
|
||
REQUIREMENTS = ['xfinity-gateway==0.0.4'] | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
DEFAULT_HOST = '10.0.0.1' | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string | ||
}) | ||
|
||
|
||
def get_scanner(hass, config): | ||
"""Validate the configuration and return an Xfinity Gateway scanner.""" | ||
from xfinity_gateway import XfinityGateway | ||
|
||
gateway = XfinityGateway(config[DOMAIN][CONF_HOST]) | ||
scanner = None | ||
try: | ||
gateway.scan_devices() | ||
scanner = XfinityDeviceScanner(gateway) | ||
except (RequestException, ValueError): | ||
_LOGGER.error("Error communicating with Xfinity Gateway. " | ||
"Check host: %s", gateway.host) | ||
|
||
return scanner | ||
|
||
|
||
class XfinityDeviceScanner(DeviceScanner): | ||
"""This class queries an Xfinity Gateway.""" | ||
|
||
def __init__(self, gateway): | ||
"""Initialize the scanner.""" | ||
self.gateway = gateway | ||
|
||
def scan_devices(self): | ||
"""Scan for new devices and return a list of found MACs.""" | ||
connected_devices = [] | ||
try: | ||
connected_devices = self.gateway.scan_devices() | ||
except (RequestException, ValueError): | ||
_LOGGER.error("Unable to scan devices. " | ||
"Check connection to gateway") | ||
return connected_devices | ||
|
||
def get_device_name(self, device): | ||
"""Return the name of the given device or None if we don't know.""" | ||
return self.gateway.get_device_name(device) |
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