|
21 | 21 | get_entity, |
22 | 22 | get_group_entity, |
23 | 23 | join_zigpy_device, |
| 24 | + zigpy_device_from_json, |
24 | 25 | ) |
25 | 26 | from zha.application import Platform |
26 | 27 | from zha.application.const import ( |
@@ -882,3 +883,86 @@ async def test_gateway_energy_scan(zha_gateway: Gateway) -> None: |
882 | 883 | assert mock_scan.mock_calls == [ |
883 | 884 | call(channels=channels, duration_exp=4, count=1) |
884 | 885 | ] |
| 886 | + |
| 887 | + |
| 888 | +async def test_gateway_shutdown_device_on_remove_failure( |
| 889 | + zha_gateway: Gateway, |
| 890 | + caplog: pytest.LogCaptureFixture, |
| 891 | +) -> None: |
| 892 | + """Test that gateway shutdown continues when device.on_remove fails.""" |
| 893 | + zigpy_dev = await zigpy_device_from_json( |
| 894 | + zha_gateway.application_controller, |
| 895 | + "tests/data/devices/centralite-3320-l.json", |
| 896 | + ) |
| 897 | + zha_device = await join_zigpy_device(zha_gateway, zigpy_dev) |
| 898 | + |
| 899 | + with patch.object( |
| 900 | + zha_device, "on_remove", side_effect=Exception("Device removal failed") |
| 901 | + ): |
| 902 | + await zha_gateway.shutdown() |
| 903 | + await zha_gateway.async_block_till_done() |
| 904 | + |
| 905 | + assert "Failed to remove device" in caplog.text |
| 906 | + assert "Device removal failed" in caplog.text |
| 907 | + |
| 908 | + |
| 909 | +async def test_gateway_shutdown_group_on_remove_failure( |
| 910 | + zha_gateway: Gateway, |
| 911 | + caplog: pytest.LogCaptureFixture, |
| 912 | +) -> None: |
| 913 | + """Test that gateway shutdown continues when group.on_remove fails.""" |
| 914 | + zigpy_dev_1 = await zigpy_device_from_json( |
| 915 | + zha_gateway.application_controller, |
| 916 | + "tests/data/devices/ikea-of-sweden-tradfri-bulb-gu10-ws-400lm.json", |
| 917 | + ) |
| 918 | + zha_device_1 = await join_zigpy_device(zha_gateway, zigpy_dev_1) |
| 919 | + |
| 920 | + zha_group = await zha_gateway.async_create_zigpy_group( |
| 921 | + "Test Group", |
| 922 | + [GroupMemberReference(ieee=zha_device_1.ieee, endpoint_id=1)], |
| 923 | + ) |
| 924 | + await zha_gateway.async_block_till_done() |
| 925 | + |
| 926 | + with patch.object( |
| 927 | + zha_group, "on_remove", side_effect=Exception("Group removal failed") |
| 928 | + ): |
| 929 | + await zha_gateway.shutdown() |
| 930 | + await zha_gateway.async_block_till_done() |
| 931 | + |
| 932 | + assert "Failed to remove group" in caplog.text |
| 933 | + assert "Group removal failed" in caplog.text |
| 934 | + |
| 935 | + |
| 936 | +async def test_group_on_remove_entity_failure( |
| 937 | + zha_gateway: Gateway, |
| 938 | + caplog: pytest.LogCaptureFixture, |
| 939 | +) -> None: |
| 940 | + """Test that group.on_remove continues when group entity removal fails.""" |
| 941 | + zigpy_dev_1 = await zigpy_device_from_json( |
| 942 | + zha_gateway.application_controller, |
| 943 | + "tests/data/devices/ikea-of-sweden-tradfri-bulb-gu10-ws-400lm.json", |
| 944 | + ) |
| 945 | + zigpy_dev_2 = await zigpy_device_from_json( |
| 946 | + zha_gateway.application_controller, |
| 947 | + "tests/data/devices/ikea-of-sweden-tradfri-bulb-e26-opal-1000lm.json", |
| 948 | + ) |
| 949 | + zha_device_1 = await join_zigpy_device(zha_gateway, zigpy_dev_1) |
| 950 | + zha_device_2 = await join_zigpy_device(zha_gateway, zigpy_dev_2) |
| 951 | + |
| 952 | + members = [ |
| 953 | + GroupMemberReference(ieee=zha_device_1.ieee, endpoint_id=1), |
| 954 | + GroupMemberReference(ieee=zha_device_2.ieee, endpoint_id=1), |
| 955 | + ] |
| 956 | + |
| 957 | + zha_group = await zha_gateway.async_create_zigpy_group("Test Group", members) |
| 958 | + await zha_gateway.async_block_till_done() |
| 959 | + |
| 960 | + group_entity = get_group_entity(zha_group, platform=Platform.LIGHT) |
| 961 | + |
| 962 | + with patch.object( |
| 963 | + group_entity, "on_remove", side_effect=Exception("Group entity removal failed") |
| 964 | + ): |
| 965 | + await zha_group.on_remove() |
| 966 | + |
| 967 | + assert "Failed to remove group entity" in caplog.text |
| 968 | + assert "Group entity removal failed" in caplog.text |
0 commit comments