@@ -139,6 +139,8 @@ async def test_rpc_device_services(
139139 {ATTR_ENTITY_ID : entity_id , ATTR_POSITION : 50 },
140140 blocking = True ,
141141 )
142+
143+ mock_rpc_device .cover_set_position .assert_called_once_with (0 , pos = 50 )
142144 assert (state := hass .states .get (entity_id ))
143145 assert state .attributes [ATTR_CURRENT_POSITION ] == 50
144146
@@ -153,6 +155,7 @@ async def test_rpc_device_services(
153155 )
154156 mock_rpc_device .mock_update ()
155157
158+ mock_rpc_device .cover_open .assert_called_once_with (0 )
156159 assert (state := hass .states .get (entity_id ))
157160 assert state .state == CoverState .OPENING
158161
@@ -167,6 +170,7 @@ async def test_rpc_device_services(
167170 )
168171 mock_rpc_device .mock_update ()
169172
173+ mock_rpc_device .cover_close .assert_called_once_with (0 )
170174 assert (state := hass .states .get (entity_id ))
171175 assert state .state == CoverState .CLOSING
172176
@@ -178,6 +182,8 @@ async def test_rpc_device_services(
178182 blocking = True ,
179183 )
180184 mock_rpc_device .mock_update ()
185+
186+ mock_rpc_device .cover_stop .assert_called_once_with (0 )
181187 assert (state := hass .states .get (entity_id ))
182188 assert state .state == CoverState .CLOSED
183189
@@ -262,9 +268,11 @@ async def test_rpc_cover_tilt(
262268 mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "slat_pos" , 50 )
263269 mock_rpc_device .mock_update ()
264270
271+ mock_rpc_device .cover_set_position .assert_called_once_with (0 , slat_pos = 50 )
265272 assert (state := hass .states .get (entity_id ))
266273 assert state .attributes [ATTR_CURRENT_TILT_POSITION ] == 50
267274
275+ mock_rpc_device .cover_set_position .reset_mock ()
268276 await hass .services .async_call (
269277 COVER_DOMAIN ,
270278 SERVICE_OPEN_COVER_TILT ,
@@ -274,9 +282,11 @@ async def test_rpc_cover_tilt(
274282 mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "slat_pos" , 100 )
275283 mock_rpc_device .mock_update ()
276284
285+ mock_rpc_device .cover_set_position .assert_called_once_with (0 , slat_pos = 100 )
277286 assert (state := hass .states .get (entity_id ))
278287 assert state .attributes [ATTR_CURRENT_TILT_POSITION ] == 100
279288
289+ mock_rpc_device .cover_set_position .reset_mock ()
280290 await hass .services .async_call (
281291 COVER_DOMAIN ,
282292 SERVICE_CLOSE_COVER_TILT ,
@@ -292,157 +302,78 @@ async def test_rpc_cover_tilt(
292302 mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "slat_pos" , 10 )
293303 mock_rpc_device .mock_update ()
294304
305+ mock_rpc_device .cover_stop .assert_called_once_with (0 )
295306 assert (state := hass .states .get (entity_id ))
296307 assert state .attributes [ATTR_CURRENT_TILT_POSITION ] == 10
297308
298309
299- async def test_update_position_closing (
310+ async def test_rpc_cover_position_update (
300311 hass : HomeAssistant ,
301312 freezer : FrozenDateTimeFactory ,
302313 mock_rpc_device : Mock ,
303314 monkeypatch : pytest .MonkeyPatch ,
304315) -> None :
305- """Test update_position while the cover is closing ."""
316+ """Test RPC update_position while the cover is moving ."""
306317 entity_id = "cover.test_name_test_cover_0"
307318 await init_integration (hass , 2 )
308319
309- # Set initial state to closing
320+ # Set initial state to closing, position 50 set by update_cover_status mock
310321 mutate_rpc_device_status (
311322 monkeypatch , mock_rpc_device , "cover:0" , "state" , "closing"
312323 )
313- mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , 40 )
314324 mock_rpc_device .mock_update ()
315325
316326 assert (state := hass .states .get (entity_id ))
317327 assert state .state == CoverState .CLOSING
318- assert state .attributes [ATTR_CURRENT_POSITION ] == 40
319-
320- # Simulate position decrement
321- async def simulated_update (* args , ** kwargs ):
322- pos = mock_rpc_device .status ["cover:0" ]["current_pos" ]
323- if pos > 0 :
324- mutate_rpc_device_status (
325- monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , pos - 10
326- )
327- else :
328- mutate_rpc_device_status (
329- monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , 0
330- )
331- mutate_rpc_device_status (
332- monkeypatch , mock_rpc_device , "cover:0" , "state" , "closed"
333- )
334-
335- # Patching the mock update_status method
336- monkeypatch .setattr (mock_rpc_device , "update_status" , simulated_update )
328+ assert state .attributes [ATTR_CURRENT_POSITION ] == 50
337329
338330 # Simulate position updates during closing
339331 for position in range (40 , - 1 , - 10 ):
340- assert (state := hass .states .get (entity_id ))
341- assert state .attributes [ATTR_CURRENT_POSITION ] == position
342- assert state .state == CoverState .CLOSING
332+ mock_rpc_device .update_cover_status .reset_mock ()
343333 await mock_polling_rpc_update (hass , freezer , RPC_COVER_UPDATE_TIME_SEC )
344334
345- # Final state should be closed
346- assert (state := hass .states .get (entity_id ))
347- assert state .state == CoverState .CLOSED
348- assert state .attributes [ATTR_CURRENT_POSITION ] == 0
349-
350-
351- async def test_update_position_opening (
352- hass : HomeAssistant ,
353- freezer : FrozenDateTimeFactory ,
354- mock_rpc_device : Mock ,
355- monkeypatch : pytest .MonkeyPatch ,
356- ) -> None :
357- """Test update_position while the cover is opening."""
358- entity_id = "cover.test_name_test_cover_0"
359- await init_integration (hass , 2 )
360-
361- # Set initial state to opening at 60
362- mutate_rpc_device_status (
363- monkeypatch , mock_rpc_device , "cover:0" , "state" , "opening"
364- )
365- mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , 60 )
366- mock_rpc_device .mock_update ()
367-
368- assert (state := hass .states .get (entity_id ))
369- assert state .state == CoverState .OPENING
370- assert state .attributes [ATTR_CURRENT_POSITION ] == 60
371-
372- # Simulate position increment
373- async def simulated_update (* args , ** kwargs ):
374- pos = mock_rpc_device .status ["cover:0" ]["current_pos" ]
375- if pos < 100 :
376- mutate_rpc_device_status (
377- monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , pos + 10
378- )
379- else :
380- mutate_rpc_device_status (
381- monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , 100
382- )
383- mutate_rpc_device_status (
384- monkeypatch , mock_rpc_device , "cover:0" , "state" , "open"
385- )
386-
387- # Patching the mock update_status method
388- monkeypatch .setattr (mock_rpc_device , "update_status" , simulated_update )
389-
390- # Check position updates during opening
391- for position in range (60 , 101 , 10 ):
335+ mock_rpc_device .update_cover_status .assert_called_once_with (0 )
392336 assert (state := hass .states .get (entity_id ))
393337 assert state .attributes [ATTR_CURRENT_POSITION ] == position
394- assert state .state == CoverState .OPENING
395- await mock_polling_rpc_update (hass , freezer , RPC_COVER_UPDATE_TIME_SEC )
396-
397- # Final state should be open
398- assert (state := hass .states .get (entity_id ))
399- assert state .state == CoverState .OPEN
400- assert state .attributes [ATTR_CURRENT_POSITION ] == 100
401-
402-
403- async def test_update_position_no_movement (
404- hass : HomeAssistant , mock_rpc_device : Mock , monkeypatch : pytest .MonkeyPatch
405- ) -> None :
406- """Test update_position when the cover is not moving."""
407- entity_id = "cover.test_name_test_cover_0"
408- await init_integration (hass , 2 )
338+ assert state .state == CoverState .CLOSING
409339
410- # Set initial state to open
411- mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "state" , "open" )
412- mutate_rpc_device_status (
413- monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , 100
414- )
340+ # Simulate cover reaching final position
341+ mock_rpc_device .update_cover_status .reset_mock ()
342+ mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "state" , "closed" )
415343 mock_rpc_device .mock_update ()
416344
417345 assert (state := hass .states .get (entity_id ))
418- assert state .state == CoverState .OPEN
419- assert state .attributes [ATTR_CURRENT_POSITION ] == 100
420-
421- # Call update_position and ensure no changes occur
422- await hass .services .async_call (
423- COVER_DOMAIN ,
424- SERVICE_OPEN_COVER ,
425- {ATTR_ENTITY_ID : entity_id },
426- blocking = True ,
427- )
346+ assert state .attributes [ATTR_CURRENT_POSITION ] == 0
347+ assert state .state == CoverState .CLOSED
428348
429- assert ( state := hass . states . get ( entity_id ))
430- assert state . state == CoverState . OPEN
431- assert state . attributes [ ATTR_CURRENT_POSITION ] == 100
349+ # Ensure update_position does not call update_cover_status when the cover is not moving
350+ await mock_polling_rpc_update ( hass , freezer , RPC_COVER_UPDATE_TIME_SEC )
351+ mock_rpc_device . update_cover_status . assert_not_called ()
432352
433353
434354async def test_rpc_not_initialized_update (
435- hass : HomeAssistant , mock_rpc_device : Mock , monkeypatch : pytest .MonkeyPatch
355+ hass : HomeAssistant ,
356+ mock_rpc_device : Mock ,
357+ monkeypatch : pytest .MonkeyPatch ,
358+ freezer : FrozenDateTimeFactory ,
436359) -> None :
437360 """Test update not called when device is not initialized."""
438361 entity_id = "cover.test_name_test_cover_0"
439362 await init_integration (hass , 2 )
440363
441- assert (state := hass .states .get (entity_id ))
442- assert state .state == CoverState .OPEN
364+ # Set initial state to closing
365+ mutate_rpc_device_status (
366+ monkeypatch , mock_rpc_device , "cover:0" , "state" , "closing"
367+ )
368+ mutate_rpc_device_status (monkeypatch , mock_rpc_device , "cover:0" , "current_pos" , 40 )
443369
370+ # mock device not initialized (e.g. disconnected)
444371 monkeypatch .setattr (mock_rpc_device , "initialized" , False )
445372 mock_rpc_device .mock_update ()
446373
374+ # wait for update interval to allow update_position to call update_cover_status
375+ await mock_polling_rpc_update (hass , freezer , RPC_COVER_UPDATE_TIME_SEC )
376+
377+ mock_rpc_device .update_cover_status .assert_not_called ()
447378 assert (state := hass .states .get (entity_id ))
448379 assert state .state == STATE_UNAVAILABLE
0 commit comments