Handle UnsupportedError in HomeWizard (#91608)

* Handle UnsupportedEror

* Make error message more clear

* Remove debug line, whoops
pull/91681/head
Duco Sebel 2023-04-19 17:14:28 +02:00 committed by GitHub
parent 55c723753e
commit 9092f6a60f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 6 deletions

View File

@ -5,7 +5,7 @@ import logging
from homewizard_energy import HomeWizardEnergy
from homewizard_energy.const import SUPPORTS_IDENTIFY, SUPPORTS_STATE, SUPPORTS_SYSTEM
from homewizard_energy.errors import DisabledError, RequestError
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
from homewizard_energy.models import Device
from homeassistant.config_entries import ConfigEntry
@ -24,6 +24,8 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
api: HomeWizardEnergy
api_disabled: bool = False
_unsupported_error: bool = False
def __init__(
self,
hass: HomeAssistant,
@ -43,11 +45,22 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry]
data=await self.api.data(),
)
if self.supports_state(data.device):
data.state = await self.api.state()
try:
if self.supports_state(data.device):
data.state = await self.api.state()
if self.supports_system(data.device):
data.system = await self.api.system()
if self.supports_system(data.device):
data.system = await self.api.system()
except UnsupportedError as ex:
# Old firmware, ignore
if not self._unsupported_error:
self._unsupported_error = True
_LOGGER.warning(
"%s is running an outdated firmware version (%s). Contact HomeWizard support to update your device",
self.entry.title,
ex,
)
except RequestError as ex:
raise UpdateFailed(ex) from ex

View File

@ -1,7 +1,7 @@
"""Test the update coordinator for HomeWizard."""
from unittest.mock import AsyncMock, patch
from homewizard_energy.errors import DisabledError, RequestError
from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError
from homewizard_energy.models import State, System
import pytest
@ -507,3 +507,39 @@ async def test_switch_handles_disablederror(
{"entity_id": "switch.product_name_aabbccddeeff_cloud_connection"},
blocking=True,
)
async def test_switch_handles_unsupportedrrror(
hass: HomeAssistant, mock_config_entry_data, mock_config_entry
) -> None:
"""Test entity raises HomeAssistantError when Disabled was raised."""
api = get_mock_device(product_type="HWE-SKT", firmware_version="3.02")
api.state = AsyncMock(side_effect=UnsupportedError())
api.system = AsyncMock(side_effect=UnsupportedError())
with patch(
"homeassistant.components.homewizard.coordinator.HomeWizardEnergy",
return_value=api,
):
entry = mock_config_entry
entry.data = mock_config_entry_data
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert (
hass.states.get("switch.product_name_aabbccddeeff_cloud_connection").state
== STATE_UNAVAILABLE
)
assert (
hass.states.get("switch.product_name_aabbccddeeff_switch_lock").state
== STATE_UNAVAILABLE
)
assert (
hass.states.get("switch.product_name_aabbccddeeff").state
== STATE_UNAVAILABLE
)