mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Update pynintendoparental to 2.0.0 (#158285)
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pynintendoparental import Authenticator
|
||||
from pynintendoparental.exceptions import (
|
||||
from pynintendoauth.exceptions import (
|
||||
InvalidOAuthConfigurationException,
|
||||
InvalidSessionTokenException,
|
||||
)
|
||||
from pynintendoparental import Authenticator
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
@@ -39,13 +39,12 @@ async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: NintendoParentalControlsConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Nintendo Switch parental controls from a config entry."""
|
||||
nintendo_auth = Authenticator(
|
||||
session_token=entry.data[CONF_SESSION_TOKEN],
|
||||
client_session=async_get_clientsession(hass),
|
||||
)
|
||||
try:
|
||||
nintendo_auth = await Authenticator.complete_login(
|
||||
auth=None,
|
||||
response_token=entry.data[CONF_SESSION_TOKEN],
|
||||
is_session_token=True,
|
||||
client_session=async_get_clientsession(hass),
|
||||
)
|
||||
await nintendo_auth.async_complete_login(use_session_token=True)
|
||||
except (InvalidSessionTokenException, InvalidOAuthConfigurationException) as err:
|
||||
raise ConfigEntryAuthFailed(
|
||||
translation_domain=DOMAIN,
|
||||
|
||||
@@ -6,9 +6,9 @@ from collections.abc import Mapping
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from pynintendoauth.exceptions import HttpException, InvalidSessionTokenException
|
||||
from pynintendoparental import Authenticator
|
||||
from pynintendoparental.api import Api
|
||||
from pynintendoparental.exceptions import HttpException, InvalidSessionTokenException
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
@@ -33,18 +33,14 @@ class NintendoConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle the initial step."""
|
||||
errors = {}
|
||||
if self.auth is None:
|
||||
self.auth = Authenticator.generate_login(
|
||||
client_session=async_get_clientsession(self.hass)
|
||||
)
|
||||
self.auth = Authenticator(client_session=async_get_clientsession(self.hass))
|
||||
|
||||
if user_input is not None:
|
||||
nintendo_api = Api(
|
||||
self.auth, self.hass.config.time_zone, self.hass.config.language
|
||||
)
|
||||
try:
|
||||
await self.auth.complete_login(
|
||||
self.auth, user_input[CONF_API_TOKEN], False
|
||||
)
|
||||
await self.auth.async_complete_login(user_input[CONF_API_TOKEN])
|
||||
except (ValueError, InvalidSessionTokenException, HttpException):
|
||||
errors["base"] = "invalid_auth"
|
||||
else:
|
||||
@@ -67,7 +63,7 @@ class NintendoConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
return self.async_create_entry(
|
||||
title=self.auth.account_id,
|
||||
data={
|
||||
CONF_SESSION_TOKEN: self.auth.get_session_token,
|
||||
CONF_SESSION_TOKEN: self.auth.session_token,
|
||||
},
|
||||
)
|
||||
return self.async_show_form(
|
||||
@@ -90,14 +86,10 @@ class NintendoConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors: dict[str, str] = {}
|
||||
reauth_entry = self._get_reauth_entry()
|
||||
if self.auth is None:
|
||||
self.auth = Authenticator.generate_login(
|
||||
client_session=async_get_clientsession(self.hass)
|
||||
)
|
||||
self.auth = Authenticator(client_session=async_get_clientsession(self.hass))
|
||||
if user_input is not None:
|
||||
try:
|
||||
await self.auth.complete_login(
|
||||
self.auth, user_input[CONF_API_TOKEN], False
|
||||
)
|
||||
await self.auth.async_complete_login(user_input[CONF_API_TOKEN])
|
||||
except (ValueError, InvalidSessionTokenException, HttpException):
|
||||
errors["base"] = "invalid_auth"
|
||||
else:
|
||||
@@ -105,7 +97,7 @@ class NintendoConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
reauth_entry,
|
||||
data={
|
||||
**reauth_entry.data,
|
||||
CONF_SESSION_TOKEN: self.auth.get_session_token,
|
||||
CONF_SESSION_TOKEN: self.auth.session_token,
|
||||
},
|
||||
)
|
||||
return self.async_show_form(
|
||||
|
||||
@@ -5,11 +5,9 @@ from __future__ import annotations
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from pynintendoauth.exceptions import InvalidOAuthConfigurationException
|
||||
from pynintendoparental import Authenticator, NintendoParental
|
||||
from pynintendoparental.exceptions import (
|
||||
InvalidOAuthConfigurationException,
|
||||
NoDevicesFoundException,
|
||||
)
|
||||
from pynintendoparental.exceptions import NoDevicesFoundException
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
@@ -7,5 +7,5 @@
|
||||
"iot_class": "cloud_polling",
|
||||
"loggers": ["pynintendoparental"],
|
||||
"quality_scale": "bronze",
|
||||
"requirements": ["pynintendoparental==1.1.3"]
|
||||
"requirements": ["pynintendoparental==2.0.0"]
|
||||
}
|
||||
|
||||
2
requirements_all.txt
generated
2
requirements_all.txt
generated
@@ -2229,7 +2229,7 @@ pynetio==0.1.9.1
|
||||
pynina==0.3.6
|
||||
|
||||
# homeassistant.components.nintendo_parental_controls
|
||||
pynintendoparental==1.1.3
|
||||
pynintendoparental==2.0.0
|
||||
|
||||
# homeassistant.components.nobo_hub
|
||||
pynobo==1.8.1
|
||||
|
||||
2
requirements_test_all.txt
generated
2
requirements_test_all.txt
generated
@@ -1876,7 +1876,7 @@ pynetgear==0.10.10
|
||||
pynina==0.3.6
|
||||
|
||||
# homeassistant.components.nintendo_parental_controls
|
||||
pynintendoparental==1.1.3
|
||||
pynintendoparental==2.0.0
|
||||
|
||||
# homeassistant.components.nobo_hub
|
||||
pynobo==1.8.1
|
||||
|
||||
@@ -6,7 +6,6 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
from pynintendoparental import NintendoParental
|
||||
from pynintendoparental.device import Device
|
||||
from pynintendoparental.exceptions import InvalidOAuthConfigurationException
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.nintendo_parental_controls.const import DOMAIN
|
||||
@@ -71,11 +70,9 @@ def mock_nintendo_authenticator() -> Generator[MagicMock]:
|
||||
mock_auth._at_expiry = datetime(2099, 12, 31, 23, 59, 59)
|
||||
mock_auth.account_id = ACCOUNT_ID
|
||||
mock_auth.login_url = LOGIN_URL
|
||||
mock_auth.get_session_token = API_TOKEN
|
||||
# Patch complete_login as an AsyncMock on both instance and class as this is a class method
|
||||
mock_auth.complete_login = AsyncMock()
|
||||
type(mock_auth).complete_login = mock_auth.complete_login
|
||||
mock_auth_class.generate_login.return_value = mock_auth
|
||||
mock_auth.session_token = API_TOKEN
|
||||
mock_auth.async_complete_login = AsyncMock()
|
||||
mock_auth_class.return_value = mock_auth
|
||||
yield mock_auth
|
||||
|
||||
|
||||
@@ -93,34 +90,6 @@ def mock_nintendo_api() -> Generator[AsyncMock]:
|
||||
yield mock_api_instance
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_failed_nintendo_authenticator() -> Generator[MagicMock]:
|
||||
"""Mock a failed Nintendo Authenticator."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.nintendo_parental_controls.Authenticator",
|
||||
autospec=True,
|
||||
) as mock_auth_class,
|
||||
patch(
|
||||
"homeassistant.components.nintendo_parental_controls.config_flow.Authenticator",
|
||||
new=mock_auth_class,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.nintendo_parental_controls.coordinator.NintendoParental.update",
|
||||
return_value=None,
|
||||
),
|
||||
):
|
||||
mock_auth = MagicMock()
|
||||
mock_auth.complete_login = AsyncMock(
|
||||
side_effect=InvalidOAuthConfigurationException(
|
||||
status_code=401,
|
||||
message="Authentication failed",
|
||||
)
|
||||
)
|
||||
mock_auth_class.complete_login = mock_auth.complete_login
|
||||
yield mock_auth
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_nintendo_client(
|
||||
mock_nintendo_device: Device, mock_nintendo_authenticator: MagicMock
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from pynintendoparental.exceptions import HttpException, InvalidSessionTokenException
|
||||
from pynintendoauth.exceptions import HttpException, InvalidSessionTokenException
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.nintendo_parental_controls.const import (
|
||||
@@ -82,7 +82,7 @@ async def test_invalid_auth(
|
||||
assert "link" in result["description_placeholders"]
|
||||
|
||||
# Simulate invalid authentication by raising an exception
|
||||
mock_nintendo_authenticator.complete_login.side_effect = (
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = (
|
||||
InvalidSessionTokenException(status_code=401, message="Test")
|
||||
)
|
||||
|
||||
@@ -95,7 +95,7 @@ async def test_invalid_auth(
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
# Now ensure that the flow can be recovered
|
||||
mock_nintendo_authenticator.complete_login.side_effect = None
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_API_TOKEN: API_TOKEN}
|
||||
@@ -121,7 +121,7 @@ async def test_missing_devices(
|
||||
assert result["step_id"] == "user"
|
||||
assert "link" in result["description_placeholders"]
|
||||
|
||||
mock_nintendo_authenticator.complete_login.side_effect = None
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = None
|
||||
|
||||
mock_nintendo_api.async_get_account_devices.side_effect = HttpException(
|
||||
status_code=404, message="TEST"
|
||||
@@ -149,7 +149,7 @@ async def test_cannot_connect(
|
||||
assert result["step_id"] == "user"
|
||||
assert "link" in result["description_placeholders"]
|
||||
|
||||
mock_nintendo_authenticator.complete_login.side_effect = None
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = None
|
||||
|
||||
mock_nintendo_api.async_get_account_devices.side_effect = HttpException(
|
||||
status_code=500, message="TEST"
|
||||
@@ -209,7 +209,7 @@ async def test_reauthentication_fail(
|
||||
assert result["errors"] == {}
|
||||
|
||||
# Simulate invalid authentication by raising an exception
|
||||
mock_nintendo_authenticator.complete_login.side_effect = (
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = (
|
||||
InvalidSessionTokenException(status_code=401, message="Test")
|
||||
)
|
||||
|
||||
@@ -222,7 +222,7 @@ async def test_reauthentication_fail(
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
# Now ensure that the flow can be recovered
|
||||
mock_nintendo_authenticator.complete_login.side_effect = None
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_API_TOKEN: API_TOKEN}
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from pynintendoparental.exceptions import (
|
||||
InvalidOAuthConfigurationException,
|
||||
NoDevicesFoundException,
|
||||
)
|
||||
from pynintendoauth.exceptions import InvalidOAuthConfigurationException
|
||||
from pynintendoparental.exceptions import NoDevicesFoundException
|
||||
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
@@ -14,10 +14,11 @@ from tests.common import MockConfigEntry
|
||||
async def test_invalid_authentication(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_failed_nintendo_authenticator: AsyncMock,
|
||||
mock_nintendo_authenticator: AsyncMock,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test handling of invalid authentication."""
|
||||
mock_nintendo_authenticator.async_complete_login.side_effect = ValueError
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
# Ensure no entities are created
|
||||
|
||||
Reference in New Issue
Block a user