Skip to content

Commit

Permalink
Merge branch 'devel' for release of v0.2.14
Browse files Browse the repository at this point in the history
  • Loading branch information
jinnatar committed Mar 24, 2018
2 parents fdbbe25 + 854a341 commit da745b1
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 231 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.cfg
*.pyc
*.egg-info
.eggs
MANIFEST
dist/
build/
Expand Down
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ New releases are cut from the devel branch as needed.

Tests
~~~~~
pytest is used for unit tests. Test coverage is still quite spotty and under active development.
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.
pytest is used for unit tests.
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.
Some tests are marked as "destructive" and will cause changes such as a light being turned on or tokens getting invalidated on purpose.

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

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

.. code:: bash
pytest -v --pyargs cozify
# or including live tests:
pytest -v --pyargs cozify --live
Roadmap, aka. Current Limitations
Expand Down
2 changes: 1 addition & 1 deletion cozify/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.13"
__version__ = "0.2.14"
29 changes: 17 additions & 12 deletions cozify/cloud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Module for handling Cozify Cloud highlevel operations.
"""

import logging, datetime
from absl import logging
import datetime

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

try:
Expand All @@ -72,34 +73,37 @@ def authenticate(trustCloud=True, trustHub=True, remote=False, autoremote=True):
# 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
hubkeys = cloud_api.hubkeys(cloud_token) # get all registered hubs and their keys from the cloud.
if not hubkeys:
logging.critical('You have not registered any hubs to the Cozify Cloud, hence a hub cannot be used yet.')
logging.fatal('You have not registered any hubs to the Cozify Cloud, hence a hub cannot be used yet.')

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

if not hub.exists(hub_id):
autoremote = True
else:
autoremote = hub.autoremote(hub_id=hub_id)
# if we're remote, we didn't get a valid ip
if not localHubs:
logging.info('No local Hubs detected, attempting authentication via Cozify Cloud.')
logging.info('No local Hubs detected, changing to remote mode.')
hub_info = hub_api.hub(remote=True, cloud_token=cloud_token, hub_token=hub_token)
# if the hub wants autoremote we flip the state
if hub.autoremote(hub_id) and not hub.remote(hub_id):
# 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
if not hub.exists(hub_id) or (hub.autoremote(hub_id) and not hub.remote(hub_id)):
logging.info('[autoremote] Flipping hub remote status from local to remote.')
hub.remote(hub_id, True)
remote = True
else:
# localHubs is valid so a hub is in the lan. A mixed environment cannot yet be detected.
# cloud_api.lan_ip cannot provide a map as to which ip is which hub. Thus we actually need to determine the right one.
# TODO(artanicus): Need to truly test how multihub works before implementing ip to hub resolution. See issue #7
logging.debug('data structure: {0}'.format(localHubs))
hub_ip = localHubs[0]
hub_info = hub_api.hub(host=hub_ip, remote=False)
# if the hub wants autoremote we flip the state
if hub.autoremote(hub_id) and hub.remote(hub_id):
# 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
if not hub.exists(hub_id) or (hub.autoremote(hub_id) and hub.remote(hub_id)):
logging.info('[autoremote] Flipping hub remote status from remote to local.')
hub.remote(hub_id, False)
remote = False

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

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

Expand Down
12 changes: 11 additions & 1 deletion cozify/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
def pytest_addoption(parser):
parser.addoption("--live", action="store_true",
default=False, help="run tests requiring a functional auth and a real hub.")
parser.addoption("--destructive", action="store_true",
default=False, help="run tests that require and modify the state of a real hub.")

def pytest_collection_modifyitems(config, items):
live = False
destructive = False
if config.getoption("--live"):
live = True
if config.getoption("--destructive"):
return
skip_live = pytest.mark.skip(reason="need --live option to run")
skip_destructive = pytest.mark.skip(reason="need --destructive option to run")

for item in items:
if "live" in item.keywords:
if "live" in item.keywords and not live:
item.add_marker(skip_live)
if "destructive" in item.keywords and not destructive:
item.add_marker(skip_destructive)
Loading

0 comments on commit da745b1

Please sign in to comment.