Change config flow unique_id for devolo Home Control (#43005)

pull/43898/head
Guido Schmitz 2020-12-03 09:10:20 +01:00 committed by GitHub
parent 40408eb0eb
commit 6f2327c6d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 15 deletions

View File

@ -12,7 +12,7 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, EVENT_HOMEASSISTAN
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.typing import HomeAssistantType
from .const import CONF_MYDEVOLO, DOMAIN, PLATFORMS
from .const import CONF_MYDEVOLO, DOMAIN, GATEWAY_SERIAL_PATTERN, PLATFORMS
async def async_setup(hass, config):
@ -22,13 +22,9 @@ async def async_setup(hass, config):
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
"""Set up the devolo account from a config entry."""
conf = entry.data
hass.data.setdefault(DOMAIN, {})
mydevolo = Mydevolo()
mydevolo.user = conf[CONF_USERNAME]
mydevolo.password = conf[CONF_PASSWORD]
mydevolo.url = conf[CONF_MYDEVOLO]
mydevolo = _mydevolo(entry.data)
credentials_valid = await hass.async_add_executor_job(mydevolo.credentials_valid)
@ -40,6 +36,10 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
gateway_ids = await hass.async_add_executor_job(mydevolo.get_gateway_ids)
if GATEWAY_SERIAL_PATTERN.match(entry.unique_id):
uuid = await hass.async_add_executor_job(mydevolo.uuid)
hass.config_entries.async_update_entry(entry, unique_id=uuid)
try:
zeroconf_instance = await zeroconf.async_get_instance(hass)
hass.data[DOMAIN][entry.entry_id] = {"gateways": [], "listener": None}
@ -95,3 +95,12 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> boo
hass.data[DOMAIN][entry.entry_id]["listener"]()
hass.data[DOMAIN].pop(entry.entry_id)
return unload
def _mydevolo(conf: dict) -> Mydevolo:
"""Configure mydevolo."""
mydevolo = Mydevolo()
mydevolo.user = conf[CONF_USERNAME]
mydevolo.password = conf[CONF_PASSWORD]
mydevolo.url = conf[CONF_MYDEVOLO]
return mydevolo

View File

@ -55,8 +55,8 @@ class DevoloHomeControlFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
if not credentials_valid:
return self._show_form({"base": "invalid_auth"})
_LOGGER.debug("Credentials valid")
gateway_ids = await self.hass.async_add_executor_job(mydevolo.get_gateway_ids)
await self.async_set_unique_id(gateway_ids[0])
uuid = await self.hass.async_add_executor_job(mydevolo.uuid)
await self.async_set_unique_id(uuid)
self._abort_if_unique_id_configured()
return self.async_create_entry(

View File

@ -1,6 +1,8 @@
"""Constants for the devolo_home_control integration."""
import re
DOMAIN = "devolo_home_control"
DEFAULT_MYDEVOLO = "https://www.mydevolo.com"
PLATFORMS = ["binary_sensor", "climate", "cover", "light", "sensor", "switch"]
CONF_MYDEVOLO = "mydevolo_url"
GATEWAY_SERIAL_PATTERN = re.compile(r"\d{16}")

View File

@ -26,8 +26,8 @@ async def test_form(hass):
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.credentials_valid",
return_value=True,
), patch(
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.get_gateway_ids",
return_value=["123456"],
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.uuid",
return_value="123456",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
@ -71,13 +71,13 @@ async def test_form_invalid_credentials(hass):
async def test_form_already_configured(hass):
"""Test if we get the error message on already configured."""
with patch(
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.get_gateway_ids",
return_value=["1234567"],
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.uuid",
return_value="123456",
), patch(
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.credentials_valid",
return_value=True,
):
MockConfigEntry(domain=DOMAIN, unique_id="1234567", data={}).add_to_hass(hass)
MockConfigEntry(domain=DOMAIN, unique_id="123456", data={}).add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
@ -105,8 +105,8 @@ async def test_form_advanced_options(hass):
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.credentials_valid",
return_value=True,
), patch(
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.get_gateway_ids",
return_value=["123456"],
"homeassistant.components.devolo_home_control.config_flow.Mydevolo.uuid",
return_value="123456",
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],