Skip to content

Commit

Permalink
Merge pull request #61 from rytilahti/master
Browse files Browse the repository at this point in the history
Slight refactoring
  • Loading branch information
kirichkov committed May 27, 2017
2 parents 93d2428 + 7457ad4 commit b7a7fdb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 29 deletions.
17 changes: 16 additions & 1 deletion pyHS100/__init__.py
@@ -1,5 +1,20 @@
"""
This module provides a way to interface with TP-Link's smart home devices,
such as smart plugs (HS1xx), wall switches (HS2xx), and light bulbs (LB1xx).
All common, shared functionalities are available through `SmartDevice` class::
x = SmartDevice("192.168.1.1")
print(x.sys_info)
For device type specific actions `SmartBulb` or `SmartPlug` must be used instead.
Module-specific errors are raised as `SmartDeviceException` and are expected
to be handled by the user of the library.
"""
# flake8: noqa
from .smartplug import SmartPlug
from .pyHS100 import SmartPlugException, SmartDevice
from .pyHS100 import SmartDevice
from .types import SmartDeviceException
from .smartbulb import SmartBulb
from .protocol import TPLinkSmartHomeProtocol
26 changes: 6 additions & 20 deletions pyHS100/pyHS100.py
Expand Up @@ -16,27 +16,13 @@
import datetime
import logging
import socket
import enum

from .types import SmartDeviceException
from .protocol import TPLinkSmartHomeProtocol

_LOGGER = logging.getLogger(__name__)


class SmartPlugException(Exception):
"""
SmartPlugException gets raised for errors reported by the plug.
"""
pass


class DeviceType(enum.Enum):
Unknown = -1,
Plug = 0,
Switch = 1
Bulb = 2


class SmartDevice(object):
# possible device features
FEATURE_ENERGY_METER = 'ENE'
Expand Down Expand Up @@ -76,16 +62,16 @@ def _query_helper(self, target, cmd, arg=None):
request={target: {cmd: arg}}
)
except Exception as ex:
raise SmartPlugException('Communication error') from ex
raise SmartDeviceException('Communication error') from ex

if target not in response:
raise SmartPlugException("No required {} in response: {}"
.format(target, response))
raise SmartDeviceException("No required {} in response: {}"
.format(target, response))

result = response[target]
if "err_code" in result and result["err_code"] != 0:
raise SmartPlugException("Error on {}.{}: {}"
.format(target, cmd, result))
raise SmartDeviceException("Error on {}.{}: {}"
.format(target, cmd, result))

result = result[cmd]
del result["err_code"]
Expand Down
4 changes: 2 additions & 2 deletions pyHS100/tests/fakes.py
@@ -1,5 +1,5 @@
from ..protocol import TPLinkSmartHomeProtocol
from .. import SmartPlugException
from .. import SmartDeviceException
import logging


Expand Down Expand Up @@ -250,7 +250,7 @@ def transition_light_state(self, x):

def query(self, host, request, port=9999):
if self.invalid:
raise SmartPlugException("Invalid connection, can't query!")
raise SmartDeviceException("Invalid connection, can't query!")

proto = self.proto

Expand Down
6 changes: 3 additions & 3 deletions pyHS100/tests/test_bulb.py
Expand Up @@ -2,7 +2,7 @@
from voluptuous import Schema, Invalid, All, Range
from functools import partial

from .. import SmartBulb, SmartPlugException
from .. import SmartBulb, SmartDeviceException
from .fakes import FakeTransportProtocol, sysinfo_lb130

BULB_IP = '192.168.250.186'
Expand Down Expand Up @@ -93,11 +93,11 @@ def test_initialize_invalid_connection(self):
bulb = SmartBulb('127.0.0.1',
protocol=FakeTransportProtocol(sysinfo_lb130,
invalid=True))
with self.assertRaises(SmartPlugException):
with self.assertRaises(SmartDeviceException):
bulb.sys_info['model']

def test_query_helper(self):
with self.assertRaises(SmartPlugException):
with self.assertRaises(SmartDeviceException):
self.bulb._query_helper("test", "testcmd", {})
# TODO check for unwrapping?

Expand Down
6 changes: 3 additions & 3 deletions pyHS100/tests/test_pyHS100.py
Expand Up @@ -4,7 +4,7 @@
import datetime
import re

from .. import SmartPlug, SmartPlugException
from .. import SmartPlug, SmartDeviceException
from .fakes import FakeTransportProtocol, sysinfo_hs110, sysinfo_hs105

PLUG_IP = '192.168.250.186'
Expand Down Expand Up @@ -87,11 +87,11 @@ def test_initialize_invalid_connection(self):
plug = SmartPlug('127.0.0.1',
protocol=FakeTransportProtocol(sysinfo_hs110,
invalid=True))
with self.assertRaises(SmartPlugException):
with self.assertRaises(SmartDeviceException):
plug.sys_info['model']

def test_query_helper(self):
with self.assertRaises(SmartPlugException):
with self.assertRaises(SmartDeviceException):
self.plug._query_helper("test", "testcmd", {})
# TODO check for unwrapping?

Expand Down
15 changes: 15 additions & 0 deletions pyHS100/types.py
@@ -0,0 +1,15 @@
import enum


class SmartDeviceException(Exception):
"""
SmartPlugException gets raised for errors reported by the plug.
"""
pass


class DeviceType(enum.Enum):
Unknown = -1,
Plug = 0,
Switch = 1
Bulb = 2

0 comments on commit b7a7fdb

Please sign in to comment.