1
- from idasen import _bytes_to_meters , _is_desk
1
+ from idasen import _bytes_to_meters_and_speed , _meters_to_bytes , _is_desk
2
2
from idasen import IdasenDesk
3
3
from types import SimpleNamespace
4
4
from typing import AsyncGenerator
@@ -31,10 +31,12 @@ class MockBleakClient:
31
31
32
32
def __init__ (self ):
33
33
self ._height = 1.0
34
+ self ._is_moving = False
34
35
self .is_connected = False
35
36
36
37
async def __aenter__ (self ):
37
38
self ._height = 1.0
39
+ self ._is_moving = False
38
40
self .is_connected = True
39
41
return self
40
42
@@ -52,22 +54,25 @@ async def start_notify(self, uuid: str, callback: Callable):
52
54
await callback (uuid , bytearray ([0x00 , 0x00 , 0x00 , 0x00 ]))
53
55
await callback (None , bytearray ([0x10 , 0x00 , 0x00 , 0x00 ]))
54
56
55
- async def write_gatt_char (
56
- self , uuid : str , command : bytearray , response : bool = False
57
- ):
57
+ async def write_gatt_char (self , uuid : str , data : bytearray , response : bool = False ):
58
58
if uuid == idasen ._UUID_COMMAND :
59
- if command == idasen ._COMMAND_UP :
59
+ if data == idasen ._COMMAND_UP :
60
60
self ._height += 0.001
61
- elif command == idasen ._COMMAND_DOWN :
61
+ elif data == idasen ._COMMAND_DOWN :
62
62
self ._height -= 0.001
63
+ if uuid == idasen ._UUID_REFERENCE_INPUT :
64
+ assert len (data ) == 2
65
+
66
+ data_with_speed = bytearray ([data [0 ], data [1 ], 0 , 0 ])
67
+ requested_height , _ = _bytes_to_meters_and_speed (data_with_speed )
68
+ self ._height += min (0.1 , max (- 0.1 , requested_height - self ._height ))
69
+
70
+ self ._is_moving = self ._height != requested_height
63
71
64
72
async def read_gatt_char (self , uuid : str ) -> bytearray :
65
- norm = self ._height - IdasenDesk .MIN_HEIGHT
66
- norm *= 10000
67
- norm = int (norm )
68
- low_byte = norm & 0xFF
69
- high_byte = (norm >> 8 ) & 0xFF
70
- return bytearray ([low_byte , high_byte , 0x00 , 0x00 ])
73
+ height_bytes = _meters_to_bytes (self ._height )
74
+ speed_byte = 0x01 if self ._is_moving else 0x00
75
+ return bytearray ([height_bytes [0 ], height_bytes [1 ], 0x00 , speed_byte ])
71
76
72
77
@property
73
78
def address (self ) -> str :
@@ -139,7 +144,7 @@ async def test_move_to_target_raises(desk: IdasenDesk, target: float):
139
144
@pytest .mark .parametrize ("target" , [0.7 , 1.1 ])
140
145
async def test_move_to_target (desk : IdasenDesk , target : float ):
141
146
await desk .move_to_target (target )
142
- assert abs (await desk .get_height () - target ) < 0.005
147
+ assert abs (await desk .get_height () - target ) < 0.001
143
148
144
149
145
150
async def test_move_abort_when_no_movement ():
@@ -190,16 +195,17 @@ async def write_gatt_char_mock(
190
195
191
196
192
197
@pytest .mark .parametrize (
193
- "raw, result " ,
198
+ "raw, height, speed " ,
194
199
[
195
- (bytearray ([0x64 , 0x19 , 0x00 , 0x00 ]), IdasenDesk .MAX_HEIGHT ),
196
- (bytearray ([0x00 , 0x00 , 0x00 , 0x00 ]), IdasenDesk .MIN_HEIGHT ),
197
- (bytearray ([0x51 , 0x04 , 0x00 , 0x00 ]), 0.7305 ),
198
- (bytearray ([0x08 , 0x08 , 0x00 , 0x00 ]), 0.8256 ),
200
+ (bytearray ([0x64 , 0x19 , 0x00 , 0x00 ]), IdasenDesk .MAX_HEIGHT , 0 ),
201
+ (bytearray ([0x00 , 0x00 , 0x00 , 0x00 ]), IdasenDesk .MIN_HEIGHT , 0 ),
202
+ (bytearray ([0x51 , 0x04 , 0x00 , 0x00 ]), 0.7305 , 0 ),
203
+ (bytearray ([0x08 , 0x08 , 0x00 , 0x00 ]), 0.8256 , 0 ),
204
+ (bytearray ([0x08 , 0x08 , 0x02 , 0x01 ]), 0.8256 , 258 ),
199
205
],
200
206
)
201
- def test_bytes_to_meters (raw : bytearray , result : float ):
202
- assert _bytes_to_meters (raw ) == result
207
+ def test_bytes_to_meters_and_speed (raw : bytearray , height : float , speed : int ):
208
+ assert _bytes_to_meters_and_speed (raw ) == ( height , speed )
203
209
204
210
205
211
async def test_fail_to_connect (caplog , monkeypatch ):
0 commit comments