implement SyncSmartDevice and sort imports and use absolute imports

This commit is contained in:
Bas Nijholt 2019-11-11 19:39:43 +01:00
parent 30677b2af0
commit 0c1d8b6efe
7 changed files with 56 additions and 29 deletions

View File

@ -1,21 +1,18 @@
"""pyHS100 cli tool."""
import asyncio
import sys
import click
import logging
import sys
from pprint import pformat as pf
import click
if sys.version_info < (3, 4):
print("To use this script you need python 3.4 or newer! got %s" % sys.version_info)
sys.exit(1)
from pyHS100 import (
SmartDevice,
SmartPlug,
SmartBulb,
SmartStrip,
Discover,
) # noqa: E402
from pyHS100 import SmartPlug # noqa: E402
from pyHS100 import Discover, SmartBulb, SmartDevice, SmartStrip
pass_dev = click.make_pass_decorator(SmartDevice)

View File

@ -1,16 +1,13 @@
import socket
import logging
import json
from typing import Dict, Type, Optional
import logging
import socket
from typing import Dict, Optional, Type
from pyHS100 import (
TPLinkSmartHomeProtocol,
SmartDevice,
SmartPlug,
SmartBulb,
SmartStrip,
SmartDeviceException,
)
from pyHS100.protocol import TPLinkSmartHomeProtocol
from pyHS100.smartbulb import SmartBulb
from pyHS100.smartdevice import SmartDevice, SmartDeviceException
from pyHS100.smartplug import SmartPlug
from pyHS100.smartstrip import SmartStrip
_LOGGER = logging.getLogger(__name__)

View File

@ -1,7 +1,7 @@
import asyncio
import json
import struct
import logging
import struct
from typing import Any, Dict, Union
_LOGGER = logging.getLogger(__name__)

View File

@ -1,8 +1,8 @@
from pyHS100 import DeviceType, SmartDevice, SmartDeviceException
from .protocol import TPLinkSmartHomeProtocol
import re
from typing import Any, Dict, Tuple
from pyHS100.protocol import TPLinkSmartHomeProtocol
from pyHS100.smartdevice import DeviceType, SmartDevice, SmartDeviceException
TPLINK_KELVIN = {
"LB130": (2500, 9000),

View File

@ -14,13 +14,15 @@ You may obtain a copy of the license at
http://www.apache.org/licenses/LICENSE-2.0
"""
import asyncio
import inspect
import functools
import logging
from collections import defaultdict
from datetime import datetime, timedelta
from enum import Enum
from typing import Any, Dict, Optional
from .protocol import TPLinkSmartHomeProtocol
from pyHS100.protocol import TPLinkSmartHomeProtocol
_LOGGER = logging.getLogger(__name__)
@ -116,6 +118,7 @@ class SmartDevice:
self.cache = defaultdict(lambda: defaultdict(lambda: None))
self._device_type = DeviceType.Unknown
self.ioloop = ioloop or asyncio.get_event_loop()
self.sync = SyncSmartDevice(self)
def _result_from_cache(self, target, cmd) -> Optional[Dict]:
"""Return query result from cache if still fresh.
@ -604,3 +607,29 @@ class SmartDevice:
asyncio.run(self.is_on()),
asyncio.run(self.get_state_information()),
)
class SyncSmartDevice:
"""A synchronous SmartDevice speaker class.
This has the same methods as `SyncSmartDevice`, however, it wraps all async
methods and call them in a blocking way.
Taken from https://github.com/basnijholt/media_player.kef/
"""
def __init__(self, async_device):
self.async_device = async_device
def __getattr__(self, attr):
method = getattr(self.async_device, attr)
if method is None:
raise AttributeError(f"'SyncSmartDevice' object has no attribute '{attr}.'")
if inspect.iscoroutinefunction(method):
@functools.wraps(method)
def wrapped(*args, **kwargs):
return asyncio.run(method(*args, **kwargs))
return wrapped
else:
return method

View File

@ -2,8 +2,12 @@ import datetime
import logging
from typing import Any, Dict
from pyHS100 import SmartDevice, DeviceType, SmartDeviceException
from .protocol import TPLinkSmartHomeProtocol
from pyHS100.protocol import TPLinkSmartHomeProtocol
from pyHS100.smartdevice import (
DeviceType,
SmartDevice,
SmartDeviceException,
)
_LOGGER = logging.getLogger(__name__)

View File

@ -3,9 +3,9 @@ import datetime
import logging
from typing import Any, Dict, Optional, Union
from pyHS100 import DeviceType, EmeterStatus, SmartDeviceException, SmartPlug
from .protocol import TPLinkSmartHomeProtocol
from pyHS100.protocol import TPLinkSmartHomeProtocol
from pyHS100.smartdevice import DeviceType, EmeterStatus, SmartDeviceException
from pyHS100.smartplug import SmartPlug
_LOGGER = logging.getLogger(__name__)