Update smartplug.py to support dimming in HS220 (#115)

* Update smartplug.py to support dimming in HS220

Switch functions essentially as a "plug" with the addition to support for dimming, for which can be test for by verifying existence of 'brightness' array value.

* Attempt at updates to pass validator

* Maybe this time?  :-)

* Add more detail to comment blocks

Make clear in requests for current brightness level the expected return values, and note that light will turn on when setting a brightness level, if not already on.  This makes clear that a state change request (turn_on) does NOT have to be made first when setting brightness.

* Update smartplug.py

* Update smartplug.py

Fixes #114
This commit is contained in:
JsChiSurf 2018-04-09 14:17:54 -05:00 committed by Teemu R
parent c766e3594b
commit 1aee353cbf

View File

@ -1,6 +1,6 @@
import datetime import datetime
import logging import logging
from typing import Any, Dict from typing import Any, Dict, Optional
from pyHS100 import SmartDevice from pyHS100 import SmartDevice
@ -80,6 +80,60 @@ class SmartPlug(SmartDevice):
else: else:
raise ValueError("State %s is not valid.", value) raise ValueError("State %s is not valid.", value)
@property
def brightness(self) -> Optional[int]:
"""
Current brightness of the device, if supported.
Will return a a range between 0 - 100.
:returns: integer
:rtype: int
"""
if not self.is_dimmable:
return None
return int(self.sys_info['brightness'])
@brightness.setter
def brightness(self, value: int):
"""
Set the new switch brightness level.
Note:
When setting brightness, if the light is not
already on, it will be turned on automatically.
:param value: integer between 1 and 100
"""
if not self.is_dimmable:
return None
if not isinstance(value, int):
raise ValueError("Brightness must be integer, "
"not of %s.", type(value))
elif value > 0 and value <= 100:
self.turn_on()
self._query_helper("smartlife.iot.dimmer", "set_brightness",
{"brightness": value})
else:
raise ValueError("Brightness value %s is not valid.", value)
@property
def is_dimmable(self):
"""
Whether the switch supports brightness changes
:return: True if switch supports brightness changes, False otherwise
:rtype: bool
"""
dimmable = False
if "brightness" in self.sys_info:
dimmable = True
return dimmable
@property @property
def has_emeter(self): def has_emeter(self):
""" """