mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Cleanup litterrobot switch entity (#136199)
This commit is contained in:
parent
f274a3eb37
commit
69900ed8cb
@ -17,18 +17,13 @@ from . import LitterRobotConfigEntry
|
|||||||
from .entity import LitterRobotEntity, _RobotT
|
from .entity import LitterRobotEntity, _RobotT
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class RequiredKeysMixin(Generic[_RobotT]):
|
class RobotSwitchEntityDescription(SwitchEntityDescription, Generic[_RobotT]):
|
||||||
"""A class that describes robot switch entity required keys."""
|
|
||||||
|
|
||||||
set_fn: Callable[[_RobotT, bool], Coroutine[Any, Any, bool]]
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class RobotSwitchEntityDescription(SwitchEntityDescription, RequiredKeysMixin[_RobotT]):
|
|
||||||
"""A class that describes robot switch entities."""
|
"""A class that describes robot switch entities."""
|
||||||
|
|
||||||
entity_category: EntityCategory = EntityCategory.CONFIG
|
entity_category: EntityCategory = EntityCategory.CONFIG
|
||||||
|
set_fn: Callable[[_RobotT, bool], Coroutine[Any, Any, bool]]
|
||||||
|
value_fn: Callable[[_RobotT], bool]
|
||||||
|
|
||||||
|
|
||||||
ROBOT_SWITCHES = [
|
ROBOT_SWITCHES = [
|
||||||
@ -36,34 +31,17 @@ ROBOT_SWITCHES = [
|
|||||||
key="night_light_mode_enabled",
|
key="night_light_mode_enabled",
|
||||||
translation_key="night_light_mode",
|
translation_key="night_light_mode",
|
||||||
set_fn=lambda robot, value: robot.set_night_light(value),
|
set_fn=lambda robot, value: robot.set_night_light(value),
|
||||||
|
value_fn=lambda robot: robot.night_light_mode_enabled,
|
||||||
),
|
),
|
||||||
RobotSwitchEntityDescription[LitterRobot | FeederRobot](
|
RobotSwitchEntityDescription[LitterRobot | FeederRobot](
|
||||||
key="panel_lock_enabled",
|
key="panel_lock_enabled",
|
||||||
translation_key="panel_lockout",
|
translation_key="panel_lockout",
|
||||||
set_fn=lambda robot, value: robot.set_panel_lockout(value),
|
set_fn=lambda robot, value: robot.set_panel_lockout(value),
|
||||||
|
value_fn=lambda robot: robot.panel_lock_enabled,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class RobotSwitchEntity(LitterRobotEntity[_RobotT], SwitchEntity):
|
|
||||||
"""Litter-Robot switch entity."""
|
|
||||||
|
|
||||||
entity_description: RobotSwitchEntityDescription[_RobotT]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool | None:
|
|
||||||
"""Return true if switch is on."""
|
|
||||||
return bool(getattr(self.robot, self.entity_description.key))
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
||||||
"""Turn the switch on."""
|
|
||||||
await self.entity_description.set_fn(self.robot, True)
|
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
||||||
"""Turn the switch off."""
|
|
||||||
await self.entity_description.set_fn(self.robot, False)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: LitterRobotConfigEntry,
|
entry: LitterRobotConfigEntry,
|
||||||
@ -78,3 +56,22 @@ async def async_setup_entry(
|
|||||||
if isinstance(robot, (LitterRobot, FeederRobot))
|
if isinstance(robot, (LitterRobot, FeederRobot))
|
||||||
]
|
]
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
|
class RobotSwitchEntity(LitterRobotEntity[_RobotT], SwitchEntity):
|
||||||
|
"""Litter-Robot switch entity."""
|
||||||
|
|
||||||
|
entity_description: RobotSwitchEntityDescription[_RobotT]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool | None:
|
||||||
|
"""Return true if switch is on."""
|
||||||
|
return self.entity_description.value_fn(self.robot)
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the switch on."""
|
||||||
|
await self.entity_description.set_fn(self.robot, True)
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the switch off."""
|
||||||
|
await self.entity_description.set_fn(self.robot, False)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user