Skip to content

Commit 2416ce1

Browse files
committed
Existing cloud data can now be updated by running an update_data method. Changed async function prefix to async_. Added filter parameter to requestHandler get method. Updated tests & examples.
1 parent b7553f4 commit 2416ce1

20 files changed

Lines changed: 578 additions & 329 deletions
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Singleton that is used to perform all requests to the Crownstone cloud"""
12
from crownstone_cloud.lib.requestHandler import RequestHandler
23

3-
RequestHandler = RequestHandler()
4+
RequestHandler = RequestHandler()

crownstone_cloud/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from crownstone_cloud._RequestHandlerInstance import RequestHandler
2-
from crownstone_cloud.lib.cloud import CrownstoneCloud
2+
from crownstone_cloud.lib.cloud import CrownstoneCloud

crownstone_cloud/const.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
BASE_URL = "https://cloud.crownstone.rocks/api/"
1+
BASE_URL = "https://cloud.crownstone.rocks/api/"
2+
3+
# Crownstone abilities
4+
DIMMING_ABILITY = 'dimming'
5+
SWITCHCRAFT_ABILITY = 'switchcraft'
6+
TAP_TO_TOGGLE_ABILITY = 'tapToToggle'

crownstone_cloud/lib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Library that communicates with the Crownstone lib"""
22

3-
__version__ = '1.1.3'
3+
__version__ = '1.2.0'

crownstone_cloud/lib/cloud.py

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Main class for the Crownstone cloud lib."""
12
import hashlib
23
import logging
34
import asyncio
@@ -11,7 +12,7 @@
1112

1213

1314
class CrownstoneCloud:
14-
"""Create a Crownstone lib hub"""
15+
"""Create a Crownstone cloud instance."""
1516

1617
def __init__(
1718
self,
@@ -28,15 +29,19 @@ def __init__(
2829
# data
2930
self.spheres: Optional[Spheres] = None
3031

31-
async def initialize(self) -> None:
32-
"""Async initialize the cloud data"""
32+
async def async_initialize(self) -> None:
33+
"""
34+
Initialize the cloud data.
35+
36+
This method is a coroutine.
37+
"""
3338
if RequestHandler.access_token is None:
34-
await self.login()
35-
await self.sync()
39+
await self.async_login()
40+
await self.async_synchronize()
3641

37-
def initialize_sync(self) -> None:
38-
"""Sync initialize the cloud data"""
39-
self.loop.run_until_complete(self.initialize())
42+
def initialize(self) -> None:
43+
"""Initialize the cloud data."""
44+
self.loop.run_until_complete(self.async_initialize())
4045

4146
@staticmethod
4247
def set_access_token(access_token: str) -> None:
@@ -46,8 +51,12 @@ def set_access_token(access_token: str) -> None:
4651
def get_access_token() -> str:
4752
return RequestHandler.access_token
4853

49-
async def login(self) -> None:
50-
"""Login to Crownstone API"""
54+
async def async_login(self) -> None:
55+
"""
56+
Login to Crownstone API.
57+
58+
This method is a coroutine.
59+
"""
5160
result = await RequestHandler.post('users', 'login', json=self.login_data)
5261

5362
# Set access token & user id
@@ -56,53 +65,62 @@ async def login(self) -> None:
5665

5766
_LOGGER.info("Login to Crownstone Cloud successful")
5867

59-
async def sync(self) -> None:
60-
"""Sync all data from cloud"""
61-
_LOGGER.info("Initiating all cloud data, please wait...")
68+
async def async_synchronize(self) -> None:
69+
"""
70+
Sync all data from cloud.
71+
72+
This method is a coroutine.
73+
"""
74+
_LOGGER.info("Initiating all cloud data")
6275
# get the sphere data
63-
await self.spheres.update()
76+
await self.spheres.async_update_sphere_data()
6477

6578
# get the data from the sphere attributes
6679
for sphere in self.spheres:
6780
await asyncio.gather(
68-
sphere.update_sphere_presence(),
69-
sphere.crownstones.update(),
70-
sphere.locations.update(),
71-
sphere.users.update()
81+
sphere.async_update_sphere_presence(),
82+
sphere.crownstones.async_update_crownstone_data(),
83+
sphere.locations.async_update_location_data(),
84+
sphere.locations.async_update_location_presence(),
85+
sphere.users.async_update_user_data()
7286
)
7387
_LOGGER.info("Cloud data successfully initialized")
7488

7589
def get_crownstone(self, crownstone_name) -> Crownstone:
76-
"""Get a crownstone by name without specifying a sphere"""
90+
"""Get a crownstone by name without specifying a sphere."""
7791
for sphere in self.spheres:
7892
for crownstone in sphere.crownstones:
7993
if crownstone.name == crownstone_name:
8094
return crownstone
8195

8296
def get_crownstone_by_id(self, crownstone_id) -> Crownstone:
83-
"""Get a crownstone by id without specifying a sphere"""
97+
"""Get a crownstone by id without specifying a sphere."""
8498
for sphere in self.spheres:
8599
return sphere.crownstones[crownstone_id]
86100

87101
@staticmethod
88102
def reset() -> None:
89-
"""Cleanup the request handler instance data"""
103+
"""Cleanup the request handler instance data."""
90104
RequestHandler.access_token = None
91105
RequestHandler.login_data = None
92106

93107
@staticmethod
94-
async def close_session() -> None:
95-
"""Close the websession after we are done"""
108+
async def async_close_session() -> None:
109+
"""
110+
Close the websession after we are done.
111+
112+
This method is a coroutine.
113+
"""
96114
await RequestHandler.websession.close()
97-
_LOGGER.warning("Session closed.")
115+
_LOGGER.info("Session closed.")
98116

99-
def close_session_sync(self) -> None:
100-
"""Sync version of close session"""
101-
self.loop.run_until_complete(self.close_session())
117+
def close_session(self) -> None:
118+
"""Close the websession after we are done."""
119+
self.loop.run_until_complete(self.async_close_session())
102120

103121
@staticmethod
104122
def password_to_hash(password):
105-
"""Generate a sha1 password from string"""
123+
"""Generate a sha1 password from string."""
106124
if password is None:
107125
return None
108126
pw_hash = hashlib.sha1(password.encode('utf-8'))

0 commit comments

Comments
 (0)