4
4
5
5
import datetime
6
6
from typing import TYPE_CHECKING , Any
7
- from unittest .mock import AsyncMock , Mock , patch
7
+ from unittest .mock import AsyncMock , Mock
8
8
9
9
import homeassistant .util .dt as dt_util
10
10
import pytest
74
74
75
75
async def async_setup (
76
76
hass : HomeAssistant ,
77
- raises : bool = True ,
77
+ raises : bool = True , # noqa: FBT001, FBT002
78
78
options : dict | None = None ,
79
79
) -> list [ServiceCall ]:
80
80
"""Load retry custom integration and basic environment."""
@@ -102,7 +102,7 @@ def async_service(service_call: ServiceCall) -> None:
102
102
"""Mock service call."""
103
103
calls .append (service_call )
104
104
if service_call .service == TEST_SERVICE and raises :
105
- raise Exception # pylint: disable=broad-exception-raised
105
+ raise Exception # noqa: TRY002
106
106
107
107
hass .services .async_register (
108
108
DOMAIN ,
@@ -209,9 +209,9 @@ async def test_entity_unavailable(
209
209
for entity in entities :
210
210
assert f"{ entity } is not available" in caplog .text
211
211
assert (
212
- f"[Failed]: attempt 7/7: { DOMAIN } .{ TEST_SERVICE } (entity_id={ entity } )[expected_state=on] "
213
- in caplog . text
214
- )
212
+ f"[Failed]: attempt 7/7: { DOMAIN } .{ TEST_SERVICE } (entity_id={ entity } )"
213
+ "[expected_state=on]"
214
+ ) in caplog . text
215
215
216
216
217
217
async def test_selective_retry (
@@ -240,12 +240,11 @@ async def test_selective_retry(
240
240
],
241
241
ids = ["default" , "grace" , "validation" ],
242
242
)
243
- @patch ("custom_components.retry.asyncio.sleep" )
244
- async def test_entity_wrong_state (
245
- sleep_mock : AsyncMock ,
243
+ async def test_entity_wrong_state ( # noqa: PLR0913
246
244
hass : HomeAssistant ,
247
245
freezer : FrozenDateTimeFactory ,
248
246
caplog : pytest .LogCaptureFixture ,
247
+ sleep : AsyncMock ,
249
248
expected_state : str | None ,
250
249
validation : str | None ,
251
250
grace : float | None ,
@@ -270,16 +269,15 @@ async def test_entity_wrong_state(
270
269
if validation :
271
270
validation = validation .replace ("[" , "{" ).replace ("]" , "}" )
272
271
assert f'"{ validation } " is False' in caplog .text
273
- wait_times = [x .args [0 ] for x in sleep_mock .await_args_list ]
272
+ wait_times = [x .args [0 ] for x in sleep .await_args_list ]
274
273
assert wait_times .count (grace or 0.2 ) == 7
275
274
276
275
277
- @patch ("custom_components.retry.asyncio.sleep" )
278
276
async def test_state_delay (
279
- sleep_mock : AsyncMock ,
280
277
hass : HomeAssistant ,
281
278
freezer : FrozenDateTimeFactory ,
282
279
caplog : pytest .LogCaptureFixture ,
280
+ sleep : AsyncMock ,
283
281
) -> None :
284
282
"""Test initial state delay time."""
285
283
await async_setup (hass , raises = False )
@@ -292,13 +290,13 @@ async def test_state_delay(
292
290
},
293
291
)
294
292
await async_shutdown (hass , freezer )
295
- wait_times = [x .args [0 ] for x in sleep_mock .await_args_list ]
293
+ wait_times = [x .args [0 ] for x in sleep .await_args_list ]
296
294
assert wait_times .count (1.2 ) == 7 # state_delay
297
295
assert wait_times .count (0.2 ) == 7 # state_grace
298
296
assert (
299
- f"[Failed]: attempt 7/7: { DOMAIN } .{ TEST_SERVICE } (entity_id=binary_sensor.test)[expected_state=off, state_delay=1.2] "
300
- in caplog . text
301
- )
297
+ f"[Failed]: attempt 7/7: { DOMAIN } .{ TEST_SERVICE } (entity_id=binary_sensor.test)"
298
+ "[expected_state=off, state_delay=1.2]"
299
+ ) in caplog . text
302
300
303
301
304
302
async def test_entity_expected_state_list (
@@ -318,9 +316,7 @@ async def test_entity_expected_state_list(
318
316
assert len (calls ) == 1
319
317
320
318
321
- @patch ("custom_components.retry.asyncio.sleep" )
322
319
async def test_validation_success (
323
- _ : AsyncMock ,
324
320
hass : HomeAssistant ,
325
321
freezer : FrozenDateTimeFactory ,
326
322
) -> None :
@@ -339,9 +335,7 @@ async def test_validation_success(
339
335
assert len (calls ) == 1
340
336
341
337
342
- @patch ("custom_components.retry.asyncio.sleep" )
343
338
async def test_float_point_zero (
344
- _ : AsyncMock ,
345
339
hass : HomeAssistant ,
346
340
freezer : FrozenDateTimeFactory ,
347
341
) -> None :
@@ -507,9 +501,7 @@ async def test_on_error(
507
501
assert calls [- 1 ].data [ATTR_ENTITY_ID ] == "binary_sensor.test"
508
502
509
503
510
- @patch ("custom_components.retry.asyncio.sleep" )
511
504
async def test_validation_in_automation (
512
- _ : AsyncMock ,
513
505
hass : HomeAssistant ,
514
506
freezer : FrozenDateTimeFactory ,
515
507
) -> None :
@@ -542,7 +534,10 @@ def async_service(service_call: ServiceCall) -> None:
542
534
ATTR_SERVICE : f"{ DOMAIN } .{ CALL_SERVICE } " ,
543
535
"data" : {
544
536
ATTR_SERVICE : f"{ DOMAIN } .tick" ,
545
- ATTR_VALIDATION : f"[[ now().timestamp() == { dt_util .now ().timestamp ()} ]]" ,
537
+ ATTR_VALIDATION : (
538
+ "[[ now().timestamp() == "
539
+ f"{ dt_util .now ().timestamp ()} ]]"
540
+ ),
546
541
},
547
542
}
548
543
],
@@ -670,9 +665,7 @@ async def test_invalid_validation(hass: HomeAssistant) -> None:
670
665
],
671
666
ids = ["call-param" , "call-target" , "actions" ],
672
667
)
673
- @patch ("custom_components.retry.asyncio.sleep" )
674
- async def test_all_entities (
675
- _ : AsyncMock ,
668
+ async def test_all_entities ( # noqa: PLR0913
676
669
hass : HomeAssistant ,
677
670
freezer : FrozenDateTimeFactory ,
678
671
caplog : pytest .LogCaptureFixture ,
@@ -697,9 +690,9 @@ async def test_all_entities(
697
690
await async_shutdown (hass , freezer )
698
691
for i in [1 , 2 ]:
699
692
assert (
700
- f"[Failed]: attempt 7/7: script.turn_off(entity_id=script.test{ i } )[expected_state=on] "
701
- in caplog . text
702
- )
693
+ f"[Failed]: attempt 7/7: script.turn_off(entity_id=script.test{ i } )"
694
+ "[expected_state=on]"
695
+ ) in caplog . text
703
696
704
697
705
698
async def test_disable_repair (
@@ -892,9 +885,7 @@ async def test_action_types() -> None:
892
885
]
893
886
894
887
895
- @patch ("custom_components.retry.asyncio.sleep" )
896
888
async def test_actions_propagating_args (
897
- _ : AsyncMock ,
898
889
hass : HomeAssistant ,
899
890
freezer : FrozenDateTimeFactory ,
900
891
caplog : pytest .LogCaptureFixture ,
@@ -915,9 +906,8 @@ async def test_actions_propagating_args(
915
906
await async_shutdown (hass , freezer )
916
907
assert len (calls ) == 4
917
908
assert (
918
- f'[Failed]: attempt 3/3: { DOMAIN } .{ TEST_SERVICE } ()[validation="{{{{ False }}}}"]'
919
- in caplog .text
920
- )
909
+ f"[Failed]: attempt 3/3: { DOMAIN } .{ TEST_SERVICE } ()" '[validation="{{ False }}"]'
910
+ ) in caplog .text
921
911
922
912
923
913
@pytest .mark .parametrize (
@@ -933,9 +923,7 @@ async def test_actions_propagating_args(
933
923
],
934
924
ids = ["default - exponential backoff" , "linear" , "slow exponential backoff" ],
935
925
)
936
- @patch ("custom_components.retry.asyncio.sleep" )
937
- async def test_actions_backoff (
938
- _ : AsyncMock ,
926
+ async def test_actions_backoff ( # noqa: PLR0913
939
927
hass : HomeAssistant ,
940
928
freezer : FrozenDateTimeFactory ,
941
929
caplog : pytest .LogCaptureFixture ,
0 commit comments