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
This commit is contained in:
Fabio Manganiello 2023-01-22 01:01:47 +01:00
parent ddd516a677
commit 6d4cf64253
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -8,8 +8,7 @@ import pysmartthings
from platypush.entities import Entity, manages 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.devices import Device
from platypush.entities.dimmers import Dimmer from platypush.entities.dimmers import Dimmer
from platypush.entities.lights import Light from platypush.entities.lights import Light
@ -488,7 +487,7 @@ class SmartthingsPlugin(RunnablePlugin):
if getattr(device.status, 'saturation', None) is not None: if getattr(device.status, 'saturation', None) is not None:
light_attrs['saturation'] = device.status.saturation light_attrs['saturation'] = device.status.saturation
light_attrs['saturation_min'] = 0 light_attrs['saturation_min'] = 0
light_attrs['saturation_max'] = 80 light_attrs['saturation_max'] = 100
return [cls._to_entity(Light, device, **light_attrs)] return [cls._to_entity(Light, device, **light_attrs)]
@ -506,12 +505,15 @@ class SmartthingsPlugin(RunnablePlugin):
] ]
@classmethod @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): if 'switchLevel' not in cls._get_capabilities(device):
return [] return []
kwargs = cls._get_status_attr_info(device, 'level')
return [ 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 @classmethod
@ -519,16 +521,43 @@ class SmartthingsPlugin(RunnablePlugin):
sensors = [] sensors = []
if 'motionSensor' in cls._get_capabilities(device): if 'motionSensor' in cls._get_capabilities(device):
kwargs = cls._get_status_attr_info(device, 'motion')
sensors.append( sensors.append(
cls._to_entity( cls._to_entity(
MotionSensor, MotionSensor,
device, device,
value=device.status.motion, 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 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): def transform_entities(self, entities):
compatible_entities = [] compatible_entities = []