From 7051526046914e738cb2670e670ce5c1e13d0a7d Mon Sep 17 00:00:00 2001 From: theSiegs <24812171+theSiegs@users.noreply.github.com> Date: Wed, 2 Dec 2026 07:11:22 -0500 Subject: [PATCH] Handle a refused grant in Nintendo add_bonus_time action (#195914) --- .../nintendo_parental_controls/services.py | 12 +++++- .../nintendo_parental_controls/strings.json | 3 ++ .../test_services.py | 39 ++++++++++++++++++- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/nintendo_parental_controls/services.py b/homeassistant/components/nintendo_parental_controls/services.py index 4356a982ae81..43be22355e9b 100644 --- a/homeassistant/components/nintendo_parental_controls/services.py +++ b/homeassistant/components/nintendo_parental_controls/services.py @@ -10,13 +10,14 @@ from pynintendoparental.enum import SafeLaunchSetting from pynintendoparental.exceptions import ( BedtimeOutOfRangeError, ExtraPlayingTimeActiveError, + ExtraPlayingTimeRequestError, InvalidDeviceStateError, ) from pynintendoparental.player import Player from homeassistant.const import CommonServiceArgument from homeassistant.core import HomeAssistant, ServiceCall, SupportsResponse, callback -from homeassistant.exceptions import ServiceValidationError +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers import ( config_validation as cv, entity_registry as er, @@ -200,7 +201,14 @@ async def async_add_bonus_time(call: ServiceCall) -> None: device_id: str = data[CommonServiceArgument.DEVICE_ID] bonus_time: int = data[NintendoParentalServiceArgument.BONUS_TIME] device = _get_nintendo_device(call.hass, device_id) - return await device.add_extra_time(bonus_time) + try: + await device.add_extra_time(bonus_time) + except ExtraPlayingTimeRequestError as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="add_bonus_time_failed", + translation_placeholders={"status": err.status}, + ) from err async def async_update_pin_code(call: ServiceCall) -> None: diff --git a/homeassistant/components/nintendo_parental_controls/strings.json b/homeassistant/components/nintendo_parental_controls/strings.json index 182a4c96f55d..d58842cea89f 100644 --- a/homeassistant/components/nintendo_parental_controls/strings.json +++ b/homeassistant/components/nintendo_parental_controls/strings.json @@ -77,6 +77,9 @@ } }, "exceptions": { + "add_bonus_time_failed": { + "message": "Nintendo did not add the bonus time (status: {status})." + }, "auth_expired": { "message": "Authentication token expired." }, diff --git a/tests/components/nintendo_parental_controls/test_services.py b/tests/components/nintendo_parental_controls/test_services.py index 928367e18aa5..161cfd84b2fb 100644 --- a/tests/components/nintendo_parental_controls/test_services.py +++ b/tests/components/nintendo_parental_controls/test_services.py @@ -4,9 +4,11 @@ from datetime import time from typing import Any from unittest.mock import AsyncMock +from pynintendoparental.enum import ExtraPlayingTimeStatus from pynintendoparental.exceptions import ( BedtimeOutOfRangeError, ExtraPlayingTimeActiveError, + ExtraPlayingTimeRequestError, InvalidDeviceStateError, ) import pytest @@ -27,7 +29,11 @@ from homeassistant.components.nintendo_parental_controls.services import ( ) from homeassistant.const import CommonServiceArgument from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, Context, HomeAssistant -from homeassistant.exceptions import ServiceValidationError, Unauthorized +from homeassistant.exceptions import ( + HomeAssistantError, + ServiceValidationError, + Unauthorized, +) from homeassistant.helpers import device_registry as dr, entity_registry as er from . import setup_integration @@ -59,6 +65,37 @@ async def test_add_bonus_time( assert len(mock_nintendo_device.add_extra_time.mock_calls) == 1 +@pytest.mark.usefixtures("mock_nintendo_client") +async def test_add_bonus_time_refused( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + mock_config_entry: MockConfigEntry, + mock_nintendo_device: AsyncMock, +) -> None: + """Test add bonus time raises when Nintendo refuses the request.""" + mock_nintendo_device.add_extra_time.side_effect = ExtraPlayingTimeRequestError( + ExtraPlayingTimeStatus.OVERTIME_ERROR, {} + ) + await setup_integration(hass, mock_config_entry) + device_entry = device_registry.async_get_device_by_identifier( + (DOMAIN, "testdevid"), mock_config_entry.entry_id + ) + assert device_entry + with pytest.raises(HomeAssistantError) as err: + await hass.services.async_call( + DOMAIN, + NintendoParentalService.ADD_BONUS_TIME, + { + CommonServiceArgument.DEVICE_ID: device_entry.id, + NintendoParentalServiceArgument.BONUS_TIME: 15, + }, + blocking=True, + ) + assert err.value.translation_domain == DOMAIN + assert err.value.translation_key == "add_bonus_time_failed" + assert err.value.translation_placeholders == {"status": "OVERTIME_ERROR"} + + @pytest.mark.parametrize( ("service", "payload", "return_response", "exception_domain", "exception_key"), [