From 6d4cf642532c197c911b3bf19229208c3981cd2c Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 22 Jan 2023 01:01:47 +0100 Subject: [PATCH] More work on `smartthings`. - Added support for `Battery` entities - Fixed saturation range for `Light` entities - Parsing `min`/`max`/`unit` from the status attributes, if available --- platypush/plugins/smartthings/__init__.py | 39 ++++++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/platypush/plugins/smartthings/__init__.py b/platypush/plugins/smartthings/__init__.py index 873e36f219..c88f278308 100644 --- a/platypush/plugins/smartthings/__init__.py +++ b/platypush/plugins/smartthings/__init__.py @@ -8,8 +8,7 @@ import pysmartthings from platypush.entities import Entity, manages -# TODO Check battery support -# from platypush.entities.batteries import Battery +from platypush.entities.batteries import Battery from platypush.entities.devices import Device from platypush.entities.dimmers import Dimmer from platypush.entities.lights import Light @@ -488,7 +487,7 @@ class SmartthingsPlugin(RunnablePlugin): if getattr(device.status, 'saturation', None) is not None: light_attrs['saturation'] = device.status.saturation light_attrs['saturation_min'] = 0 - light_attrs['saturation_max'] = 80 + light_attrs['saturation_max'] = 100 return [cls._to_entity(Light, device, **light_attrs)] @@ -506,12 +505,15 @@ class SmartthingsPlugin(RunnablePlugin): ] @classmethod - def _get_dimmers(cls, device) -> Iterable[Dimmer]: + def _get_dimmers(cls, device: pysmartthings.Device) -> Iterable[Dimmer]: if 'switchLevel' not in cls._get_capabilities(device): return [] + kwargs = cls._get_status_attr_info(device, 'level') return [ - cls._to_entity(Dimmer, device, value=device.status.level, min=0, max=100) + cls._to_entity( + Dimmer, device, value=device.status.level, min=0, max=100, **kwargs + ) ] @classmethod @@ -519,16 +521,43 @@ class SmartthingsPlugin(RunnablePlugin): sensors = [] if 'motionSensor' in cls._get_capabilities(device): + kwargs = cls._get_status_attr_info(device, 'motion') sensors.append( cls._to_entity( MotionSensor, device, value=device.status.motion, + **kwargs, + ) + ) + + if 'battery' in cls._get_capabilities(device): + kwargs = cls._get_status_attr_info(device, 'battery') + sensors.append( + cls._to_entity( + Battery, + device, + value=device.status.attributes['battery'].value, + **kwargs, ) ) return sensors + @staticmethod + def _get_status_attr_info(device: pysmartthings.Device, attr: str) -> dict: + status = device.status.attributes.get(attr) + info = {} + if status: + if getattr(status, 'unit', None) is not None: + info['unit'] = status.unit + if getattr(status, 'min', None) is not None: + info['min'] = status.min + if getattr(status, 'max', None) is not None: + info['max'] = status.max + + return info + def transform_entities(self, entities): compatible_entities = []