Added SmartThings switch web plugin

This commit is contained in:
Fabio Manganiello 2021-03-05 21:29:32 +01:00
parent 36aee6f787
commit d860d8aef1
2 changed files with 46 additions and 7 deletions

View file

@ -0,0 +1,26 @@
<template>
<div class="switches smartthings-switches">
<Loading v-if="loading" />
<div class="no-content" v-else-if="!Object.keys(devices).length">No switches found on SmartThings.</div>
<Switch :loading="loading" :name="name" :state="device.on" @toggle="toggle(name)"
v-for="(device, name) in devices" :key="name" :has-info="true"
@info="selectedDevice = name; $refs.switchInfoModal.show()" />
</div>
</template>
<script>
import Loading from "@/components/Loading";
import SwitchMixin from "@/components/panels/Switches/Mixin";
import Switch from "@/components/panels/Switches/Switch";
export default {
name: "Smartthings",
components: {Switch, Loading},
mixins: [SwitchMixin],
}
</script>
<style lang="scss" scoped>
@import "../common";
</style>

View file

@ -489,36 +489,43 @@ class SmartthingsPlugin(SwitchPlugin):
loop.stop() loop.stop()
@action @action
def on(self, device: str, *args, **kwargs): def on(self, device: str, *args, **kwargs) -> dict:
""" """
Turn on a device with ``switch`` capability. Turn on a device with ``switch`` capability.
:param device: Device name or ID. :param device: Device name or ID.
:return: Device status
""" """
return self.execute(device, 'switch', 'on') self.execute(device, 'switch', 'on')
# noinspection PyUnresolvedReferences
return self.status(device).output[0]
@action @action
def off(self, device: str, *args, **kwargs): def off(self, device: str, *args, **kwargs) -> dict:
""" """
Turn off a device with ``switch`` capability. Turn off a device with ``switch`` capability.
:param device: Device name or ID. :param device: Device name or ID.
:return: Device status
""" """
return self.execute(device, 'switch', 'off') self.execute(device, 'switch', 'off')
# noinspection PyUnresolvedReferences
return self.status(device).output[0]
@action @action
def toggle(self, device: str, *args, **kwargs): def toggle(self, device: str, *args, **kwargs) -> dict:
""" """
Toggle a device with ``switch`` capability. Toggle a device with ``switch`` capability.
:param device: Device name or ID. :param device: Device name or ID.
:return: Device status
""" """
import pysmartthings import pysmartthings
device = self._get_device(device) device = self._get_device(device)
device_id = device.device_id device_id = device.device_id
async def _toggle(): async def _toggle() -> bool:
async with aiohttp.ClientSession(timeout=self._timeout) as session: async with aiohttp.ClientSession(timeout=self._timeout) as session:
api = pysmartthings.SmartThings(session, self._access_token) api = pysmartthings.SmartThings(session, self._access_token)
dev = await api.device(device_id) dev = await api.device(device_id)
@ -529,10 +536,16 @@ class SmartthingsPlugin(SwitchPlugin):
ret = await dev.command(component_id='main', capability='switch', command=state, args=args) ret = await dev.command(component_id='main', capability='switch', command=state, args=args)
assert ret, 'The command switch={state} failed on device {device}'.format(state=state, device=dev.label) assert ret, 'The command switch={state} failed on device {device}'.format(state=state, device=dev.label)
return not dev.status.switch
with self._refresh_lock: with self._refresh_lock:
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
loop.run_until_complete(_toggle()) state = loop.run_until_complete(_toggle())
return {
'id': device_id,
'name': device.label,
'on': state,
}
@property @property
def switches(self) -> List[dict]: def switches(self) -> List[dict]: