Skip to content
New issue

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

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

Already on GitHub? # to your account

Bugfix: Electric Kiwi migrate unique id #135231

Draft
wants to merge 18 commits into
base: dev
Choose a base branch
from

Conversation

mikey0000
Copy link
Contributor

@mikey0000 mikey0000 commented Jan 9, 2025

Migrate from a unique id of DOMAIN to customer number

Breaking change

Proposed change

Fix unique id and migrate, fix small issue where title would be Home Assistant Cloud, now is the customer id

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@mikey0000 mikey0000 marked this pull request as draft January 9, 2025 19:58
homeassistant/components/electric_kiwi/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/electric_kiwi/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/electric_kiwi/config_flow.py Outdated Show resolved Hide resolved
homeassistant/components/electric_kiwi/quality_scale.yaml Outdated Show resolved Hide resolved
@mikey0000 mikey0000 requested a review from joostlek January 9, 2025 22:07
@mikey0000 mikey0000 marked this pull request as ready for review January 9, 2025 22:08
@mikey0000 mikey0000 requested a review from joostlek January 9, 2025 22:29
homeassistant/components/electric_kiwi/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/electric_kiwi/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/electric_kiwi/__init__.py Outdated Show resolved Hide resolved
homeassistant/components/electric_kiwi/config_flow.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft January 10, 2025 11:34
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@joostlek
Copy link
Member

But I am missing, why do we do this? What was the unique id before and what will it be now?

@mikey0000
Copy link
Contributor Author

But I am missing, why do we do this? What was the unique id before and what will it be now?

Unique id was "electric_kiwi" and now it will be their customer number.

I do not believe the array of customers is ever greater than 1 but for the sake of completeness/safety I've used join.

@mikey0000 mikey0000 marked this pull request as ready for review January 14, 2025 19:39
@home-assistant home-assistant bot requested a review from joostlek January 14, 2025 19:39
@mikey0000 mikey0000 changed the title Bugfix: Electric Kiwi Bugfix: Electric Kiwi migrate unique id Jan 15, 2025
Comment on lines +81 to +83
if config_entry.unique_id is not None and config_entry.unique_id.startswith(
DOMAIN
):
Copy link
Member

Choose a reason for hiding this comment

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

why would this not be the case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed, your right it will always be the case. FYI I'm updating the PR for the new release of the API, it fixes a few of the weird items with the unique id so now its just the customer number which is part of the session object, instead of out of an array. Otherwise not much changes. Updating tests as well.

@@ -27,6 +27,17 @@
type ComponentSetup = Callable[[], Awaitable[bool]]


@pytest.fixture
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@pytest.fixture
@pytest.fixture(autouse=True)

There are not a lot of tests where you don't need this

Comment on lines +112 to +115
aioclient_mock.get(
"https://api.electrickiwi.co.nz/session/",
json=load_json_value_fixture("session.json", DOMAIN),
)
Copy link
Member

Choose a reason for hiding this comment

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

I am not sure about the state of the rest of the tests, but ideally we don't mock direct aiohttp calls, but rather patch out the library.

(Although, with oauth there's a part where HA makes calls to an endpoint, in those cases its fine)

config_entry: MockConfigEntry,
setup_credentials: None,
) -> None:
"""Test Electric Kiwi reauthentication."""
config_entry.add_to_hass(hass)

hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = {}
Copy link
Member

Choose a reason for hiding this comment

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

Let's not touch internals


await hass.config_entries.async_setup(config_entry.entry_id)

await electric_kiwi.async_migrate_entry(hass, config_entry)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
await electric_kiwi.async_migrate_entry(hass, config_entry)

This should happen automatically when setting up the integration



@pytest.mark.usefixtures("setup_credentials")
async def test_cloud_unique_id_migration(
Copy link
Member

Choose a reason for hiding this comment

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

Let's also add tests for what happens when the migration fails

Comment on lines +18 to +19
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
entity_registry: er.EntityRegistry,
aioclient_mock: AiohttpClientMocker,

@home-assistant home-assistant bot marked this pull request as draft January 24, 2025 11:09
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants