Add retry before unavailable to Honeywell (#101702)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
mkmer 2023-10-30 10:16:41 -04:00 committed by GitHub
parent 4ed3676a56
commit 92ec525de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View File

@ -38,6 +38,7 @@ from .const import (
CONF_COOL_AWAY_TEMPERATURE,
CONF_HEAT_AWAY_TEMPERATURE,
DOMAIN,
RETRY,
)
ATTR_FAN_ACTION = "fan_action"
@ -155,6 +156,7 @@ class HoneywellUSThermostat(ClimateEntity):
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
@ -483,21 +485,28 @@ class HoneywellUSThermostat(ClimateEntity):
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

View File

@ -10,3 +10,4 @@ DEFAULT_HEAT_AWAY_TEMPERATURE = 61
CONF_DEV_ID = "thermostat"
CONF_LOC_ID = "location"
_LOGGER = logging.getLogger(__name__)
RETRY = 3

View File

@ -28,7 +28,7 @@ from homeassistant.components.climate import (
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,
@ -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,
@ -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(
@ -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,