-
-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Add device tracking for the BT Smart Hub router #17158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2aa76e5
Support for BT smarthub router device tracking
jxwolstenholme c4fa797
Update requirements_all.txt for bt_smarthub device tracker
jxwolstenholme e8b7ece
Added bt_smarthub.py exclusion
jxwolstenholme 0e276ab
Update .coveragerc
jxwolstenholme d8e55d3
Update bt_smarthub.py
jxwolstenholme b1ac1dd
Update bt_smarthub.py
jxwolstenholme 4672482
Update bt_smarthub.py
jxwolstenholme 7333abc
Update bt_smarthub.py
jxwolstenholme 773b83f
Fix linting issues
jxwolstenholme 0d245e1
Update bt_smarthub.py
jxwolstenholme 8d96008
Update bt_smarthub.py
jxwolstenholme a61bf8c
Update bt_smarthub.py
jxwolstenholme e747cad
Update bt_smarthub.py
jxwolstenholme a29b46f
Update bt_smarthub.py
jxwolstenholme 585d25a
Update bt_smarthub.py
jxwolstenholme 40b9c19
Update bt_smarthub.py
jxwolstenholme 07e1245
Update bt_smarthub.py
jxwolstenholme c91ec99
Update .coveragerc
jxwolstenholme 644a5cb
Update bt_smarthub.py
jxwolstenholme 2d37649
Minor changes
fabaff e1d92df
Update bt_smarthub.py
jxwolstenholme ee2ca3e
Update bt_smarthub.py
jxwolstenholme 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 hidden or 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 hidden or 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,97 @@ | ||
| """ | ||
| Support for BT Smart Hub (Sometimes referred to as BT Home Hub 6). | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/device_tracker.bt_smarthub/ | ||
| """ | ||
| 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 | ||
|
|
||
| REQUIREMENTS = ['btsmarthub_devicelist==0.1.1'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_DEFAULT_IP = '192.168.1.254' | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Optional(CONF_HOST, default=CONF_DEFAULT_IP): cv.string, | ||
| }) | ||
|
|
||
|
|
||
| def get_scanner(hass, config): | ||
| """Return a BT Smart Hub scanner if successful.""" | ||
| scanner = BTSmartHubScanner(config[DOMAIN]) | ||
|
|
||
| return scanner if scanner.success_init else None | ||
|
|
||
|
|
||
| class BTSmartHubScanner(DeviceScanner): | ||
| """This class queries a BT Smart Hub.""" | ||
|
|
||
| def __init__(self, config): | ||
| """Initialise the scanner.""" | ||
| _LOGGER.debug("Initialising BT Smart Hub") | ||
| self.host = config[CONF_HOST] | ||
| self.last_results = {} | ||
| self.success_init = False | ||
|
|
||
| # Test the router is accessible | ||
| data = self.get_bt_smarthub_data() | ||
| if data: | ||
| self.success_init = True | ||
| else: | ||
| _LOGGER.info("Failed to connect to %s", self.host) | ||
|
|
||
| def scan_devices(self): | ||
| """Scan for new devices and return a list with found device IDs.""" | ||
| self._update_info() | ||
| return [client['mac'] for client 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 self.last_results: | ||
| return None | ||
| for client in self.last_results: | ||
| if client['mac'] == device: | ||
| return client['host'] | ||
| return None | ||
|
|
||
| def _update_info(self): | ||
| """Ensure the information from the BT Smart Hub is up to date.""" | ||
| if not self.success_init: | ||
| return | ||
|
|
||
| _LOGGER.info("Scanning") | ||
| data = self.get_bt_smarthub_data() | ||
| if not data: | ||
| _LOGGER.warning("Error scanning devices") | ||
| return | ||
|
|
||
| clients = [client for client in data.values()] | ||
| self.last_results = clients | ||
|
|
||
| def get_bt_smarthub_data(self): | ||
| """Retrieve data from BT Smart Hub and return parsed result.""" | ||
| import btsmarthub_devicelist | ||
| # Request data from bt smarthub into a list of dicts. | ||
| data = btsmarthub_devicelist.get_devicelist( | ||
| router_ip=self.host, only_active_devices=True) | ||
| # Renaming keys from parsed result. | ||
| devices = {} | ||
| for device in data: | ||
| try: | ||
| devices[device['UserHostName']] = { | ||
| 'ip': device['IPAddress'], | ||
| 'mac': device['PhysAddress'], | ||
| 'host': device['UserHostName'], | ||
| 'status': device['Active'] | ||
| } | ||
| except KeyError: | ||
| pass | ||
| return devices | ||
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.