-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add Salt Fiber Box device tracker #30986
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
MartinHjelmare
merged 15 commits into
home-assistant:dev
from
bjornorri:salt_device_tracker
Feb 2, 2020
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
390bcb1
Add salt component
bjornorri c9e86d5
Update files from development checklist
bjornorri 05563bc
Use warning log level when data cannot be retrieved
bjornorri 288a953
Remove empty fields from manifest.json
bjornorri dca9fc8
Remove default arguments for host and username
bjornorri b952641
Bump saltbox library version
bjornorri fc3ada9
Refactor and improve error handling
bjornorri 08f839f
Dev checklist
bjornorri 3f9c1bd
Fix linting errors
bjornorri 5a69b16
Check for None and return empty list
bjornorri 4627b96
Log on debug level instead of info
bjornorri f7825f5
More compact None checks
bjornorri c4f43fe
Remove redundant None check
bjornorri e3dda84
Return None on exception but store as empty list
bjornorri 3017fa4
More compact syntax
bjornorri 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
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 @@ | ||
| """The salt component.""" |
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,71 @@ | ||
| """Support for Salt Fiber Box routers.""" | ||
| import logging | ||
|
|
||
| from saltbox import RouterLoginException, RouterNotReachableException, SaltBox | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.device_tracker import ( | ||
| DOMAIN, | ||
| PLATFORM_SCHEMA, | ||
| DeviceScanner, | ||
| ) | ||
| from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( | ||
| { | ||
| vol.Required(CONF_HOST): cv.string, | ||
| vol.Required(CONF_USERNAME): cv.string, | ||
| vol.Required(CONF_PASSWORD): cv.string, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def get_scanner(hass, config): | ||
| """Return the Salt device scanner.""" | ||
| scanner = SaltDeviceScanner(config[DOMAIN]) | ||
|
|
||
| # Test whether the router is accessible. | ||
| data = scanner.get_salt_data() | ||
| return scanner if data is not None else None | ||
|
|
||
|
|
||
| class SaltDeviceScanner(DeviceScanner): | ||
| """This class queries a Salt Fiber Box router.""" | ||
|
|
||
| def __init__(self, config): | ||
| """Initialize the scanner.""" | ||
| host = config[CONF_HOST] | ||
| username = config[CONF_USERNAME] | ||
| password = config[CONF_PASSWORD] | ||
| self.saltbox = SaltBox(f"http://{host}", username, password) | ||
| self.online_clients = [] | ||
|
|
||
| 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.online_clients] | ||
|
|
||
| def get_device_name(self, device): | ||
| """Return the name of the given device or None if we don't know.""" | ||
| for client in self.online_clients: | ||
| if client["mac"] == device: | ||
| return client["name"] | ||
| return None | ||
|
|
||
| def get_salt_data(self): | ||
| """Retrieve data from Salt router and return parsed result.""" | ||
| try: | ||
| clients = self.saltbox.get_online_clients() | ||
| return clients | ||
| except (RouterLoginException, RouterNotReachableException) as error: | ||
| _LOGGER.warning(error) | ||
| return None | ||
|
|
||
| def _update_info(self): | ||
| """Pull the current information from the Salt router.""" | ||
| _LOGGER.debug("Loading data from Salt Fiber Box") | ||
| data = self.get_salt_data() | ||
| self.online_clients = data or [] | ||
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,8 @@ | ||
| { | ||
| "domain": "salt", | ||
| "name": "Salt Fiber Box", | ||
| "documentation": "https://www.home-assistant.io/integrations/salt", | ||
| "requirements": ["saltbox==0.1.3"], | ||
| "dependencies": [], | ||
| "codeowners": ["@bjornorri"] | ||
| } |
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.