core/homeassistant/components/jewish_calendar/config_flow.py

151 lines
4.6 KiB
Python
Raw Normal View History

Add config flow to Jewish Calendar (#84464) * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Initial fixes for tests * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Fix bad merges in rebase * Get tests to run again * Fixes due to fails in ruff/pylint * Fix binary sensor tests * Fix config flow tests * Fix sensor tests * Apply review * Adjust candle lights * Apply suggestion * revert unrelated change * Address some of the comments * We should only allow a single jewish calendar config entry * Make data schema easier to read * Add test and confirm only single entry is allowed * Move OPTIONS_SCHEMA to top of file * Add options test * Simplify import tests * Test import end2end * Use a single async_forward_entry_setups statement * Revert schema updates for YAML schema * Remove unneeded brackets * Remove CONF_NAME from config_flow * Assign hass.data[DOMAIN][config_entry.entry_id] to a local variable before creating the sensors * Data doesn't have a name remove slugifying of it * Test that the entry has been created correctly * Simplify setup_entry * Use suggested values helper and flatten location dictionary * Remove the string for name exists as this error doesn't exist * Remove name from config entry * Remove _attr_has_entity_name - will be added in a subsequent PR * Don't override entity id's - we'll fixup the naming later * Make location optional - will by default revert to the user's home location * Update homeassistant/components/jewish_calendar/strings.json Co-authored-by: Erik Montnemery <erik@montnemery.com> * No need for local lat/long variable * Return name attribute, will deal with it in another PR * Revert unique_id changes, will deal with this in a subsequent PR * Add time zone data description * Don't break the YAML config until the user has removed it. * Cleanup initial config flow test --------- Co-authored-by: Tsvi Mostovicz <ttmost@gmail.com> Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-05-24 12:04:17 +00:00
"""Config flow for Jewish calendar integration."""
from __future__ import annotations
import logging
from typing import Any
import zoneinfo
import voluptuous as vol
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlowWithConfigEntry,
)
from homeassistant.const import (
CONF_ELEVATION,
CONF_LANGUAGE,
CONF_LATITUDE,
CONF_LOCATION,
CONF_LONGITUDE,
CONF_TIME_ZONE,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.selector import (
BooleanSelector,
LocationSelector,
SelectOptionDict,
SelectSelector,
SelectSelectorConfig,
)
from homeassistant.helpers.typing import ConfigType
from .const import (
CONF_CANDLE_LIGHT_MINUTES,
CONF_DIASPORA,
CONF_HAVDALAH_OFFSET_MINUTES,
DEFAULT_CANDLE_LIGHT,
DEFAULT_DIASPORA,
DEFAULT_HAVDALAH_OFFSET_MINUTES,
DEFAULT_LANGUAGE,
DEFAULT_NAME,
DOMAIN,
)
Add config flow to Jewish Calendar (#84464) * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Initial fixes for tests * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Fix bad merges in rebase * Get tests to run again * Fixes due to fails in ruff/pylint * Fix binary sensor tests * Fix config flow tests * Fix sensor tests * Apply review * Adjust candle lights * Apply suggestion * revert unrelated change * Address some of the comments * We should only allow a single jewish calendar config entry * Make data schema easier to read * Add test and confirm only single entry is allowed * Move OPTIONS_SCHEMA to top of file * Add options test * Simplify import tests * Test import end2end * Use a single async_forward_entry_setups statement * Revert schema updates for YAML schema * Remove unneeded brackets * Remove CONF_NAME from config_flow * Assign hass.data[DOMAIN][config_entry.entry_id] to a local variable before creating the sensors * Data doesn't have a name remove slugifying of it * Test that the entry has been created correctly * Simplify setup_entry * Use suggested values helper and flatten location dictionary * Remove the string for name exists as this error doesn't exist * Remove name from config entry * Remove _attr_has_entity_name - will be added in a subsequent PR * Don't override entity id's - we'll fixup the naming later * Make location optional - will by default revert to the user's home location * Update homeassistant/components/jewish_calendar/strings.json Co-authored-by: Erik Montnemery <erik@montnemery.com> * No need for local lat/long variable * Return name attribute, will deal with it in another PR * Revert unique_id changes, will deal with this in a subsequent PR * Add time zone data description * Don't break the YAML config until the user has removed it. * Cleanup initial config flow test --------- Co-authored-by: Tsvi Mostovicz <ttmost@gmail.com> Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-05-24 12:04:17 +00:00
LANGUAGE = [
SelectOptionDict(value="hebrew", label="Hebrew"),
SelectOptionDict(value="english", label="English"),
]
OPTIONS_SCHEMA = vol.Schema(
{
vol.Optional(CONF_CANDLE_LIGHT_MINUTES, default=DEFAULT_CANDLE_LIGHT): int,
vol.Optional(
CONF_HAVDALAH_OFFSET_MINUTES, default=DEFAULT_HAVDALAH_OFFSET_MINUTES
): int,
}
)
_LOGGER = logging.getLogger(__name__)
def _get_data_schema(hass: HomeAssistant) -> vol.Schema:
default_location = {
CONF_LATITUDE: hass.config.latitude,
CONF_LONGITUDE: hass.config.longitude,
}
return vol.Schema(
{
vol.Required(CONF_DIASPORA, default=DEFAULT_DIASPORA): BooleanSelector(),
vol.Required(CONF_LANGUAGE, default=DEFAULT_LANGUAGE): SelectSelector(
SelectSelectorConfig(options=LANGUAGE)
),
vol.Optional(CONF_LOCATION, default=default_location): LocationSelector(),
vol.Optional(CONF_ELEVATION, default=hass.config.elevation): int,
vol.Optional(CONF_TIME_ZONE, default=hass.config.time_zone): SelectSelector(
SelectSelectorConfig(
options=sorted(zoneinfo.available_timezones()),
)
),
}
)
class JewishCalendarConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Jewish calendar."""
VERSION = 1
@staticmethod
@callback
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlowWithConfigEntry:
"""Get the options flow for this handler."""
return JewishCalendarOptionsFlowHandler(config_entry)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
if user_input is not None:
_options = {}
if CONF_CANDLE_LIGHT_MINUTES in user_input:
_options[CONF_CANDLE_LIGHT_MINUTES] = user_input[
CONF_CANDLE_LIGHT_MINUTES
]
del user_input[CONF_CANDLE_LIGHT_MINUTES]
if CONF_HAVDALAH_OFFSET_MINUTES in user_input:
_options[CONF_HAVDALAH_OFFSET_MINUTES] = user_input[
CONF_HAVDALAH_OFFSET_MINUTES
]
del user_input[CONF_HAVDALAH_OFFSET_MINUTES]
Add config flow to Jewish Calendar (#84464) * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Initial fixes for tests * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Fix bad merges in rebase * Get tests to run again * Fixes due to fails in ruff/pylint * Fix binary sensor tests * Fix config flow tests * Fix sensor tests * Apply review * Adjust candle lights * Apply suggestion * revert unrelated change * Address some of the comments * We should only allow a single jewish calendar config entry * Make data schema easier to read * Add test and confirm only single entry is allowed * Move OPTIONS_SCHEMA to top of file * Add options test * Simplify import tests * Test import end2end * Use a single async_forward_entry_setups statement * Revert schema updates for YAML schema * Remove unneeded brackets * Remove CONF_NAME from config_flow * Assign hass.data[DOMAIN][config_entry.entry_id] to a local variable before creating the sensors * Data doesn't have a name remove slugifying of it * Test that the entry has been created correctly * Simplify setup_entry * Use suggested values helper and flatten location dictionary * Remove the string for name exists as this error doesn't exist * Remove name from config entry * Remove _attr_has_entity_name - will be added in a subsequent PR * Don't override entity id's - we'll fixup the naming later * Make location optional - will by default revert to the user's home location * Update homeassistant/components/jewish_calendar/strings.json Co-authored-by: Erik Montnemery <erik@montnemery.com> * No need for local lat/long variable * Return name attribute, will deal with it in another PR * Revert unique_id changes, will deal with this in a subsequent PR * Add time zone data description * Don't break the YAML config until the user has removed it. * Cleanup initial config flow test --------- Co-authored-by: Tsvi Mostovicz <ttmost@gmail.com> Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-05-24 12:04:17 +00:00
if CONF_LOCATION in user_input:
user_input[CONF_LATITUDE] = user_input[CONF_LOCATION][CONF_LATITUDE]
user_input[CONF_LONGITUDE] = user_input[CONF_LOCATION][CONF_LONGITUDE]
return self.async_create_entry(
title=DEFAULT_NAME, data=user_input, options=_options
)
Add config flow to Jewish Calendar (#84464) * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Initial fixes for tests * Initial commit * add basic tests (will probably fail) * Set basic UID for now * Various improvements * use new naming convention? * bit by bit, still not working tho * Add tz selection * Remove failing tests * update unique_id * add the tests again * revert to previous binary_sensor test * remove translations * apply suggestions * remove const.py * Address review * revert changes * Fix bad merges in rebase * Get tests to run again * Fixes due to fails in ruff/pylint * Fix binary sensor tests * Fix config flow tests * Fix sensor tests * Apply review * Adjust candle lights * Apply suggestion * revert unrelated change * Address some of the comments * We should only allow a single jewish calendar config entry * Make data schema easier to read * Add test and confirm only single entry is allowed * Move OPTIONS_SCHEMA to top of file * Add options test * Simplify import tests * Test import end2end * Use a single async_forward_entry_setups statement * Revert schema updates for YAML schema * Remove unneeded brackets * Remove CONF_NAME from config_flow * Assign hass.data[DOMAIN][config_entry.entry_id] to a local variable before creating the sensors * Data doesn't have a name remove slugifying of it * Test that the entry has been created correctly * Simplify setup_entry * Use suggested values helper and flatten location dictionary * Remove the string for name exists as this error doesn't exist * Remove name from config entry * Remove _attr_has_entity_name - will be added in a subsequent PR * Don't override entity id's - we'll fixup the naming later * Make location optional - will by default revert to the user's home location * Update homeassistant/components/jewish_calendar/strings.json Co-authored-by: Erik Montnemery <erik@montnemery.com> * No need for local lat/long variable * Return name attribute, will deal with it in another PR * Revert unique_id changes, will deal with this in a subsequent PR * Add time zone data description * Don't break the YAML config until the user has removed it. * Cleanup initial config flow test --------- Co-authored-by: Tsvi Mostovicz <ttmost@gmail.com> Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-05-24 12:04:17 +00:00
return self.async_show_form(
step_id="user",
data_schema=self.add_suggested_values_to_schema(
_get_data_schema(self.hass), user_input
),
)
async def async_step_import(
self, import_config: ConfigType | None
) -> ConfigFlowResult:
"""Import a config entry from configuration.yaml."""
return await self.async_step_user(import_config)
class JewishCalendarOptionsFlowHandler(OptionsFlowWithConfigEntry):
"""Handle Jewish Calendar options."""
async def async_step_init(
self, user_input: dict[str, str] | None = None
) -> ConfigFlowResult:
"""Manage the Jewish Calendar options."""
if user_input is not None:
return self.async_create_entry(data=user_input)
return self.async_show_form(
step_id="init",
data_schema=self.add_suggested_values_to_schema(
OPTIONS_SCHEMA, self.config_entry.options
),
)