Add files via upload

This commit is contained in:
sudoxnym 2024-05-17 14:18:04 -06:00 committed by GitHub
parent fdce9b854c
commit a54d507b34
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 25 additions and 12 deletions

View file

@ -68,49 +68,62 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
"""Manage the options.""" """Manage the options."""
_logger.debug("Entering async_step_init with user_input: %s", user_input) _logger.debug("Entering async_step_init with user_input: %s", user_input)
errors = {} # Define errors here
try: try:
# Fetch the initial configuration data # Fetch the initial configuration data
current_data = self.hass.data[DOMAIN].get(self.config_entry.entry_id, self.config_entry.options) current_data = self.hass.data[DOMAIN].get(self.config_entry.entry_id, self.config_entry.options)
_logger.debug("Current data fetched: %s", current_data) _logger.debug("Current data fetched: %s", current_data)
# Get the list of notify targets
notify_services = self.hass.services.async_services().get('notify', {})
notify_targets = {target.replace('mobile_app_', '').title(): target for target in notify_services.keys() if target.startswith('mobile_app_')}
if user_input is not None: if user_input is not None:
# Validate the user input here
# If the input is valid, create an entry
# If the input is not valid, add an error message to the 'errors' dictionary
# For example:
if not user_input[CONF_NAME]:
errors[CONF_NAME] = "required"
if errors:
return self.async_show_form(step_id="init", data_schema=self.get_data_schema(current_data), errors=errors) # Pass errors to async_show_form
# Map the selected option back to the actual notify target name # Map the selected option back to the actual notify target name
user_input[CONF_NOTIFY_TARGET] = notify_targets[user_input[CONF_NOTIFY_TARGET]] user_input[CONF_NOTIFY_TARGET] = notify_targets[user_input[CONF_NOTIFY_TARGET]]
# Merge current_data with user_input # Merge current_data with user_input
updated_data = {**current_data, **user_input} updated_data = {**current_data, **user_input}
_logger.debug("User input is not None, updated data: %s", updated_data) _logger.debug("User input is not None, updated data: %s", updated_data)
_logger.debug("Updating entry with updated data: %s", updated_data) _logger.debug("Updating entry with updated data: %s", updated_data)
if updated_data is not None: if updated_data is not None:
self.hass.data[DOMAIN][self.config_entry.entry_id] = updated_data # Save updated data self.hass.data[DOMAIN][self.config_entry.entry_id] = updated_data # Save updated data
# Update the entry data # Update the entry data
self.hass.config_entries.async_update_entry(self.config_entry, data=updated_data) self.hass.config_entries.async_update_entry(self.config_entry, data=updated_data)
# Send a signal to reload the integration # Send a signal to reload the integration
async_dispatcher_send(self.hass, f"{DOMAIN}_reload_{self.config_entry.entry_id}") async_dispatcher_send(self.hass, f"{DOMAIN}_reload_{self.config_entry.entry_id}")
return self.async_create_entry(title="", data=updated_data) return self.async_create_entry(title="", data=updated_data)
_logger.debug("User input is None, showing form with current_data: %s", current_data) _logger.debug("User input is None, showing form with current_data: %s", current_data)
return self.async_show_form(step_id="init", data_schema=self.get_data_schema(current_data)) return self.async_show_form(step_id="init", data_schema=self.get_data_schema(current_data), errors=errors) # Pass errors to async_show_form
except Exception as e: except Exception as e:
_logger.error("Error in async_step_init: %s", str(e)) _logger.error("Error in async_step_init: %s", str(e))
return self.async_abort(reason=str(e)) return self.async_abort(reason=str(e))
def get_data_schema(self, current_data): def get_data_schema(self, current_data):
# Get the list of notify targets # Get the list of notify targets
notify_services = self.hass.services.async_services().get('notify', {}) notify_services = self.hass.services.async_services().get('notify', {})
notify_targets = {target.replace('mobile_app_', '').replace('_', ' ').title(): target for target in notify_services.keys() if target.startswith('mobile_app_')} notify_targets = {target.replace('mobile_app_', '').title(): target for target in notify_services.keys() if target.startswith('mobile_app_')}
# Extract the part after 'mobile_app_' and remove underscore # Extract the part after 'mobile_app_' and capitalize
notify_target = current_data.get(CONF_NOTIFY_TARGET, "") notify_target = current_data.get(CONF_NOTIFY_TARGET, "")
notify_target = notify_target.replace('mobile_app_', '').replace('_', ' ').title() if notify_target.startswith('mobile_app_') else notify_target notify_target = notify_target.replace('mobile_app_', '').title() if notify_target.startswith('mobile_app_') else notify_target
return Schema( return Schema(
{ {