1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
from unittest import TestCase
import pytest
from miio import WifiRepeater
from miio.tests.dummies import DummyDevice
from miio.wifirepeater import WifiRepeaterConfiguration, WifiRepeaterStatus
class DummyWifiRepeater(DummyDevice, WifiRepeater):
def __init__(self, *args, **kwargs):
self.state = {
"sta": {"count": 2, "access_policy": 0},
"mat": [
{
"mac": "aa:aa:aa:aa:aa:aa",
"ip": "192.168.1.133",
"last_time": 54371873,
},
{
"mac": "bb:bb:bb:bb:bb:bb",
"ip": "192.168.1.156",
"last_time": 54371496,
},
],
"access_list": {"mac": ""},
}
self.config = {"ssid": "SSID", "pwd": "PWD", "hidden": 0}
self.device_info = {
"life": 543452,
"cfg_time": 543452,
"token": "ffffffffffffffffffffffffffffffff",
"fw_ver": "2.2.14",
"hw_ver": "R02",
"uid": 1583412143,
"api_level": 2,
"mcu_fw_ver": "1000",
"wifi_fw_ver": "1.0.0",
"mac": "FF:FF:FF:FF:FF:FF",
"model": "xiaomi.repeater.v2",
"ap": {
"rssi": -63,
"ssid": "SSID",
"bssid": "EE:EE:EE:EE:EE:EE",
"rx": 136695922,
"tx": 1779521233,
},
"sta": {
"count": 2,
"ssid": "REPEATER-SSID",
"hidden": 0,
"assoclist": "cc:cc:cc:cc:cc:cc;bb:bb:bb:bb:bb:bb;",
},
"netif": {
"localIp": "192.168.1.170",
"mask": "255.255.255.0",
"gw": "192.168.1.1",
},
"desc": {
"wifi_explorer": 1,
"sn": "14923 / 20191356",
"color": 101,
"channel": "release",
},
}
self.return_values = {
"miIO.get_repeater_sta_info": self._get_state,
"miIO.get_repeater_ap_info": self._get_configuration,
"miIO.switch_wifi_explorer": self._set_wifi_explorer,
"miIO.switch_wifi_ssid": self._set_configuration,
"miIO.info": self._get_info,
}
self.start_state = self.state.copy()
self.start_config = self.config.copy()
self.start_device_info = self.device_info.copy()
super().__init__(args, kwargs)
def _reset_state(self):
"""Revert back to the original state."""
self.state = self.start_state.copy()
self.config = self.start_config.copy()
self.device_info = self.start_device_info.copy()
def _get_state(self, param):
return self.state
def _get_configuration(self, param):
return self.config
def _get_info(self, param):
return self.device_info
def _set_wifi_explorer(self, data):
self.device_info["desc"]["wifi_explorer"] = data[0]["wifi_explorer"]
def _set_configuration(self, data):
self.config = {
"ssid": data[0]["ssid"],
"pwd": data[0]["pwd"],
"hidden": data[0]["hidden"],
}
self.device_info["desc"]["wifi_explorer"] = data[0]["wifi_explorer"]
return True
@pytest.fixture(scope="class")
def wifirepeater(request):
request.cls.device = DummyWifiRepeater()
# TODO add ability to test on a real device
@pytest.mark.usefixtures("wifirepeater")
class TestWifiRepeater(TestCase):
def state(self):
return self.device.status()
def configuration(self):
return self.device.configuration()
def info(self):
return self.device.info()
def wifi_roaming(self):
return self.device.wifi_roaming()
def rssi_accesspoint(self):
return self.device.rssi_accesspoint()
def test_status(self):
self.device._reset_state()
assert repr(self.state()) == repr(WifiRepeaterStatus(self.device.start_state))
assert (
self.state().access_policy
== self.device.start_state["sta"]["access_policy"]
)
assert self.state().associated_stations == self.device.start_state["mat"]
def test_set_wifi_roaming(self):
self.device.set_wifi_roaming(True)
assert self.wifi_roaming() is True
self.device.set_wifi_roaming(False)
assert self.wifi_roaming() is False
def test_configuration(self):
self.device._reset_state()
assert repr(self.configuration()) == repr(
WifiRepeaterConfiguration(self.device.start_config)
)
assert self.configuration().ssid == self.device.start_config["ssid"]
assert self.configuration().password == self.device.start_config["pwd"]
assert self.configuration().ssid_hidden is (
self.device.start_config["hidden"] == 1
)
def test_set_configuration(self):
def configuration():
return self.device.configuration()
dummy_configuration = {"ssid": "SSID2", "password": "PASSWORD2", "hidden": True}
self.device.set_configuration(
dummy_configuration["ssid"],
dummy_configuration["password"],
dummy_configuration["hidden"],
)
assert configuration().ssid == dummy_configuration["ssid"]
assert configuration().password == dummy_configuration["password"]
assert configuration().ssid_hidden is dummy_configuration["hidden"]
def test_rssi_accesspoint(self):
self.device._reset_state()
assert self.rssi_accesspoint() is self.device.start_device_info["ap"]["rssi"]
|