diff --git a/homeassistant/components/homewizard/coordinator.py b/homeassistant/components/homewizard/coordinator.py index 533af445c84..fb89989b2a5 100644 --- a/homeassistant/components/homewizard/coordinator.py +++ b/homeassistant/components/homewizard/coordinator.py @@ -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 diff --git a/tests/components/homewizard/test_switch.py b/tests/components/homewizard/test_switch.py index f55550ee825..6a2623e964f 100644 --- a/tests/components/homewizard/test_switch.py +++ b/tests/components/homewizard/test_switch.py @@ -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 + )