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: 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) inspector = schema_inspect(cls)
return tuple(inspector.mapper.column_attrs) 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(): def _discover_entity_types():
from platypush.context import get_plugin from platypush.context import get_plugin

View file

@ -13,3 +13,11 @@ class Switch(Device):
'polymorphic_identity': __tablename__, '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 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 import action
from platypush.plugins.switch import SwitchPlugin from platypush.plugins.switch import SwitchPlugin
@ -24,7 +32,8 @@ class SwitchTplinkPlugin(SwitchPlugin):
self, self,
plugs: Optional[Union[Mapping[str, str], List[str]]] = None, plugs: Optional[Union[Mapping[str, str], List[str]]] = None,
bulbs: 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 :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._alias_to_dev[info.get('name', dev.alias)] = dev
self._ip_to_dev[addr] = dev self._ip_to_dev[addr] = dev
except SmartDeviceException as e: 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(): for (ip, dev) in (devices or {}).items():
self._ip_to_dev[ip] = dev self._ip_to_dev[ip] = dev
@ -84,20 +95,23 @@ class SwitchTplinkPlugin(SwitchPlugin):
def transform_entities(self, devices: Collection[SmartDevice]): def transform_entities(self, devices: Collection[SmartDevice]):
from platypush.entities.switches import Switch from platypush.entities.switches import Switch
return super().transform_entities([ # type: ignore
Switch( return super().transform_entities( # type: ignore
id=dev.host, [
name=dev.alias, Switch(
state=dev.is_on, id=dev.host,
data={ name=dev.alias,
'current_consumption': dev.current_consumption(), state=dev.is_on,
'ip': dev.host, data={
'host': dev.host, 'current_consumption': dev.current_consumption(),
'hw_info': dev.hw_info, 'ip': dev.host,
} 'host': dev.host,
) 'hw_info': dev.hw_info,
for dev in (devices or []) },
]) )
for dev in (devices or [])
]
)
def _scan(self): def _scan(self):
devices = Discover.discover() devices = Discover.discover()
@ -108,6 +122,9 @@ class SwitchTplinkPlugin(SwitchPlugin):
if not use_cache: if not use_cache:
self._scan() self._scan()
if isinstance(device, Entity):
device = device.external_id or device.name
if device in self._ip_to_dev: if device in self._ip_to_dev:
return self._ip_to_dev[device] return self._ip_to_dev[device]
@ -123,7 +140,7 @@ class SwitchTplinkPlugin(SwitchPlugin):
action_name = 'turn_on' if state else 'turn_off' action_name = 'turn_on' if state else 'turn_off'
action = getattr(device, action_name) action = getattr(device, action_name)
action() action()
self.publish_entities([device]) # type: ignore self.publish_entities([device]) # type: ignore
return self._serialize(device) return self._serialize(device)
@action @action
@ -176,10 +193,7 @@ class SwitchTplinkPlugin(SwitchPlugin):
@property @property
def switches(self) -> List[dict]: def switches(self) -> List[dict]:
return [ return [self._serialize(dev) for dev in self._scan().values()]
self._serialize(dev)
for dev in self._scan().values()
]
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: