core/homeassistant/components/downloader/config_flow.py

70 lines
2.3 KiB
Python
Raw Normal View History

Add Downloader config flow, including tests (#98722) * Adding base line, including tests * Adding validatge input and expanding tests * Updating manifest * Minor patch * Revert minor patch, wrong nesting * Adding proper translations * Including abort message * Update homeassistant/components/downloader/config_flow.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename exception class * Refactor import * Update strings * Apply suggestions from code review * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Reverting back filename and fix typing * Reverting back mutex/lock * Upgrade version * Adding typing * Removing coroutine * Removing unload entry (for now) * Removing comment * Change type * Putting download back in setup_entry * Revert back code --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-18 15:16:24 +00:00
"""Config flow for Downloader integration."""
Add Downloader config flow, including tests (#98722) * Adding base line, including tests * Adding validatge input and expanding tests * Updating manifest * Minor patch * Revert minor patch, wrong nesting * Adding proper translations * Including abort message * Update homeassistant/components/downloader/config_flow.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename exception class * Refactor import * Update strings * Apply suggestions from code review * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Reverting back filename and fix typing * Reverting back mutex/lock * Upgrade version * Adding typing * Removing coroutine * Removing unload entry (for now) * Removing comment * Change type * Putting download back in setup_entry * Revert back code --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-18 15:16:24 +00:00
from __future__ import annotations
import os
from typing import Any
import voluptuous as vol
from homeassistant import exceptions
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.helpers import config_validation as cv
from .const import _LOGGER, CONF_DOWNLOAD_DIR, DEFAULT_NAME, DOMAIN
class DownloaderConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Downloader."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
try:
await self._validate_input(user_input)
except DirectoryDoesNotExist:
errors["base"] = "directory_does_not_exist"
Add Downloader config flow, including tests (#98722) * Adding base line, including tests * Adding validatge input and expanding tests * Updating manifest * Minor patch * Revert minor patch, wrong nesting * Adding proper translations * Including abort message * Update homeassistant/components/downloader/config_flow.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename exception class * Refactor import * Update strings * Apply suggestions from code review * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Reverting back filename and fix typing * Reverting back mutex/lock * Upgrade version * Adding typing * Removing coroutine * Removing unload entry (for now) * Removing comment * Change type * Putting download back in setup_entry * Revert back code --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-18 15:16:24 +00:00
else:
return self.async_create_entry(title=DEFAULT_NAME, data=user_input)
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_DOWNLOAD_DIR): cv.string,
}
),
errors=errors,
)
async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult:
Add Downloader config flow, including tests (#98722) * Adding base line, including tests * Adding validatge input and expanding tests * Updating manifest * Minor patch * Revert minor patch, wrong nesting * Adding proper translations * Including abort message * Update homeassistant/components/downloader/config_flow.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename exception class * Refactor import * Update strings * Apply suggestions from code review * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Reverting back filename and fix typing * Reverting back mutex/lock * Upgrade version * Adding typing * Removing coroutine * Removing unload entry (for now) * Removing comment * Change type * Putting download back in setup_entry * Revert back code --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-18 15:16:24 +00:00
"""Handle a flow initiated by configuration file."""
try:
await self._validate_input(user_input)
except DirectoryDoesNotExist:
return self.async_abort(reason="directory_does_not_exist")
return self.async_create_entry(title=DEFAULT_NAME, data=user_input)
Add Downloader config flow, including tests (#98722) * Adding base line, including tests * Adding validatge input and expanding tests * Updating manifest * Minor patch * Revert minor patch, wrong nesting * Adding proper translations * Including abort message * Update homeassistant/components/downloader/config_flow.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename exception class * Refactor import * Update strings * Apply suggestions from code review * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Reverting back filename and fix typing * Reverting back mutex/lock * Upgrade version * Adding typing * Removing coroutine * Removing unload entry (for now) * Removing comment * Change type * Putting download back in setup_entry * Revert back code --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-18 15:16:24 +00:00
async def _validate_input(self, user_input: dict[str, Any]) -> None:
"""Validate the user input if the directory exists."""
2024-04-03 07:56:19 +00:00
download_path = user_input[CONF_DOWNLOAD_DIR]
if not os.path.isabs(download_path):
download_path = self.hass.config.path(download_path)
Add Downloader config flow, including tests (#98722) * Adding base line, including tests * Adding validatge input and expanding tests * Updating manifest * Minor patch * Revert minor patch, wrong nesting * Adding proper translations * Including abort message * Update homeassistant/components/downloader/config_flow.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename exception class * Refactor import * Update strings * Apply suggestions from code review * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Reverting back filename and fix typing * Reverting back mutex/lock * Upgrade version * Adding typing * Removing coroutine * Removing unload entry (for now) * Removing comment * Change type * Putting download back in setup_entry * Revert back code --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-18 15:16:24 +00:00
if not await self.hass.async_add_executor_job(os.path.isdir, download_path):
Add Downloader config flow, including tests (#98722) * Adding base line, including tests * Adding validatge input and expanding tests * Updating manifest * Minor patch * Revert minor patch, wrong nesting * Adding proper translations * Including abort message * Update homeassistant/components/downloader/config_flow.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Rename exception class * Refactor import * Update strings * Apply suggestions from code review * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update homeassistant/components/downloader/__init__.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Reverting back filename and fix typing * Reverting back mutex/lock * Upgrade version * Adding typing * Removing coroutine * Removing unload entry (for now) * Removing comment * Change type * Putting download back in setup_entry * Revert back code --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-03-18 15:16:24 +00:00
_LOGGER.error(
"Download path %s does not exist. File Downloader not active",
download_path,
)
raise DirectoryDoesNotExist
class DirectoryDoesNotExist(exceptions.HomeAssistantError):
"""Error to indicate the specified download directory does not exist."""