2016-11-22 01:31:25 +00:00
|
|
|
"""
|
|
|
|
pyHS100
|
|
|
|
Python library supporting TP-Link Smart Plugs/Switches (HS100/HS110/Hs200).
|
|
|
|
|
|
|
|
The communication protocol was reverse engineered by Lubomir Stroetmann and
|
|
|
|
Tobias Esser in 'Reverse Engineering the TP-Link HS110':
|
|
|
|
https://www.softscheck.com/en/reverse-engineering-tp-link-hs110/
|
|
|
|
|
|
|
|
This library reuses codes and concepts of the TP-Link WiFi SmartPlug Client
|
|
|
|
at https://github.com/softScheck/tplink-smartplug, developed by Lubomir
|
|
|
|
Stroetmann which is licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
|
|
You may obtain a copy of the license at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
"""
|
2016-11-21 22:18:12 +00:00
|
|
|
import datetime
|
2016-07-09 12:36:33 +00:00
|
|
|
import logging
|
2017-09-14 18:47:41 +00:00
|
|
|
import warnings
|
2017-08-05 15:28:45 +00:00
|
|
|
from collections import defaultdict
|
2017-09-18 16:13:06 +00:00
|
|
|
from typing import Any, Dict, List, Tuple, Optional
|
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-04-14 12:24:58 +00:00
|
|
|
from .protocol import TPLinkSmartHomeProtocol
|
2016-07-09 11:34:27 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-07-09 12:36:33 +00:00
|
|
|
|
2017-10-07 15:41:51 +00:00
|
|
|
class SmartDeviceException(Exception):
|
|
|
|
"""
|
|
|
|
SmartDeviceException gets raised for errors reported by device.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
class EmeterStatus(dict):
|
|
|
|
"""Container for converting different representations of emeter data.
|
|
|
|
|
|
|
|
Newer FW/HW versions postfix the variable names with the used units,
|
|
|
|
where-as the olders do not have this feature.
|
|
|
|
|
|
|
|
This class automatically converts between these two to allow
|
|
|
|
backwards and forwards compatibility.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
valid_keys = ['voltage_mv', 'power_mw', 'current_ma',
|
|
|
|
'energy_wh', 'total_wh',
|
|
|
|
'voltage', 'power', 'current', 'total',
|
|
|
|
'energy']
|
|
|
|
|
|
|
|
# 1. if requested data is available, return it
|
|
|
|
if item in super().keys():
|
|
|
|
return super().__getitem__(item)
|
|
|
|
# otherwise decide how to convert it
|
|
|
|
else:
|
|
|
|
if item not in valid_keys:
|
|
|
|
raise KeyError(item)
|
|
|
|
if '_' in item: # upscale
|
|
|
|
return super().__getitem__(item[:item.find('_')]) * 10**3
|
|
|
|
else: # downscale
|
|
|
|
for i in super().keys():
|
|
|
|
if i.startswith(item):
|
|
|
|
return self.__getitem__(i) / 10**3
|
|
|
|
|
|
|
|
raise SmartDeviceException("Unable to find a value for '%s'" %
|
|
|
|
item)
|
|
|
|
|
|
|
|
|
2017-01-17 13:38:23 +00:00
|
|
|
class SmartDevice(object):
|
2017-04-24 17:28:22 +00:00
|
|
|
# possible device features
|
|
|
|
FEATURE_ENERGY_METER = 'ENE'
|
|
|
|
FEATURE_TIMER = 'TIM'
|
|
|
|
|
|
|
|
ALL_FEATURES = (FEATURE_ENERGY_METER, FEATURE_TIMER)
|
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def __init__(self,
|
2018-01-17 21:03:19 +00:00
|
|
|
host: str,
|
2019-01-08 19:13:25 +00:00
|
|
|
protocol: Optional[TPLinkSmartHomeProtocol] = None,
|
|
|
|
context: str = None) -> None:
|
2016-11-22 01:31:25 +00:00
|
|
|
"""
|
2018-01-17 21:03:19 +00:00
|
|
|
Create a new SmartDevice instance.
|
2016-11-22 01:31:25 +00:00
|
|
|
|
2018-01-17 21:03:19 +00:00
|
|
|
:param str host: host name or ip address on which the device listens
|
2019-01-08 19:13:25 +00:00
|
|
|
:param context: optional child ID for context in a parent device
|
2016-11-22 01:31:25 +00:00
|
|
|
"""
|
2018-01-17 21:03:19 +00:00
|
|
|
self.host = host
|
2017-01-07 22:45:47 +00:00
|
|
|
if not protocol:
|
|
|
|
protocol = TPLinkSmartHomeProtocol()
|
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.protocol = protocol
|
2017-09-18 16:13:06 +00:00
|
|
|
self.emeter_type = "emeter" # type: str
|
2019-01-08 19:13:25 +00:00
|
|
|
self.context = context
|
2019-03-16 20:32:59 +00:00
|
|
|
self.num_children = 0
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def _query_helper(self,
|
|
|
|
target: str,
|
|
|
|
cmd: str,
|
|
|
|
arg: Optional[Dict] = None) -> Any:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Helper returning unwrapped result object and doing error handling.
|
|
|
|
|
|
|
|
:param target: Target system {system, time, emeter, ..}
|
|
|
|
:param cmd: Command to execute
|
2017-04-14 12:24:58 +00:00
|
|
|
:param arg: JSON object passed as parameter to the command
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
:return: Unwrapped result for the call.
|
|
|
|
:rtype: dict
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: if command was not executed correctly
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
2019-01-08 19:13:25 +00:00
|
|
|
if self.context is None:
|
|
|
|
request = {target: {cmd: arg}}
|
|
|
|
else:
|
|
|
|
request = {"context": {"child_ids": [self.context]},
|
|
|
|
target: {cmd: arg}}
|
2017-04-14 12:24:58 +00:00
|
|
|
if arg is None:
|
|
|
|
arg = {}
|
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
|
|
|
try:
|
|
|
|
response = self.protocol.query(
|
2018-01-17 21:03:19 +00:00
|
|
|
host=self.host,
|
2019-01-08 19:13:25 +00:00
|
|
|
request=request,
|
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
|
|
|
)
|
|
|
|
except Exception as ex:
|
2017-05-26 14:11:03 +00:00
|
|
|
raise SmartDeviceException('Communication error') from ex
|
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-01-07 22:45:47 +00:00
|
|
|
if target not in response:
|
2017-05-26 14:11:03 +00:00
|
|
|
raise SmartDeviceException("No required {} in response: {}"
|
|
|
|
.format(target, response))
|
2017-01-07 22:45:47 +00:00
|
|
|
|
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
|
|
|
result = response[target]
|
|
|
|
if "err_code" in result and result["err_code"] != 0:
|
2017-05-26 14:11:03 +00:00
|
|
|
raise SmartDeviceException("Error on {}.{}: {}"
|
|
|
|
.format(target, cmd, result))
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
2018-10-18 17:57:44 +00:00
|
|
|
if cmd not in result:
|
|
|
|
raise SmartDeviceException("No command in response: {}"
|
|
|
|
.format(response))
|
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
|
|
|
result = result[cmd]
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
del result["err_code"]
|
|
|
|
|
|
|
|
return result
|
2016-07-09 11:34:27 +00:00
|
|
|
|
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
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def features(self) -> List[str]:
|
2017-04-24 17:28:22 +00:00
|
|
|
"""
|
|
|
|
Returns features of the devices
|
|
|
|
|
|
|
|
:return: list of features
|
|
|
|
:rtype: list
|
|
|
|
"""
|
2017-09-14 18:47:41 +00:00
|
|
|
warnings.simplefilter('always', DeprecationWarning)
|
|
|
|
warnings.warn(
|
|
|
|
"features works only on plugs and its use is discouraged, "
|
|
|
|
"and it will likely to be removed at some point",
|
2017-10-07 15:36:49 +00:00
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2
|
2017-09-14 18:47:41 +00:00
|
|
|
)
|
|
|
|
warnings.simplefilter('default', DeprecationWarning)
|
2017-08-06 13:42:33 +00:00
|
|
|
if "feature" not in self.sys_info:
|
2017-09-18 16:13:06 +00:00
|
|
|
return []
|
2017-08-06 13:42:33 +00:00
|
|
|
|
2017-04-24 17:28:22 +00:00
|
|
|
features = self.sys_info['feature'].split(':')
|
|
|
|
|
|
|
|
for feature in features:
|
|
|
|
if feature not in SmartDevice.ALL_FEATURES:
|
|
|
|
_LOGGER.warning("Unknown feature %s on device %s.",
|
|
|
|
feature, self.model)
|
|
|
|
|
|
|
|
return features
|
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-04-14 12:24:58 +00:00
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def has_emeter(self) -> bool:
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
2017-10-07 15:36:49 +00:00
|
|
|
Checks feature list for energy meter support.
|
|
|
|
Note: this has to be implemented on a device specific class.
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2017-04-24 17:28:22 +00:00
|
|
|
:return: True if energey meter is available
|
|
|
|
False if energymeter is missing
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
2017-10-07 15:36:49 +00:00
|
|
|
raise NotImplementedError()
|
2017-04-14 12:24:58 +00:00
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def sys_info(self) -> Dict[str, Any]:
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
2017-04-24 17:28:22 +00:00
|
|
|
Returns the complete system information from the device.
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2017-04-24 17:28:22 +00:00
|
|
|
:return: System information dict.
|
|
|
|
:rtype: dict
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
2017-08-05 15:28:45 +00:00
|
|
|
return defaultdict(lambda: None, self.get_sysinfo())
|
2017-04-14 12:24:58 +00:00
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def get_sysinfo(self) -> Dict:
|
2016-11-22 01:31:25 +00:00
|
|
|
"""
|
|
|
|
Retrieve system information.
|
|
|
|
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
:return: sysinfo
|
|
|
|
:rtype dict
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
2016-11-22 01:31:25 +00:00
|
|
|
"""
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
return self._query_helper("system", "get_sysinfo")
|
2016-10-13 15:31:34 +00:00
|
|
|
|
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
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def model(self) -> 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
|
|
|
"""
|
|
|
|
Get model of the device
|
|
|
|
|
|
|
|
:return: device model
|
|
|
|
:rtype: str
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
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-09-18 16:13:06 +00:00
|
|
|
return str(self.sys_info['model'])
|
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
|
|
|
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def alias(self) -> str:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Get current device alias (name)
|
|
|
|
|
|
|
|
:return: Device name aka alias.
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2017-09-18 16:13:06 +00:00
|
|
|
return str(self.sys_info['alias'])
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
|
|
|
@alias.setter
|
2017-09-18 16:13:06 +00:00
|
|
|
def alias(self, alias: str) -> None:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Sets the device name aka alias.
|
|
|
|
|
|
|
|
:param alias: New alias (name)
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
self._query_helper("system", "set_dev_alias", {"alias": alias})
|
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def icon(self) -> Dict:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Returns device icon
|
|
|
|
|
|
|
|
Note: not working on HS110, but is always empty.
|
|
|
|
|
|
|
|
:return: icon and its hash
|
|
|
|
:rtype: dict
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
return self._query_helper("system", "get_dev_icon")
|
|
|
|
|
|
|
|
@icon.setter
|
2017-09-18 16:13:06 +00:00
|
|
|
def icon(self, icon: str) -> None:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Content for hash and icon are unknown.
|
|
|
|
|
|
|
|
:param str icon: Icon path(?)
|
|
|
|
:raises NotImplementedError: when not implemented
|
|
|
|
:raises SmartPlugError: on error
|
|
|
|
"""
|
2017-04-14 12:24:58 +00:00
|
|
|
raise NotImplementedError()
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
# here just for the sake of completeness
|
2017-04-14 12:24:58 +00:00
|
|
|
# self._query_helper("system",
|
|
|
|
# "set_dev_icon", {"icon": "", "hash": ""})
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
# self.initialize()
|
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def time(self) -> Optional[datetime.datetime]:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Returns current time from the device.
|
|
|
|
|
|
|
|
:return: datetime for device's time
|
2017-08-05 13:49:56 +00:00
|
|
|
:rtype: datetime.datetime or None when not available
|
|
|
|
:raises SmartDeviceException: on error
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
2017-08-05 13:49:56 +00:00
|
|
|
try:
|
|
|
|
res = self._query_helper("time", "get_time")
|
|
|
|
return datetime.datetime(res["year"], res["month"], res["mday"],
|
|
|
|
res["hour"], res["min"], res["sec"])
|
|
|
|
except SmartDeviceException:
|
|
|
|
return None
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
|
|
|
@time.setter
|
2017-09-18 16:13:06 +00:00
|
|
|
def time(self, ts: datetime.datetime) -> None:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Sets time based on datetime object.
|
|
|
|
Note: this calls set_timezone() for setting.
|
|
|
|
|
|
|
|
:param datetime.datetime ts: New date and time
|
|
|
|
:return: result
|
|
|
|
:type: dict
|
|
|
|
:raises NotImplemented: when not implemented.
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError("Fails with err_code == 0 with HS110.")
|
2017-04-14 12:24:58 +00:00
|
|
|
"""
|
|
|
|
here just for the sake of completeness.
|
|
|
|
if someone figures out why it doesn't work,
|
|
|
|
please create a PR :-)
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
ts_obj = {
|
|
|
|
"index": self.timezone["index"],
|
|
|
|
"hour": ts.hour,
|
|
|
|
"min": ts.minute,
|
|
|
|
"sec": ts.second,
|
|
|
|
"year": ts.year,
|
|
|
|
"month": ts.month,
|
|
|
|
"mday": ts.day,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response = self._query_helper("time", "set_timezone", ts_obj)
|
|
|
|
self.initialize()
|
|
|
|
|
|
|
|
return response
|
|
|
|
"""
|
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def timezone(self) -> Dict:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Returns timezone information
|
|
|
|
|
|
|
|
:return: Timezone information
|
|
|
|
:rtype: dict
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
return self._query_helper("time", "get_timezone")
|
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def hw_info(self) -> Dict:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Returns information about hardware
|
|
|
|
|
|
|
|
:return: Information about hardware
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
2017-08-05 13:49:56 +00:00
|
|
|
keys = ["sw_ver", "hw_ver", "mac", "mic_mac", "type",
|
|
|
|
"mic_type", "hwId", "fwId", "oemId", "dev_name"]
|
2017-01-11 08:17:48 +00:00
|
|
|
info = self.sys_info
|
2017-08-05 13:49:56 +00:00
|
|
|
return {key: info[key] for key in keys if key in info}
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def location(self) -> Dict:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Location of the device, as read from sysinfo
|
|
|
|
|
|
|
|
:return: latitude and longitude
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
2017-01-11 08:17:48 +00:00
|
|
|
info = self.sys_info
|
2017-04-26 16:43:50 +00:00
|
|
|
loc = {"latitude": None,
|
|
|
|
"longitude": None}
|
|
|
|
|
|
|
|
if "latitude" in info and "longitude" in info:
|
|
|
|
loc["latitude"] = info["latitude"]
|
|
|
|
loc["longitude"] = info["longitude"]
|
|
|
|
elif "latitude_i" in info and "longitude_i" in info:
|
|
|
|
loc["latitude"] = info["latitude_i"]
|
|
|
|
loc["longitude"] = info["longitude_i"]
|
|
|
|
else:
|
|
|
|
_LOGGER.warning("Unsupported device location.")
|
|
|
|
|
|
|
|
return loc
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def rssi(self) -> Optional[int]:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Returns WiFi signal strenth (rssi)
|
|
|
|
|
|
|
|
:return: rssi
|
|
|
|
:rtype: int
|
|
|
|
"""
|
2017-09-18 16:13:06 +00:00
|
|
|
if "rssi" in self.sys_info:
|
|
|
|
return int(self.sys_info["rssi"])
|
|
|
|
return None
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def mac(self) -> str:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Returns mac address
|
|
|
|
|
|
|
|
:return: mac address in hexadecimal with colons, e.g. 01:23:45:67:89:ab
|
|
|
|
:rtype: str
|
|
|
|
"""
|
2017-08-05 13:49:56 +00:00
|
|
|
info = self.sys_info
|
|
|
|
|
|
|
|
if 'mac' in info:
|
2017-09-18 16:13:06 +00:00
|
|
|
return str(info["mac"])
|
2017-08-05 13:49:56 +00:00
|
|
|
elif 'mic_mac' in info:
|
2017-09-18 16:13:06 +00:00
|
|
|
return str(info['mic_mac'])
|
2017-08-05 13:49:56 +00:00
|
|
|
else:
|
|
|
|
raise SmartDeviceException("Unknown mac, please submit a bug"
|
|
|
|
"with sysinfo output.")
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
|
|
|
|
@mac.setter
|
2017-09-18 16:13:06 +00:00
|
|
|
def mac(self, mac: str) -> None:
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
Sets new mac address
|
|
|
|
|
|
|
|
:param str mac: mac in hexadecimal with colons, e.g. 01:23:45:67:89:ab
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
Read all data from the device, disable double-encoding, implement more APIs, refactor querying, update README (#11)
* Read from socket until no data available, disable double string encoding
HS110 sends sometimes datagrams in chunks especially for get_daystat,
this patch makes it to read until there is no more data to be read.
As json.dumps() does JSON encoding already, there's no need to str()
the year or month either.
* Add cli.py, a simple script to query devices for debugging purposes.
* allow easier importing with from pyHS100 import SmartPlug
* move cli.py to examples, add short usage into README.md
* Implement more available APIs, refactor querying code.
This commit adds access to new properties, both read & write, while keeping the old one (mostly) intact.
Querying is refactored to be done inside _query_helper() method,
which unwraps results automatically and rises SmartPlugException() in case of errors.
Errors are to be handled by clients.
New features:
* Setting device alias (plug.alias = "name")
* led read & write
* icon read (doesn't seem to return anything without cloud support at least), write API is not known, throws an exception currently
* time read (returns datetime), time write implemented, but not working even when no error is returned from the device
* timezone read
* mac read & write, writing is untested for now.
Properties for easier access:
* hw_info: return hw-specific elements from sysinfo
* on_since: pretty-printed from sysinfo
* location: latitude and longitued from sysinfo
* rssi: rssi from sysinfo
* Update README.md with examples of available features.
* Handle comments from mweinelt
* Refactor state handling, use booleans instead of strings
* Fix issues raised during the review.
Following issues are addressed by this commit:
* All API is more or less commented (including return types, exceptions, ..)
* Converted state to use
* Added properties is_on, is_off for those who don't want to check against strings.
* Handled most issues reported by pylint.
* Adjusted _query_helper() to strip off err_code from the result object.
* Fixed broken format() syntax for string formattings.
* Fix ci woes plus one typo.
* Do initialization after changing device properties, fix nits.
2016-12-12 09:13:45 +00:00
|
|
|
"""
|
|
|
|
self._query_helper("system", "set_mac_addr", {"mac": mac})
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def get_emeter_realtime(self) -> Optional[Dict]:
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
2019-01-08 19:13:25 +00:00
|
|
|
Retrieve current energy readings from device.
|
2017-01-17 13:38:23 +00:00
|
|
|
|
|
|
|
:returns: current readings or False
|
2017-08-05 15:20:20 +00:00
|
|
|
:rtype: dict, None
|
2019-01-08 19:13:25 +00:00
|
|
|
None if device has no energy meter or error occurred
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
if not self.has_emeter:
|
2017-08-05 15:20:20 +00:00
|
|
|
return None
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
return EmeterStatus(self._query_helper(self.emeter_type,
|
|
|
|
"get_realtime"))
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def get_emeter_daily(self,
|
|
|
|
year: int = None,
|
2018-06-16 19:16:35 +00:00
|
|
|
month: int = None,
|
|
|
|
kwh: bool = True) -> Optional[Dict]:
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
Retrieve daily statistics for a given month
|
|
|
|
|
|
|
|
:param year: year for which to retrieve statistics (default: this year)
|
2019-01-08 19:13:25 +00:00
|
|
|
:param month: month for which to retrieve statistics (default: this
|
2017-01-17 13:38:23 +00:00
|
|
|
month)
|
2018-06-16 19:16:35 +00:00
|
|
|
:param kwh: return usage in kWh (default: True)
|
2017-01-17 13:38:23 +00:00
|
|
|
:return: mapping of day of month to value
|
2019-01-08 19:13:25 +00:00
|
|
|
None if device has no energy meter or error occurred
|
2017-01-17 13:38:23 +00:00
|
|
|
:rtype: dict
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
if not self.has_emeter:
|
2017-08-05 15:20:20 +00:00
|
|
|
return None
|
2017-01-17 13:38:23 +00:00
|
|
|
|
|
|
|
if year is None:
|
|
|
|
year = datetime.datetime.now().year
|
|
|
|
if month is None:
|
|
|
|
month = datetime.datetime.now().month
|
|
|
|
|
|
|
|
response = self._query_helper(self.emeter_type, "get_daystat",
|
|
|
|
{'month': month, 'year': year})
|
2018-06-16 19:16:35 +00:00
|
|
|
response = [EmeterStatus(**x) for x in response["day_list"]]
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
key = 'energy_wh'
|
|
|
|
if kwh:
|
2017-01-17 13:38:23 +00:00
|
|
|
key = 'energy'
|
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
data = {entry['day']: entry[key]
|
|
|
|
for entry in response}
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def get_emeter_monthly(self, year: int = None,
|
|
|
|
kwh: bool = True) -> Optional[Dict]:
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
Retrieve monthly statistics for a given year.
|
|
|
|
|
|
|
|
:param year: year for which to retrieve statistics (default: this year)
|
2018-06-16 19:16:35 +00:00
|
|
|
:param kwh: return usage in kWh (default: True)
|
2017-01-17 13:38:23 +00:00
|
|
|
:return: dict: mapping of month to value
|
2017-08-05 15:20:20 +00:00
|
|
|
None if device has no energy meter
|
2017-01-17 13:38:23 +00:00
|
|
|
:rtype: dict
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
if not self.has_emeter:
|
2017-08-05 15:20:20 +00:00
|
|
|
return None
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
if year is None:
|
|
|
|
year = datetime.datetime.now().year
|
|
|
|
|
2017-01-17 13:38:23 +00:00
|
|
|
response = self._query_helper(self.emeter_type, "get_monthstat",
|
|
|
|
{'year': year})
|
2018-06-16 19:16:35 +00:00
|
|
|
response = [EmeterStatus(**x) for x in response["month_list"]]
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
key = 'energy_wh'
|
|
|
|
if kwh:
|
2017-01-17 13:38:23 +00:00
|
|
|
key = 'energy'
|
|
|
|
|
|
|
|
return {entry['month']: entry[key]
|
2018-06-16 19:16:35 +00:00
|
|
|
for entry in response}
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def erase_emeter_stats(self) -> bool:
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
Erase energy meter statistics
|
|
|
|
|
|
|
|
:return: True if statistics were deleted
|
|
|
|
False if device has no energy meter.
|
|
|
|
:rtype: bool
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
if not self.has_emeter:
|
2017-09-18 16:13:06 +00:00
|
|
|
return False
|
2017-01-17 13:38:23 +00:00
|
|
|
|
|
|
|
self._query_helper(self.emeter_type, "erase_emeter_stat", None)
|
|
|
|
|
|
|
|
# As query_helper raises exception in case of failure, we have
|
|
|
|
# succeeded when we are this far.
|
|
|
|
return True
|
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def current_consumption(self) -> Optional[float]:
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
2019-01-08 19:13:25 +00:00
|
|
|
Get the current power consumption in Watts.
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2019-01-08 19:13:25 +00:00
|
|
|
:return: the current power consumption in Watts.
|
2017-08-05 15:20:20 +00:00
|
|
|
None if device has no energy meter.
|
2017-08-05 13:49:56 +00:00
|
|
|
:raises SmartDeviceException: on error
|
2017-01-17 13:38:23 +00:00
|
|
|
"""
|
|
|
|
if not self.has_emeter:
|
2017-08-05 15:20:20 +00:00
|
|
|
return None
|
2017-01-17 13:38:23 +00:00
|
|
|
|
2018-06-16 19:16:35 +00:00
|
|
|
response = EmeterStatus(self.get_emeter_realtime())
|
|
|
|
return response['power']
|
2017-04-24 17:28:22 +00:00
|
|
|
|
2018-09-08 14:11:58 +00:00
|
|
|
def reboot(self, delay=1) -> None:
|
|
|
|
"""
|
|
|
|
Reboot the device.
|
|
|
|
|
|
|
|
:param delay: Delay the reboot for `delay` seconds.
|
|
|
|
:return: None
|
|
|
|
|
|
|
|
Note that giving a delay of zero causes this to block.
|
|
|
|
"""
|
|
|
|
self._query_helper("system", "reboot", {"delay": delay})
|
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def turn_off(self) -> None:
|
2017-04-24 17:28:22 +00:00
|
|
|
"""
|
|
|
|
Turns the device off.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("Device subclass needs to implement this.")
|
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def is_off(self) -> bool:
|
2017-04-24 17:28:22 +00:00
|
|
|
"""
|
|
|
|
Returns whether device is off.
|
|
|
|
|
|
|
|
:return: True if device is off, False otherwise.
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
|
|
|
return not self.is_on
|
|
|
|
|
2017-09-18 16:13:06 +00:00
|
|
|
def turn_on(self) -> None:
|
2017-04-24 17:28:22 +00:00
|
|
|
"""
|
|
|
|
Turns the device on.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("Device subclass needs to implement this.")
|
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def is_on(self) -> bool:
|
2017-04-24 17:28:22 +00:00
|
|
|
"""
|
|
|
|
Returns whether the device is on.
|
|
|
|
|
|
|
|
:return: True if the device is on, False otherwise.
|
|
|
|
:rtype: bool
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("Device subclass needs to implement this.")
|
|
|
|
|
|
|
|
@property
|
2017-09-18 16:13:06 +00:00
|
|
|
def state_information(self) -> Dict[str, Any]:
|
2017-04-24 17:28:22 +00:00
|
|
|
"""
|
|
|
|
Returns device-type specific, end-user friendly state information.
|
|
|
|
:return: dict with state information.
|
|
|
|
:rtype: dict
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("Device subclass needs to implement this.")
|
2017-08-05 13:49:56 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2019-04-09 04:19:42 +00:00
|
|
|
is_on = self.is_on
|
|
|
|
if callable(is_on):
|
|
|
|
is_on = is_on()
|
2017-09-14 19:00:19 +00:00
|
|
|
return "<%s at %s (%s), is_on: %s - dev specific: %s>" % (
|
|
|
|
self.__class__.__name__,
|
2018-01-17 21:03:19 +00:00
|
|
|
self.host,
|
2017-09-14 19:00:19 +00:00
|
|
|
self.alias,
|
2019-04-09 04:19:42 +00:00
|
|
|
is_on,
|
2017-09-14 19:00:19 +00:00
|
|
|
self.state_information)
|