2021-05-10 18:43:00 +02:00
|
|
|
from typing import List, Union
|
2021-02-19 02:54:12 +01:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2017-11-04 00:13:22 +01:00
|
|
|
|
2019-07-02 12:02:28 +02:00
|
|
|
|
2017-11-04 00:13:22 +01:00
|
|
|
class SwitchPlugin(Plugin):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Abstract class for interacting with switch devices
|
|
|
|
"""
|
|
|
|
|
2019-07-02 12:02:28 +02:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
2018-07-06 02:08:38 +02:00
|
|
|
|
|
|
|
@action
|
2019-07-02 12:02:28 +02:00
|
|
|
def on(self, device, *args, **kwargs):
|
2018-06-25 19:57:43 +02:00
|
|
|
""" Turn the device on """
|
2017-11-04 00:13:22 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2019-07-02 12:02:28 +02:00
|
|
|
def off(self, device, *args, **kwargs):
|
2018-06-25 19:57:43 +02:00
|
|
|
""" Turn the device off """
|
2017-11-04 00:13:22 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2019-07-02 12:02:28 +02:00
|
|
|
def toggle(self, device, *args, **kwargs):
|
2018-06-25 19:57:43 +02:00
|
|
|
""" Toggle the device status (on/off) """
|
2017-11-04 00:13:22 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2021-05-10 18:43:00 +02:00
|
|
|
def switch_status(self, device=None) -> Union[dict, List[dict]]:
|
|
|
|
"""
|
|
|
|
Get the status of a specified device or of all the configured devices (default).
|
|
|
|
|
|
|
|
:param device: Filter by device name or ID.
|
|
|
|
:return: .. schema:: switch.SwitchStatusSchema(many=True)
|
|
|
|
"""
|
2021-02-19 02:54:12 +01:00
|
|
|
devices = self.switches
|
2019-07-02 12:02:28 +02:00
|
|
|
if device:
|
2021-02-19 02:54:12 +01:00
|
|
|
devices = [d for d in self.switches if d.get('id') == device or d.get('name') == device]
|
2021-05-10 18:43:00 +02:00
|
|
|
return devices[0] if devices else []
|
2019-07-02 12:02:28 +02:00
|
|
|
|
|
|
|
return devices
|
|
|
|
|
2021-03-05 02:23:28 +01:00
|
|
|
@action
|
2021-05-10 18:43:00 +02:00
|
|
|
def status(self, device=None, *args, **kwargs) -> Union[dict, List[dict]]:
|
2021-03-05 02:23:28 +01:00
|
|
|
"""
|
2021-05-10 18:43:00 +02:00
|
|
|
Get the status of all the devices, or filter by device name or ID (alias for :meth:`.switch_status`).
|
|
|
|
|
|
|
|
:param device: Filter by device name or ID.
|
|
|
|
:return: .. schema:: switch.SwitchStatusSchema(many=True)
|
2021-03-05 02:23:28 +01:00
|
|
|
"""
|
|
|
|
return self.switch_status(device)
|
|
|
|
|
2019-07-02 12:02:28 +02:00
|
|
|
@property
|
2021-02-19 02:54:12 +01:00
|
|
|
def switches(self) -> List[dict]:
|
|
|
|
"""
|
2021-05-10 18:43:00 +02:00
|
|
|
:return: .. schema:: switch.SwitchStatusSchema(many=True)
|
2021-02-19 02:54:12 +01:00
|
|
|
"""
|
2017-11-04 00:13:22 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|