smartthings.toggle should properly publish the updated entity

This commit is contained in:
Fabio Manganiello 2022-04-11 00:42:14 +02:00
parent f17245e8c7
commit 4471001110
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -413,17 +413,16 @@ class SmartthingsPlugin(SwitchPlugin):
finally: finally:
loop.stop() loop.stop()
async def _get_device_status(self, api, device_id: str) -> dict: def transform_entities(self, entities):
from platypush.entities.switches import Switch from platypush.entities.switches import Switch
device = await api.device(device_id) compatible_entities = []
await device.status.refresh()
for device in entities:
if 'switch' in device.capabilities: if 'switch' in device.capabilities:
self.publish_entities( # type: ignore compatible_entities.append(
[
Switch( Switch(
id=device_id, id=device.device_id,
name=device.label, name=device.label,
state=device.status.switch, state=device.status.switch,
data={ data={
@ -431,9 +430,15 @@ class SmartthingsPlugin(SwitchPlugin):
'room_id': getattr(device, 'room_id', None), 'room_id': getattr(device, 'room_id', None),
}, },
) )
]
) )
return super().transform_entities(compatible_entities) # type: ignore
async def _get_device_status(self, api, device_id: str) -> dict:
device = await api.device(device_id)
await device.status.refresh()
self.publish_entities([device]) # type: ignore
return { return {
'device_id': device_id, 'device_id': device_id,
'name': device.label, 'name': device.label,
@ -526,7 +531,7 @@ class SmartthingsPlugin(SwitchPlugin):
loop.stop() loop.stop()
@action @action
def on(self, device: str, *args, **kwargs) -> dict: def on(self, device: str, *_, **__) -> dict:
""" """
Turn on a device with ``switch`` capability. Turn on a device with ``switch`` capability.
@ -534,11 +539,10 @@ class SmartthingsPlugin(SwitchPlugin):
:return: Device status :return: Device status
""" """
self.execute(device, 'switch', 'on') self.execute(device, 'switch', 'on')
# noinspection PyUnresolvedReferences return self.status(device).output[0] # type: ignore
return self.status(device).output[0]
@action @action
def off(self, device: str, *args, **kwargs) -> dict: def off(self, device: str, *_, **__) -> dict:
""" """
Turn off a device with ``switch`` capability. Turn off a device with ``switch`` capability.
@ -546,11 +550,10 @@ class SmartthingsPlugin(SwitchPlugin):
:return: Device status :return: Device status
""" """
self.execute(device, 'switch', 'off') self.execute(device, 'switch', 'off')
# noinspection PyUnresolvedReferences return self.status(device).output[0] # type: ignore
return self.status(device).output[0]
@action @action
def toggle(self, device: str, *args, **kwargs) -> dict: def toggle(self, device: str, *args, **__) -> dict:
""" """
Toggle a device with ``switch`` capability. Toggle a device with ``switch`` capability.
@ -584,6 +587,8 @@ class SmartthingsPlugin(SwitchPlugin):
with self._refresh_lock: with self._refresh_lock:
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
state = loop.run_until_complete(_toggle()) state = loop.run_until_complete(_toggle())
device.status.switch = state
self.publish_entities([device]) # type: ignore
return { return {
'id': device_id, 'id': device_id,
'name': device.label, 'name': device.label,