Skip to content

Commit

Permalink
Rework config flow, support for multiple config entries (#479), notif…
Browse files Browse the repository at this point in the history
…ier configuration from UI (#478)
  • Loading branch information
dext0r committed May 7, 2024
1 parent 65c198e commit 6e114f2
Show file tree
Hide file tree
Showing 33 changed files with 1,567 additions and 917 deletions.
70 changes: 57 additions & 13 deletions custom_components/yandex_smart_home/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TYPE_CHECKING, Any

from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import MAJOR_VERSION, MINOR_VERSION, SERVICE_RELOAD
from homeassistant.const import CONF_ID, CONF_PLATFORM, CONF_TOKEN, MAJOR_VERSION, MINOR_VERSION, SERVICE_RELOAD
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entityfilter import BASE_FILTER_SCHEMA, FILTER_SCHEMA, EntityFilter
Expand All @@ -16,8 +16,9 @@

from . import config_validation as ycv, const
from .cloud import delete_cloud_instance
from .const import DOMAIN, ConnectionType, EntityFilterSource
from .const import CONF_SKILL, CONF_USER_ID, DOMAIN, ConnectionType, EntityFilterSource
from .entry_data import ConfigEntryData
from .helpers import SmartHomePlatform
from .http import async_register_http

if TYPE_CHECKING:
Expand Down Expand Up @@ -194,11 +195,20 @@ def get_entry_data(self, entry: ConfigEntry) -> ConfigEntryData:
"""Return a config entry data for a config entry."""
return self._entry_datas[entry.entry_id]

def get_direct_connection_entry_data(self) -> ConfigEntryData | None:
"""Return a config entry data for a config entry using direct connection."""
def get_direct_connection_entry_data(
self, platform: SmartHomePlatform, user_id: str | None
) -> ConfigEntryData | None:
"""Return a config entry data with direct connection config entry."""
for data in self._entry_datas.values():
if data.connection_type == ConnectionType.DIRECT and data.entry.state == ConfigEntryState.LOADED:
return data
if (
data.connection_type == ConnectionType.DIRECT
and data.entry.state == ConfigEntryState.LOADED
and data.platform == platform
):
if user_id and data.skill and data.skill.user_id == user_id:
return data
if not user_id:
return data

return None

Expand Down Expand Up @@ -271,8 +281,8 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate the config entry upon new versions."""
version = entry.version
component: YandexSmartHome = hass.data[DOMAIN]
data = {**entry.data}
options = {**entry.options}
data: ConfigType = {**entry.data}
options: ConfigType = {**entry.options}

_LOGGER.debug(f"Migrating from version {version}")

Expand Down Expand Up @@ -307,11 +317,6 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
_LOGGER.debug(f"Migration to version {version} successful")

if version == 2:
from .config_flow import DEFAULT_CONFIG_ENTRY_TITLE, config_entry_title

if entry.title == DEFAULT_CONFIG_ENTRY_TITLE:
hass.config_entries.async_update_entry(entry, title=config_entry_title(data))

version = 3
_LOGGER.debug(f"Migration to version {version} successful")

Expand All @@ -333,6 +338,45 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
) # type: ignore[call-arg]
_LOGGER.debug(f"Migration to version {version} successful")

if version == 4:
from .config_flow import DEFAULT_CONFIG_ENTRY_TITLE, PRE_V1_DIRECT_CONFIG_ENTRY_TITLE, async_config_entry_title

title = entry.title
data.setdefault(CONF_PLATFORM, SmartHomePlatform.YANDEX)

if (
len(hass.config_entries.async_entries(DOMAIN)) == 1
and data[const.CONF_CONNECTION_TYPE] == ConnectionType.DIRECT
):
for notifier_config in component._yaml_config.get(const.CONF_NOTIFIER, []):
options.setdefault(
CONF_SKILL,
{
CONF_USER_ID: notifier_config[const.CONF_NOTIFIER_USER_ID],
CONF_ID: notifier_config[const.CONF_NOTIFIER_SKILL_ID],
CONF_TOKEN: notifier_config[const.CONF_NOTIFIER_OAUTH_TOKEN],
},
)
break

if entry.title in (DEFAULT_CONFIG_ENTRY_TITLE, PRE_V1_DIRECT_CONFIG_ENTRY_TITLE):
title = await async_config_entry_title(hass, data, options)

version = 5
if int(MAJOR_VERSION) < 2024 or (int(MAJOR_VERSION) == 2024 and int(MINOR_VERSION) < 5):
entry.version = version
hass.config_entries.async_update_entry(entry, title=title, data=data, options=options)
else:
hass.config_entries.async_update_entry(
entry,
title=title,
data=data,
options=options,
version=version,
) # type: ignore[call-arg]

_LOGGER.debug(f"Migration to version {version} successful")

return True


Expand Down
4 changes: 3 additions & 1 deletion custom_components/yandex_smart_home/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from . import handlers
from .const import CLOUD_BASE_URL, DOMAIN
from .helpers import RequestData
from .helpers import RequestData, SmartHomePlatform

if TYPE_CHECKING:
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
Expand Down Expand Up @@ -45,6 +45,7 @@ class CloudRequest(BaseModel):
"""Request from the cloud."""

request_id: str
platform: SmartHomePlatform
action: str
message: str = ""

Expand Down Expand Up @@ -121,6 +122,7 @@ async def _on_message(self, message: WSMessage) -> None:
data = RequestData(
entry_data=self._entry_data,
context=Context(user_id=await self._entry_data.async_get_user_id()),
platform=request.platform,
request_user_id=self._entry_data.cloud_instance_id,
request_id=request.request_id,
)
Expand Down
Loading

0 comments on commit 6e114f2

Please sign in to comment.