Skip to content

Tado migrate to OAuth Device Flow #140761

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
52d2c70
Bump PyTado 0.19.0
erwindouna Mar 14, 2025
6787492
Initial setup
erwindouna Mar 15, 2025
64e973d
Current state
erwindouna Mar 16, 2025
ef35be2
Update to PyTado 0.18.8
erwindouna Mar 16, 2025
6f415c9
First concept for review
erwindouna Mar 16, 2025
18bb4c9
Merge branch 'dev' into tado-oauth-deviceflow
erwindouna Mar 16, 2025
043c9c9
Merge branch 'dev' into tado-oauth-deviceflow
joostlek Mar 17, 2025
99c7979
Fix
joostlek Mar 17, 2025
0117489
Fix
joostlek Mar 17, 2025
870dd69
Fix
joostlek Mar 17, 2025
c8d55ca
First concept for review
erwindouna Mar 17, 2025
a0b338c
Merge branch 'dev' into tado-oauth-deviceflow
erwindouna Mar 17, 2025
615551e
Bump PyTado to 0.18.9
erwindouna Mar 17, 2025
0b453f2
Remove redundant part
erwindouna Mar 17, 2025
36d7f98
Initial test setup
erwindouna Mar 18, 2025
68e430d
Authentication exceptions
erwindouna Mar 18, 2025
e7bf8e4
Merge branch 'dev' into tado-oauth-deviceflow
erwindouna Mar 18, 2025
f914dc3
Fix
joostlek Mar 19, 2025
9a85ca9
Fix
joostlek Mar 19, 2025
16020b1
Fix
joostlek Mar 19, 2025
8c4d4f3
Update version to 2
erwindouna Mar 19, 2025
7e717a1
All migration code
erwindouna Mar 19, 2025
e8e8902
Small tuning
erwindouna Mar 19, 2025
21e22bf
Add reauth unique ID check
erwindouna Mar 19, 2025
25b850b
Add reauth test
erwindouna Mar 19, 2025
c973162
100% on config flow
erwindouna Mar 19, 2025
5be4413
Merge branch 'dev' into tado-oauth-deviceflow
erwindouna Mar 19, 2025
85b0b9a
Making tests working on new device flow
erwindouna Mar 19, 2025
31283c8
Fix
joostlek Mar 21, 2025
b43842c
Fix
joostlek Mar 21, 2025
575bacb
Fix
joostlek Mar 21, 2025
b840cc5
Merge branch 'dev' into tado-oauth-deviceflow
erwindouna Mar 22, 2025
6066ceb
Update homeassistant/components/tado/strings.json
zweckj Mar 23, 2025
6e5d52d
Merge branch 'dev' into tado-oauth-deviceflow
erwindouna Mar 24, 2025
c66a923
Update homeassistant/components/tado/strings.json
joostlek Mar 24, 2025
551fb6e
Merge branch 'dev' into tado-oauth-deviceflow
erwindouna Mar 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions homeassistant/components/tado/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
ConfigEntryError,
ConfigEntryNotReady,
)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType

from .const import (
CONF_FALLBACK,
CONF_REFRESH_TOKEN,
CONST_OVERLAY_MANUAL,
CONST_OVERLAY_TADO_DEFAULT,
CONST_OVERLAY_TADO_MODE,
Expand Down Expand Up @@ -56,23 +61,34 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

async def async_setup_entry(hass: HomeAssistant, entry: TadoConfigEntry) -> bool:
"""Set up Tado from a config entry."""
if CONF_REFRESH_TOKEN not in entry.data:
raise ConfigEntryAuthFailed

_async_import_options_from_data_if_missing(hass, entry)

_LOGGER.debug("Setting up Tado connection")
_LOGGER.debug(
"Creating tado instance with refresh token: %s",
entry.data[CONF_REFRESH_TOKEN],
)

def create_tado_instance() -> tuple[Tado, str]:
"""Create a Tado instance, this time with a previously obtained refresh token."""
tado = Tado(saved_refresh_token=entry.data[CONF_REFRESH_TOKEN])
return tado, tado.device_activation_status()

try:
tado = await hass.async_add_executor_job(
Tado,
entry.data[CONF_USERNAME],
entry.data[CONF_PASSWORD],
)
tado, device_status = await hass.async_add_executor_job(create_tado_instance)
except PyTado.exceptions.TadoWrongCredentialsException as err:
raise ConfigEntryError(f"Invalid Tado credentials. Error: {err}") from err
except PyTado.exceptions.TadoException as err:
raise ConfigEntryNotReady(f"Error during Tado setup: {err}") from err
_LOGGER.debug(
"Tado connection established for username: %s", entry.data[CONF_USERNAME]
)
if device_status != "COMPLETED":
raise ConfigEntryAuthFailed(
f"Device login flow status is {device_status}. Starting re-authentication."
)

_LOGGER.debug("Tado connection established")

coordinator = TadoDataUpdateCoordinator(hass, entry, tado)
await coordinator.async_config_entry_first_refresh()
Expand All @@ -82,11 +98,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: TadoConfigEntry) -> bool

entry.runtime_data = TadoData(coordinator, mobile_coordinator)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))

return True


async def async_migrate_entry(hass: HomeAssistant, entry: TadoConfigEntry) -> bool:
"""Migrate old entry."""

if entry.version < 2:
_LOGGER.debug("Migrating Tado entry to version 2. Current data: %s", entry.data)
data = dict(entry.data)
data.pop(CONF_USERNAME, None)
data.pop(CONF_PASSWORD, None)
hass.config_entries.async_update_entry(entry=entry, data=data, version=2)
_LOGGER.debug("Migration to version 2 successful")
return True


@callback
def _async_import_options_from_data_if_missing(
hass: HomeAssistant, entry: TadoConfigEntry
Expand All @@ -106,11 +134,6 @@ def _async_import_options_from_data_if_missing(
hass.config_entries.async_update_entry(entry, options=options)


async def update_listener(hass: HomeAssistant, entry: TadoConfigEntry):
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_unload_entry(hass: HomeAssistant, entry: TadoConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
Loading