From 3902be598e84cbca12d8bdb9ee575562a98e67da Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Mon, 4 Dec 2023 07:52:24 -0600 Subject: [PATCH] Miscellaneous improvements --- custom_components/sun2/config.py | 4 ++-- custom_components/sun2/config_flow.py | 21 ++++++--------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/custom_components/sun2/config.py b/custom_components/sun2/config.py index 833b06c..d1943d6 100644 --- a/custom_components/sun2/config.py +++ b/custom_components/sun2/config.py @@ -68,12 +68,12 @@ vol.Coerce(float), vol.Range(min=-90, max=90), msg="invalid elevation" ) -_DIRECTIONS = [dir.lower() for dir in SunDirection.__members__] +SUN_DIRECTIONS = [dir.lower() for dir in SunDirection.__members__] TIME_AT_ELEVATION_SCHEMA_BASE = vol.Schema( { vol.Required(CONF_TIME_AT_ELEVATION): val_elevation, - vol.Optional(CONF_DIRECTION, default=_DIRECTIONS[0]): vol.In(_DIRECTIONS), + vol.Optional(CONF_DIRECTION, default=SUN_DIRECTIONS[0]): vol.In(SUN_DIRECTIONS), vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_NAME): cv.string, } diff --git a/custom_components/sun2/config_flow.py b/custom_components/sun2/config_flow.py index e00f327..7f5af66 100644 --- a/custom_components/sun2/config_flow.py +++ b/custom_components/sun2/config_flow.py @@ -2,11 +2,9 @@ from __future__ import annotations from abc import ABC, abstractmethod -from collections.abc import Callable from contextlib import suppress from typing import Any, cast -from astral import SunDirection import voluptuous as vol from homeassistant.config_entries import ( @@ -27,7 +25,7 @@ CONF_TIME_ZONE, CONF_UNIQUE_ID, ) -from homeassistant.core import callback, HomeAssistant +from homeassistant.core import callback from homeassistant.data_entry_flow import FlowResult import homeassistant.helpers.config_validation as cv from homeassistant.helpers.selector import ( @@ -44,7 +42,7 @@ ) from homeassistant.util.uuid import random_uuid_hex -from .config import val_elevation +from .config import SUN_DIRECTIONS, val_elevation from .const import ( CONF_DIRECTION, CONF_ELEVATION_AT_TIME, @@ -206,9 +204,6 @@ async def async_step_time_at_elevation_sensor( ) -> FlowResult: """Handle time_at_elevation sensor options.""" if user_input is not None: - user_input[CONF_DIRECTION] = vol.All(vol.Upper, cv.enum(SunDirection))( - user_input[CONF_DIRECTION] - ) return await self.async_finish_sensor(user_input, CONF_SENSORS) schema = { @@ -219,7 +214,7 @@ async def async_step_time_at_elevation_sensor( ), vol.Required(CONF_DIRECTION): SelectSelector( SelectSelectorConfig( - options=["rising", "setting"], translation_key="direction" + options=SUN_DIRECTIONS, translation_key="direction" ) ), vol.Optional(CONF_ICON): IconSelector(), @@ -277,21 +272,17 @@ def async_supports_options_flow(cls, config_entry: ConfigEntry) -> bool: async def async_step_import(self, data: dict[str, Any]) -> FlowResult: """Import config entry from configuration.""" - self._location_name = cast( - str, data.pop(CONF_LOCATION, self.hass.config.location_name) - ) + title = cast(str, data.pop(CONF_LOCATION, self.hass.config.location_name)) if existing_entry := await self.async_set_unique_id(data.pop(CONF_UNIQUE_ID)): if not self.hass.config_entries.async_update_entry( - existing_entry, title=self._location_name, options=data + existing_entry, title=title, options=data ): self.hass.async_create_task( self.hass.config_entries.async_reload(existing_entry.entry_id) ) return self.async_abort(reason="already_configured") - self.options.clear() - self.options.update(data) - return await self.async_step_done() + return self.async_create_entry(title=title, data={}, options=data) async def async_step_user(self, _: dict[str, Any] | None = None) -> FlowResult: """Start user config flow."""