mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Handle a refused grant in Nintendo add_bonus_time action (#195914)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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."
|
||||
},
|
||||
|
||||
@@ -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"),
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user