@@ -45,7 +45,7 @@ $ python setup.py install
4545#### Async example
4646
4747``` python
48- from crownstone_cloud import CrownstoneCloud
48+ from crownstone_cloud import CrownstoneCloud, create_clientsession
4949import logging
5050import asyncio
5151
@@ -54,37 +54,38 @@ logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
5454
5555
5656async def main ():
57+ # Every instance creates it's own websession for easy accessibility, however using 1 websession is recommended.
58+ # Create your websession like so:
59+ websession = create_clientsession()
5760 # Initialize cloud.
58- cloud = CrownstoneCloud()
61+ cloud_user_1 = CrownstoneCloud(' email_user_1 ' , ' password_user_1 ' , websession )
5962 # Login to the Crownstone Cloud and synchronize all cloud data.
60- # Important is to save your user id which is returned from the function!
61- user_id_1 = await cloud.async_initialize(' email_user_1' , ' password_user_1' )
63+ await cloud_user_1.async_initialize()
6264
63- # Get a crownstone by name that can dim, and put it on 20% brightness for user 1.
64- crownstone_lamp = cloud .get_crownstone(' Lamp' , user_id_1 )
65+ # Get a crownstone by name that can dim, and put it on 20% brightness for user 1
66+ crownstone_lamp = cloud_user_1 .get_crownstone(' Lamp' )
6567 await crownstone_lamp.async_set_brightness(20 )
6668
6769 # Login & synchronize data for an other account.
68- user_id_2 = await cloud.async_initialize(" email_user_2" , " password_user_2" )
70+ cloud_user_2 = CrownstoneCloud(' email_user_2' , ' password_user_2' , websession)
71+ await cloud_user_2.async_initialize()
6972
7073 # Get a crownstone by name and turn it on for user 2.
71- crownstone_tv = cloud .get_crownstone(' TV' , user_id_2 )
74+ crownstone_tv = cloud_user_2 .get_crownstone(' TV' )
7275 await crownstone_tv.async_turn_on()
7376
7477 # If you want to update specific data you can get the cloud data object for your user.
7578 # This object has all the cloud data for your user saved in it, which was synced with async_initialize()
7679 # Parts of the data can also be synced individually without touching the other data.
7780 # To sync all data at once, use async_synchronize() instead.
78- my_cloud_data = cloud.get_cloud_data(user_id_1)
79- # Now find the specific sphere object
80- my_sphere = my_cloud_data.find(" my_sphere_name" )
81+ my_sphere = cloud_user_1.cloud_data.find(" my_sphere_name" )
8182 # request to sync only the locations with the cloud
8283 my_sphere.locations.async_update_location_data()
8384 # get the keys for this sphere so you can use them with the Crownstone BLE python library
8485 sphere_keys = my_sphere.async_get_keys()
8586
8687 # Close the aiohttp clientsession after we are done.
87- await cloud.async_close_session ()
88+ await websession.close ()
8889
8990asyncio.run(main())
9091```
@@ -99,13 +100,13 @@ import logging
99100logging.basicConfig(format = ' %(levelname)s :%(message)s ' , level = logging.DEBUG )
100101
101102# Initialize cloud.
102- cloud = CrownstoneCloud()
103+ cloud = CrownstoneCloud(' email ' , ' password ' )
103104# Use 'run_async' to run async functions in sync context.
104- # Login & synchronize all cloud data. Save the user id that the function returns for later use.
105- my_user_id = run_async(cloud.async_initialize(' email ' , ' password ' ))
105+ # Login & synchronize all cloud data.
106+ run_async(cloud.async_initialize())
106107
107108# Get a crownstone by name and turn it on.
108- crownstone_coffee_machine = cloud.get_crownstone(' Coffee machine' , my_user_id )
109+ crownstone_coffee_machine = cloud.get_crownstone(' Coffee machine' )
109110run_async(crownstone_coffee_machine.async_turn_on())
110111
111112# Close the session after we are done.
@@ -114,29 +115,23 @@ run_async(cloud.async_close_session())
114115
115116### Initialization
116117
117- The Crownstone cloud can be created without any arguments like so :
118+ The Crownstone cloud is initialized with the email and password of a user :
118119``` python
119- cloud = CrownstoneCloud()
120+ cloud = CrownstoneCloud(' email ' , ' password ' )
120121```
121- A new aiohttp session will be created, which has to be closed at the end of the program.
122- If you are using this library in existing software that already uses an own websession, you can use this like so:
123- ``` python
124- cloud = CrownstoneCloud(websession)
125- ```
126- To log in to the Crownstone Cloud, the following are required:
127-
128- * User email
129- * User password
130-
131122If you do not yet have a Crownstone account, go to [ My Crownstone] ( https://my.crownstone.rocks ) to set one up.
132123The email and password are used to re-login after an access token has expired.
133124
125+ You can log into multiple accounts by creating more CrownstoneCloud objects. When doing so, it is recommended to use
126+ only 1 websession for all your requests. Create a websession and append it as parameter to all your CrownstoneCloud
127+ objects. Take a look at the async example above.
128+ ``` python
129+ cloud = CrownstoneCloud(' email' , ' password' , websession)
130+ ```
134131To log in and get all your Crownstone from the cloud:
135132``` python
136- await cloud.async_initialize(' email ' , ' password ' )
133+ await cloud.async_initialize()
137134```
138- This library supports logging in to multiple accounts. Simply call ` async_initialize() ` again with the email and
139- password of the other account. It is only required to call initialize once for each account.
140135
141136## Data structure
142137
@@ -205,7 +200,7 @@ Example names of Crownstones:
205200
206201A Crownstone has the following fields in the cloud lib:
207202* abilities: Dict
208- * state: Float (0..1 )
203+ * state: Int (0..100 )
209204* name: String
210205* unique_id: String
211206* cloud_id: String
@@ -232,20 +227,17 @@ A User has the following fields in the cloud lib:
232227
233228### Cloud
234229
235- #### async_initialize(email: String, password: String )
230+ #### async_initialize()
236231> Login and sync all data for the user from the cloud.
237232
238- #### async_synchronize(user_id: String )
233+ #### async_synchronize()
239234> Synchronize all data for a user. Use case is to update the local data with new data from the cloud.
240- > This function is already called in ` async_initialize() ` for new logins .
235+ > This function is already called in ` async_initialize() ` .
241236
242- #### get_cloud_data(user_id: String)
243- > Get the cloud data object for a logged in user.
244-
245- #### get_crownstone(crownstone_name: String, user_id: String) -> Crownstone
237+ #### get_crownstone(crownstone_name: String) -> Crownstone
246238> Get a Crownstone object by name for a user, if it exists.
247239
248- #### get_crownstone_by_id(crownstone_id: String, user_id: String ) -> Crownstone
240+ #### get_crownstone_by_id(crownstone_id: String) -> Crownstone
249241> Get a Crownstone object by it's id for a user, if it exists.
250242
251243#### async_close_session()
@@ -349,8 +341,6 @@ Make sure to see the examples above!
349341
350342## Testing
351343
352- ### Tests are not up-to-date yet for the newest commit, only run for version 1.2.1.
353-
354344To run the tests using tox install tox first by running:
355345``` console
356346$ pip install tox
@@ -359,7 +349,7 @@ To execute the tests cd to the project folder and run:
359349``` console
360350$ tox
361351```
362- To see which parts of the code are covered by the tests, a coverage report is generated after the tests have been successfull .<br >
352+ To see which parts of the code are covered by the tests, a coverage report is generated after the tests have been successful .<br >
363353To see the coverage report run:
364354``` console
365355$ coverage report
0 commit comments