1+ """Main class for the Crownstone cloud lib."""
12import hashlib
23import logging
34import asyncio
1112
1213
1314class 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