Skip to content

Commit

Permalink
maint: Add support for migration from o365 integration
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Nov 30, 2024
1 parent 12663df commit f01add8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
33 changes: 33 additions & 0 deletions custom_components/ms365_teams/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .helpers.config_entry import MS365ConfigEntry
from .integration.config_flow_integration import (
MS365OptionsFlowHandler,
async_integration_imports,
integration_reconfigure_schema,
integration_validate_schema,
)
Expand Down Expand Up @@ -76,6 +77,8 @@ def __init__(self):
self._config_schema: dict[vol.Required, type[str | int]] | None = None
self._reconfigure = False
self._entry: MS365ConfigEntry | None = None
# self._o365_config = None
# self._ms365_config = None

@staticmethod
@callback
Expand All @@ -89,6 +92,8 @@ def is_matching(self, other_flow: Self) -> bool:

async def async_step_user(self, user_input=None):
"""Handle the initial step."""
# self._o365_config = self.hass.data.get("o365", None)
# self._ms365_config = self.hass.config_entries.async_entries(DOMAIN)
errors = integration_validate_schema(user_input) if user_input else {}

if user_input and not errors:
Expand Down Expand Up @@ -297,6 +302,34 @@ async def _redo_configuration(

return await self.async_step_user()

async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
"""Import a config entry."""
data = import_data["data"]
options = import_data["options"]
self._entity_name = data[CONF_ENTITY_NAME]
if self._check_existing():
return self.async_abort(reason="already_configured")
await async_integration_imports(self.hass, import_data)
result = self.async_create_entry(
title=self._entity_name, data=data, options=options
)
self._disable_new()
return result

def _check_existing(self):
config_entries = self.hass.config_entries.async_entries(DOMAIN)
for config_entry in config_entries:
if config_entry.title == self._entity_name:
return True
return False

def _disable_new(self):
config_entries = self.hass.config_entries.async_entries(DOMAIN)
for config_entry in config_entries:
if config_entry.title == self._entity_name:
config_entry.disabled_by = "migration"
return


def get_callback_url(hass, alt_config):
"""Get the callback URL."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ def integration_validate_schema(user_input):
return {}


async def async_integration_imports(hass, import_data): # pylint: disable=unused-argument
"""Do the integration level import tasks."""
return


class MS365OptionsFlowHandler(config_entries.OptionsFlow):
"""Config flow options for MS365."""

Expand Down
5 changes: 5 additions & 0 deletions tests/integration/const_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
RECONFIGURE_CONFIG_ENTRY = deepcopy(BASE_CONFIG_ENTRY)
del RECONFIGURE_CONFIG_ENTRY["entity_name"]

MIGRATION_CONFIG_ENTRY = {
"data": BASE_CONFIG_ENTRY,
"options": {},
}

DIAGNOSTIC_GRANTED_PERMISSIONS = [
"Chat.Read",
"Presence.Read",
Expand Down
40 changes: 40 additions & 0 deletions tests/test_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Test migration"""

import pytest
from homeassistant import config_entries
from homeassistant.core import HomeAssistant
from requests_mock import Mocker

from .helpers.utils import mock_token
from .integration.const_integration import (
BASE_TOKEN_PERMS,
DOMAIN,
MIGRATION_CONFIG_ENTRY,
)
from .integration.helpers_integration.mocks import MS365MOCKS


async def test_default_flow(
tmp_path,
hass: HomeAssistant,
requests_mock: Mocker,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the default config_flow."""
mock_token(requests_mock, BASE_TOKEN_PERMS)
MS365MOCKS.standard_mocks(requests_mock)

await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=MIGRATION_CONFIG_ENTRY,
)
assert (
f"Could not locate token at {tmp_path}/storage/tokens/{DOMAIN}_test.token"
in caplog.text
)
await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=MIGRATION_CONFIG_ENTRY,
)

0 comments on commit f01add8

Please sign in to comment.