Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyError in nest integration when the old key format does not exist #130057

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions homeassistant/components/nest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ async def new_subscriber(
implementation, config_entry_oauth2_flow.LocalOAuth2Implementation
):
raise TypeError(f"Unexpected auth implementation {implementation}")
subscription_name = entry.data.get(
CONF_SUBSCRIPTION_NAME, entry.data[CONF_SUBSCRIBER_ID]
)
if not (
subscription_name := entry.data.get(
CONF_SUBSCRIPTION_NAME, entry.data.get(CONF_SUBSCRIBER_ID)
)
):
raise ValueError(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when this is raised? How can the user recover from this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a shouldn't happen / bug. Let me try one more simpler way...

"Configuration option 'subscription_name' or 'subscriber_id' missing"
)
auth = AsyncConfigEntryAuth(
aiohttp_client.async_get_clientsession(hass),
config_entry_oauth2_flow.OAuth2Session(hass, entry, implementation),
Expand Down
12 changes: 12 additions & 0 deletions tests/components/nest/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
CLIENT_SECRET = "some-client-secret"
CLOUD_PROJECT_ID = "cloud-id-9876"
SUBSCRIBER_ID = "projects/cloud-id-9876/subscriptions/subscriber-id-9876"
SUBSCRIPTION_NAME = "projects/cloud-id-9876/subscriptions/subscriber-id-9876"


@dataclass
Expand Down Expand Up @@ -86,6 +87,17 @@ class NestTestConfig:
},
)

TEST_CONFIG_NEW_SUBSCRIPTION = NestTestConfig(
config_entry_data={
"sdm": {},
"project_id": PROJECT_ID,
"cloud_project_id": CLOUD_PROJECT_ID,
"subscription_name": SUBSCRIPTION_NAME,
"auth_implementation": "imported-cred",
},
credential=ClientCredential(CLIENT_ID, CLIENT_SECRET),
)


class FakeSubscriber(GoogleNestSubscriber):
"""Fake subscriber that supplies a FakeDeviceManager."""
Expand Down
14 changes: 14 additions & 0 deletions tests/components/nest/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
SUBSCRIBER_ID,
TEST_CONFIG_ENTRY_LEGACY,
TEST_CONFIG_LEGACY,
TEST_CONFIG_NEW_SUBSCRIPTION,
TEST_CONFIGFLOW_APP_CREDS,
FakeSubscriber,
PlatformSetup,
Expand Down Expand Up @@ -97,6 +98,19 @@ async def test_setup_success(
assert entries[0].state is ConfigEntryState.LOADED


@pytest.mark.parametrize("nest_test_config", [(TEST_CONFIG_NEW_SUBSCRIPTION)])
async def test_setup_success_new_subscription_format(
hass: HomeAssistant, error_caplog: pytest.LogCaptureFixture, setup_platform
) -> None:
"""Test successful setup."""
await setup_platform()
assert not error_caplog.records

entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
assert entries[0].state is ConfigEntryState.LOADED


@pytest.mark.parametrize("subscriber_id", [("invalid-subscriber-format")])
async def test_setup_configuration_failure(
hass: HomeAssistant,
Expand Down
Loading