Added entities engine support to WeMo switch plugin

This commit is contained in:
Fabio Manganiello 2022-04-04 17:22:31 +02:00
parent 7459f0115b
commit 53da19b638
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -1,9 +1,11 @@
import contextlib
import ipaddress import ipaddress
from typing import List from typing import List, Optional
from platypush.plugins import action from platypush.plugins import action
from platypush.plugins.switch import SwitchPlugin from platypush.plugins.switch import SwitchPlugin
from platypush.utils.workers import Workers from platypush.utils.workers import Workers
from .lib import WemoRunner from .lib import WemoRunner
from .scanner import Scanner from .scanner import Scanner
@ -16,7 +18,13 @@ class SwitchWemoPlugin(SwitchPlugin):
_default_port = 49153 _default_port = 49153
def __init__(self, devices=None, netmask: str = None, port: int = _default_port, **kwargs): def __init__(
self,
devices=None,
netmask: Optional[str] = None,
port: int = _default_port,
**kwargs
):
""" """
:param devices: List of IP addresses or name->address map containing the WeMo Switch devices to control. :param devices: List of IP addresses or name->address map containing the WeMo Switch devices to control.
This plugin previously used ouimeaux for auto-discovery but it's been dropped because This plugin previously used ouimeaux for auto-discovery but it's been dropped because
@ -37,8 +45,11 @@ class SwitchWemoPlugin(SwitchPlugin):
def _init_devices(self, devices): def _init_devices(self, devices):
if devices: if devices:
self._devices.update(devices if isinstance(devices, dict) else self._devices.update(
{addr: addr for addr in devices}) devices
if isinstance(devices, dict)
else {addr: addr for addr in devices}
)
else: else:
self._devices = {} self._devices = {}
@ -68,37 +79,53 @@ class SwitchWemoPlugin(SwitchPlugin):
""" """
return [ return [
self.status(device).output self.status(device).output # type: ignore
for device in self._devices.values() for device in self._devices.values()
] ]
def _get_address(self, device: str) -> str: def _get_address(self, device: str) -> str:
if device not in self._addresses: if device not in self._addresses:
try: with contextlib.suppress(KeyError):
return self._devices[device] return self._devices[device]
except KeyError:
pass
return device return device
@action @action
def status(self, device: str = None, *args, **kwargs): def status(self, device: Optional[str] = None, *_, **__):
devices = {device: device} if device else self._devices.copy() devices = {device: device} if device else self._devices.copy()
ret = [ ret = [
{ {
'id': addr, "id": addr,
'ip': addr, "ip": addr,
'name': name if name != addr else WemoRunner.get_name(addr), "name": name if name != addr else WemoRunner.get_name(addr),
'on': WemoRunner.get_state(addr), "on": WemoRunner.get_state(addr),
} }
for (name, addr) in devices.items() for (name, addr) in devices.items()
] ]
self.publish_entities(ret) # type: ignore
return ret[0] if device else ret return ret[0] if device else ret
def transform_entities(self, devices: List[dict]):
from platypush.entities.switches import Switch
return super().transform_entities( # type: ignore
[
Switch(
id=dev["id"],
name=dev["name"],
state=dev["on"],
data={
"ip": dev["ip"],
},
)
for dev in (devices or [])
]
)
@action @action
def on(self, device: str, **kwargs): def on(self, device: str, **_):
""" """
Turn a switch on Turn a switch on
@ -109,7 +136,7 @@ class SwitchWemoPlugin(SwitchPlugin):
return self.status(device) return self.status(device)
@action @action
def off(self, device: str, **kwargs): def off(self, device: str, **_):
""" """
Turn a switch off Turn a switch off
@ -120,7 +147,7 @@ class SwitchWemoPlugin(SwitchPlugin):
return self.status(device) return self.status(device)
@action @action
def toggle(self, device: str, *args, **kwargs): def toggle(self, device: str, *_, **__):
""" """
Toggle a device on/off state Toggle a device on/off state
@ -151,19 +178,16 @@ class SwitchWemoPlugin(SwitchPlugin):
return WemoRunner.get_name(device) return WemoRunner.get_name(device)
@action @action
def scan(self, netmask: str = None): def scan(self, netmask: Optional[str] = None):
netmask = netmask or self.netmask netmask = netmask or self.netmask
assert netmask, 'Scan not supported: No netmask specified' assert netmask, "Scan not supported: No netmask specified"
workers = Workers(10, Scanner, port=self.port) workers = Workers(10, Scanner, port=self.port)
with workers: with workers:
for addr in ipaddress.IPv4Network(netmask): for addr in ipaddress.IPv4Network(netmask):
workers.put(addr.exploded) workers.put(addr.exploded)
devices = { devices = {dev.name: dev.addr for dev in workers.responses}
dev.name: dev.addr
for dev in workers.responses
}
self._init_devices(devices) self._init_devices(devices)
return self.status() return self.status()