Fix `KeyError` in nest integration when the old key format does not exist (#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 <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
pull/130156/head
Allen Porter 2024-11-07 20:03:07 -08:00 committed by Franck Nijhof
parent 7ff501f3ec
commit 5f5f6cc3d5
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 28 additions and 3 deletions

View File

@ -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),

View File

@ -30,6 +30,7 @@ CLIENT_ID = "some-client-id"
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
@ -86,6 +87,17 @@ TEST_CONFIG_ENTRY_LEGACY = 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."""

View File

@ -31,6 +31,7 @@ from .common import (
SUBSCRIBER_ID,
TEST_CONFIG_ENTRY_LEGACY,
TEST_CONFIG_LEGACY,
TEST_CONFIG_NEW_SUBSCRIPTION,
TEST_CONFIGFLOW_APP_CREDS,
FakeSubscriber,
PlatformSetup,
@ -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,