Skip to content

Commit

Permalink
Add Xiaomi Miio EU gateway support (home-assistant#47955)
Browse files Browse the repository at this point in the history
* Add EU gateway support

* add options flow translations

* fix options flow

* fix missing import

* try to fix async_add_executor_job

* try to fix async_add_executor_job

* fix unload

* check for login succes

* fix not reloading

* use cloud option

* fix styling

* Return after if

Co-authored-by: Nathan Tilley <[email protected]>

* cleanup

* add options flow tests

* fix new tests

* fix typo in docstring

* add missing blank line

* Use async_on_unload

Co-authored-by: Martin Hjelmare <[email protected]>

* Use async_on_unload

Co-authored-by: Martin Hjelmare <[email protected]>

* Use async_setup_platforms

Co-authored-by: Martin Hjelmare <[email protected]>

* Use async_unload_platforms

Co-authored-by: Martin Hjelmare <[email protected]>

* Update homeassistant/components/xiaomi_miio/__init__.py

Co-authored-by: Martin Hjelmare <[email protected]>

* Update homeassistant/components/xiaomi_miio/const.py

Co-authored-by: Martin Hjelmare <[email protected]>

* default use_cloud False

* add options flow checks

* fix styling

* fix issort

* add MiCloud check tests

* fix indent

* fix styling

* fix tests

* fix tests

* fix black

* re-write config flow

* add explicit return type

* update strings.json

* black formatting

* fix config flow

Tested the config flow and it is now fully working

* fix styling

* Fix current tests

* Add missing tests

* fix styling

* add re-auth flow

* fix styling

* fix reauth flow

* Add reauth flow test

* use ConfigEntryAuthFailed

* also trigger reauth @ login error

* fix styling

* remove unused import

* fix spelling

Co-authored-by: Martin Hjelmare <[email protected]>

* Fix spelling

Co-authored-by: Martin Hjelmare <[email protected]>

* fix spelling

Co-authored-by: Martin Hjelmare <[email protected]>

* remove unessesary .keys()

Co-authored-by: Martin Hjelmare <[email protected]>

* combine async_add_executor_job calls

* remove async_step_model

* fix wrong indent

* fix gatway.py

* fix tests

Co-authored-by: Nathan Tilley <[email protected]>
Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
3 people authored Jun 14, 2021
1 parent 8705168 commit 3a2d50f
Show file tree
Hide file tree
Showing 10 changed files with 1,046 additions and 194 deletions.
75 changes: 54 additions & 21 deletions homeassistant/components/xiaomi_miio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ async def async_setup_entry(
)


def get_platforms(config_entry):
"""Return the platforms belonging to a config_entry."""
model = config_entry.data[CONF_MODEL]
flow_type = config_entry.data[CONF_FLOW_TYPE]

if flow_type == CONF_GATEWAY:
return GATEWAY_PLATFORMS
if flow_type == CONF_DEVICE:
if model in MODELS_SWITCH:
return SWITCH_PLATFORMS
if model in MODELS_FAN:
return FAN_PLATFORMS
if model in MODELS_LIGHT:
return LIGHT_PLATFORMS
for vacuum_model in MODELS_VACUUM:
if model.startswith(vacuum_model):
return VACUUM_PLATFORMS
for air_monitor_model in MODELS_AIR_MONITOR:
if model.startswith(air_monitor_model):
return AIR_MONITOR_PLATFORMS

return []


async def async_setup_gateway_entry(
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
):
Expand All @@ -64,8 +88,10 @@ async def async_setup_gateway_entry(
if entry.unique_id.endswith("-gateway"):
hass.config_entries.async_update_entry(entry, unique_id=entry.data["mac"])

entry.async_on_unload(entry.add_update_listener(update_listener))

# Connect to gateway
gateway = ConnectXiaomiGateway(hass)
gateway = ConnectXiaomiGateway(hass, entry)
if not await gateway.async_connect_gateway(host, token):
return False
gateway_info = gateway.gateway_info
Expand Down Expand Up @@ -128,29 +154,36 @@ async def async_setup_device_entry(
hass: core.HomeAssistant, entry: config_entries.ConfigEntry
):
"""Set up the Xiaomi Miio device component from a config entry."""
model = entry.data[CONF_MODEL]

# Identify platforms to setup
platforms = []
if model in MODELS_SWITCH:
platforms = SWITCH_PLATFORMS
elif model in MODELS_FAN:
platforms = FAN_PLATFORMS
elif model in MODELS_LIGHT:
platforms = LIGHT_PLATFORMS
for vacuum_model in MODELS_VACUUM:
if model.startswith(vacuum_model):
platforms = VACUUM_PLATFORMS
for air_monitor_model in MODELS_AIR_MONITOR:
if model.startswith(air_monitor_model):
platforms = AIR_MONITOR_PLATFORMS
platforms = get_platforms(entry)

if not platforms:
return False

for platform in platforms:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
entry.async_on_unload(entry.add_update_listener(update_listener))

hass.config_entries.async_setup_platforms(entry, platforms)

return True


async def async_unload_entry(
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
):
"""Unload a config entry."""
platforms = get_platforms(config_entry)

unload_ok = await hass.config_entries.async_unload_platforms(
config_entry, platforms
)

if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)

return unload_ok


async def update_listener(
hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry
):
"""Handle options update."""
await hass.config_entries.async_reload(config_entry.entry_id)
Loading

0 comments on commit 3a2d50f

Please sign in to comment.