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

fixes undefined
pull/82661/head
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
1 changed files with 13 additions and 3 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from dataclasses import dataclass, field
import logging
from typing import NamedTuple
from miio.fan_common import LedBrightness as FanLedBrightness
@ -66,6 +67,9 @@ ATTR_LED_BRIGHTNESS = "led_brightness"
ATTR_PTC_LEVEL = "ptc_level"
_LOGGER = logging.getLogger(__name__)
@dataclass
class XiaomiMiioSelectDescription(SelectEntityDescription):
"""A class that describes select entities."""
@ -248,11 +252,17 @@ class XiaomiGenericSelector(XiaomiSelector):
@callback
def _handle_coordinator_update(self):
"""Fetch state from the device."""
attr = self._enum_class(
self._extract_value_from_attribute(
try:
value = self._extract_value_from_attribute(
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:
self._current_attr = attr
self.async_write_ha_state()