core/tests/components/tplink/test_device_tracker.py

60 lines
2.1 KiB
Python
Raw Normal View History

"""The tests for the tplink device tracker platform."""
import os
import pytest
from homeassistant.components.device_tracker.legacy import YAML_DEVICES
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.tplink.device_tracker import Tplink4DeviceScanner
from homeassistant.const import (CONF_PLATFORM, CONF_PASSWORD, CONF_USERNAME,
CONF_HOST)
import requests_mock
@pytest.fixture(autouse=True)
def setup_comp(hass):
"""Initialize components."""
yaml_devices = hass.config.path(YAML_DEVICES)
yield
if os.path.isfile(yaml_devices):
os.remove(yaml_devices)
async def test_get_mac_addresses_from_both_bands(hass):
"""Test grabbing the mac addresses from 2.4 and 5 GHz clients pages."""
with requests_mock.Mocker() as m:
conf_dict = {
CONF_PLATFORM: 'tplink',
2016-11-16 05:25:54 +00:00
CONF_HOST: 'fake-host',
CONF_USERNAME: 'fake_user',
CONF_PASSWORD: 'fake_pass'
}
# Mock the token retrieval process
FAKE_TOKEN = 'fake_token'
fake_auth_token_response = 'window.parent.location.href = ' \
'"https://a/{}/userRpm/Index.htm";'.format(FAKE_TOKEN)
m.get('http://{}/userRpm/LoginRpm.htm?Save=Save'.format(
conf_dict[CONF_HOST]), text=fake_auth_token_response)
FAKE_MAC_1 = 'CA-FC-8A-C8-BB-53'
FAKE_MAC_2 = '6C-48-83-21-46-8D'
FAKE_MAC_3 = '77-98-75-65-B1-2B'
mac_response_2_4 = '{} {}'.format(FAKE_MAC_1, FAKE_MAC_2)
mac_response_5 = '{}'.format(FAKE_MAC_3)
# Mock the 2.4 GHz clients page
m.get('http://{}/{}/userRpm/WlanStationRpm.htm'.format(
conf_dict[CONF_HOST], FAKE_TOKEN), text=mac_response_2_4)
# Mock the 5 GHz clients page
m.get('http://{}/{}/userRpm/WlanStationRpm_5g.htm'.format(
conf_dict[CONF_HOST], FAKE_TOKEN), text=mac_response_5)
tplink = Tplink4DeviceScanner(conf_dict)
expected_mac_results = [mac.replace('-', ':') for mac in
[FAKE_MAC_1, FAKE_MAC_2, FAKE_MAC_3]]
assert tplink.last_results == expected_mac_results