mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
Add state feature for iot devices (#924)
This is allows a generic implementation for the switch platform in the homeassistant integration. Also elevates set_state(bool) to be part of the standard API.
This commit is contained in:
parent
db6e335346
commit
23c5ee089a
@ -138,6 +138,14 @@ class Device(ABC):
|
|||||||
async def turn_off(self, **kwargs) -> dict | None:
|
async def turn_off(self, **kwargs) -> dict | None:
|
||||||
"""Turn off the device."""
|
"""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
|
@property
|
||||||
def host(self) -> str:
|
def host(self) -> str:
|
||||||
"""The device host."""
|
"""The device host."""
|
||||||
|
@ -323,6 +323,18 @@ class IotDevice(Device):
|
|||||||
"""Initialize modules not added in init."""
|
"""Initialize modules not added in init."""
|
||||||
|
|
||||||
async def _initialize_features(self):
|
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(
|
self._add_feature(
|
||||||
Feature(
|
Feature(
|
||||||
device=self,
|
device=self,
|
||||||
@ -634,6 +646,13 @@ class IotDevice(Device):
|
|||||||
"""Return True if the device is on."""
|
"""Return True if the device is on."""
|
||||||
raise NotImplementedError("Device subclass needs to implement this.")
|
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
|
@property # type: ignore
|
||||||
@requires_update
|
@requires_update
|
||||||
def on_since(self) -> datetime | None:
|
def on_since(self) -> datetime | None:
|
||||||
|
Loading…
Reference in New Issue
Block a user