mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
backport try_parse_enum
This commit is contained in:
parent
0d140426cc
commit
3e3936e783
@ -5,9 +5,10 @@ from collections import deque
|
|||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
import contextlib
|
import contextlib
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from enum import Enum
|
||||||
import logging
|
import logging
|
||||||
import statistics
|
import statistics
|
||||||
from typing import Any, Literal, cast
|
from typing import Any, Literal, TypeVar, cast
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -48,7 +49,6 @@ from homeassistant.helpers.reload import async_setup_reload_service
|
|||||||
from homeassistant.helpers.start import async_at_start
|
from homeassistant.helpers.start import async_at_start
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
from homeassistant.util.enum import try_parse_enum
|
|
||||||
|
|
||||||
from . import DOMAIN, PLATFORMS
|
from . import DOMAIN, PLATFORMS
|
||||||
|
|
||||||
@ -769,3 +769,16 @@ class StatisticsSensor(SensorEntity):
|
|||||||
if len(self.states) > 0:
|
if len(self.states) > 0:
|
||||||
return 100.0 / len(self.states) * self.states.count(True)
|
return 100.0 / len(self.states) * self.states.count(True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
_EnumT = TypeVar("_EnumT", bound=Enum)
|
||||||
|
|
||||||
|
|
||||||
|
def try_parse_enum(cls: type[_EnumT], value: Any) -> _EnumT | None:
|
||||||
|
"""Try to parse the value into an Enum.
|
||||||
|
|
||||||
|
Return None if parsing fails.
|
||||||
|
"""
|
||||||
|
with contextlib.suppress(ValueError):
|
||||||
|
return cls(value)
|
||||||
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user