Skip to content

Commit da745b1

Browse files
committed
Merge branch 'devel' for release of v0.2.14
2 parents fdbbe25 + 854a341 commit da745b1

13 files changed

Lines changed: 329 additions & 231 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.cfg
22
*.pyc
33
*.egg-info
4+
.eggs
45
MANIFEST
56
dist/
67
build/

README.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ New releases are cut from the devel branch as needed.
147147

148148
Tests
149149
~~~~~
150-
pytest is used for unit tests. Test coverage is still quite spotty and under active development.
151-
Certain tests are marked as "live" tests and require an active authentication state and a real hub to query against.
152-
Live tests are non-destructive.
150+
pytest is used for unit tests.
151+
Certain tests are marked as "live" tests and require an active authentication state and a real hub to query against. Live tests are non-destructive.
152+
Some tests are marked as "destructive" and will cause changes such as a light being turned on or tokens getting invalidated on purpose.
153153

154154
During development you can run the test suite right from the source directory:
155155

@@ -158,14 +158,14 @@ During development you can run the test suite right from the source directory:
158158
pytest -v cozify/
159159
# or include the live tests as well:
160160
pytest -v cozify/ --live
161+
# or for the brave, also run destructive tests (also implies --live):
162+
pytest -v cozify/ --destructive
161163
162164
To run the test suite on an already installed python-cozify:
163165

164166
.. code:: bash
165167
166168
pytest -v --pyargs cozify
167-
# or including live tests:
168-
pytest -v --pyargs cozify --live
169169
170170
171171
Roadmap, aka. Current Limitations

cozify/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.13"
1+
__version__ = "0.2.14"

cozify/cloud.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Module for handling Cozify Cloud highlevel operations.
22
"""
33

4-
import logging, datetime
4+
from absl import logging
5+
import datetime
56

67
from . import config
78
from . import hub_api
@@ -50,7 +51,7 @@ def authenticate(trustCloud=True, trustHub=True, remote=False, autoremote=True):
5051
otp = _getotp()
5152
if not otp:
5253
message = "OTP unavailable, authentication cannot succeed. This may happen if running non-interactively (closed stdin)."
53-
logging.critical(message)
54+
logging.fatal(message)
5455
raise AuthenticationError(message)
5556

5657
try:
@@ -72,34 +73,37 @@ def authenticate(trustCloud=True, trustHub=True, remote=False, autoremote=True):
7273
# TODO(artanicus): unknown what will happen if there is a local hub but another one remote. Needs testing by someone with multiple hubs. Issue #7
7374
hubkeys = cloud_api.hubkeys(cloud_token) # get all registered hubs and their keys from the cloud.
7475
if not hubkeys:
75-
logging.critical('You have not registered any hubs to the Cozify Cloud, hence a hub cannot be used yet.')
76+
logging.fatal('You have not registered any hubs to the Cozify Cloud, hence a hub cannot be used yet.')
7677

7778
# evaluate all returned Hubs and store them
78-
logging.debug('Listing all hubs returned by cloud hubkeys query:')
7979
for hub_id, hub_token in hubkeys.items():
8080
logging.debug('hub: {0} token: {1}'.format(hub_id, hub_token))
8181
hub_info = None
8282
hub_ip = None
8383

84+
if not hub.exists(hub_id):
85+
autoremote = True
86+
else:
87+
autoremote = hub.autoremote(hub_id=hub_id)
8488
# if we're remote, we didn't get a valid ip
8589
if not localHubs:
86-
logging.info('No local Hubs detected, attempting authentication via Cozify Cloud.')
90+
logging.info('No local Hubs detected, changing to remote mode.')
8791
hub_info = hub_api.hub(remote=True, cloud_token=cloud_token, hub_token=hub_token)
88-
# if the hub wants autoremote we flip the state
89-
if hub.autoremote(hub_id) and not hub.remote(hub_id):
92+
# if the hub wants autoremote we flip the state. If this is the first time the hub is seen, act as if autoremote=True, remote=False
93+
if not hub.exists(hub_id) or (hub.autoremote(hub_id) and not hub.remote(hub_id)):
9094
logging.info('[autoremote] Flipping hub remote status from local to remote.')
91-
hub.remote(hub_id, True)
95+
remote = True
9296
else:
9397
# localHubs is valid so a hub is in the lan. A mixed environment cannot yet be detected.
9498
# cloud_api.lan_ip cannot provide a map as to which ip is which hub. Thus we actually need to determine the right one.
9599
# TODO(artanicus): Need to truly test how multihub works before implementing ip to hub resolution. See issue #7
96100
logging.debug('data structure: {0}'.format(localHubs))
97101
hub_ip = localHubs[0]
98102
hub_info = hub_api.hub(host=hub_ip, remote=False)
99-
# if the hub wants autoremote we flip the state
100-
if hub.autoremote(hub_id) and hub.remote(hub_id):
103+
# if the hub wants autoremote we flip the state. If this is the first time the hub is seen, act as if autoremote=True, remote=False
104+
if not hub.exists(hub_id) or (hub.autoremote(hub_id) and hub.remote(hub_id)):
101105
logging.info('[autoremote] Flipping hub remote status from remote to local.')
102-
hub.remote(hub_id, False)
106+
remote = False
103107

104108
hub_name = hub_info['name']
105109
if hub_id in hubkeys:
@@ -121,6 +125,7 @@ def authenticate(trustCloud=True, trustHub=True, remote=False, autoremote=True):
121125
hub._setAttr(hub_id, 'host', hub_ip, commit=False)
122126
hub._setAttr(hub_id, 'hubName', hub_name, commit=False)
123127
hub.token(hub_id, hub_token)
128+
hub.remote(hub_id, remote)
124129
return True
125130

126131
def resetState():
@@ -258,7 +263,7 @@ def _need_hub_token(trust=True):
258263
logging.debug("We don't have a valid hubtoken or it's not trusted.")
259264
return True
260265
else: # if we have a token, we need to test if the API is callable
261-
ping = hub.ping()
266+
ping = hub.ping(autorefresh=False) # avoid compliating things by disabling autorefresh on failure.
262267
logging.debug("Testing hub.ping() for hub_token validity: {0}".format(ping))
263268
return not ping
264269

cozify/conftest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@
44
def pytest_addoption(parser):
55
parser.addoption("--live", action="store_true",
66
default=False, help="run tests requiring a functional auth and a real hub.")
7+
parser.addoption("--destructive", action="store_true",
8+
default=False, help="run tests that require and modify the state of a real hub.")
79

810
def pytest_collection_modifyitems(config, items):
11+
live = False
12+
destructive = False
913
if config.getoption("--live"):
14+
live = True
15+
if config.getoption("--destructive"):
1016
return
1117
skip_live = pytest.mark.skip(reason="need --live option to run")
18+
skip_destructive = pytest.mark.skip(reason="need --destructive option to run")
19+
1220
for item in items:
13-
if "live" in item.keywords:
21+
if "live" in item.keywords and not live:
1422
item.add_marker(skip_live)
23+
if "destructive" in item.keywords and not destructive:
24+
item.add_marker(skip_destructive)

0 commit comments

Comments
 (0)