Support for direct actions on native entities [WIP]

This commit is contained in:
Fabio Manganiello 2022-04-05 23:22:54 +02:00
parent 91ff47167b
commit 061268cdaf
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 51 additions and 22 deletions

View file

@ -55,6 +55,13 @@ class Entity(Base):
inspector = schema_inspect(cls)
return tuple(inspector.mapper.column_attrs)
def get_plugin(self):
from platypush.context import get_plugin
plugin = get_plugin(self.plugin)
assert plugin, f'No such plugin: {plugin}'
return plugin
def _discover_entity_types():
from platypush.context import get_plugin

View file

@ -13,3 +13,11 @@ class Switch(Device):
'polymorphic_identity': __tablename__,
}
def on(self):
return self.get_plugin().on(self)
def off(self):
return self.get_plugin().off(self)
def toggle(self):
return self.get_plugin().toggle(self)

View file

@ -1,7 +1,15 @@
from typing import Union, Mapping, List, Collection, Optional
from pyHS100 import SmartDevice, SmartPlug, SmartBulb, SmartStrip, Discover, SmartDeviceException
from pyHS100 import (
SmartDevice,
SmartPlug,
SmartBulb,
SmartStrip,
Discover,
SmartDeviceException,
)
from platypush.entities import Entity
from platypush.plugins import action
from platypush.plugins.switch import SwitchPlugin
@ -24,7 +32,8 @@ class SwitchTplinkPlugin(SwitchPlugin):
self,
plugs: Optional[Union[Mapping[str, str], List[str]]] = None,
bulbs: Optional[Union[Mapping[str, str], List[str]]] = None,
strips: Optional[Union[Mapping[str, str], List[str]]] = None, **kwargs
strips: Optional[Union[Mapping[str, str], List[str]]] = None,
**kwargs
):
"""
:param plugs: Optional list of IP addresses or name->address mapping if you have a static list of
@ -73,7 +82,9 @@ class SwitchTplinkPlugin(SwitchPlugin):
self._alias_to_dev[info.get('name', dev.alias)] = dev
self._ip_to_dev[addr] = dev
except SmartDeviceException as e:
self.logger.warning('Could not communicate with device {}: {}'.format(addr, str(e)))
self.logger.warning(
'Could not communicate with device {}: {}'.format(addr, str(e))
)
for (ip, dev) in (devices or {}).items():
self._ip_to_dev[ip] = dev
@ -84,7 +95,9 @@ class SwitchTplinkPlugin(SwitchPlugin):
def transform_entities(self, devices: Collection[SmartDevice]):
from platypush.entities.switches import Switch
return super().transform_entities([ # type: ignore
return super().transform_entities( # type: ignore
[
Switch(
id=dev.host,
name=dev.alias,
@ -94,10 +107,11 @@ class SwitchTplinkPlugin(SwitchPlugin):
'ip': dev.host,
'host': dev.host,
'hw_info': dev.hw_info,
}
},
)
for dev in (devices or [])
])
]
)
def _scan(self):
devices = Discover.discover()
@ -108,6 +122,9 @@ class SwitchTplinkPlugin(SwitchPlugin):
if not use_cache:
self._scan()
if isinstance(device, Entity):
device = device.external_id or device.name
if device in self._ip_to_dev:
return self._ip_to_dev[device]
@ -176,10 +193,7 @@ class SwitchTplinkPlugin(SwitchPlugin):
@property
def switches(self) -> List[dict]:
return [
self._serialize(dev)
for dev in self._scan().values()
]
return [self._serialize(dev) for dev in self._scan().values()]
# vim:sw=4:ts=4:et: