mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
parent
aefadd6684
commit
ea50bbeb11
@ -1,6 +1,5 @@
|
||||
"""The Flipr integration."""
|
||||
|
||||
from collections import Counter
|
||||
import logging
|
||||
|
||||
from flipr_api import FliprAPIRestClient
|
||||
@ -8,10 +7,7 @@ from flipr_api import FliprAPIRestClient
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryError
|
||||
from homeassistant.helpers import issue_registry as ir
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import (
|
||||
FliprConfigEntry,
|
||||
FliprData,
|
||||
@ -27,9 +23,6 @@ _LOGGER = logging.getLogger(__name__)
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: FliprConfigEntry) -> bool:
|
||||
"""Set up flipr from a config entry."""
|
||||
|
||||
# Detect invalid old config entry and raise error if found
|
||||
detect_invalid_old_configuration(hass, entry)
|
||||
|
||||
config = entry.data
|
||||
|
||||
username = config[CONF_EMAIL]
|
||||
@ -64,47 +57,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
def detect_invalid_old_configuration(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Detect invalid old configuration and raise error if found."""
|
||||
|
||||
def find_duplicate_entries(entries):
|
||||
values = [e.data["email"] for e in entries]
|
||||
_LOGGER.debug("Detecting duplicates in values : %s", values)
|
||||
return any(count > 1 for count in Counter(values).values())
|
||||
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
|
||||
if find_duplicate_entries(entries):
|
||||
ir.async_create_issue(
|
||||
hass,
|
||||
DOMAIN,
|
||||
"duplicate_config",
|
||||
breaks_in_ha_version="2025.4.0",
|
||||
is_fixable=False,
|
||||
severity=ir.IssueSeverity.ERROR,
|
||||
translation_key="duplicate_config",
|
||||
)
|
||||
|
||||
raise ConfigEntryError(
|
||||
"Duplicate entries found for flipr with the same user email. Please remove one of it manually. Multiple fliprs will be automatically detected after restart."
|
||||
)
|
||||
|
||||
|
||||
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Migrate config entry."""
|
||||
_LOGGER.debug("Migration of flipr config from version %s", entry.version)
|
||||
|
||||
if entry.version == 1:
|
||||
# In version 1, we have flipr device as config entry unique id
|
||||
# and one device per config entry.
|
||||
# We need to migrate to a new config entry that may contain multiple devices.
|
||||
# So we change the entry data to match config_flow evolution.
|
||||
login = entry.data[CONF_EMAIL]
|
||||
|
||||
hass.config_entries.async_update_entry(entry, version=2, unique_id=login)
|
||||
|
||||
_LOGGER.debug("Migration of flipr config to version 2 successful")
|
||||
|
||||
return True
|
||||
|
@ -50,11 +50,5 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"issues": {
|
||||
"duplicate_config": {
|
||||
"title": "Multiple flipr configurations with the same account",
|
||||
"description": "The Flipr integration has been updated to work account based rather than device based. This means that if you have 2 devices, you only need one configuration. For every account you have, please delete all but one configuration and restart Home Assistant for it to set up the devices linked to your account."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,7 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from homeassistant.components.flipr.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_integration
|
||||
@ -29,62 +27,3 @@ async def test_unload_entry(
|
||||
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
async def test_duplicate_config_entries(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_flipr_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test duplicate config entries."""
|
||||
|
||||
mock_config_entry_dup = MockConfigEntry(
|
||||
version=2,
|
||||
domain=DOMAIN,
|
||||
unique_id="toto@toto.com",
|
||||
data={
|
||||
CONF_EMAIL: "toto@toto.com",
|
||||
CONF_PASSWORD: "myPassword",
|
||||
"flipr_id": "myflipr_id_dup",
|
||||
},
|
||||
)
|
||||
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
# Initialize the first entry with default mock
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Initialize the second entry with another flipr id
|
||||
mock_config_entry_dup.add_to_hass(hass)
|
||||
assert not await hass.config_entries.async_setup(mock_config_entry_dup.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_config_entry_dup.state is ConfigEntryState.SETUP_ERROR
|
||||
|
||||
|
||||
async def test_migrate_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_flipr_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test migrate config entry from v1 to v2."""
|
||||
|
||||
mock_config_entry_v1 = MockConfigEntry(
|
||||
version=1,
|
||||
domain=DOMAIN,
|
||||
title="myfliprid",
|
||||
unique_id="test_entry_unique_id",
|
||||
data={
|
||||
CONF_EMAIL: "toto@toto.com",
|
||||
CONF_PASSWORD: "myPassword",
|
||||
"flipr_id": "myfliprid",
|
||||
},
|
||||
)
|
||||
|
||||
await setup_integration(hass, mock_config_entry_v1)
|
||||
assert mock_config_entry_v1.state is ConfigEntryState.LOADED
|
||||
assert mock_config_entry_v1.version == 2
|
||||
assert mock_config_entry_v1.unique_id == "toto@toto.com"
|
||||
assert mock_config_entry_v1.data == {
|
||||
CONF_EMAIL: "toto@toto.com",
|
||||
CONF_PASSWORD: "myPassword",
|
||||
"flipr_id": "myfliprid",
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user