FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add state feature for iot devices by rytilahti · Pull Request #924 · python-kasa/python-kasa · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 8 additions & 0 deletions kasa/device.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL