Skip to content

Commit

Permalink
Add menu to options flow
Browse files Browse the repository at this point in the history
  • Loading branch information
WaresWichall committed Oct 30, 2024
1 parent 0cc8bf5 commit 6fe4933
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 49 deletions.
165 changes: 124 additions & 41 deletions custom_components/anycubic_cloud/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,40 +360,23 @@ def __init__(self, entry: ConfigEntry) -> None:
self._anycubic_api: AnycubicAPI | None = None
self._supports_drying = False

def _build_options_schema(self) -> vol.Schema:
def _build_drying_options_schema(self) -> vol.Schema:
schema: dict[Any, Any] = dict()

schema[vol.Optional(
CONF_MQTT_CONNECT_MODE,
default=self.entry.options.get(CONF_MQTT_CONNECT_MODE, AnycubicMQTTConnectMode.Printing_Only)
)] = vol.In(MQTT_CONNECT_MODES)

if self._supports_drying:

for x in range(MAX_DRYING_PRESETS):
num = x + 1
for x in range(MAX_DRYING_PRESETS):
num = x + 1

dur_key = f"{CONF_DRYING_PRESET_DURATION_}{num}"
schema[vol.Optional(
dur_key,
default=self.entry.options.get(dur_key, vol.UNDEFINED)
)] = cv.positive_int
dur_key = f"{CONF_DRYING_PRESET_DURATION_}{num}"
schema[vol.Optional(
dur_key,
default=self.entry.options.get(dur_key, vol.UNDEFINED)
)] = cv.positive_int

temp_key = f"{CONF_DRYING_PRESET_TEMPERATURE_}{num}"
schema[vol.Optional(
temp_key,
default=self.entry.options.get(temp_key, vol.UNDEFINED)
)] = cv.positive_int

schema[vol.Optional(
CONF_CARD_CONFIG,
default=self.entry.options.get(CONF_CARD_CONFIG, None)
)] = ObjectSelector()

schema[vol.Optional(
CONF_DEBUG,
default=self.entry.options.get(CONF_DEBUG, False)
)] = BooleanSelector()
temp_key = f"{CONF_DRYING_PRESET_TEMPERATURE_}{num}"
schema[vol.Optional(
temp_key,
default=self.entry.options.get(temp_key, vol.UNDEFINED)
)] = cv.positive_int

return vol.Schema(schema)

Expand Down Expand Up @@ -428,20 +411,120 @@ async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage Anycubic Cloud options."""
errors: dict[str, Any] = {}
return await self.async_step_options_menu()

if user_input:
if CONF_CARD_CONFIG in user_input:
if isinstance(user_input[CONF_CARD_CONFIG], dict):
user_input[CONF_CARD_CONFIG] = extract_panel_card_config(
user_input[CONF_CARD_CONFIG]
)
return self.async_create_entry(data=user_input)
async def async_step_options_menu(
self, _: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Options menu."""

await self._async_check_printer_options()

menu_options = list([
"mqtt",
"card_config",
"debug",
])

if self._supports_drying:
menu_options.insert(1, "drying")

return self.async_show_menu(
step_id="options_menu",
menu_options=menu_options,
)

@callback
def async_create_entry_with_existing_options(
self,
user_input: Mapping[str, Any],
) -> ConfigFlowResult:
return self.async_create_entry(
data={
**self.entry.options,
**user_input,
}
)

async def async_step_mqtt(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage Anycubic Cloud MQTT options."""
if user_input:
return self.async_create_entry_with_existing_options(user_input)

default_mqtt_connect_mode = self.entry.options.get(
CONF_MQTT_CONNECT_MODE,
AnycubicMQTTConnectMode.Printing_Only,
)

return self.async_show_form(
step_id="init",
data_schema=self._build_options_schema(),
errors=errors,
step_id="mqtt",
data_schema=vol.Schema({
vol.Optional(
CONF_MQTT_CONNECT_MODE, default=default_mqtt_connect_mode
): vol.In(MQTT_CONNECT_MODES)
}),
errors={},
)

async def async_step_drying(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage Anycubic Cloud drying options."""
if user_input:
return self.async_create_entry_with_existing_options(user_input)

return self.async_show_form(
step_id="drying",
data_schema=self._build_drying_options_schema(),
errors={},
)

async def async_step_card_config(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage Anycubic Cloud card_config options."""
if user_input:
if isinstance(user_input[CONF_CARD_CONFIG], dict):
user_input[CONF_CARD_CONFIG] = extract_panel_card_config(
user_input[CONF_CARD_CONFIG]
)
return self.async_create_entry_with_existing_options(user_input)

default_card_config = self.entry.options.get(
CONF_CARD_CONFIG,
None,
)

return self.async_show_form(
step_id="card_config",
data_schema=vol.Schema({
vol.Optional(
CONF_CARD_CONFIG, default=default_card_config
): ObjectSelector()
}),
errors={},
)

async def async_step_debug(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage Anycubic Cloud debug options."""
if user_input:
return self.async_create_entry_with_existing_options(user_input)

default_debug = self.entry.options.get(
CONF_DEBUG,
False,
)

return self.async_show_form(
step_id="debug",
data_schema=vol.Schema({
vol.Optional(
CONF_DEBUG, default=default_debug
): BooleanSelector()
}),
errors={},
)
33 changes: 29 additions & 4 deletions custom_components/anycubic_cloud/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@
},
"options": {
"step": {
"init": {
"options_menu": {
"title": "Anycubic Cloud Options",
"menu_options": {
"mqtt": "MQTT Settings",
"drying": "Drying Presets",
"card_config": "Panel Card Config",
"debug": "Debug Settings"
}
},
"mqtt": {
"title": "MQTT Settings",
"data": {
"mqtt_connect_mode": "MQTT Connection Mode"
}
},
"drying": {
"title": "Drying Presets",
"data": {
"mqtt_connect_mode": "MQTT Connection Mode",
"card_config": "Panel Card Config (YAML format)",
"debug": "Debug Mode",
"drying_preset_duration_1": "Dry Preset 1 Duration",
"drying_preset_temperature_1": "Dry Preset 1 Temperature",
"drying_preset_duration_2": "Dry Preset 2 Duration",
Expand All @@ -60,6 +73,18 @@
"drying_preset_duration_4": "Dry Preset 4 Duration",
"drying_preset_temperature_4": "Dry Preset 4 Temperature"
}
},
"card_config": {
"title": "Panel Card Config",
"data": {
"card_config": "Panel Card Config (YAML format)"
}
},
"debug": {
"title": "Debug Settings",
"data": {
"debug": "Debug Mode"
}
}
}
},
Expand Down
33 changes: 29 additions & 4 deletions custom_components/anycubic_cloud/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@
},
"options": {
"step": {
"init": {
"options_menu": {
"title": "Anycubic Cloud Options",
"menu_options": {
"mqtt": "MQTT Settings",
"drying": "Drying Presets",
"card_config": "Panel Card Config",
"debug": "Debug Settings"
}
},
"mqtt": {
"title": "MQTT Settings",
"data": {
"mqtt_connect_mode": "MQTT Connection Mode"
}
},
"drying": {
"title": "Drying Presets",
"data": {
"mqtt_connect_mode": "MQTT Connection Mode",
"card_config": "Panel Card Config (YAML format)",
"debug": "Debug Mode",
"drying_preset_duration_1": "Dry Preset 1 Duration",
"drying_preset_temperature_1": "Dry Preset 1 Temperature",
"drying_preset_duration_2": "Dry Preset 2 Duration",
Expand All @@ -60,6 +73,18 @@
"drying_preset_duration_4": "Dry Preset 4 Duration",
"drying_preset_temperature_4": "Dry Preset 4 Temperature"
}
},
"card_config": {
"title": "Panel Card Config",
"data": {
"card_config": "Panel Card Config (YAML format)"
}
},
"debug": {
"title": "Debug Settings",
"data": {
"debug": "Debug Mode"
}
}
}
},
Expand Down

0 comments on commit 6fe4933

Please sign in to comment.