Skip to content

Commit

Permalink
Add retry before unavailable to Honeywell (#101702)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Resch <robert@resch.dev>
  • Loading branch information
mkmer and edenhaus committed Oct 30, 2023
1 parent 4ed3676 commit 92ec525
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
13 changes: 11 additions & 2 deletions homeassistant/components/honeywell/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
CONF_COOL_AWAY_TEMPERATURE,
CONF_HEAT_AWAY_TEMPERATURE,
DOMAIN,
RETRY,
)

ATTR_FAN_ACTION = "fan_action"
Expand Down Expand Up @@ -155,6 +156,7 @@ def __init__(
self._cool_away_temp = cool_away_temp
self._heat_away_temp = heat_away_temp
self._away = False
self._retry = 0

self._attr_unique_id = device.deviceid

Expand Down Expand Up @@ -483,21 +485,28 @@ async def async_update(self) -> None:
try:
await self._device.refresh()
self._attr_available = True
self._retry = 0

except UnauthorizedError:
try:
await self._data.client.login()
await self._device.refresh()
self._attr_available = True
self._retry = 0

except (
SomeComfortError,
ClientConnectionError,
asyncio.TimeoutError,
):
self._attr_available = False
self._retry += 1
if self._retry > RETRY:
self._attr_available = False

except (ClientConnectionError, asyncio.TimeoutError):
self._attr_available = False
self._retry += 1
if self._retry > RETRY:
self._attr_available = False

except UnexpectedResponse:
pass
1 change: 1 addition & 0 deletions homeassistant/components/honeywell/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
CONF_DEV_ID = "thermostat"
CONF_LOC_ID = "location"
_LOGGER = logging.getLogger(__name__)
RETRY = 3
26 changes: 24 additions & 2 deletions tests/components/honeywell/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
SERVICE_SET_TEMPERATURE,
HVACMode,
)
from homeassistant.components.honeywell.climate import SCAN_INTERVAL
from homeassistant.components.honeywell.climate import RETRY, SCAN_INTERVAL
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_TEMPERATURE,
Expand Down Expand Up @@ -1083,6 +1083,17 @@ async def test_async_update_errors(
state = hass.states.get(entity_id)
assert state.state == "off"

# Due to server instability, only mark entity unavailable after RETRY update attempts
for _ in range(RETRY):
async_fire_time_changed(
hass,
utcnow() + SCAN_INTERVAL,
)
await hass.async_block_till_done()

state = hass.states.get(entity_id)
assert state.state == "off"

async_fire_time_changed(
hass,
utcnow() + SCAN_INTERVAL,
Expand Down Expand Up @@ -1126,7 +1137,6 @@ async def test_async_update_errors(
state = hass.states.get(entity_id)
assert state.state == "off"

# "reload integration" test
device.refresh.side_effect = aiosomecomfort.SomeComfortError
client.login.side_effect = aiosomecomfort.AuthError
async_fire_time_changed(
Expand All @@ -1139,6 +1149,18 @@ async def test_async_update_errors(
assert state.state == "off"

device.refresh.side_effect = ClientConnectionError

# Due to server instability, only mark entity unavailable after RETRY update attempts
for _ in range(RETRY):
async_fire_time_changed(
hass,
utcnow() + SCAN_INTERVAL,
)
await hass.async_block_till_done()

state = hass.states.get(entity_id)
assert state.state == "off"

async_fire_time_changed(
hass,
utcnow() + SCAN_INTERVAL,
Expand Down

0 comments on commit 92ec525

Please sign in to comment.