Skip to content

Commit b34a0f3

Browse files
author
bruxy70
committed
#441 Fixed default options
1 parent 89bd946 commit b34a0f3

File tree

2 files changed

+96
-45
lines changed

2 files changed

+96
-45
lines changed

custom_components/garbage_collection/config_flow.py

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import homeassistant.helpers.config_validation as cv
1111
import voluptuous as vol
12-
from homeassistant.const import ATTR_HIDDEN, CONF_ENTITIES, CONF_NAME, WEEKDAYS
12+
from homeassistant.const import ATTR_HIDDEN, CONF_ENTITIES, CONF_NAME
1313
from homeassistant.core import callback
1414
from homeassistant.helpers import selector
1515
from homeassistant.helpers.schema_config_entry_flow import (
@@ -63,15 +63,18 @@ def optional(
6363
return vol.Optional(key, description={"suggested_value": suggested_value})
6464

6565

66-
async def general_options_schema(
66+
async def general_config_schema(
6767
handler: SchemaConfigFlowHandler | SchemaOptionsFlowHandler,
6868
) -> vol.Schema:
69-
"""Generate options schema."""
69+
"""Generate config schema."""
7070
return vol.Schema(
7171
{
72+
optional(CONF_NAME, handler.options): selector.TextSelector(),
7273
required(
7374
const.CONF_FREQUENCY, handler.options, const.DEFAULT_FREQUENCY
74-
): vol.In(const.FREQUENCY_OPTIONS),
75+
): selector.SelectSelector(
76+
selector.SelectSelectorConfig(options=const.FREQUENCY_OPTIONS)
77+
),
7578
optional(
7679
const.CONF_ICON_NORMAL, handler.options, const.DEFAULT_ICON_NORMAL
7780
): selector.IconSelector(),
@@ -91,16 +94,17 @@ async def general_options_schema(
9194
)
9295

9396

94-
async def general_config_schema(
97+
async def general_options_schema(
9598
handler: SchemaConfigFlowHandler | SchemaOptionsFlowHandler,
9699
) -> vol.Schema:
97-
"""Generate config schema."""
100+
"""Generate options schema."""
98101
return vol.Schema(
99102
{
100-
optional(CONF_NAME, handler.options): selector.TextSelector(),
101103
required(
102104
const.CONF_FREQUENCY, handler.options, const.DEFAULT_FREQUENCY
103-
): vol.In(const.FREQUENCY_OPTIONS),
105+
): selector.SelectSelector(
106+
selector.SelectSelectorConfig(options=const.FREQUENCY_OPTIONS)
107+
),
104108
optional(
105109
const.CONF_ICON_NORMAL, handler.options, const.DEFAULT_ICON_NORMAL
106110
): selector.IconSelector(),
@@ -127,7 +131,9 @@ async def detail_config_schema(
127131
options_schema: Dict[vol.Optional | vol.Required, Any] = {}
128132
if handler.options[const.CONF_FREQUENCY] in const.ANNUAL_FREQUENCY:
129133
# "annual"
130-
options_schema[required(const.CONF_DATE, handler.options)] = str
134+
options_schema[
135+
required(const.CONF_DATE, handler.options)
136+
] = selector.TextSelector()
131137
elif handler.options[const.CONF_FREQUENCY] in const.GROUP_FREQUENCY:
132138
# "group"
133139
options_schema[
@@ -140,41 +146,67 @@ async def detail_config_schema(
140146
elif handler.options[const.CONF_FREQUENCY] not in const.BLANK_FREQUENCY:
141147
# everything else except "blank" and every-n-days
142148
if handler.options[const.CONF_FREQUENCY] not in const.DAILY_FREQUENCY:
143-
weekdays_dict = {weekday: weekday for weekday in WEEKDAYS}
144149
options_schema[
145150
required(const.CONF_COLLECTION_DAYS, handler.options)
146-
] = cv.multi_select(weekdays_dict)
151+
] = selector.SelectSelector(
152+
selector.SelectSelectorConfig(
153+
options=const.WEEKDAY_OPTIONS,
154+
multiple=True,
155+
mode=selector.SelectSelectorMode.LIST,
156+
)
157+
)
147158
# everything else except "blank"
148159
options_schema[
149160
optional(const.CONF_FIRST_MONTH, handler.options, const.DEFAULT_FIRST_MONTH)
150-
] = vol.In(const.MONTH_OPTIONS)
161+
] = selector.SelectSelector(
162+
selector.SelectSelectorConfig(options=const.MONTH_OPTIONS)
163+
)
151164
options_schema[
152165
optional(const.CONF_LAST_MONTH, handler.options, const.DEFAULT_LAST_MONTH)
153-
] = vol.In(const.MONTH_OPTIONS)
166+
] = selector.SelectSelector(
167+
selector.SelectSelectorConfig(options=const.MONTH_OPTIONS)
168+
)
154169
if handler.options[const.CONF_FREQUENCY] in const.MONTHLY_FREQUENCY:
155170
# "monthly"
156171
options_schema[
157172
optional(const.CONF_WEEKDAY_ORDER_NUMBER, handler.options)
158-
] = vol.All(
159-
cv.multi_select(
160-
{"1": "1st", "2": "2nd", "3": "3rd", "4": "4th", "5": "5th"}
161-
),
173+
] = selector.SelectSelector(
174+
selector.SelectSelectorConfig(
175+
options=const.ORDER_OPTIONS,
176+
multiple=True,
177+
mode=selector.SelectSelectorMode.LIST,
178+
)
162179
)
163180
options_schema[
164181
optional(const.CONF_FORCE_WEEK_NUMBERS, handler.options)
165-
] = bool
182+
] = selector.BooleanSelector()
166183
if handler.options[const.CONF_FREQUENCY] in const.WEEKLY_DAILY_MONTHLY:
167184
# "every-n-weeks", "every-n-days", "monthly"
168-
options_schema[required(const.CONF_PERIOD, handler.options)] = vol.All(
169-
vol.Coerce(int), vol.Range(min=1, max=1000)
185+
uom = {"every-n-weeks": "weeks", "every-n-days": "days", "monthly": "month"}
186+
options_schema[
187+
required(const.CONF_PERIOD, handler.options)
188+
] = selector.NumberSelector(
189+
selector.NumberSelectorConfig(
190+
min=1,
191+
max=1000,
192+
mode=selector.NumberSelectorMode.BOX,
193+
unit_of_measurement=uom[handler.options[const.CONF_FREQUENCY]],
194+
)
170195
)
171196
if handler.options[const.CONF_FREQUENCY] in const.WEEKLY_FREQUENCY_X:
172197
# every-n-weeks
173198
options_schema[
174199
required(
175200
const.CONF_FIRST_WEEK, handler.options, const.DEFAULT_FIRST_WEEK
176201
)
177-
] = vol.All(vol.Coerce(int), vol.Range(min=1, max=52))
202+
] = selector.NumberSelector(
203+
selector.NumberSelectorConfig(
204+
min=1,
205+
max=52,
206+
mode=selector.NumberSelectorMode.BOX,
207+
unit_of_measurement="weeks",
208+
)
209+
)
178210
if handler.options[const.CONF_FREQUENCY] in const.DAILY_FREQUENCY:
179211
# every-n-days
180212
options_schema[
@@ -186,10 +218,10 @@ async def detail_config_schema(
186218
required(
187219
const.CONF_VERBOSE_FORMAT, handler.options, const.DEFAULT_VERBOSE_FORMAT
188220
)
189-
] = cv.string
221+
] = selector.TextSelector()
190222
options_schema[
191223
required(const.CONF_DATE_FORMAT, handler.options, const.DEFAULT_DATE_FORMAT)
192-
] = cv.string
224+
] = selector.TextSelector()
193225
return vol.Schema(options_schema)
194226

195227

custom_components/garbage_collection/const.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Define constants used in garbage_collection."""
2-
2+
from homeassistant.helpers import selector
33

44
# Constants for garbage_collection.
55
# Base component constants
@@ -67,15 +67,15 @@
6767
STATE_TOMORROW = "tomorrow"
6868

6969
FREQUENCY_OPTIONS = [
70-
"weekly",
71-
"even-weeks",
72-
"odd-weeks",
73-
"every-n-weeks",
74-
"every-n-days",
75-
"monthly",
76-
"annual",
77-
"blank",
78-
"group",
70+
selector.SelectOptionDict(value="weekly", label="weekly"),
71+
selector.SelectOptionDict(value="even-weeks", label="even-weeks"),
72+
selector.SelectOptionDict(value="odd-weeks", label="odd-weeks"),
73+
selector.SelectOptionDict(value="every-n-weeks", label="every-n-weeks"),
74+
selector.SelectOptionDict(value="every-n-days", label="every-n-days"),
75+
selector.SelectOptionDict(value="monthly", label="monthly"),
76+
selector.SelectOptionDict(value="annual", label="annual"),
77+
selector.SelectOptionDict(value="blank", label="blank"),
78+
selector.SelectOptionDict(value="group", label="group"),
7979
]
8080

8181
WEEKLY_FREQUENCY = ["weekly", "even-weeks", "odd-weeks"]
@@ -106,17 +106,36 @@
106106
GROUP_FREQUENCY = ["group"]
107107
BLANK_FREQUENCY = ["blank"]
108108

109+
WEEKDAY_OPTIONS = [
110+
selector.SelectOptionDict(value="mon", label="Monday"),
111+
selector.SelectOptionDict(value="tue", label="Tuesday"),
112+
selector.SelectOptionDict(value="wed", label="Wednesday"),
113+
selector.SelectOptionDict(value="thu", label="Thursday"),
114+
selector.SelectOptionDict(value="fri", label="Friday"),
115+
selector.SelectOptionDict(value="sat", label="Saturday"),
116+
selector.SelectOptionDict(value="sun", label="Sunday"),
117+
]
118+
119+
109120
MONTH_OPTIONS = [
110-
"jan",
111-
"feb",
112-
"mar",
113-
"apr",
114-
"may",
115-
"jun",
116-
"jul",
117-
"aug",
118-
"sep",
119-
"oct",
120-
"nov",
121-
"dec",
121+
selector.SelectOptionDict(value="jan", label="January"),
122+
selector.SelectOptionDict(value="feb", label="February"),
123+
selector.SelectOptionDict(value="mar", label="March"),
124+
selector.SelectOptionDict(value="apr", label="April"),
125+
selector.SelectOptionDict(value="may", label="May"),
126+
selector.SelectOptionDict(value="jun", label="June"),
127+
selector.SelectOptionDict(value="jul", label="July"),
128+
selector.SelectOptionDict(value="aug", label="August"),
129+
selector.SelectOptionDict(value="sep", label="September"),
130+
selector.SelectOptionDict(value="oct", label="October"),
131+
selector.SelectOptionDict(value="nov", label="November"),
132+
selector.SelectOptionDict(value="dec", label="December"),
133+
]
134+
135+
ORDER_OPTIONS = [
136+
selector.SelectOptionDict(value="1", label="1st"),
137+
selector.SelectOptionDict(value="2", label="2nd"),
138+
selector.SelectOptionDict(value="3", label="3rd"),
139+
selector.SelectOptionDict(value="4", label="4th"),
140+
selector.SelectOptionDict(value="5", label="5th"),
122141
]

0 commit comments

Comments
 (0)