mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-23 03:33:35 +00:00
Cleanup constructors by removing ioloop and protocol arguments (#23)
* Cleanup constructors by removing ioloop and protocol * force kwarg for other arguments besides the host
This commit is contained in:
parent
7eea21f2dd
commit
72d6205ad2
@ -2,7 +2,6 @@
|
|||||||
import re
|
import re
|
||||||
from typing import Any, Dict, Optional, Tuple
|
from typing import Any, Dict, Optional, Tuple
|
||||||
|
|
||||||
from kasa.protocol import TPLinkSmartHomeProtocol
|
|
||||||
from kasa.smartdevice import (
|
from kasa.smartdevice import (
|
||||||
DeviceType,
|
DeviceType,
|
||||||
SmartDevice,
|
SmartDevice,
|
||||||
@ -72,23 +71,8 @@ class SmartBulb(SmartDevice):
|
|||||||
|
|
||||||
LIGHT_SERVICE = "smartlife.iot.smartbulb.lightingservice"
|
LIGHT_SERVICE = "smartlife.iot.smartbulb.lightingservice"
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, host: str, *, child_id: str = None, cache_ttl: int = 3) -> None:
|
||||||
self,
|
SmartDevice.__init__(self, host=host, child_id=child_id, cache_ttl=cache_ttl)
|
||||||
host: str,
|
|
||||||
protocol: TPLinkSmartHomeProtocol = None,
|
|
||||||
child_id: str = None,
|
|
||||||
cache_ttl: int = 3,
|
|
||||||
*,
|
|
||||||
ioloop=None,
|
|
||||||
) -> None:
|
|
||||||
SmartDevice.__init__(
|
|
||||||
self,
|
|
||||||
host=host,
|
|
||||||
protocol=protocol,
|
|
||||||
child_id=child_id,
|
|
||||||
cache_ttl=cache_ttl,
|
|
||||||
ioloop=ioloop,
|
|
||||||
)
|
|
||||||
self.emeter_type = "smartlife.iot.common.emeter"
|
self.emeter_type = "smartlife.iot.common.emeter"
|
||||||
self._device_type = DeviceType.Bulb
|
self._device_type = DeviceType.Bulb
|
||||||
self._light_state = None
|
self._light_state = None
|
||||||
|
@ -11,7 +11,6 @@ Stroetmann which is licensed under the Apache License, Version 2.0.
|
|||||||
You may obtain a copy of the license at
|
You may obtain a copy of the license at
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
"""
|
"""
|
||||||
import asyncio
|
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
@ -104,27 +103,15 @@ def requires_update(f):
|
|||||||
class SmartDevice:
|
class SmartDevice:
|
||||||
"""Base class for all supported device types."""
|
"""Base class for all supported device types."""
|
||||||
|
|
||||||
STATE_ON = "ON"
|
def __init__(self, host: str, *, child_id: str = None, cache_ttl: int = 3) -> None:
|
||||||
STATE_OFF = "OFF"
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
host: str,
|
|
||||||
protocol: Optional[TPLinkSmartHomeProtocol] = None,
|
|
||||||
child_id: str = None,
|
|
||||||
cache_ttl: int = 3,
|
|
||||||
*,
|
|
||||||
ioloop=None,
|
|
||||||
) -> None:
|
|
||||||
"""Create a new SmartDevice instance.
|
"""Create a new SmartDevice instance.
|
||||||
|
|
||||||
:param str host: host name or ip address on which the device listens
|
:param str host: host name or ip address on which the device listens
|
||||||
:param child_id: optional child ID for context in a parent device
|
:param child_id: optional child ID for context in a parent device
|
||||||
"""
|
"""
|
||||||
self.host = host
|
self.host = host
|
||||||
if protocol is None: # pragma: no cover
|
|
||||||
protocol = TPLinkSmartHomeProtocol()
|
self.protocol = TPLinkSmartHomeProtocol()
|
||||||
self.protocol = protocol
|
|
||||||
self.emeter_type = "emeter"
|
self.emeter_type = "emeter"
|
||||||
self.child_id = child_id
|
self.child_id = child_id
|
||||||
self.cache_ttl = timedelta(seconds=cache_ttl)
|
self.cache_ttl = timedelta(seconds=cache_ttl)
|
||||||
@ -136,7 +123,6 @@ class SmartDevice:
|
|||||||
)
|
)
|
||||||
self.cache = defaultdict(lambda: defaultdict(lambda: None)) # type: ignore
|
self.cache = defaultdict(lambda: defaultdict(lambda: None)) # type: ignore
|
||||||
self._device_type = DeviceType.Unknown
|
self._device_type = DeviceType.Unknown
|
||||||
self.ioloop = ioloop or asyncio.get_event_loop()
|
|
||||||
self._sys_info = None
|
self._sys_info = None
|
||||||
|
|
||||||
def _result_from_cache(self, target, cmd) -> Optional[Dict]:
|
def _result_from_cache(self, target, cmd) -> Optional[Dict]:
|
||||||
|
@ -3,7 +3,6 @@ import datetime
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
from kasa.protocol import TPLinkSmartHomeProtocol
|
|
||||||
from kasa.smartdevice import (
|
from kasa.smartdevice import (
|
||||||
DeviceType,
|
DeviceType,
|
||||||
SmartDevice,
|
SmartDevice,
|
||||||
@ -36,16 +35,8 @@ class SmartPlug(SmartDevice):
|
|||||||
and should be handled by the user of the library.
|
and should be handled by the user of the library.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, host: str, *, child_id: str = None, cache_ttl: int = 3) -> None:
|
||||||
self,
|
SmartDevice.__init__(self, host, child_id=child_id, cache_ttl=cache_ttl)
|
||||||
host: str,
|
|
||||||
protocol: "TPLinkSmartHomeProtocol" = None,
|
|
||||||
child_id: str = None,
|
|
||||||
cache_ttl: int = 3,
|
|
||||||
*,
|
|
||||||
ioloop=None,
|
|
||||||
) -> None:
|
|
||||||
SmartDevice.__init__(self, host, protocol, child_id, cache_ttl, ioloop=ioloop)
|
|
||||||
self.emeter_type = "emeter"
|
self.emeter_type = "emeter"
|
||||||
self._device_type = DeviceType.Plug
|
self._device_type = DeviceType.Plug
|
||||||
|
|
||||||
|
@ -7,7 +7,6 @@ import logging
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from typing import Any, DefaultDict, Dict, List
|
from typing import Any, DefaultDict, Dict, List
|
||||||
|
|
||||||
from kasa.protocol import TPLinkSmartHomeProtocol
|
|
||||||
from kasa.smartdevice import DeviceType, requires_update
|
from kasa.smartdevice import DeviceType, requires_update
|
||||||
from kasa.smartplug import SmartPlug
|
from kasa.smartplug import SmartPlug
|
||||||
|
|
||||||
@ -41,14 +40,8 @@ class SmartStrip(SmartPlug):
|
|||||||
and should be handled by the user of the library.
|
and should be handled by the user of the library.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, host: str, *, cache_ttl: int = 3) -> None:
|
||||||
self,
|
SmartPlug.__init__(self, host=host, cache_ttl=cache_ttl)
|
||||||
host: str,
|
|
||||||
protocol: TPLinkSmartHomeProtocol = None,
|
|
||||||
cache_ttl: int = 3,
|
|
||||||
ioloop=None,
|
|
||||||
) -> None:
|
|
||||||
SmartPlug.__init__(self, host=host, protocol=protocol, cache_ttl=cache_ttl)
|
|
||||||
self.emeter_type = "emeter"
|
self.emeter_type = "emeter"
|
||||||
self._device_type = DeviceType.Strip
|
self._device_type = DeviceType.Strip
|
||||||
self.plugs: List[SmartPlug] = []
|
self.plugs: List[SmartPlug] = []
|
||||||
@ -78,16 +71,11 @@ class SmartStrip(SmartPlug):
|
|||||||
self.plugs.append(
|
self.plugs.append(
|
||||||
SmartPlug(
|
SmartPlug(
|
||||||
self.host,
|
self.host,
|
||||||
self.protocol,
|
|
||||||
child_id=child["id"],
|
child_id=child["id"],
|
||||||
cache_ttl=self.cache_ttl.total_seconds(),
|
cache_ttl=self.cache_ttl.total_seconds(),
|
||||||
ioloop=self.ioloop,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
for plug in self.plugs:
|
|
||||||
await plug.update()
|
|
||||||
|
|
||||||
async def turn_on(self):
|
async def turn_on(self):
|
||||||
"""Turn the strip on.
|
"""Turn the strip on.
|
||||||
|
|
||||||
|
@ -37,12 +37,6 @@ def filter_model(desc, filter):
|
|||||||
return filtered
|
return filtered
|
||||||
|
|
||||||
|
|
||||||
def get_ioloop():
|
|
||||||
ioloop = asyncio.new_event_loop()
|
|
||||||
asyncio.set_event_loop(ioloop)
|
|
||||||
return ioloop
|
|
||||||
|
|
||||||
|
|
||||||
has_emeter = pytest.mark.parametrize(
|
has_emeter = pytest.mark.parametrize(
|
||||||
"dev", filter_model("has emeter", EMETER), indirect=True
|
"dev", filter_model("has emeter", EMETER), indirect=True
|
||||||
)
|
)
|
||||||
@ -94,13 +88,13 @@ def dev(request):
|
|||||||
Provides a device (given --ip) or parametrized fixture for the supported devices.
|
Provides a device (given --ip) or parametrized fixture for the supported devices.
|
||||||
The initial update is called automatically before returning the device.
|
The initial update is called automatically before returning the device.
|
||||||
"""
|
"""
|
||||||
ioloop = get_ioloop()
|
loop = asyncio.get_event_loop()
|
||||||
file = request.param
|
file = request.param
|
||||||
|
|
||||||
ip = request.config.getoption("--ip")
|
ip = request.config.getoption("--ip")
|
||||||
if ip:
|
if ip:
|
||||||
d = ioloop.run_until_complete(Discover.discover_single(ip))
|
d = loop.run_until_complete(Discover.discover_single(ip))
|
||||||
ioloop.run_until_complete(d.update())
|
loop.run_until_complete(d.update())
|
||||||
print(d.model)
|
print(d.model)
|
||||||
if d.model in file:
|
if d.model in file:
|
||||||
return d
|
return d
|
||||||
@ -122,13 +116,10 @@ def dev(request):
|
|||||||
with open(file) as f:
|
with open(file) as f:
|
||||||
sysinfo = json.load(f)
|
sysinfo = json.load(f)
|
||||||
model = basename(file)
|
model = basename(file)
|
||||||
params = {
|
params = {"host": "123.123.123.123", "cache_ttl": 0}
|
||||||
"host": "123.123.123.123",
|
p = device_for_file(model)(**params)
|
||||||
"protocol": FakeTransportProtocol(sysinfo),
|
p.protocol = FakeTransportProtocol(sysinfo)
|
||||||
"cache_ttl": 0,
|
loop.run_until_complete(p.update())
|
||||||
}
|
|
||||||
p = device_for_file(model)(**params, ioloop=ioloop)
|
|
||||||
ioloop.run_until_complete(p.update())
|
|
||||||
yield p
|
yield p
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user