Fix EZVIZ LightEntity occasional ValueError (#95679)

This commit is contained in:
Renier Moorcroft 2023-07-24 20:03:31 +02:00 committed by GitHub
parent d0722e2312
commit 4c3d9e5205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,7 @@ from pyezviz.exceptions import HTTPError, PyEzvizError
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.percentage import ( from homeassistant.util.percentage import (
@ -61,22 +61,14 @@ class EzvizLight(EzvizEntity, LightEntity):
) )
self._attr_unique_id = f"{serial}_Light" self._attr_unique_id = f"{serial}_Light"
self._attr_name = "Light" self._attr_name = "Light"
self._attr_is_on = self.data["switches"][DeviceSwitchType.ALARM_LIGHT.value]
@property self._attr_brightness = round(
def brightness(self) -> int | None:
"""Return the brightness of this light between 0..255."""
return round(
percentage_to_ranged_value( percentage_to_ranged_value(
BRIGHTNESS_RANGE, BRIGHTNESS_RANGE,
self.coordinator.data[self._serial]["alarm_light_luminance"], self.coordinator.data[self._serial]["alarm_light_luminance"],
) )
) )
@property
def is_on(self) -> bool:
"""Return the state of the light."""
return self.data["switches"][DeviceSwitchType.ALARM_LIGHT.value]
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on light.""" """Turn on light."""
try: try:
@ -85,41 +77,55 @@ class EzvizLight(EzvizEntity, LightEntity):
BRIGHTNESS_RANGE, kwargs[ATTR_BRIGHTNESS] BRIGHTNESS_RANGE, kwargs[ATTR_BRIGHTNESS]
) )
update_ok = await self.hass.async_add_executor_job( if await self.hass.async_add_executor_job(
self.coordinator.ezviz_client.set_floodlight_brightness, self.coordinator.ezviz_client.set_floodlight_brightness,
self._serial, self._serial,
data, data,
) ):
else: self._attr_brightness = kwargs[ATTR_BRIGHTNESS]
update_ok = await self.hass.async_add_executor_job(
self.coordinator.ezviz_client.switch_status, if await self.hass.async_add_executor_job(
self._serial, self.coordinator.ezviz_client.switch_status,
DeviceSwitchType.ALARM_LIGHT.value, self._serial,
1, DeviceSwitchType.ALARM_LIGHT.value,
) 1,
):
self._attr_is_on = True
self.async_write_ha_state()
except (HTTPError, PyEzvizError) as err: except (HTTPError, PyEzvizError) as err:
raise HomeAssistantError( raise HomeAssistantError(
f"Failed to turn on light {self._attr_name}" f"Failed to turn on light {self._attr_name}"
) from err ) from err
if update_ok:
await self.coordinator.async_request_refresh()
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off light.""" """Turn off light."""
try: try:
update_ok = await self.hass.async_add_executor_job( if await self.hass.async_add_executor_job(
self.coordinator.ezviz_client.switch_status, self.coordinator.ezviz_client.switch_status,
self._serial, self._serial,
DeviceSwitchType.ALARM_LIGHT.value, DeviceSwitchType.ALARM_LIGHT.value,
0, 0,
) ):
self._attr_is_on = False
self.async_write_ha_state()
except (HTTPError, PyEzvizError) as err: except (HTTPError, PyEzvizError) as err:
raise HomeAssistantError( raise HomeAssistantError(
f"Failed to turn off light {self._attr_name}" f"Failed to turn off light {self._attr_name}"
) from err ) from err
if update_ok: @callback
await self.coordinator.async_request_refresh() def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._attr_is_on = self.data["switches"].get(DeviceSwitchType.ALARM_LIGHT.value)
if isinstance(self.data["alarm_light_luminance"], int):
self._attr_brightness = round(
percentage_to_ranged_value(
BRIGHTNESS_RANGE,
self.data["alarm_light_luminance"],
)
)
super()._handle_coordinator_update()