From 3e85a74f15dd2ed53d65312f0f8ee7e63dc0a908 Mon Sep 17 00:00:00 2001 From: mib1185 Date: Wed, 19 Feb 2025 18:59:50 +0000 Subject: [PATCH] fallback to getdevicelistinfos in wait for txbusy --- pyfritzhome/fritzhome.py | 16 +++++++++--- .../responses/base/device_list_not_txbusy.xml | 26 +++++++++++++++++++ tests/responses/base/device_list_txbusy.xml | 26 +++++++++++++++++++ tests/test_fritzhome.py | 11 +++++++- 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 tests/responses/base/device_list_not_txbusy.xml create mode 100644 tests/responses/base/device_list_txbusy.xml diff --git a/pyfritzhome/fritzhome.py b/pyfritzhome/fritzhome.py index da1972b..380e580 100644 --- a/pyfritzhome/fritzhome.py +++ b/pyfritzhome/fritzhome.py @@ -11,7 +11,7 @@ from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC -from requests import Session +from requests import exceptions, Session from .errors import InvalidError, LoginError, NotLoggedInError from .fritzhomedevice import FritzhomeDevice @@ -36,6 +36,7 @@ def __init__(self, host, user, password, ssl_verify=True): self._password = password self._session = Session() self._ssl_verify = ssl_verify + self._has_getdeviceinfos = True def _request(self, url, params=None, timeout=10): """Send a request with parameters.""" @@ -200,8 +201,17 @@ def _get_listinfo_elements(self, entity_type): def wait_device_txbusy(self, ain, retries=10): """Wait for device to finish command execution.""" for _ in range(retries): - plain = self.get_device_infos(ain) - dom = ElementTree.fromstring(plain) + if self._has_getdeviceinfos: + try: + plain = self.get_device_infos(ain) + dom = ElementTree.fromstring(plain) + except exceptions.HTTPError: + _LOGGER.debug("fallback to getdevicelistinfos") + self._has_getdeviceinfos = False + + if not self._has_getdeviceinfos: + dom = self.get_device_element(ain) + txbusy = dom.findall("txbusy") if txbusy[0].text == "0": return True diff --git a/tests/responses/base/device_list_not_txbusy.xml b/tests/responses/base/device_list_not_txbusy.xml new file mode 100644 index 0000000..57c76b8 --- /dev/null +++ b/tests/responses/base/device_list_not_txbusy.xml @@ -0,0 +1,26 @@ + + + + 0 + 0 + Kitchen + + + + + + + + + + + + 0 + 0 + + 0 + 255 + + + + \ No newline at end of file diff --git a/tests/responses/base/device_list_txbusy.xml b/tests/responses/base/device_list_txbusy.xml new file mode 100644 index 0000000..b724320 --- /dev/null +++ b/tests/responses/base/device_list_txbusy.xml @@ -0,0 +1,26 @@ + + + + 0 + 1 + Kitchen + + + + + + + + + + + + 0 + 0 + + 0 + 255 + + + + \ No newline at end of file diff --git a/tests/test_fritzhome.py b/tests/test_fritzhome.py index 4cfb4e3..b7e56f5 100644 --- a/tests/test_fritzhome.py +++ b/tests/test_fritzhome.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from requests.exceptions import ConnectionError +from requests.exceptions import ConnectionError, HTTPError from unittest.mock import MagicMock, patch import pytest @@ -346,6 +346,15 @@ def test_wait_tx_busy(self): assert self.fritz.wait_device_txbusy("11960 0089208") + def test_wait_tx_busy_fallback(self): + self.mock.side_effect = [ + HTTPError("400 Client Error: Bad Request"), + Helper.response("base/device_list_txbusy"), + Helper.response("base/device_list_not_txbusy"), + ] + + assert self.fritz.wait_device_txbusy("22960 0089208") + def test_wait_tx_busy_failed(self): self.mock.side_effect = [ Helper.response("base/device_txbusy"),