Do not crash on non-existing enum values in xiaomi_miio.select (#82625)

fixes undefined
This commit is contained in:
Teemu R 2022-11-24 20:49:30 +01:00 committed by GitHub
parent 3b0a42f8f4
commit e4fbbdfa05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass, field from dataclasses import dataclass, field
import logging
from typing import NamedTuple from typing import NamedTuple
from miio.fan_common import LedBrightness as FanLedBrightness from miio.fan_common import LedBrightness as FanLedBrightness
@ -66,6 +67,9 @@ ATTR_LED_BRIGHTNESS = "led_brightness"
ATTR_PTC_LEVEL = "ptc_level" ATTR_PTC_LEVEL = "ptc_level"
_LOGGER = logging.getLogger(__name__)
@dataclass @dataclass
class XiaomiMiioSelectDescription(SelectEntityDescription): class XiaomiMiioSelectDescription(SelectEntityDescription):
"""A class that describes select entities.""" """A class that describes select entities."""
@ -248,11 +252,17 @@ class XiaomiGenericSelector(XiaomiSelector):
@callback @callback
def _handle_coordinator_update(self): def _handle_coordinator_update(self):
"""Fetch state from the device.""" """Fetch state from the device."""
attr = self._enum_class( try:
self._extract_value_from_attribute( value = self._extract_value_from_attribute(
self.coordinator.data, self.entity_description.attr_name self.coordinator.data, self.entity_description.attr_name
) )
) attr = self._enum_class(value)
except ValueError: # if the value does not exist in
_LOGGER.debug(
"Value '%s' does not exist in enum %s", value, self._enum_class
)
attr = None
if attr is not None: if attr is not None:
self._current_attr = attr self._current_attr = attr
self.async_write_ha_state() self.async_write_ha_state()