mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 06:37:52 +00:00
Define ToggleEntity entity attributes as class variables (#51231)
* Define ToggleEntity entity attributes as class variables * Fix upcloud overriding state property * Implement available state for upcloud, to compensate removed state
This commit is contained in:
parent
101864aab8
commit
d51fc5814a
@ -273,18 +273,20 @@ class UpCloudServerEntity(CoordinatorEntity):
|
|||||||
"""Return the icon of this server."""
|
"""Return the icon of this server."""
|
||||||
return "mdi:server" if self.is_on else "mdi:server-off"
|
return "mdi:server" if self.is_on else "mdi:server-off"
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self) -> str | None:
|
|
||||||
"""Return state of the server."""
|
|
||||||
try:
|
|
||||||
return STATE_MAP.get(self._server.state, self._server.state)
|
|
||||||
except AttributeError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return true if the server is on."""
|
"""Return true if the server is on."""
|
||||||
return self.state == STATE_ON
|
try:
|
||||||
|
return STATE_MAP.get(self._server.state, self._server.state) == STATE_ON
|
||||||
|
except AttributeError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return True if entity is available."""
|
||||||
|
return super().available and STATE_MAP.get(
|
||||||
|
self._server.state, self._server.state
|
||||||
|
) in [STATE_ON, STATE_OFF]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
|
@ -10,7 +10,7 @@ import logging
|
|||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
from timeit import default_timer as timer
|
from timeit import default_timer as timer
|
||||||
from typing import Any, TypedDict
|
from typing import Any, TypedDict, final
|
||||||
|
|
||||||
from homeassistant.config import DATA_CUSTOMIZE
|
from homeassistant.config import DATA_CUSTOMIZE
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -766,7 +766,11 @@ class Entity(ABC):
|
|||||||
class ToggleEntity(Entity):
|
class ToggleEntity(Entity):
|
||||||
"""An abstract class for entities that can be turned on and off."""
|
"""An abstract class for entities that can be turned on and off."""
|
||||||
|
|
||||||
|
_attr_is_on: bool
|
||||||
|
_attr_state: None = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@final
|
||||||
def state(self) -> str | None:
|
def state(self) -> str | None:
|
||||||
"""Return the state."""
|
"""Return the state."""
|
||||||
return STATE_ON if self.is_on else STATE_OFF
|
return STATE_ON if self.is_on else STATE_OFF
|
||||||
@ -774,7 +778,7 @@ class ToggleEntity(Entity):
|
|||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return True if entity is on."""
|
"""Return True if entity is on."""
|
||||||
raise NotImplementedError()
|
return self._attr_is_on
|
||||||
|
|
||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity on."""
|
"""Turn the entity on."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user