Use unique_id for config_entries of HomematicIP Cloud (#31133)
* use hapid as unique_id for config_entry of HomematicIP Cloud * Add qualita_scale to manifest * Update config_flow * use core interface for config_flow tests * refactor to fail earlier * use asynctest * rewrite tests * rewrite tests * fix test * add assert
This commit is contained in:
1 parent
3f03848a07
commit
1b3c4ed4b3
15 files changed
+254
-250
No files matched your search
@@ -17,7 +17,6 @@ import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.config_validation import comp_entity_ids
|
||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||
|
||||
from .config_flow import configured_haps
|
||||
from .const import (
|
||||
CONF_ACCESSPOINT,
|
||||
CONF_AUTHTOKEN,
|
||||
@@ -130,7 +129,10 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
||||
accesspoints = config.get(DOMAIN, [])
|
||||
|
||||
for conf in accesspoints:
|
||||
if conf[CONF_ACCESSPOINT] not in configured_haps(hass):
|
||||
if conf[CONF_ACCESSPOINT] not in set(
|
||||
entry.data[HMIPC_HAPID]
|
||||
for entry in hass.config_entries.async_entries(DOMAIN)
|
||||
):
|
||||
hass.async_add_job(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
@@ -274,7 +276,7 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
||||
anonymize = service.data[ATTR_ANONYMIZE]
|
||||
|
||||
for hap in hass.data[DOMAIN].values():
|
||||
hap_sgtin = hap.config_entry.title
|
||||
hap_sgtin = hap.config_entry.unique_id
|
||||
|
||||
if anonymize:
|
||||
hap_sgtin = hap_sgtin[-4:]
|
||||
@@ -331,9 +333,17 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
||||
"""Set up an access point from a config entry."""
|
||||
|
||||
# 0.104 introduced config entry unique id, this makes upgrading possible
|
||||
if entry.unique_id is None:
|
||||
new_data = dict(entry.data)
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
entry, unique_id=new_data[HMIPC_HAPID], data=new_data
|
||||
)
|
||||
|
||||
hap = HomematicipHAP(hass, entry)
|
||||
hapid = entry.data[HMIPC_HAPID].replace("-", "").upper()
|
||||
hass.data[DOMAIN][hapid] = hap
|
||||
hass.data[DOMAIN][entry.unique_id] = hap
|
||||
|
||||
if not await hap.async_setup():
|
||||
return False
|
||||
@@ -356,5 +366,5 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
hap = hass.data[DOMAIN].pop(entry.data[HMIPC_HAPID])
|
||||
hap = hass.data[DOMAIN].pop(entry.unique_id)
|
||||
return await hap.async_reset()
|
||||
@@ -18,7 +18,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID
|
||||
from . import DOMAIN as HMIPC_DOMAIN
|
||||
from .hap import HomematicipHAP
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -30,7 +30,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP alrm control panel from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
async_add_entities([HomematicipAlarmControlPanel(hap)])
|
||||
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ from homeassistant.components.binary_sensor import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice
|
||||
from .hap import HomematicipHAP
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -79,7 +79,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP Cloud binary sensor from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
entities = []
|
||||
for device in hap.home.devices:
|
||||
if isinstance(device, AsyncAccelerationSensor):
|
||||
|
||||
@@ -27,7 +27,7 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice
|
||||
from .hap import HomematicipHAP
|
||||
|
||||
HEATING_PROFILES = {"PROFILE_1": 0, "PROFILE_2": 1, "PROFILE_3": 2}
|
||||
@@ -47,7 +47,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP climate from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
entities = []
|
||||
for device in hap.home.groups:
|
||||
if isinstance(device, AsyncHeatingGroup):
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
"""Config flow to configure the HomematicIP Cloud component."""
|
||||
from typing import Any, Dict, Set
|
||||
from typing import Any, Dict
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from .const import (
|
||||
_LOGGER,
|
||||
@@ -18,15 +16,6 @@ from .const import (
|
||||
from .hap import HomematicipAuth
|
||||
|
||||
|
||||
@callback
|
||||
def configured_haps(hass: HomeAssistantType) -> Set[str]:
|
||||
"""Return a set of the configured access points."""
|
||||
return set(
|
||||
entry.data[HMIPC_HAPID]
|
||||
for entry in hass.config_entries.async_entries(HMIPC_DOMAIN)
|
||||
)
|
||||
|
||||
|
||||
@config_entries.HANDLERS.register(HMIPC_DOMAIN)
|
||||
class HomematicipCloudFlowHandler(config_entries.ConfigFlow):
|
||||
"""Config flow for the HomematicIP Cloud component."""
|
||||
@@ -48,8 +37,9 @@ class HomematicipCloudFlowHandler(config_entries.ConfigFlow):
|
||||
|
||||
if user_input is not None:
|
||||
user_input[HMIPC_HAPID] = user_input[HMIPC_HAPID].replace("-", "").upper()
|
||||
if user_input[HMIPC_HAPID] in configured_haps(self.hass):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
await self.async_set_unique_id(user_input[HMIPC_HAPID])
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
self.auth = HomematicipAuth(self.hass, user_input)
|
||||
connected = await self.auth.async_setup()
|
||||
@@ -93,16 +83,14 @@ class HomematicipCloudFlowHandler(config_entries.ConfigFlow):
|
||||
|
||||
async def async_step_import(self, import_info) -> Dict[str, Any]:
|
||||
"""Import a new access point as a config entry."""
|
||||
hapid = import_info[HMIPC_HAPID]
|
||||
hapid = import_info[HMIPC_HAPID].replace("-", "").upper()
|
||||
authtoken = import_info[HMIPC_AUTHTOKEN]
|
||||
name = import_info[HMIPC_NAME]
|
||||
|
||||
hapid = hapid.replace("-", "").upper()
|
||||
if hapid in configured_haps(self.hass):
|
||||
return self.async_abort(reason="already_configured")
|
||||
await self.async_set_unique_id(hapid)
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
_LOGGER.info("Imported authentication for %s", hapid)
|
||||
|
||||
return self.async_create_entry(
|
||||
title=hapid,
|
||||
data={HMIPC_AUTHTOKEN: authtoken, HMIPC_HAPID: hapid, HMIPC_NAME: name},
|
||||
|
||||
@@ -17,7 +17,7 @@ from homeassistant.components.cover import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -31,7 +31,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP cover from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
entities = []
|
||||
for device in hap.home.devices:
|
||||
if isinstance(device, AsyncFullFlushBlind):
|
||||
|
||||
@@ -95,8 +95,7 @@ class HomematicipHAP:
|
||||
raise ConfigEntryNotReady
|
||||
|
||||
_LOGGER.info(
|
||||
"Connected to HomematicIP with HAP %s",
|
||||
self.config_entry.data.get(HMIPC_HAPID),
|
||||
"Connected to HomematicIP with HAP %s", self.config_entry.unique_id
|
||||
)
|
||||
|
||||
for component in COMPONENTS:
|
||||
@@ -193,7 +192,7 @@ class HomematicipHAP:
|
||||
_LOGGER.error(
|
||||
"Error connecting to HomematicIP with HAP %s. "
|
||||
"Retrying in %d seconds",
|
||||
self.config_entry.data.get(HMIPC_HAPID),
|
||||
self.config_entry.unique_id,
|
||||
retry_delay,
|
||||
)
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ from homeassistant.components.light import (
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice
|
||||
from .hap import HomematicipHAP
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -38,7 +38,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP Cloud lights from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
entities = []
|
||||
for device in hap.home.devices:
|
||||
if isinstance(device, AsyncBrandSwitchMeasuring):
|
||||
|
||||
@@ -5,5 +5,6 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/homematicip_cloud",
|
||||
"requirements": ["homematicip==0.10.15"],
|
||||
"dependencies": [],
|
||||
"codeowners": ["@SukramJ"]
|
||||
"codeowners": ["@SukramJ"],
|
||||
"quality_scale": "platinum"
|
||||
}
|
||||
@@ -34,7 +34,7 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice
|
||||
from .device import ATTR_IS_GROUP, ATTR_MODEL_TYPE
|
||||
from .hap import HomematicipHAP
|
||||
|
||||
@@ -60,7 +60,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP Cloud sensors from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
entities = [HomematicipAccesspointStatus(hap)]
|
||||
for device in hap.home.devices:
|
||||
if isinstance(device, (AsyncHeatingThermostat, AsyncHeatingThermostatCompact)):
|
||||
|
||||
@@ -19,7 +19,7 @@ from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice
|
||||
from .device import ATTR_GROUP_MEMBER_UNREACHABLE
|
||||
from .hap import HomematicipHAP
|
||||
|
||||
@@ -30,7 +30,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP switch from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
entities = []
|
||||
for device in hap.home.devices:
|
||||
if isinstance(device, AsyncBrandSwitchMeasuring):
|
||||
|
||||
@@ -13,7 +13,7 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice
|
||||
from . import DOMAIN as HMIPC_DOMAIN, HomematicipGenericDevice
|
||||
from .hap import HomematicipHAP
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -41,7 +41,7 @@ async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up the HomematicIP weather sensor from a config entry."""
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.data[HMIPC_HAPID]]
|
||||
hap = hass.data[HMIPC_DOMAIN][config_entry.unique_id]
|
||||
entities = []
|
||||
for device in hap.home.devices:
|
||||
if isinstance(device, AsyncWeatherSensorPro):
|
||||
|
||||
Reference in new issue
Block a user