pass an ioloop

This commit is contained in:
Bas Nijholt 2019-11-11 19:16:55 +01:00
parent 7d3d105e47
commit 533c6b5ca2
4 changed files with 30 additions and 10 deletions

View File

@ -65,9 +65,16 @@ class SmartBulb(SmartDevice):
protocol: TPLinkSmartHomeProtocol = None,
context: str = None,
cache_ttl: int = 3,
*,
ioloop=None,
) -> None:
SmartDevice.__init__(
self, host=host, protocol=protocol, context=context, cache_ttl=cache_ttl
self,
host=host,
protocol=protocol,
context=context,
cache_ttl=cache_ttl,
ioloop=ioloop,
)
self.emeter_type = "smartlife.iot.common.emeter"
self._device_type = DeviceType.Bulb
@ -120,7 +127,9 @@ class SmartBulb(SmartDevice):
async def set_light_state(self, state: Dict) -> Dict:
"""Set the light state."""
return await self._query_helper(self.LIGHT_SERVICE, "transition_light_state", state)
return await self._query_helper(
self.LIGHT_SERVICE, "transition_light_state", state
)
async def get_hsv(self) -> Tuple[int, int, int]:
"""Return the current HSV state of the bulb.
@ -203,10 +212,7 @@ class SmartBulb(SmartDevice):
raise SmartDeviceException("Bulb does not support colortemp.")
valid_temperature_range = await self.get_valid_temperature_range()
if (
temp < valid_temperature_range[0]
or temp > valid_temperature_range[1]
):
if temp < valid_temperature_range[0] or temp > valid_temperature_range[1]:
raise ValueError(
"Temperature should be between {} "
"and {}".format(*valid_temperature_range)

View File

@ -91,6 +91,8 @@ class SmartDevice:
protocol: Optional[TPLinkSmartHomeProtocol] = None,
context: str = None,
cache_ttl: int = 3,
*,
ioloop=None,
) -> None:
"""Create a new SmartDevice instance.
@ -113,6 +115,7 @@ class SmartDevice:
)
self.cache = defaultdict(lambda: defaultdict(lambda: None))
self._device_type = DeviceType.Unknown
self.ioloop = ioloop or asyncio.get_event_loop()
def _result_from_cache(self, target, cmd) -> Optional[Dict]:
"""Return query result from cache if still fresh.
@ -402,7 +405,9 @@ class SmartDevice:
if "mac" in sys_info:
return str(sys_info["mac"])
elif "mic_mac" in sys_info:
return ":".join(format(s, "02x") for s in bytes.fromhex(sys_info["mic_mac"]))
return ":".join(
format(s, "02x") for s in bytes.fromhex(sys_info["mic_mac"])
)
raise SmartDeviceException(
"Unknown mac, please submit a bug report with sys_info output."

View File

@ -31,8 +31,10 @@ class SmartPlug(SmartDevice):
protocol: "TPLinkSmartHomeProtocol" = None,
context: str = None,
cache_ttl: int = 3,
*,
ioloop=None,
) -> None:
SmartDevice.__init__(self, host, protocol, context, cache_ttl)
SmartDevice.__init__(self, host, protocol, context, cache_ttl, ioloop=ioloop)
self.emeter_type = "emeter"
self._device_type = DeviceType.Plug

View File

@ -37,9 +37,16 @@ class SmartStrip(SmartPlug):
"""
def __init__(
self, host: str, protocol: TPLinkSmartHomeProtocol = None, cache_ttl: int = 3
self,
host: str,
protocol: TPLinkSmartHomeProtocol = None,
cache_ttl: int = 3,
*,
ioloop=None
) -> None:
SmartPlug.__init__(self, host=host, protocol=protocol, cache_ttl=cache_ttl)
SmartPlug.__init__(
self, host=host, protocol=protocol, cache_ttl=cache_ttl, ioloop=ioloop
)
self.emeter_type = "emeter"
self._device_type = DeviceType.Strip
self.plugs = {}