Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pyfritzhome/fritzhome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""
Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mib1185,
I think we should check for the status code here, to avoid sending more requests to the box in case of other errors 🙂.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh. I can not predict if there are other codes besides 400 returned on even older FritzOS - because of this and that it would only be one additional request for the whole login session, i've not add a check for the response code, but just for httperrors - if there will be an connection error, then it will raise as already done before

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember getting 503 from a Fritzbox while testing something, but I don't remember how that happened 😉.

_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
Expand Down
26 changes: 26 additions & 0 deletions tests/responses/base/device_list_not_txbusy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" ?>
<devicelist version="1">
<device functionbitmask="320" fwversion="03.54" id="18" identifier="22960 0089208" manufacturer="AVM" productname="Comet DECT">
<present>0</present>
<txbusy>0</txbusy>
<name>Kitchen</name>
<temperature>
<celsius/>
<offset/>
</temperature>
<hkr>
<tist/>
<tsoll/>
<absenk/>
<komfort/>
<lock/>
<devicelock/>
<errorcode>0</errorcode>
<batterylow>0</batterylow>
<nextchange>
<endperiod>0</endperiod>
<tchange>255</tchange>
</nextchange>
</hkr>
</device>
</devicelist>
26 changes: 26 additions & 0 deletions tests/responses/base/device_list_txbusy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" ?>
<devicelist version="1">
<device functionbitmask="320" fwversion="03.54" id="18" identifier="22960 0089208" manufacturer="AVM" productname="Comet DECT">
<present>0</present>
<txbusy>1</txbusy>
<name>Kitchen</name>
<temperature>
<celsius/>
<offset/>
</temperature>
<hkr>
<tist/>
<tsoll/>
<absenk/>
<komfort/>
<lock/>
<devicelock/>
<errorcode>0</errorcode>
<batterylow>0</batterylow>
<nextchange>
<endperiod>0</endperiod>
<tchange>255</tchange>
</nextchange>
</hkr>
</device>
</devicelist>
11 changes: 10 additions & 1 deletion tests/test_fritzhome.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"),
Expand Down
Loading