Skip to content
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
merged 9 commits into from
Aug 6, 2018
45 changes: 16 additions & 29 deletions homeassistant/components/device_tracker/bt_home_hub_5.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
"""
Support for BT Home Hub 5.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/device_tracker.bt_home_hub_5/
"""

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this blank line.

import logging
import re
import xml.etree.ElementTree as ET
import json
from urllib.parse import unquote

import requests
import voluptuous as vol
from html_table_extractor import HTMLParser

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'html_table_extractor.HTMLParser' imported but unused


import homeassistant.helpers.config_validation as cv
from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
import voluptuous as vol
from homeassistant.components.device_tracker import (DOMAIN, PLATFORM_SCHEMA,
DeviceScanner)
from homeassistant.const import CONF_HOST

REQUIREMENTS = ['html-table-parser-python3==0.1.2']

_LOGGER = logging.getLogger(__name__)
_MAC_REGEX = re.compile(r'(([0-9A-Fa-f]{1,2}\:){5}[0-9A-Fa-f]{1,2})')
_MAC_REGEX = re.compile(r'(([0-9A-Fa-f]{1,2}:){5}[0-9A-Fa-f]{1,2})')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't used anymore.


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string
})


def get_scanner(hass, config):
# pylint: disable=unused-argument
def get_scanner(config):
"""Return a BT Home Hub 5 scanner if successful."""
scanner = BTHomeHub5DeviceScanner(config[DOMAIN])

Expand All @@ -41,7 +42,7 @@ def __init__(self, config):
_LOGGER.info("Initialising BT Home Hub 5")
self.host = config.get(CONF_HOST, '192.168.1.254')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move the default value to the config schema.

self.last_results = {}
self.url = 'http://{}/nonAuth/home_status.xml'.format(self.host)
self.url = 'http://{}/'.format(self.host)

# Test the router is accessible
data = _get_homehub_data(self.url)
Expand All @@ -66,7 +67,6 @@ def get_device_name(self, 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it can be removed.

Expand Down Expand Up @@ -100,29 +100,16 @@ def _get_homehub_data(url):

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('\",', '\",\"'))
p = HTMLTableParser()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HTMLTableParser'

p.feed(data_str)

known_devices = [x for x in json.loads(clean_json) if x]
known_devices = p.tables[9]

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
if len(device) == 5 and device[2] != '':
devices[device[2]] = device[1]

return devices