2018-03-01 00:21:10 +00:00
|
|
|
"""Tests for the TekSavvy sensor platform."""
|
|
|
|
from homeassistant.bootstrap import async_setup_component
|
2019-03-19 06:07:39 +00:00
|
|
|
from homeassistant.components.teksavvy.sensor import TekSavvyData
|
2020-04-08 22:57:47 +00:00
|
|
|
from homeassistant.const import DATA_GIGABYTES, HTTP_NOT_FOUND, UNIT_PERCENTAGE
|
2018-03-01 00:21:10 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
|
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
async def test_capped_setup(hass, aioclient_mock):
|
2018-03-01 00:21:10 +00:00
|
|
|
"""Test the default setup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
await async_setup_component(hass, "sensor", {"sensor": config})
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_data_limit")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "400"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_off_peak_download")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "36.24"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_off_peak_upload")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "1.58"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_off_peak_total")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "37.82"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_on_peak_download")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "226.75"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_on_peak_upload")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "8.82"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_on_peak_total")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "235.57"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_usage_ratio")
|
2020-02-28 19:46:48 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "56.69"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_usage")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "226.75"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_remaining")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "173.25"
|
2018-03-01 00:21:10 +00:00
|
|
|
|
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
async def test_unlimited_setup(hass, aioclient_mock):
|
2018-03-01 00:21:10 +00:00
|
|
|
"""Test the default setup."""
|
2019-07-31 19:25:30 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
await async_setup_component(hass, "sensor", {"sensor": config})
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_data_limit")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "inf"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_off_peak_download")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "36.24"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_off_peak_upload")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "1.58"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_off_peak_total")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "37.82"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_on_peak_download")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "226.75"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_on_peak_upload")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "8.82"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_on_peak_total")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "235.57"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_usage")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "226.75"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_usage_ratio")
|
2020-02-28 19:46:48 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == UNIT_PERCENTAGE
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "0"
|
|
|
|
|
|
|
|
state = hass.states.get("sensor.teksavvy_remaining")
|
2020-02-13 16:52:58 +00:00
|
|
|
assert state.attributes.get("unit_of_measurement") == DATA_GIGABYTES
|
2019-07-31 19:25:30 +00:00
|
|
|
assert state.state == "inf"
|
2018-03-01 00:21:10 +00:00
|
|
|
|
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
async def test_bad_return_code(hass, aioclient_mock):
|
2018-03-01 00:21:10 +00:00
|
|
|
"""Test handling a return code that isn't HTTP OK."""
|
2019-07-31 19:25:30 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
"https://api.teksavvy.com/"
|
|
|
|
"web/Usage/UsageSummaryRecords?"
|
|
|
|
"$filter=IsCurrent%20eq%20true",
|
2020-04-08 22:57:47 +00:00
|
|
|
status=HTTP_NOT_FOUND,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-03-01 00:21:10 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
tsd = TekSavvyData(hass.loop, async_get_clientsession(hass), "notakey", 400)
|
2018-03-01 00:21:10 +00:00
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
result = await tsd.async_update()
|
2018-03-01 00:21:10 +00:00
|
|
|
assert result is False
|
|
|
|
|
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
async def test_bad_json_decode(hass, aioclient_mock):
|
2018-03-01 00:21:10 +00:00
|
|
|
"""Test decoding invalid json result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
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)
|
2018-03-01 00:21:10 +00:00
|
|
|
|
2020-01-01 15:18:17 +00:00
|
|
|
result = await tsd.async_update()
|
2018-03-01 00:21:10 +00:00
|
|
|
assert result is False
|