Skip to content

Commit a8f0895

Browse files
committed
Updated to new API with switch 0-100.
1 parent 320a379 commit a8f0895

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

crownstone_cloud/cloud_models/crownstones.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,18 @@ def icon(self) -> str:
140140
return self.data['icon']
141141

142142
@property
143-
def state(self) -> float:
144-
"""Return the last reported state for this Crownstone."""
145-
return self.data['currentSwitchState']['switchState']
143+
def state(self) -> int:
144+
"""Return the last reported switch state (0 - 100) of this Crownstone."""
145+
return self.data['currentSwitchStateV2']['switchState']
146146

147147
@state.setter
148-
def state(self, value: float) -> None:
149-
"""Set a new state for this Crownstone."""
150-
self.data['currentSwitchState']['switchState'] = value
148+
def state(self, value: int) -> None:
149+
"""
150+
Set a new switch state (0 - 100) of this Crownstone.
151+
152+
Only updates state in cloud, does not send a switch command to the actual Crownstone.
153+
"""
154+
self.data['currentSwitchStateV2']['switchState'] = value
151155

152156
def update_abilities(self) -> None:
153157
"""Add/update the abilities for this Crownstone."""
@@ -163,8 +167,8 @@ async def async_turn_on(self) -> None:
163167
# make sure to use the context of what the object was created in.
164168
self.cloud.login_manager.set_context(self._context)
165169
# send a command to the cloud to turn the Crownstone on.
166-
await self.cloud.request_handler.put(
167-
'Stones', 'setSwitchStateRemotely', model_id=self.cloud_id, command='switchState', value=1
170+
await self.cloud.request_handler.post(
171+
'Stones', 'switch', model_id=self.cloud_id, json={"type": "TURN_ON"}
168172
)
169173

170174
async def async_turn_off(self) -> None:
@@ -176,8 +180,8 @@ async def async_turn_off(self) -> None:
176180
# make sure to use the context of what the object was created in.
177181
self.cloud.login_manager.set_context(self._context)
178182
# send a command to the cloud to turn the Crownstone off.
179-
await self.cloud.request_handler.put(
180-
'Stones', 'setSwitchStateRemotely', model_id=self.cloud_id, command='switchState', value=0
183+
await self.cloud.request_handler.post(
184+
'Stones', 'switch', model_id=self.cloud_id, json={"type": "TURN_OFF"}
181185
)
182186

183187
async def async_set_brightness(self, brightness: int) -> None:
@@ -195,10 +199,9 @@ async def async_set_brightness(self, brightness: int) -> None:
195199
if brightness < 0 or brightness > 100:
196200
raise ValueError("Enter a value between 0 and 100")
197201
else:
198-
# cloud still uses float value from 0 ... 1
199-
float_val = brightness / 100
200-
await self.cloud.request_handler.put(
201-
'Stones', 'setSwitchStateRemotely', model_id=self.cloud_id, command='switchState', value=float_val
202+
await self.cloud.request_handler.post(
203+
'Stones', 'switch', model_id=self.cloud_id, json={"type": "PERCENTAGE","percentage": brightness}
202204
)
203205
else:
204206
_LOGGER.warning("Dimming is not enabled for this crownstone. Go to the crownstone app to enable it")
207+
# TODO: raise error, or just try to set brightness anyway?

tests/test_cloud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ async def test_data_structure(self, mock_request):
155155
# test setting brightness of a crownstone
156156
with asynctest.patch.object(RequestHandler, 'put') as brightness_mock:
157157
# test if it doesn't run if dimming not enabled
158-
await crownstone_by_id.async_set_brightness(0.5)
158+
await crownstone_by_id.async_set_brightness(50)
159159
brightness_mock.assert_not_called()
160160
# test error when wrong value is given
161161
with self.assertRaises(ValueError):
162-
await crownstone.async_set_brightness(2)
162+
await crownstone.async_set_brightness(101)
163163

164164
def tearDown(self) -> None:
165165
self.cloud.close_session()

0 commit comments

Comments
 (0)