-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bt_home_hub_5 device tracker #15096
Merged
MartinHjelmare
merged 9 commits into
home-assistant:dev
from
ahobsonsayers:bt-home-hub-5-fix
Aug 6, 2018
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f4e8b4f
Fix bt_home_hub_5 device tracker
ahobsonsayers cfd9ea4
Moved part of code to a published PyPi library
ahobsonsayers 9cced7d
Fixed Violations
ahobsonsayers daf36ea
Fixed bugs in device tracker
ahobsonsayers c3696b9
Moved API Specific Code to PyPi Repository
ahobsonsayers 9434316
Merge branch 'dev' into bt-home-hub-5-fix
ahobsonsayers 36ddf79
Updated to fit requested changes, removed test as it is no longer val…
ahobsonsayers 3aa41ef
Update to fit style requirements and remove redundant code
ahobsonsayers a598a49
Removed Unnecessary Comment
ahobsonsayers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,24 +5,22 @@ | |
https://home-assistant.io/components/device_tracker.bt_home_hub_5/ | ||
""" | ||
import logging | ||
import re | ||
import xml.etree.ElementTree as ET | ||
import json | ||
from urllib.parse import unquote | ||
|
||
import requests | ||
import voluptuous as vol | ||
|
||
import homeassistant.helpers.config_validation as cv | ||
from homeassistant.components.device_tracker import ( | ||
DOMAIN, PLATFORM_SCHEMA, DeviceScanner) | ||
from homeassistant.components.device_tracker import (DOMAIN, PLATFORM_SCHEMA, | ||
DeviceScanner) | ||
from homeassistant.const import CONF_HOST | ||
|
||
REQUIREMENTS = ['bthomehub5-devicelist==0.1.1'] | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
_MAC_REGEX = re.compile(r'(([0-9A-Fa-f]{1,2}\:){5}[0-9A-Fa-f]{1,2})') | ||
|
||
CONF_DEFAULT_IP = '192.168.1.254' | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
vol.Required(CONF_HOST): cv.string | ||
vol.Optional(CONF_HOST, default=CONF_DEFAULT_IP): cv.string, | ||
}) | ||
|
||
|
||
|
@@ -38,90 +36,44 @@ class BTHomeHub5DeviceScanner(DeviceScanner): | |
|
||
def __init__(self, config): | ||
"""Initialise the scanner.""" | ||
import bthomehub5_devicelist | ||
|
||
_LOGGER.info("Initialising BT Home Hub 5") | ||
self.host = config.get(CONF_HOST, '192.168.1.254') | ||
self.host = config[CONF_HOST] | ||
self.last_results = {} | ||
self.url = 'http://{}/nonAuth/home_status.xml'.format(self.host) | ||
|
||
# Test the router is accessible | ||
data = _get_homehub_data(self.url) | ||
data = bthomehub5_devicelist.get_devicelist(self.host) | ||
self.success_init = data is not None | ||
|
||
def scan_devices(self): | ||
"""Scan for new devices and return a list with found device IDs.""" | ||
self._update_info() | ||
self.update_info() | ||
|
||
return (device 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.""" | ||
# If not initialised and not already scanned and not found. | ||
if device not in self.last_results: | ||
self._update_info() | ||
self.update_info() | ||
|
||
if not self.last_results: | ||
return None | ||
|
||
return self.last_results.get(device) | ||
|
||
def _update_info(self): | ||
"""Ensure the information from the BT Home Hub 5 is up to date. | ||
|
||
Return boolean if scanning successful. | ||
""" | ||
if not self.success_init: | ||
return False | ||
def update_info(self): | ||
"""Ensure the information from the BT Home Hub 5 is up to date.""" | ||
# Return boolean if scanning successful. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't return a boolean anymore. |
||
import bthomehub5_devicelist | ||
|
||
_LOGGER.info("Scanning") | ||
|
||
data = _get_homehub_data(self.url) | ||
data = bthomehub5_devicelist.get_devicelist(self.host) | ||
|
||
if not data: | ||
_LOGGER.warning("Error scanning devices") | ||
return False | ||
return | ||
|
||
self.last_results = data | ||
|
||
return True | ||
|
||
|
||
def _get_homehub_data(url): | ||
"""Retrieve data from BT Home Hub 5 and return parsed result.""" | ||
try: | ||
response = requests.get(url, timeout=5) | ||
except requests.exceptions.Timeout: | ||
_LOGGER.exception("Connection to the router timed out") | ||
return | ||
if response.status_code == 200: | ||
return _parse_homehub_response(response.text) | ||
_LOGGER.error("Invalid response from Home Hub: %s", response) | ||
|
||
|
||
def _parse_homehub_response(data_str): | ||
"""Parse the BT Home Hub 5 data format.""" | ||
root = ET.fromstring(data_str) | ||
|
||
dirty_json = root.find('known_device_list').get('value') | ||
|
||
# Normalise the JavaScript data to JSON. | ||
clean_json = unquote(dirty_json.replace('\'', '\"') | ||
.replace('{', '{\"') | ||
.replace(':\"', '\":\"') | ||
.replace('\",', '\",\"')) | ||
|
||
known_devices = [x for x in json.loads(clean_json) if x] | ||
|
||
devices = {} | ||
|
||
for device in known_devices: | ||
name = device.get('name') | ||
mac = device.get('mac') | ||
|
||
if _MAC_REGEX.match(mac) or ',' in mac: | ||
for mac_addr in mac.split(','): | ||
if _MAC_REGEX.match(mac_addr): | ||
devices[mac_addr] = name | ||
else: | ||
devices[mac] = name | ||
|
||
return devices |
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 was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a blank line between standard library and 3rd party imports.