- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.2k
 
Help confusion with subentries #2708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4b327be
              ac7c02e
              36c2e43
              c9d268a
              ea77a1d
              b4b935b
              14795c6
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -434,8 +434,41 @@ class LocationSubentryFlowHandler(ConfigSubentryFlow): | |||||
| async def async_step_user( | ||||||
| self, user_input: dict[str, Any] | None = None | ||||||
| ) -> SubentryFlowResult: | ||||||
| """User flow to add a new location.""" | ||||||
| ... | ||||||
| """User flow to add a new location. | ||||||
| 
     | 
||||||
| Function name must be in the format "async_step_{step_id}" | ||||||
| The first step is always "user" | ||||||
| """ | ||||||
| 
     | 
||||||
| errors: dict[str, str] = {} | ||||||
| 
     | 
||||||
| # The function is called once to start the step and then again | ||||||
| # each time the user submits an input. This handles the latter | ||||||
| if user_input is not None: | ||||||
| try: | ||||||
| user_input = await _validate_subentry(user_input) | ||||||
| return self.async_create_entry( | ||||||
                
      
                  HarvsG marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||||||
| title=user_input.get(CONF_NAME), | ||||||
| data=user_input, | ||||||
| ) | ||||||
| except (SchemaFlowError) as err: | ||||||
| # errors can be attached the base of a form or to a specific field | ||||||
| errors["base"] = str(err) | ||||||
| 
     | 
||||||
| # This runs when the step starts or after a failed validation | ||||||
| return self.async_show_form( | ||||||
| step_id=str(ObservationTypes.STATE), | ||||||
| data_schema=self.add_suggested_values_to_schema( | ||||||
| data_schema=LOCATION_SUBSCHEMA, suggested_values=user_input | ||||||
| ), | ||||||
| last_step=True, | ||||||
| errors=errors, | ||||||
| description_placeholders={ | ||||||
| # the parent config entry can be accessed with self._get_entry() | ||||||
| "parent_entry_title": self._get_entry().title, | ||||||
| }, | ||||||
| ) | ||||||
| 
     | 
||||||
| ``` | ||||||
| 
     | 
||||||
| ### Subentry unique ID | ||||||
| 
        
          
        
         | 
    @@ -454,7 +487,7 @@ Subentries can set a unique ID. The rules are similar to [unique IDs](#unique-id | |||||
| "step": { | ||||||
| "user": { | ||||||
| "title": "Add location", | ||||||
| "description": "Configure the weather location" | ||||||
| "description": "Configure a weather location for {parent_entry_title}." | ||||||
| }, | ||||||
| "reconfigure": { | ||||||
| "title": "Update location", | ||||||
| 
          
            
          
           | 
    @@ -488,11 +521,44 @@ class LocationSubentryFlowHandler(ConfigSubentryFlow): | |||||
| self, user_input: dict[str, Any] | None = None | ||||||
| ) -> SubentryFlowResult: | ||||||
| """User flow to modify an existing location.""" | ||||||
| 
     | 
||||||
| errors: dict[str, str] = {} | ||||||
| # Retrieve the parent config entry for reference. | ||||||
| config_entry = self._get_reconfigure_entry() | ||||||
| # Retrieve the specific subentry targeted for update. | ||||||
| config_subentry = self._get_reconfigure_subentry() | ||||||
| ... | ||||||
| 
     | 
||||||
| if user_input is not None: | ||||||
| # validate user_input, possibly with some checks on ther subentries | ||||||
| # If checking for duplicates remeber to remove the entry you are reconfiguring | ||||||
| other_subentries = [ | ||||||
| dict(se.data) for se in config_entry.subentries.values() | ||||||
| ] | ||||||
| other_subentries.remove(dict(config_subentry.data)) | ||||||
| 
         
      Comment on lines
    
      +532
     to 
      +537
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would assume that this is something we should take into account with helpers There are some typos in the comment  | 
||||||
| try: | ||||||
| ... #validation | ||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
  | 
||||||
| return self.async_update_and_abort( | ||||||
| config_entry, | ||||||
| config_subentry, | ||||||
| title=user_input.get(CONF_NAME, config_subentry.data[CONF_NAME]), | ||||||
| data_updates=user_input, | ||||||
| ) | ||||||
| 
         
      Comment on lines
    
      +540
     to 
      +545
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep this out of the try  | 
||||||
| except (SchemaFlowError) as err: | ||||||
| # errors can be attached the base of a form or to a specific field | ||||||
| errors["base"] = str(err) | ||||||
| 
     | 
||||||
| return self.async_show_form( | ||||||
| step_id="reconfigure", | ||||||
| # You will likely want to fetch original values | ||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
  | 
||||||
| data_schema=self.add_suggested_values_to_schema( | ||||||
| data_schema=SUBENTRY_SCHEMA, | ||||||
| suggested_values=config_subentry.data, | ||||||
| 
         
      Comment on lines
    
      +554
     to 
      +555
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we hardly use kwargs for these  | 
||||||
| ), | ||||||
| errors=errors, | ||||||
| description_placeholders={ | ||||||
| "parent_entry_title": self._get_entry().title, | ||||||
| }, | ||||||
| ) | ||||||
| 
     | 
||||||
| ``` | ||||||
| 
     | 
||||||
| 
          
            
          
           | 
    ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not use docstrings for this