Skip to content

Commit

Permalink
Fix bugs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Apr 23, 2022
1 parent 7f9e54b commit 660453b
Show file tree
Hide file tree
Showing 44 changed files with 2,280 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
source = custom_components/miwifi
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ dmypy.json
.pyre/

.DS_Store
.idea
1 change: 1 addition & 0 deletions custom_components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""MiWifi custom integration."""
2 changes: 1 addition & 1 deletion custom_components/miwifi/luci.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async def get(
self._debug("Successful request", _url, response.content, path)

_data: dict = json.loads(response.content)
except (HTTPError, ValueError, TypeError) as _e:
except (HTTPError, ValueError, TypeError, json.JSONDecodeError) as _e:
self._debug("Connection error", _url, _e, path)

raise LuciConnectionException("Connection error") from _e
Expand Down
31 changes: 22 additions & 9 deletions custom_components/miwifi/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,14 @@ async def update(self, retry: int = 1) -> dict:

await self._async_load_manufacturers()

self.code = codes.OK

_is_before_reauthorization: bool = self._is_reauthorization
_err: LuciException | None = None

try:
if self._is_reauthorization or self._is_only_login or self._is_first_update:
if self._is_first_update:
if self._is_first_update and retry == 1:
await self.luci.logout()
await asyncio.sleep(DEFAULT_CALL_DELAY)

Expand All @@ -263,8 +265,6 @@ async def update(self, retry: int = 1) -> dict:
self._is_reauthorization = True
self.code = codes.FORBIDDEN
else:
self.code = codes.OK

self._is_reauthorization = False

if self._is_first_update:
Expand All @@ -279,7 +279,11 @@ async def update(self, retry: int = 1) -> dict:
):
self.data[ATTR_STATE] = True

if self._is_first_update and not self.data[ATTR_STATE]:
if (
not self._is_only_login
and self._is_first_update
and not self.data[ATTR_STATE]
):
if retry > DEFAULT_RETRY and _err is not None:
raise _err

Expand Down Expand Up @@ -403,6 +407,8 @@ async def _async_prepare_init(self, data: dict) -> None:

self.code = codes.CONFLICT

return

data[ATTR_CAMERA_IMAGE] = await self.luci.image(response["hardware"])

return
Expand Down Expand Up @@ -467,8 +473,6 @@ async def _async_prepare_rom_update(self, data: dict) -> None:
if ATTR_UPDATE_CURRENT_VERSION not in data:
return

response: dict = await self.luci.rom_update()

_rom_info: dict = {
ATTR_UPDATE_CURRENT_VERSION: data[ATTR_UPDATE_CURRENT_VERSION],
ATTR_UPDATE_LATEST_VERSION: data[ATTR_UPDATE_CURRENT_VERSION],
Expand All @@ -477,7 +481,16 @@ async def _async_prepare_rom_update(self, data: dict) -> None:
+ f" ({data.get(ATTR_DEVICE_NAME, DEFAULT_NAME)})",
}

if "needUpdate" not in response or response["needUpdate"] != 1:
try:
response: dict = await self.luci.rom_update()
except LuciException:
response = {}

if (
not isinstance(response, dict)
or "needUpdate" not in response
or response["needUpdate"] != 1
):
data[ATTR_UPDATE_FIRMWARE] = _rom_info

return
Expand Down Expand Up @@ -549,7 +562,7 @@ async def _async_prepare_wifi(self, data: dict) -> None:
response: dict = await self.luci.wifi_detail_all()

if "bsd" in response:
data[ATTR_BINARY_SENSOR_DUAL_BAND] = response["bsd"] == 1
data[ATTR_BINARY_SENSOR_DUAL_BAND] = int(response["bsd"]) == 1
else:
data[ATTR_BINARY_SENSOR_DUAL_BAND] = False

Expand Down Expand Up @@ -587,7 +600,7 @@ async def _async_prepare_wifi(self, data: dict) -> None:

for data_field, field in ATTR_WIFI_DATA_FIELDS.items():
if "channelInfo" in data_field and "channelInfo" in wifi:
data_field = data_field.replace("channelInfo", "")
data_field = data_field.replace("channelInfo.", "")

if data_field in wifi["channelInfo"]:
wifi_data[field] = wifi["channelInfo"][data_field]
Expand Down
10 changes: 10 additions & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
aiohttp_cors==0.7.0
aiodiscover==1.4.8
scapy==2.4.5
async_upnp_client==0.27.0

codecov==2.1.12
coverage==6.3.2
pytest==7.1.1
pytest-cov==2.12.1
pytest-homeassistant-custom-component==0.8.11
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for the miwifi component."""
103 changes: 103 additions & 0 deletions tests/fixtures/avaliable_channels_2g_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"list": [
{
"c": "0",
"b": [
"20",
"40"
]
},
{
"c": "1",
"b": [
"20",
"40"
]
},
{
"c": "2",
"b": [
"20",
"40"
]
},
{
"c": "3",
"b": [
"20",
"40"
]
},
{
"c": "4",
"b": [
"20",
"40"
]
},
{
"c": "5",
"b": [
"20",
"40"
]
},
{
"c": "6",
"b": [
"20",
"40"
]
},
{
"c": "7",
"b": [
"20",
"40"
]
},
{
"c": "8",
"b": [
"20",
"40"
]
},
{
"c": "9",
"b": [
"20",
"40"
]
},
{
"c": "10",
"b": [
"20",
"40"
]
},
{
"c": "11",
"b": [
"20",
"40"
]
},
{
"c": "12",
"b": [
"20",
"40"
]
},
{
"c": "13",
"b": [
"20",
"40"
]
}
],
"code": 0
}
76 changes: 76 additions & 0 deletions tests/fixtures/avaliable_channels_5g_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"list": [
{
"c": "0",
"b": [
"20",
"40"
]
},
{
"c": "36",
"b": [
"20",
"40",
"80"
]
},
{
"c": "40",
"b": [
"20",
"40",
"80"
]
},
{
"c": "44",
"b": [
"20",
"40",
"80"
]
},
{
"c": "48",
"b": [
"20",
"40",
"80"
]
},
{
"c": "149",
"b": [
"20",
"40",
"80"
]
},
{
"c": "153",
"b": [
"20",
"40",
"80"
]
},
{
"c": "157",
"b": [
"20",
"40",
"80"
]
},
{
"c": "161",
"b": [
"20",
"40",
"80"
]
}
],
"code": 0
}
Loading

0 comments on commit 660453b

Please sign in to comment.