core/tests/components/teksavvy/test_sensor.py

186 lines
6.9 KiB
Python
Raw Normal View History

"""Tests for the TekSavvy sensor platform."""
import asyncio
from homeassistant.bootstrap import async_setup_component
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.teksavvy.sensor 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