Add value_fn to switch entity description in pyLoad (#120569)

This commit is contained in:
Mr. Bubbles 2024-06-26 15:09:42 +02:00 committed by GitHub
parent 43d686e0f1
commit af9b4b98ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,6 +18,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import PyLoadConfigEntry from . import PyLoadConfigEntry
from .coordinator import pyLoadData
from .entity import BasePyLoadEntity from .entity import BasePyLoadEntity
@ -35,6 +36,7 @@ class PyLoadSwitchEntityDescription(SwitchEntityDescription):
turn_on_fn: Callable[[PyLoadAPI], Awaitable[Any]] turn_on_fn: Callable[[PyLoadAPI], Awaitable[Any]]
turn_off_fn: Callable[[PyLoadAPI], Awaitable[Any]] turn_off_fn: Callable[[PyLoadAPI], Awaitable[Any]]
toggle_fn: Callable[[PyLoadAPI], Awaitable[Any]] toggle_fn: Callable[[PyLoadAPI], Awaitable[Any]]
value_fn: Callable[[pyLoadData], bool]
SENSOR_DESCRIPTIONS: tuple[PyLoadSwitchEntityDescription, ...] = ( SENSOR_DESCRIPTIONS: tuple[PyLoadSwitchEntityDescription, ...] = (
@ -45,6 +47,7 @@ SENSOR_DESCRIPTIONS: tuple[PyLoadSwitchEntityDescription, ...] = (
turn_on_fn=lambda api: api.unpause(), turn_on_fn=lambda api: api.unpause(),
turn_off_fn=lambda api: api.pause(), turn_off_fn=lambda api: api.pause(),
toggle_fn=lambda api: api.toggle_pause(), toggle_fn=lambda api: api.toggle_pause(),
value_fn=lambda data: data.download,
), ),
PyLoadSwitchEntityDescription( PyLoadSwitchEntityDescription(
key=PyLoadSwitch.RECONNECT, key=PyLoadSwitch.RECONNECT,
@ -53,6 +56,7 @@ SENSOR_DESCRIPTIONS: tuple[PyLoadSwitchEntityDescription, ...] = (
turn_on_fn=lambda api: api.toggle_reconnect(), turn_on_fn=lambda api: api.toggle_reconnect(),
turn_off_fn=lambda api: api.toggle_reconnect(), turn_off_fn=lambda api: api.toggle_reconnect(),
toggle_fn=lambda api: api.toggle_reconnect(), toggle_fn=lambda api: api.toggle_reconnect(),
value_fn=lambda data: data.reconnect,
), ),
) )
@ -80,7 +84,9 @@ class PyLoadSwitchEntity(BasePyLoadEntity, SwitchEntity):
@property @property
def is_on(self) -> bool | None: def is_on(self) -> bool | None:
"""Return the state of the device.""" """Return the state of the device."""
return getattr(self.coordinator.data, self.entity_description.key) return self.entity_description.value_fn(
self.coordinator.data,
)
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on.""" """Turn the entity on."""