11"""Module for handling Cozify Cloud highlevel operations.
22"""
33
4- import logging , datetime
4+ from absl import logging
5+ import datetime
56
67from . import config
78from . 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
126131def 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
0 commit comments