Skip to content

Commit

Permalink
Handle unreliable session refreshs more graceful (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shutgun authored May 5, 2022
1 parent 5715891 commit eebf299
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 6 additions & 0 deletions devolo_home_control_api/homecontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import Any, Dict, List, Optional

import requests
from requests.adapters import HTTPAdapter
from urllib3 import Retry
from zeroconf import Zeroconf

from . import __version__
Expand Down Expand Up @@ -41,9 +43,13 @@ class HomeControl(Mprm):
"""

def __init__(self, gateway_id: str, mydevolo_instance: Mydevolo, zeroconf_instance: Optional[Zeroconf] = None) -> None:
retry = Retry(total=5, backoff_factor=0.1, allowed_methods=("GET", "POST"))
adapter = HTTPAdapter(max_retries=retry)

self._mydevolo = mydevolo_instance
self._session = requests.Session()
self._session.headers.update({"User-Agent": f"devolo_home_control_api/{__version__}"})
self._session.mount("http://", adapter)
self._zeroconf = zeroconf_instance
self.gateway = Gateway(gateway_id, mydevolo_instance)

Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.18.2] - 2022/05/06

### Fixed

- Handle unreliable session refreshs more graceful

## [v0.18.1] - 2022/04/11

### Fixed
Expand Down
15 changes: 8 additions & 7 deletions tests/test_mprm_websocket.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from unittest.mock import patch

import pytest
from requests.exceptions import ConnectionError
Expand Down Expand Up @@ -79,10 +80,10 @@ def test__try_reconnect(self, mocker):

@pytest.mark.usefixtures("mock_mprmwebsocket_websocketapp")
def test__try_reconnect_with_detect(self, mocker):
spy_sleep = mocker.spy(time, "sleep")
spy_detect_gateway = mocker.spy(StubMprmWebsocket, "detect_gateway_in_lan")
self.mprm._ws = ConnectionError
self.mprm._local_ip = self.gateway["local_ip"]
self.mprm._try_reconnect(4)
spy_sleep.assert_called_once_with(1)
spy_detect_gateway.assert_called_once()
with patch("time.sleep") as sleep:
spy_detect_gateway = mocker.spy(StubMprmWebsocket, "detect_gateway_in_lan")
self.mprm._ws = ConnectionError
self.mprm._local_ip = self.gateway["local_ip"]
self.mprm._try_reconnect(4)
sleep.assert_called_once_with(1)
spy_detect_gateway.assert_called_once()

0 comments on commit eebf299

Please sign in to comment.