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
TekSavvy Sensor unlimited bandwidth support #12325
Merged
balloob
merged 2 commits into
home-assistant:dev
from
mikeodr:teksavvy-unlimited-bw-support
Mar 1, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,185 @@ | ||
"""Tests for the TekSavvy sensor platform.""" | ||
import asyncio | ||
from homeassistant.bootstrap import async_setup_component | ||
from homeassistant.components.sensor.teksavvy import TekSavvyData | ||
from homeassistant.helpers.aiohttp_client import async_get_clientsession | ||
|
||
|
||
@asyncio.coroutine | ||
def test_capped_setup(hass, aioclient_mock): | ||
"""Test the default setup.""" | ||
config = {'platform': 'teksavvy', | ||
'api_key': 'NOTAKEY', | ||
'total_bandwidth': 400, | ||
'monitored_variables': [ | ||
'usage', | ||
'usage_gb', | ||
'limit', | ||
'onpeak_download', | ||
'onpeak_upload', | ||
'onpeak_total', | ||
'offpeak_download', | ||
'offpeak_upload', | ||
'offpeak_total', | ||
'onpeak_remaining']} | ||
|
||
result = '{"odata.metadata":"http://api.teksavvy.com/web/Usage/$metadata'\ | ||
'#UsageSummaryRecords","value":[{'\ | ||
'"StartDate":"2018-01-01T00:00:00",'\ | ||
'"EndDate":"2018-01-31T00:00:00",'\ | ||
'"OID":"999999","IsCurrent":true,'\ | ||
'"OnPeakDownload":226.75,'\ | ||
'"OnPeakUpload":8.82,'\ | ||
'"OffPeakDownload":36.24,"OffPeakUpload":1.58'\ | ||
'}]}' | ||
aioclient_mock.get("https://api.teksavvy.com/" | ||
"web/Usage/UsageSummaryRecords?" | ||
"$filter=IsCurrent%20eq%20true", | ||
text=result) | ||
|
||
yield from async_setup_component(hass, 'sensor', {'sensor': config}) | ||
|
||
state = hass.states.get('sensor.teksavvy_data_limit') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '400' | ||
|
||
state = hass.states.get('sensor.teksavvy_off_peak_download') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '36.24' | ||
|
||
state = hass.states.get('sensor.teksavvy_off_peak_upload') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '1.58' | ||
|
||
state = hass.states.get('sensor.teksavvy_off_peak_total') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '37.82' | ||
|
||
state = hass.states.get('sensor.teksavvy_on_peak_download') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '226.75' | ||
|
||
state = hass.states.get('sensor.teksavvy_on_peak_upload') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '8.82' | ||
|
||
state = hass.states.get('sensor.teksavvy_on_peak_total') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '235.57' | ||
|
||
state = hass.states.get('sensor.teksavvy_usage_ratio') | ||
assert state.attributes.get('unit_of_measurement') == '%' | ||
assert state.state == '56.69' | ||
|
||
state = hass.states.get('sensor.teksavvy_usage') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '226.75' | ||
|
||
state = hass.states.get('sensor.teksavvy_remaining') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '173.25' | ||
|
||
|
||
@asyncio.coroutine | ||
def test_unlimited_setup(hass, aioclient_mock): | ||
"""Test the default setup.""" | ||
config = {'platform': 'teksavvy', | ||
'api_key': 'NOTAKEY', | ||
'total_bandwidth': 0, | ||
'monitored_variables': [ | ||
'usage', | ||
'usage_gb', | ||
'limit', | ||
'onpeak_download', | ||
'onpeak_upload', | ||
'onpeak_total', | ||
'offpeak_download', | ||
'offpeak_upload', | ||
'offpeak_total', | ||
'onpeak_remaining']} | ||
|
||
result = '{"odata.metadata":"http://api.teksavvy.com/web/Usage/$metadata'\ | ||
'#UsageSummaryRecords","value":[{'\ | ||
'"StartDate":"2018-01-01T00:00:00",'\ | ||
'"EndDate":"2018-01-31T00:00:00",'\ | ||
'"OID":"999999","IsCurrent":true,'\ | ||
'"OnPeakDownload":226.75,'\ | ||
'"OnPeakUpload":8.82,'\ | ||
'"OffPeakDownload":36.24,"OffPeakUpload":1.58'\ | ||
'}]}' | ||
aioclient_mock.get("https://api.teksavvy.com/" | ||
"web/Usage/UsageSummaryRecords?" | ||
"$filter=IsCurrent%20eq%20true", | ||
text=result) | ||
|
||
yield from async_setup_component(hass, 'sensor', {'sensor': config}) | ||
|
||
state = hass.states.get('sensor.teksavvy_data_limit') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == 'inf' | ||
|
||
state = hass.states.get('sensor.teksavvy_off_peak_download') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '36.24' | ||
|
||
state = hass.states.get('sensor.teksavvy_off_peak_upload') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '1.58' | ||
|
||
state = hass.states.get('sensor.teksavvy_off_peak_total') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '37.82' | ||
|
||
state = hass.states.get('sensor.teksavvy_on_peak_download') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '226.75' | ||
|
||
state = hass.states.get('sensor.teksavvy_on_peak_upload') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '8.82' | ||
|
||
state = hass.states.get('sensor.teksavvy_on_peak_total') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '235.57' | ||
|
||
state = hass.states.get('sensor.teksavvy_usage') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == '226.75' | ||
|
||
state = hass.states.get('sensor.teksavvy_usage_ratio') | ||
assert state.attributes.get('unit_of_measurement') == '%' | ||
assert state.state == '0' | ||
|
||
state = hass.states.get('sensor.teksavvy_remaining') | ||
assert state.attributes.get('unit_of_measurement') == 'GB' | ||
assert state.state == 'inf' | ||
|
||
|
||
@asyncio.coroutine | ||
def test_bad_return_code(hass, aioclient_mock): | ||
"""Test handling a return code that isn't HTTP OK.""" | ||
aioclient_mock.get("https://api.teksavvy.com/" | ||
"web/Usage/UsageSummaryRecords?" | ||
"$filter=IsCurrent%20eq%20true", | ||
status=404) | ||
|
||
tsd = TekSavvyData(hass.loop, async_get_clientsession(hass), | ||
'notakey', 400) | ||
|
||
result = yield from tsd.async_update() | ||
assert result is False | ||
|
||
|
||
@asyncio.coroutine | ||
def test_bad_json_decode(hass, aioclient_mock): | ||
"""Test decoding invalid json result.""" | ||
aioclient_mock.get("https://api.teksavvy.com/" | ||
"web/Usage/UsageSummaryRecords?" | ||
"$filter=IsCurrent%20eq%20true", | ||
text='this is not json') | ||
|
||
tsd = TekSavvyData(hass.loop, async_get_clientsession(hass), | ||
'notakey', 400) | ||
|
||
result = yield from tsd.async_update() | ||
assert result is False |
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.
This and line 35 would be breaking changes?
How do I ensure that makes it into the release notes?
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.
Notably
sensor.teksavvy_on_peak_upload_
(which is incorrect with the trailing underscore) becomessensor.teksavvy_on_peak_upload
Additionally with both the percentage and GB values both being named
usage
you would get a non-deterministic result in tests forsensor.teksavvy_usage
andsensor.teksavvy_usage_2
.Renaming to
Usage Ratio
makes it very deterministic.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 section to your PR description and I'll add the label "Breaking Change"
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.
Added a description, thanks!