Allow toggles (switches) state to be None (#64621)

This commit is contained in:
Franck Nijhof 2022-01-23 11:31:01 +01:00 committed by GitHub
parent 01fbc4257b
commit 176f03d4ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 45 additions and 32 deletions

View file

@ -927,17 +927,19 @@ class ToggleEntity(Entity):
"""An abstract class for entities that can be turned on and off."""
entity_description: ToggleEntityDescription
_attr_is_on: bool
_attr_is_on: bool | None = None
_attr_state: None = None
@property
@final
def state(self) -> str | None:
def state(self) -> Literal["on", "off"] | None:
"""Return the state."""
return STATE_ON if self.is_on else STATE_OFF
if (is_on := self.is_on) is None:
return None
return STATE_ON if is_on else STATE_OFF
@property
def is_on(self) -> bool:
def is_on(self) -> bool | None:
"""Return True if entity is on."""
return self._attr_is_on