forked from platypush/platypush
Support for direct actions on native entities [WIP]
This commit is contained in:
parent
91ff47167b
commit
061268cdaf
3 changed files with 51 additions and 22 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,20 +95,23 @@ class SwitchTplinkPlugin(SwitchPlugin):
|
|||
|
||||
def transform_entities(self, devices: Collection[SmartDevice]):
|
||||
from platypush.entities.switches import Switch
|
||||
return super().transform_entities([ # type: ignore
|
||||
Switch(
|
||||
id=dev.host,
|
||||
name=dev.alias,
|
||||
state=dev.is_on,
|
||||
data={
|
||||
'current_consumption': dev.current_consumption(),
|
||||
'ip': dev.host,
|
||||
'host': dev.host,
|
||||
'hw_info': dev.hw_info,
|
||||
}
|
||||
)
|
||||
for dev in (devices or [])
|
||||
])
|
||||
|
||||
return super().transform_entities( # type: ignore
|
||||
[
|
||||
Switch(
|
||||
id=dev.host,
|
||||
name=dev.alias,
|
||||
state=dev.is_on,
|
||||
data={
|
||||
'current_consumption': dev.current_consumption(),
|
||||
'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]
|
||||
|
||||
|
@ -123,7 +140,7 @@ class SwitchTplinkPlugin(SwitchPlugin):
|
|||
action_name = 'turn_on' if state else 'turn_off'
|
||||
action = getattr(device, action_name)
|
||||
action()
|
||||
self.publish_entities([device]) # type: ignore
|
||||
self.publish_entities([device]) # type: ignore
|
||||
return self._serialize(device)
|
||||
|
||||
@action
|
||||
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue