Skip to content

Commit 3d07c91

Browse files
committed
Added getting the dimming ability for every crownstone. Updated update functions
1 parent 3e09ebf commit 3d07c91

7 files changed

Lines changed: 54 additions & 24 deletions

File tree

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.0.5'
3+
__version__ = '1.1.0'

crownstone_cloud/lib/cloud.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def login(self) -> None:
6161

6262
async def sync(self) -> None:
6363
"""Sync all data from cloud"""
64-
_LOGGER.warning("Initiating all cloud data, please wait...")
64+
_LOGGER.info("Initiating all cloud data, please wait...")
6565
# get the sphere data
6666
await self.spheres.update()
6767

@@ -70,12 +70,10 @@ async def sync(self) -> None:
7070
await asyncio.gather(
7171
sphere.update_sphere_presence(),
7272
sphere.crownstones.update(),
73-
sphere.crownstones.update_state(),
7473
sphere.locations.update(),
75-
sphere.locations.update_location_presence(),
7674
sphere.users.update()
7775
)
78-
_LOGGER.warning("Cloud data successfully initialized")
76+
_LOGGER.info("Cloud data successfully initialized")
7977

8078
def get_crownstone(self, crownstone_name) -> Crownstone:
8179
"""Get a crownstone by name without specifying a sphere"""

crownstone_cloud/lib/cloudModels/crownstones.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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))

crownstone_cloud/lib/cloudModels/locations.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ def __iter__(self):
1717
return iter(self.locations.values())
1818

1919
async def update(self) -> None:
20+
"""
21+
Update all cloud data
22+
The requests are done separately for testing.
23+
"""
24+
# get location data first
25+
await self.update_location_data()
26+
# get the presence
27+
await self.update_location_presence()
28+
29+
async def update_location_data(self) -> None:
2030
"""
2131
Get the locations and presence from the cloud
2232
This will replace all current data with new data from the cloud
@@ -39,10 +49,6 @@ def update_sync(self) -> None:
3949
"""Sync function for updating the location data"""
4050
self.loop.run_until_complete(self.update())
4151

42-
def update_presence_sync(self) -> None:
43-
"""Sync function for updating the presence"""
44-
self.loop.run_until_complete(self.update_location_presence())
45-
4652
def find(self, location_name: str) -> object or None:
4753
"""Search for a sphere by name and return sphere object if found"""
4854
for location in self.locations.values():

crownstone_cloud/lib/cloudModels/spheres.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async def update(self) -> None:
2424
Get the spheres for the user from the cloud
2525
This will replace all current data with new data from the cloud
2626
"""
27+
# get sphere data
2728
self.spheres = {}
2829
sphere_data = await RequestHandler.get('users', 'spheres', model_id=self.user_id)
2930
for sphere in sphere_data:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='crownstone-lib-python-cloud',
5-
version='1.0.5',
5+
version='1.1.0',
66
url='https://github.com/crownstone/crownstone-lib-python-cloud',
77
author='Crownstone B.V.',
88
description='Async library to get & store data from the Crownstone cloud.',

tests/test_cloud.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ async def test_data_structure(self, mock_request):
9494

9595
# add fake crownstone data for test
9696
mock_request.return_value = crownstone_data
97-
await sphere.crownstones.update()
97+
await sphere.crownstones.update_crownstone_data()
9898
mock_request.assert_awaited()
9999
# state
100100
mock_request.return_value = switch_state_data
@@ -108,7 +108,7 @@ async def test_data_structure(self, mock_request):
108108

109109
# add fake location data for test
110110
mock_request.return_value = location_data
111-
await sphere.locations.update()
111+
await sphere.locations.update_location_data()
112112
mock_request.assert_awaited()
113113
# presence
114114
mock_request.return_value = presence_data
@@ -146,5 +146,6 @@ async def test_data_structure(self, mock_request):
146146
brightness_mock.assert_not_called()
147147
# set dimming to true for test
148148
crownstone.dimming_enabled = True
149+
crownstone.dimming_synced_to_crownstone = True
149150
with self.assertRaises(ValueError):
150151
await crownstone.set_brightness(200)

0 commit comments

Comments
 (0)