-
-
Notifications
You must be signed in to change notification settings - Fork 118
Add ha core reload command to reload YAML configuration
#640
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a185048
Add `ha core reload` command with `--component` flag
paulczar 3eb02a4
string validation on component name
paulczar 1fe1a38
treat all 4xx error codes as not found or does not support reload
paulczar a25af2a
Suppress false-positive gosec lint warnings
paulczar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| helper "github.com/home-assistant/cli/client" | ||
| log "github.com/sirupsen/logrus" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var coreReloadCmd = &cobra.Command{ | ||
| Use: "reload", | ||
| Aliases: []string{"refresh"}, | ||
| Short: "Reload the Home Assistant Core configuration", | ||
| Long: ` | ||
| Reload the Home Assistant Core YAML configuration without restarting. | ||
| Use --component to reload a specific component (e.g., automations, scripts). | ||
| Without --component, reloads the core configuration.`, | ||
| Example: ` | ||
| ha core reload | ||
| ha core reload --component automation | ||
| ha core reload --component script | ||
| ha core reload --component scene | ||
| ha core reload --component group`, | ||
| ValidArgsFunction: cobra.NoFileCompletions, | ||
| Args: cobra.NoArgs, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| log.WithField("args", args).Debug("core reload") | ||
|
|
||
| component, _ := cmd.Flags().GetString("component") | ||
|
|
||
| var section, command string | ||
| if component != "" { | ||
| section = "core/api/services/" + component | ||
| command = "reload" | ||
| } else { | ||
| section = "core/api/services/homeassistant" | ||
| command = "reload_all" | ||
| } | ||
|
|
||
| url, err := helper.URLHelper(section, command) | ||
| if err != nil { | ||
| helper.PrintError(err) | ||
| ExitWithError = true | ||
| return | ||
| } | ||
|
|
||
| resp, err := helper.GetRequest().Post(url) | ||
| if err != nil { | ||
| helper.PrintError(err) | ||
| ExitWithError = true | ||
| return | ||
| } | ||
|
|
||
| if resp.StatusCode() != http.StatusOK { | ||
| if component != "" && resp.StatusCode() == http.StatusBadRequest { | ||
| helper.PrintErrorString(fmt.Sprintf( | ||
| "component %q not found or does not support reload", component)) | ||
| } else { | ||
| helper.PrintErrorString(fmt.Sprintf("unexpected response (status: %d)", resp.StatusCode())) | ||
| } | ||
| ExitWithError = true | ||
| } else { | ||
| fmt.Println("Command completed successfully.") | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| coreReloadCmd.Flags().StringP("component", "c", "", | ||
| "Specific component to reload (e.g., automation, script, scene, group)") | ||
| coreReloadCmd.RegisterFlagCompletionFunc("component", | ||
| func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return []string{ | ||
| "automation", | ||
| "script", | ||
| "scene", | ||
| "group", | ||
| "input_boolean", | ||
| "input_number", | ||
| "input_select", | ||
| "input_text", | ||
| "input_datetime", | ||
| "input_button", | ||
| "timer", | ||
| "counter", | ||
| "template", | ||
| "zone", | ||
| "schedule", | ||
| "person", | ||
| }, cobra.ShellCompDirectiveNoFileComp | ||
| }) | ||
|
|
||
| coreCmd.AddCommand(coreReloadCmd) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.