2017-10-07 15:41:51 +00:00
|
|
|
from unittest import TestCase, skip
|
2017-10-07 15:36:49 +00:00
|
|
|
from voluptuous import Schema, Invalid, All, Any, Range, Coerce
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
from functools import partial
|
|
|
|
import datetime
|
|
|
|
import re
|
2017-09-18 16:13:06 +00:00
|
|
|
from typing import Dict # noqa: F401
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
2017-05-26 14:11:03 +00:00
|
|
|
from .. import SmartPlug, SmartDeviceException
|
2017-08-05 15:22:36 +00:00
|
|
|
from .fakes import (FakeTransportProtocol,
|
2017-08-05 15:27:06 +00:00
|
|
|
sysinfo_hs100,
|
2017-08-05 15:22:36 +00:00
|
|
|
sysinfo_hs105,
|
2017-08-05 15:27:06 +00:00
|
|
|
sysinfo_hs110,
|
2018-06-16 19:16:35 +00:00
|
|
|
sysinfo_hs110_au_v2,
|
2018-10-18 17:57:44 +00:00
|
|
|
sysinfo_hs200,
|
|
|
|
sysinfo_hs220,
|
|
|
|
)
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
2017-10-07 15:36:49 +00:00
|
|
|
# Set IP instead of None if you want to run tests on a device.
|
|
|
|
PLUG_IP = None
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_int_bool(x):
|
|
|
|
if x != 0 and x != 1:
|
|
|
|
raise Invalid(x)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
def check_mac(x):
|
|
|
|
if re.match("[0-9a-f]{2}([-:])[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", x.lower()):
|
|
|
|
return x
|
|
|
|
raise Invalid(x)
|
|
|
|
|
|
|
|
|
|
|
|
def check_mode(x):
|
2017-08-05 15:22:36 +00:00
|
|
|
if x in ['schedule', 'none']:
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
return x
|
|
|
|
|
|
|
|
raise Invalid("invalid mode {}".format(x))
|
|
|
|
|
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
class TestSmartPlugHS100(TestCase):
|
|
|
|
SYSINFO = sysinfo_hs100 # type: Dict
|
2017-01-07 22:45:47 +00:00
|
|
|
# these schemas should go to the mainlib as
|
|
|
|
# they can be useful when adding support for new features/devices
|
|
|
|
# as well as to check that faked devices are operating properly.
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
sysinfo_schema = Schema({
|
|
|
|
'active_mode': check_mode,
|
2017-04-14 12:24:58 +00:00
|
|
|
'alias': str,
|
|
|
|
'dev_name': str,
|
|
|
|
'deviceId': str,
|
|
|
|
'feature': str,
|
|
|
|
'fwId': str,
|
|
|
|
'hwId': str,
|
|
|
|
'hw_ver': str,
|
|
|
|
'icon_hash': str,
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
'led_off': check_int_bool,
|
2017-08-05 15:22:36 +00:00
|
|
|
'latitude': Any(All(float, Range(min=-90, max=90)), None),
|
|
|
|
'latitude_i': Any(All(float, Range(min=-90, max=90)), None),
|
|
|
|
'longitude': Any(All(float, Range(min=-180, max=180)), None),
|
|
|
|
'longitude_i': Any(All(float, Range(min=-180, max=180)), None),
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
'mac': check_mac,
|
2017-04-14 12:24:58 +00:00
|
|
|
'model': str,
|
|
|
|
'oemId': str,
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
'on_time': int,
|
|
|
|
'relay_state': int,
|
2017-08-05 15:33:08 +00:00
|
|
|
'rssi': Any(int, None), # rssi can also be positive, see #54
|
2017-04-14 12:24:58 +00:00
|
|
|
'sw_ver': str,
|
|
|
|
'type': str,
|
2017-08-05 15:22:36 +00:00
|
|
|
'mic_type': str,
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
'updating': check_int_bool,
|
2018-10-18 17:57:44 +00:00
|
|
|
# these are available on hs220
|
|
|
|
'brightness': int,
|
|
|
|
'preferred_state': [{
|
|
|
|
'brightness': All(int, Range(min=0, max=100)),
|
|
|
|
'index': int,
|
|
|
|
}],
|
|
|
|
"next_action": {"type": int},
|
|
|
|
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
})
|
|
|
|
|
2017-08-05 15:22:36 +00:00
|
|
|
current_consumption_schema = Schema(Any({
|
2018-06-16 19:16:35 +00:00
|
|
|
'voltage': Any(All(float, Range(min=0, max=300)), None),
|
|
|
|
'power': Any(Coerce(float, Range(min=0)), None),
|
|
|
|
'total': Any(Coerce(float, Range(min=0)), None),
|
|
|
|
'current': Any(All(float, Range(min=0)), None),
|
|
|
|
|
|
|
|
'voltage_mw': Any(All(float, Range(min=0, max=300000)), None),
|
|
|
|
'power_mw': Any(Coerce(float, Range(min=0)), None),
|
|
|
|
'total_wh': Any(Coerce(float, Range(min=0)), None),
|
|
|
|
'current_ma': Any(All(float, Range(min=0)), None),
|
2017-08-05 15:22:36 +00:00
|
|
|
}, None))
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
|
|
|
tz_schema = Schema({
|
2017-04-14 12:24:58 +00:00
|
|
|
'zone_str': str,
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
'dst_offset': int,
|
|
|
|
'index': All(int, Range(min=0)),
|
2017-04-14 12:24:58 +00:00
|
|
|
'tz_str': str,
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
def setUp(self):
|
2017-10-07 15:36:49 +00:00
|
|
|
if PLUG_IP is not None:
|
|
|
|
self.plug = SmartPlug(PLUG_IP)
|
|
|
|
else:
|
|
|
|
self.plug = SmartPlug("127.0.0.1",
|
|
|
|
protocol=FakeTransportProtocol(self.SYSINFO))
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.plug = None
|
|
|
|
|
|
|
|
def test_initialize(self):
|
|
|
|
self.assertIsNotNone(self.plug.sys_info)
|
|
|
|
self.sysinfo_schema(self.plug.sys_info)
|
|
|
|
|
|
|
|
def test_initialize_invalid_connection(self):
|
2017-01-17 13:38:23 +00:00
|
|
|
plug = SmartPlug('127.0.0.1',
|
2017-08-05 15:22:36 +00:00
|
|
|
protocol=FakeTransportProtocol(self.SYSINFO,
|
2017-01-17 13:38:23 +00:00
|
|
|
invalid=True))
|
2017-05-26 14:11:03 +00:00
|
|
|
with self.assertRaises(SmartDeviceException):
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
plug.sys_info['model']
|
|
|
|
|
|
|
|
def test_query_helper(self):
|
2017-05-26 14:11:03 +00:00
|
|
|
with self.assertRaises(SmartDeviceException):
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
self.plug._query_helper("test", "testcmd", {})
|
|
|
|
# TODO check for unwrapping?
|
|
|
|
|
|
|
|
def test_state(self):
|
|
|
|
def set_invalid(x):
|
|
|
|
self.plug.state = x
|
|
|
|
|
|
|
|
set_invalid_int = partial(set_invalid, 1234)
|
|
|
|
self.assertRaises(ValueError, set_invalid_int)
|
|
|
|
|
|
|
|
set_invalid_str = partial(set_invalid, "1234")
|
|
|
|
self.assertRaises(ValueError, set_invalid_str)
|
|
|
|
|
|
|
|
set_invalid_bool = partial(set_invalid, True)
|
|
|
|
self.assertRaises(ValueError, set_invalid_bool)
|
|
|
|
|
|
|
|
orig_state = self.plug.state
|
|
|
|
if orig_state == SmartPlug.SWITCH_STATE_OFF:
|
|
|
|
self.plug.state = "ON"
|
|
|
|
self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_ON)
|
|
|
|
self.plug.state = "OFF"
|
|
|
|
self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_OFF)
|
|
|
|
elif orig_state == SmartPlug.SWITCH_STATE_ON:
|
|
|
|
self.plug.state = "OFF"
|
|
|
|
self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_OFF)
|
|
|
|
self.plug.state = "ON"
|
|
|
|
self.assertTrue(self.plug.state == SmartPlug.SWITCH_STATE_ON)
|
|
|
|
elif orig_state == SmartPlug.SWITCH_STATE_UNKNOWN:
|
|
|
|
self.fail("can't test for unknown state")
|
|
|
|
|
|
|
|
def test_get_sysinfo(self):
|
|
|
|
# initialize checks for this already, but just to be sure
|
|
|
|
self.sysinfo_schema(self.plug.get_sysinfo())
|
|
|
|
|
|
|
|
def test_turns_and_isses(self):
|
|
|
|
orig_state = self.plug.is_on
|
|
|
|
|
|
|
|
if orig_state:
|
|
|
|
self.plug.turn_off()
|
|
|
|
self.assertFalse(self.plug.is_on)
|
|
|
|
self.assertTrue(self.plug.is_off)
|
|
|
|
self.plug.turn_on()
|
|
|
|
self.assertTrue(self.plug.is_on)
|
|
|
|
else:
|
|
|
|
self.plug.turn_on()
|
|
|
|
self.assertFalse(self.plug.is_off)
|
|
|
|
self.assertTrue(self.plug.is_on)
|
|
|
|
self.plug.turn_off()
|
|
|
|
self.assertTrue(self.plug.is_off)
|
|
|
|
|
|
|
|
def test_has_emeter(self):
|
|
|
|
# a not so nice way for checking for emeter availability..
|
|
|
|
if "110" in self.plug.sys_info["model"]:
|
|
|
|
self.assertTrue(self.plug.has_emeter)
|
|
|
|
else:
|
|
|
|
self.assertFalse(self.plug.has_emeter)
|
|
|
|
|
|
|
|
def test_get_emeter_realtime(self):
|
2017-08-05 15:22:36 +00:00
|
|
|
if self.plug.has_emeter:
|
2017-10-07 15:36:49 +00:00
|
|
|
current_emeter = self.plug.get_emeter_realtime()
|
|
|
|
self.current_consumption_schema(current_emeter)
|
2017-08-05 15:22:36 +00:00
|
|
|
else:
|
|
|
|
self.assertEqual(self.plug.get_emeter_realtime(), None)
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
|
|
|
def test_get_emeter_daily(self):
|
2017-08-05 15:22:36 +00:00
|
|
|
if self.plug.has_emeter:
|
|
|
|
self.assertEqual(self.plug.get_emeter_daily(year=1900, month=1),
|
|
|
|
{})
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
2017-10-07 15:36:49 +00:00
|
|
|
d = self.plug.get_emeter_daily()
|
|
|
|
if len(d) < 1:
|
|
|
|
print("no emeter daily information, skipping..")
|
|
|
|
return
|
|
|
|
k, v = d.popitem()
|
2017-08-05 15:22:36 +00:00
|
|
|
self.assertTrue(isinstance(k, int))
|
|
|
|
self.assertTrue(isinstance(v, float))
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.plug.get_emeter_daily(year=1900, month=1),
|
|
|
|
None)
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
|
|
|
def test_get_emeter_monthly(self):
|
2017-08-05 15:22:36 +00:00
|
|
|
if self.plug.has_emeter:
|
|
|
|
self.assertEqual(self.plug.get_emeter_monthly(year=1900), {})
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
2017-08-05 15:22:36 +00:00
|
|
|
d = self.plug.get_emeter_monthly()
|
2017-10-07 15:36:49 +00:00
|
|
|
if len(d) < 1:
|
|
|
|
print("no emeter monthly information, skipping..")
|
|
|
|
return
|
2017-08-05 15:22:36 +00:00
|
|
|
k, v = d.popitem()
|
|
|
|
self.assertTrue(isinstance(k, int))
|
|
|
|
self.assertTrue(isinstance(v, float))
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.plug.get_emeter_monthly(year=1900), None)
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
|
|
|
@skip("not clearing your stats..")
|
|
|
|
def test_erase_emeter_stats(self):
|
|
|
|
self.fail()
|
|
|
|
|
|
|
|
def test_current_consumption(self):
|
2017-08-05 15:22:36 +00:00
|
|
|
if self.plug.has_emeter:
|
|
|
|
x = self.plug.current_consumption()
|
|
|
|
self.assertTrue(isinstance(x, float))
|
|
|
|
self.assertTrue(x >= 0.0)
|
|
|
|
else:
|
|
|
|
self.assertEqual(self.plug.current_consumption(), None)
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
|
|
|
|
def test_alias(self):
|
|
|
|
test_alias = "TEST1234"
|
|
|
|
original = self.plug.alias
|
2017-04-14 12:24:58 +00:00
|
|
|
self.assertTrue(isinstance(original, str))
|
Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35 (#17)
* Refactor & add unittests for almost all functionality, add tox for running tests on py27 and py35
This commit adds unit tests for current api functionality.
- currently no mocking, all tests are run on the device.
- the library is now compatible with python 2.7 and python 3.5, use tox for tests
- schema checks are done with voluptuous
refactoring:
- protocol is separated into its own file, smartplug adapted to receive protocol worker as parameter.
- cleaned up the initialization routine, initialization is done on use, not on creation of smartplug
- added model and features properties, identity kept for backwards compatibility
- no more storing of local variables outside _sys_info, paves a way to handle state changes sanely (without complete reinitialization)
* Fix CI warnings, remove unused leftover code
* Rename _initialize to _fetch_sysinfo, as that's what it does.
* examples.cli: fix identify call, prettyprint sysinfo, update readme which had false format for led setting
* Add tox-travis for automated testing.
2016-12-16 22:51:56 +00:00
|
|
|
self.plug.alias = test_alias
|
|
|
|
self.assertEqual(self.plug.alias, test_alias)
|
|
|
|
self.plug.alias = original
|
|
|
|
self.assertEqual(self.plug.alias, original)
|
|
|
|
|
|
|
|
def test_led(self):
|
|
|
|
original = self.plug.led
|
|
|
|
|
|
|
|
self.plug.led = False
|
|
|
|
self.assertFalse(self.plug.led)
|
|
|
|
self.plug.led = True
|
|
|
|
self.assertTrue(self.plug.led)
|
|
|
|
|
|
|
|
self.plug.led = original
|
|
|
|
|
|
|
|
def test_icon(self):
|
|
|
|
self.assertEqual(set(self.plug.icon.keys()), {'icon', 'hash'})
|
|
|
|
|
|
|
|
def test_time(self):
|
|
|
|
self.assertTrue(isinstance(self.plug.time, datetime.datetime))
|
|
|
|
# TODO check setting?
|
|
|
|
|
|
|
|
def test_timezone(self):
|
|
|
|
self.tz_schema(self.plug.timezone)
|
|
|
|
|
|
|
|
def test_hw_info(self):
|
|
|
|
self.sysinfo_schema(self.plug.hw_info)
|
|
|
|
|
|
|
|
def test_on_since(self):
|
|
|
|
self.assertTrue(isinstance(self.plug.on_since, datetime.datetime))
|
|
|
|
|
|
|
|
def test_location(self):
|
|
|
|
self.sysinfo_schema(self.plug.location)
|
|
|
|
|
|
|
|
def test_rssi(self):
|
|
|
|
self.sysinfo_schema({'rssi': self.plug.rssi}) # wrapping for vol
|
|
|
|
|
|
|
|
def test_mac(self):
|
|
|
|
self.sysinfo_schema({'mac': self.plug.mac}) # wrapping for val
|
|
|
|
# TODO check setting?
|
2017-08-05 15:22:36 +00:00
|
|
|
|
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
class TestSmartPlugHS110(TestSmartPlugHS100):
|
|
|
|
SYSINFO = sysinfo_hs110
|
|
|
|
|
|
|
|
def test_emeter_upcast(self):
|
|
|
|
emeter = self.plug.get_emeter_realtime()
|
|
|
|
self.assertAlmostEqual(emeter["power"] * 10**3, emeter["power_mw"])
|
|
|
|
self.assertAlmostEqual(emeter["voltage"] * 10**3, emeter["voltage_mv"])
|
|
|
|
self.assertAlmostEqual(emeter["current"] * 10**3, emeter["current_ma"])
|
|
|
|
self.assertAlmostEqual(emeter["total"] * 10**3, emeter["total_wh"])
|
|
|
|
|
|
|
|
def test_emeter_daily_upcast(self):
|
|
|
|
emeter = self.plug.get_emeter_daily()
|
|
|
|
_, v = emeter.popitem()
|
|
|
|
|
|
|
|
emeter = self.plug.get_emeter_daily(kwh=False)
|
|
|
|
_, v2 = emeter.popitem()
|
|
|
|
|
|
|
|
self.assertAlmostEqual(v * 10**3, v2)
|
|
|
|
|
|
|
|
def test_get_emeter_monthly_upcast(self):
|
|
|
|
emeter = self.plug.get_emeter_monthly()
|
|
|
|
_, v = emeter.popitem()
|
|
|
|
|
|
|
|
emeter = self.plug.get_emeter_monthly(kwh=False)
|
|
|
|
_, v2 = emeter.popitem()
|
|
|
|
|
|
|
|
self.assertAlmostEqual(v * 10**3, v2)
|
|
|
|
|
|
|
|
|
|
|
|
class TestSmartPlugHS110_HW2(TestSmartPlugHS100):
|
|
|
|
SYSINFO = sysinfo_hs110_au_v2
|
|
|
|
|
|
|
|
def test_emeter_downcast(self):
|
|
|
|
emeter = self.plug.get_emeter_realtime()
|
|
|
|
self.assertAlmostEqual(emeter["power"], emeter["power_mw"] / 10**3)
|
|
|
|
self.assertAlmostEqual(emeter["voltage"], emeter["voltage_mv"] / 10**3)
|
|
|
|
self.assertAlmostEqual(emeter["current"], emeter["current_ma"] / 10**3)
|
|
|
|
self.assertAlmostEqual(emeter["total"], emeter["total_wh"] / 10**3)
|
|
|
|
|
|
|
|
def test_emeter_daily_downcast(self):
|
|
|
|
emeter = self.plug.get_emeter_daily()
|
|
|
|
_, v = emeter.popitem()
|
|
|
|
|
|
|
|
emeter = self.plug.get_emeter_daily(kwh=False)
|
|
|
|
_, v2 = emeter.popitem()
|
|
|
|
|
|
|
|
self.assertAlmostEqual(v * 10**3, v2)
|
|
|
|
|
|
|
|
def test_get_emeter_monthly_downcast(self):
|
|
|
|
emeter = self.plug.get_emeter_monthly()
|
|
|
|
_, v = emeter.popitem()
|
|
|
|
|
|
|
|
emeter = self.plug.get_emeter_monthly(kwh=False)
|
|
|
|
_, v2 = emeter.popitem()
|
|
|
|
|
|
|
|
self.assertAlmostEqual(v * 10**3, v2)
|
2017-08-05 15:27:06 +00:00
|
|
|
|
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
class TestSmartPlugHS200(TestSmartPlugHS100):
|
2017-08-05 15:22:36 +00:00
|
|
|
SYSINFO = sysinfo_hs200
|
|
|
|
|
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
class TestSmartPlugHS105(TestSmartPlugHS100):
|
2017-08-05 15:22:36 +00:00
|
|
|
SYSINFO = sysinfo_hs105
|
|
|
|
|
|
|
|
def test_location_i(self):
|
2017-10-07 15:36:49 +00:00
|
|
|
if PLUG_IP is not None:
|
|
|
|
plug_i = SmartPlug(PLUG_IP)
|
|
|
|
else:
|
|
|
|
plug_i = SmartPlug("127.0.0.1",
|
|
|
|
protocol=FakeTransportProtocol(self.SYSINFO))
|
|
|
|
|
2017-08-05 15:22:36 +00:00
|
|
|
self.sysinfo_schema(plug_i.location)
|
2018-10-18 17:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestSmartPlugHS220(TestSmartPlugHS105):
|
|
|
|
"""HS220 with dimming functionality. Sysinfo looks similar to HS105."""
|
|
|
|
SYSINFO = sysinfo_hs220
|
|
|
|
|
|
|
|
def test_dimmable(self):
|
|
|
|
assert self.plug.is_dimmable
|
|
|
|
assert self.plug.brightness == 25
|
|
|
|
self.plug.brightness = 100
|
|
|
|
assert self.plug.brightness == 100
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.plug.brightness = 110
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.plug.brightness = -1
|
2018-10-18 18:16:40 +00:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.plug.brightness = "foo"
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.plug.brightness = 11.1
|