Skip to content

Commit

Permalink
Fix KeyError in nest integration when the old key format does not e…
Browse files Browse the repository at this point in the history
…xist (#130057)

* Fix bug in nest setup when the old key format does not exist

* Further simplify the entry.data check

* Update homeassistant/components/nest/api.py

Co-authored-by: Joost Lekkerkerker <[email protected]>

---------

Co-authored-by: Joost Lekkerkerker <[email protected]>
  • Loading branch information
allenporter and joostlek authored Nov 8, 2024
1 parent 0d19e85 commit e407b47
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
5 changes: 2 additions & 3 deletions homeassistant/components/nest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ 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 (subscription_name := entry.data.get(CONF_SUBSCRIPTION_NAME)) is None:
subscription_name = entry.data[CONF_SUBSCRIBER_ID]
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

0 comments on commit e407b47

Please sign in to comment.