-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for hal.yaml config file
- Loading branch information
Showing
8 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,7 +1,14 @@ | ||
package hal | ||
|
||
type Automation interface { | ||
// Name is a friendly name for the automation, used in logs and stats. | ||
Name() string | ||
|
||
// Entities should return a list of entities that this automation should | ||
// listen on. For every state change to one of these entities, the | ||
// automation will be trigged. | ||
Entities() Entities | ||
|
||
// Action is called when the automation is triggered. | ||
Action() | ||
} |
This file contains 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 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,77 @@ | ||
package hal | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const configFilename = "hal.yaml" | ||
|
||
type Config struct { | ||
HomeAssistant HomeAssistantConfig `yaml:"homeAssistant"` | ||
} | ||
|
||
type HomeAssistantConfig struct { | ||
Host string `yaml:"host"` | ||
Token string `yaml:"token"` | ||
} | ||
|
||
func LoadConfig() (*Config, error) { | ||
configPath, err := searchParentsForFileFromCwd(configFilename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
yamlBytes, err := os.ReadFile(configPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config := Config{} | ||
if err := yaml.Unmarshal(yamlBytes, &config); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &config, nil | ||
} | ||
|
||
func searchParentsForFile(filename, searchPath string) (path string, err error) { | ||
for _, path := range getParents(searchPath) { | ||
f := filepath.Join(path, filename) | ||
if fileExists(f) { | ||
return f, nil | ||
} | ||
} | ||
|
||
return "", nil | ||
} | ||
|
||
func searchParentsForFileFromCwd(filename string) (path string, err error) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return searchParentsForFile(filename, wd) | ||
} | ||
|
||
func getParents(basePath string) (paths []string) { | ||
root := basePath | ||
|
||
for root != "/" { | ||
paths = append(paths, root) | ||
root = filepath.Dir(root) | ||
} | ||
|
||
paths = append(paths, "/") | ||
|
||
return paths | ||
} | ||
|
||
func fileExists(path string) bool { | ||
_, err := os.Stat(path) | ||
|
||
return !os.IsNotExist(err) | ||
} |
This file contains 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 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 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 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 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