1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-23 19:37:12 +00:00
Files
core/homeassistant/util/enum.py
epenet 1225c5e97d Add enum util (#87082)
* Add enum helper

* docstring

* Move to util

* Add use case
2023-02-01 15:00:27 +01:00

17 lines
391 B
Python

"""Helpers for working with enums."""
import contextlib
from enum import Enum
from typing import Any, TypeVar
_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