@@ -21,7 +21,20 @@ def __iter__(self):
2121
2222 async def update (self ) -> None :
2323 """
24- Get the crownstones and their state for this sphere from the cloud
24+ Update all cloud data
25+ The requests are done separately for testing.
26+ """
27+ # get all data first
28+ await self .update_crownstone_data ()
29+ # get the other data concurrently
30+ await asyncio .gather (
31+ self .update_state (),
32+ self .update_abilities ()
33+ )
34+
35+ async def update_crownstone_data (self ) -> None :
36+ """
37+ Get the crownstones data from the cloud
2538 This will replace all current data from the cloud with new data
2639 """
2740 self .crownstones = {}
@@ -35,14 +48,20 @@ async def update_state(self) -> None:
3548 crownstone_state = await RequestHandler .get ('Stones' , 'currentSwitchState' , model_id = crownstone .cloud_id )
3649 crownstone .state = crownstone_state ['switchState' ]
3750
51+ async def update_abilities (self ) -> None :
52+ """Get the abilities for all crownstones"""
53+ # only dimming is requested for now
54+ for crownstone in self .crownstones .values ():
55+ abilities = await RequestHandler .get ('Stones' , 'abilities' , model_id = crownstone .cloud_id )
56+ for ability in abilities :
57+ if ability ['type' ] == 'dimming' :
58+ crownstone .dimming_enabled = ability ['enabled' ]
59+ crownstone .dimming_synced_to_crownstone = ability ['syncedToCrownstone' ]
60+
3861 def update_sync (self ) -> None :
3962 """Sync function for updating the crownstone data"""
4063 self .loop .run_until_complete (self .update ())
4164
42- def update_state_sync (self ) -> None :
43- """Sync function for updating the crownstone state"""
44- self .loop .run_until_complete (self .update_state ())
45-
4665 def find (self , crownstone_name : str ) -> object or None :
4766 """Search for a crownstone by name and return crownstone object if found"""
4867 for crownstone in self .crownstones .values ():
@@ -63,7 +82,8 @@ def __init__(self, loop: asyncio.AbstractEventLoop, data: dict) -> None:
6382 self .loop = loop
6483 self .data = data
6584 self .state : Optional [float ] = None
66- self .dimming_enabled = False
85+ self .dimming_enabled : Optional [bool ] = None
86+ self .dimming_synced_to_crownstone : Optional [bool ] = None
6787
6888 @property
6989 def name (self ) -> str :
@@ -110,12 +130,16 @@ async def set_brightness(self, percentage: int) -> None:
110130 :param percentage: the brightness percentage (0 - 100)
111131 """
112132 if self .dimming_enabled :
113- if percentage < 0 or percentage > 100 :
114- raise ValueError ("Enter a percentage between 0 and 100" )
133+ if self .dimming_synced_to_crownstone :
134+ if percentage < 0 or percentage > 100 :
135+ raise ValueError ("Enter a percentage between 0 and 100" )
136+ else :
137+ brightness = percentage / 100
138+ await RequestHandler .put ('Stones' , 'setSwitchStateRemotely' , model_id = self .cloud_id ,
139+ command = 'switchState' , value = brightness )
115140 else :
116- brightness = percentage / 100
117- await RequestHandler .put ('Stones' , 'setSwitchStateRemotely' , model_id = self .cloud_id ,
118- command = 'switchState' , value = brightness )
141+ _LOGGER .error ("Dimming is enabled but not synced to crownstone yet. Make sure to be in your sphere "
142+ "and have Bluetooth enabled" )
119143 else :
120144 _LOGGER .error ("Dimming is not enabled for this crownstone. Go to the crownstone app to enable it" )
121145
@@ -125,4 +149,4 @@ def set_brightness_sync(self, percentage: int) -> None:
125149
126150 :param percentage: the brightness percentage (0 - 100)
127151 """
128- self .loop .run_until_complete (self .set_brightness (percentage ))
152+ self .loop .run_until_complete (self .set_brightness (percentage ))
0 commit comments