Skip to content

Commit

Permalink
Miscellaneous improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Dec 4, 2023
1 parent 4c590f7 commit 3902be5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
4 changes: 2 additions & 2 deletions custom_components/sun2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
21 changes: 6 additions & 15 deletions custom_components/sun2/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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(),
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 3902be5

Please sign in to comment.