Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrErohin committed Oct 28, 2024
1 parent 10acc65 commit 7e9065a
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 19 deletions.
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[flake8]

max-line-length = 120
ignore = W605, W503
exclude = .git,__pycache__,build,dist,venv

per-file-ignores =
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tplinkrouterc6u",
version="4.2.2",
version="4.2.3",
author="Alex Erohin",
author_email="[email protected]",
description="TP-Link Router API",
Expand Down
4 changes: 2 additions & 2 deletions test/test_client_deco.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_get_status(self) -> None:
"online": true, "name": "d2lyZWxlc3M0", "enable_priority": false, "remain_time": 0, "owner_id": "",
"client_type": "other", "interface": "guest"},
{"mac": "56:32:c3:de:ce:f0", "up_speed": 3, "down_speed": 1, "wire_type": "wireless", "access_host": "1",
"connection_type": "band2_4", "space_id": "1", "ip": "192.168.68.105", "client_mesh": true,
"connection_type": "band2_4", "space_id": "1", "ip": "UNKNOWN", "client_mesh": true,
"online": true, "name": "d2lyZWxlc3M1", "enable_priority": false, "remain_time": 0, "owner_id": "",
"client_type": "other", "interface": "guest"}
]}, "error_code": 0}
Expand Down Expand Up @@ -150,7 +150,7 @@ def request(self, path: str, data: str,
self.assertIsInstance(status.devices[4], Device)
self.assertEqual(status.devices[4].type, Connection.GUEST_2G)
self.assertEqual(status.devices[4].macaddr, '56-32-C3-DE-CE-F0')
self.assertEqual(status.devices[4].ipaddr, '192.168.68.105')
self.assertEqual(status.devices[4].ipaddr, '0.0.0.0')
self.assertEqual(status.devices[4].hostname, 'wireless5')
self.assertEqual(status.devices[4].packets_sent, None)
self.assertEqual(status.devices[4].packets_received, None)
Expand Down
2 changes: 1 addition & 1 deletion tplinkrouterc6u/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
AbstractRouter,
TPLinkDecoClient,
)
from tplinkrouterc6u.enum import Connection
from tplinkrouterc6u.package_enum import Connection
from tplinkrouterc6u.dataclass import (
Firmware,
Status,
Expand Down
20 changes: 7 additions & 13 deletions tplinkrouterc6u/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from macaddress import EUI48
from ipaddress import IPv4Address
from logging import Logger
from tplinkrouterc6u.helper import get_ip
from tplinkrouterc6u.encryption import EncryptionWrapper, EncryptionWrapperMR
from tplinkrouterc6u.enum import Connection
from tplinkrouterc6u.package_enum import Connection
from tplinkrouterc6u.dataclass import Firmware, Status, Device, IPv4Reservation, IPv4DHCPLease, IPv4Status
from tplinkrouterc6u.exception import ClientException, ClientError
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -353,15 +354,9 @@ def get_status(self) -> Status:

devices = {}

def _getIP(ip: str) -> IPv4Address:
try:
return IPv4Address(ip)
except Exception:
return IPv4Address('0.0.0.0')

def _add_device(conn: Connection, item: dict) -> None:
devices[item['macaddr']] = Device(conn, EUI48(item['macaddr']),
_getIP(item['ipaddr']),
get_ip(item['ipaddr']),
item['hostname'])

for item in data.get('access_devices_wired', []):
Expand All @@ -387,7 +382,7 @@ def _add_device(conn: Connection, item: dict) -> None:
for item in smart_network:
if item['mac'] not in devices:
conn = self._map_wire_type(item.get('deviceTag'), not item.get('isGuest'))
devices[item['mac']] = Device(conn, EUI48(item['mac']), _getIP(item['ip']),
devices[item['mac']] = Device(conn, EUI48(item['mac']), get_ip(item['ip']),
item['deviceName'])
if conn.is_iot():
if status.iot_clients_total is None:
Expand Down Expand Up @@ -585,10 +580,9 @@ def get_status(self) -> Status:
status.iot_clients_total = 0
status.iot_clients_total += 1

ip = item['ip'] if item.get('ip') else '0.0.0.0'
device = Device(conn,
EUI48(item['mac']),
IPv4Address(ip),
get_ip(item.get('ip', '0.0.0.0')),
b64decode(item['name']).decode())
device.down_speed = item.get('down_speed')
device.up_speed = item.get('up_speed')
Expand Down Expand Up @@ -1131,7 +1125,7 @@ def _merge_response(response: str) -> dict:
lines = response.split('\n')
for line in lines:
if line.startswith('['):
regexp = search('\[\d,\d,\d,\d,\d,\d\](\d)', line)
regexp = search(r'\[\d,\d,\d,\d,\d,\d\](\d)', line)
if regexp is not None:
obj = {}
index = regexp.group(1)
Expand Down Expand Up @@ -1321,7 +1315,7 @@ def _parse_ret_val(self, response_text):
Return value:
return code (int)
'''
result = search('\$\.ret=(.*);', response_text)
result = search(r'\$\.ret=(.*);', response_text)
assert result is not None
assert result.group(1).isnumeric()

Expand Down
2 changes: 1 addition & 1 deletion tplinkrouterc6u/dataclass.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from macaddress import EUI48
from ipaddress import IPv4Address
from dataclasses import dataclass
from tplinkrouterc6u.enum import Connection
from tplinkrouterc6u.package_enum import Connection


@dataclass
Expand Down
8 changes: 8 additions & 0 deletions tplinkrouterc6u/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ipaddress import IPv4Address


def get_ip(ip: str) -> IPv4Address:
try:
return IPv4Address(ip)
except Exception:
return IPv4Address('0.0.0.0')
File renamed without changes.

0 comments on commit 7e9065a

Please sign in to comment.