Skip to content

Commit

Permalink
Defensively code to handle missing properties and add more debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
amosyuen committed Mar 8, 2022
1 parent 057cc4d commit 6d26ca6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
20 changes: 11 additions & 9 deletions custom_components/tplink_deco/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ async def async_list_devices(self) -> dict:
raise Exception(f"List devices error {error_code}")

device_list = data["result"]["device_list"]
_LOGGER.debug("len(device_list)=%d", len(device_list))
_LOGGER.debug("List devices device_count=%d", len(device_list))
_LOGGER.debug("List devices device_list=%s", device_list)

for device in device_list:
custom_nickname = device.get("custom_nickname")
Expand All @@ -163,11 +164,12 @@ async def async_list_clients(self, deco_mac="default") -> dict:
error_code = data.get("error_code")
if error_code != 0:
_LOGGER.debug("%s error: data=%s", context, data)
raise Exception(f"List clients error {error_code}")
raise Exception(f"{context} error {error_code}")

client_list = data["result"]["client_list"]
# client_list is only the connected clients
_LOGGER.debug("%s client_count=%d", context, len(client_list))
_LOGGER.debug("%s client_list=%s", context, client_list)

for client in client_list:
client["name"] = base64.b64decode(client["name"]).decode()
Expand Down Expand Up @@ -249,6 +251,7 @@ async def async_login(self):
f"Invalid login credentials. {attempts} attempts remaining."
)
if error_code != 0:
_LOGGER.debug("Login error_code=%s, data=%s", error_code, data)
raise Exception(f"Login error {data['error_code']}")

self._stok = result["stok"]
Expand Down Expand Up @@ -289,6 +292,12 @@ async def _async_post(
if "error_code" in response_json:
error_code = response_json.get("error_code")
if error_code != 0:
_LOGGER.debug(
"%s error_code=%s, response_json=%s",
context,
error_code,
response_json,
)
raise Exception(f"{context} error: {error_code}")

return response_json
Expand All @@ -315,13 +324,6 @@ async def _async_post(
err,
)
raise err
except Exception as err: # pylint: disable=broad-except
_LOGGER.error(
"%s error: %s",
context,
err,
)
raise err

def _encode_payload(self, payload: Any):
data = self._encode_data(payload)
Expand Down
39 changes: 20 additions & 19 deletions custom_components/tplink_deco/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,23 @@ def update(
self,
data: dict[str:Any],
) -> None:
self.hw_version = data["hardware_ver"]
self.sw_version = data["software_ver"]
self.device_model = data["device_model"]
self.hw_version = data.get("hardware_ver")
self.sw_version = data.get("software_ver")
self.device_model = data.get("device_model")

self.name = data.get("custom_nickname") # Only set if custom value
if self.name is None:
self.name = snake_case_to_title_space(data["nickname"])
self.ip_address = filter_invalid_ip(data["device_ip"])
self.online = data["group_status"] == "connected"
self.internet_online = data["inet_status"] == "online"
self.master = data["role"] == "master"
self.name = snake_case_to_title_space(data.get("nickname"))
self.ip_address = filter_invalid_ip(data.get("device_ip"))
self.online = data.get("group_status") == "connected"
self.internet_online = data.get("inet_status") == "online"
self.master = data.get("role") == "master"
self.connection_type = data.get("connection_type")
self.bssid_band2_4 = data["bssid_2g"]
self.bssid_band5 = data["bssid_5g"]
self.signal_band2_4 = data["signal_level"].get("band2_4")
self.signal_band5 = data["signal_level"].get("band5")
self.bssid_band2_4 = data.get("bssid_2g")
self.bssid_band5 = data.get("bssid_5g")
signal_level = data.get("signal_level", {})
self.signal_band2_4 = signal_level.get("band2_4")
self.signal_band5 = signal_level.get("band5")


class TpLinkDecoClient:
Expand All @@ -108,13 +109,13 @@ def update(
utc_point_in_time: datetime,
) -> None:
self.deco_mac = deco_mac
self.name = data["name"]
self.ip_address = filter_invalid_ip(data["ip"])
self.online = data["online"]
self.connection_type = data["connection_type"]
self.interface = data["interface"]
self.down_kilobytes_per_s = bytes_to_bits(data["down_speed"])
self.up_kilobytes_per_s = bytes_to_bits(data["up_speed"])
self.name = data.get("name")
self.ip_address = filter_invalid_ip(data.get("ip"))
self.online = data.get("online")
self.connection_type = data.get("connection_type")
self.interface = data.get("interface")
self.down_kilobytes_per_s = bytes_to_bits(data.get("down_speed", 0))
self.up_kilobytes_per_s = bytes_to_bits(data.get("up_speed", 0))
self.last_activity = utc_point_in_time


Expand Down

0 comments on commit 6d26ca6

Please sign in to comment.