Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions kasa/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ async def turn_on(self, **kwargs) -> dict | None:
async def turn_off(self, **kwargs) -> dict | None:
"""Turn off the device."""

@abstractmethod
async def set_state(self, on: bool):
"""Set the device state to *on*.

This allows turning the device on and off.
See also *turn_off* and *turn_on*.
"""

@property
def host(self) -> str:
"""The device host."""
Expand Down
19 changes: 19 additions & 0 deletions kasa/iot/iotdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,18 @@ async def _initialize_modules(self):
"""Initialize modules not added in init."""

async def _initialize_features(self):
"""Initialize common features."""
self._add_feature(
Feature(
self,
id="state",
name="State",
attribute_getter="is_on",
attribute_setter="set_state",
type=Feature.Type.Switch,
category=Feature.Category.Primary,
)
)
self._add_feature(
Feature(
device=self,
Expand Down Expand Up @@ -634,6 +646,13 @@ def is_on(self) -> bool:
"""Return True if the device is on."""
raise NotImplementedError("Device subclass needs to implement this.")

async def set_state(self, on: bool):
"""Set the device state."""
if on:
return await self.turn_on()
else:
return await self.turn_off()

@property # type: ignore
@requires_update
def on_since(self) -> datetime | None:
Expand Down