diff --git a/homeassistant/components/unifiprotect/services.py b/homeassistant/components/unifiprotect/services.py index 6a1daef178e..402aae2eeba 100644 --- a/homeassistant/components/unifiprotect/services.py +++ b/homeassistant/components/unifiprotect/services.py @@ -236,6 +236,9 @@ async def get_user_keyring_info(call: ServiceCall) -> ServiceResponse: """Get the user keyring info.""" camera = _async_get_ufp_camera(call) ulp_users = camera.api.bootstrap.ulp_users.as_list() + if not ulp_users: + raise HomeAssistantError("No users found, please check Protect permissions.") + user_keyrings: list[JsonValueType] = [ { KEYRINGS_USER_FULL_NAME: user.full_name, diff --git a/tests/components/unifiprotect/test_services.py b/tests/components/unifiprotect/test_services.py index efc9d1ace9e..9697d1f11a4 100644 --- a/tests/components/unifiprotect/test_services.py +++ b/tests/components/unifiprotect/test_services.py @@ -262,13 +262,13 @@ async def test_remove_privacy_zone( @pytest.mark.asyncio -async def test_get_doorbell_user( +async def get_user_keyring_info( hass: HomeAssistant, entity_registry: er.EntityRegistry, ufp: MockUFPFixture, doorbell: Camera, ) -> None: - """Test get_doorbell_user service.""" + """Test get_user_keyring_info service.""" ulp_user = Mock(full_name="Test User", status="active", ulp_id="user_ulp_id") keyring = Mock( @@ -315,3 +315,30 @@ async def test_get_doorbell_user( }, ], } + + +async def test_get_user_keyring_info_no_users( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + ufp: MockUFPFixture, + doorbell: Camera, +) -> None: + """Test get_user_keyring_info service with no users.""" + + ufp.api.bootstrap.ulp_users.as_list = Mock(return_value=[]) + ufp.api.bootstrap.keyrings.as_list = Mock(return_value=[]) + + await init_entry(hass, ufp, [doorbell]) + + camera_entry = entity_registry.async_get("binary_sensor.test_camera_doorbell") + + with pytest.raises( + HomeAssistantError, match="No users found, please check Protect permissions." + ): + await hass.services.async_call( + DOMAIN, + SERVICE_GET_USER_KEYRING_INFO, + {ATTR_DEVICE_ID: camera_entry.device_id}, + blocking=True, + return_response=True, + )