Mod ToggleEntity and implement state in LightEntity

This commit is contained in:
G Johansson 2024-09-24 18:56:21 +00:00
parent ddf7f08567
commit 5b886d48cf
2 changed files with 5 additions and 9 deletions

View File

@ -913,13 +913,10 @@ class LightEntity(ToggleEntity, cached_properties=CACHED_PROPERTIES_WITH_ATTR_):
__color_mode_reported = False
@property # type: ignore[misc]
@property
@final
def state(self) -> str | None: # type: ignore[override]
"""Return the state."""
# Implemented in Home Assistant 2024.10 to enable light entity
# to return an enum instead of a string.
# When ToggleEntity can return an enum this can be removed.
def state(self) -> str | None:
"""Return the light state."""
if (_is_on := self.is_on) is None:
return None
return LightState.ON if _is_on else LightState.OFF

View File

@ -17,7 +17,7 @@ import sys
import threading
import time
from types import FunctionType
from typing import TYPE_CHECKING, Any, Final, Literal, NotRequired, TypedDict, final
from typing import TYPE_CHECKING, Any, Final, NotRequired, TypedDict, final
import voluptuous as vol
@ -1664,8 +1664,7 @@ class ToggleEntity(
_attr_state: None = None
@property
@final
def state(self) -> Literal["on", "off"] | None:
def state(self) -> str | None:
"""Return the state."""
if (is_on := self.is_on) is None:
return None