Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
HomeAssistant
Core
Commits
a557cf38
Commit
a557cf38
authored
9 years ago
by
Paulus Schoutsen
Browse files
Options
Downloads
Patches
Plain Diff
Better approach writing default configuration
parent
17af1a68
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homeassistant/config.py
+34
-18
34 additions, 18 deletions
homeassistant/config.py
with
34 additions
and
18 deletions
homeassistant/config.py
+
34
−
18
View file @
a557cf38
...
...
@@ -19,8 +19,20 @@ _LOGGER = logging.getLogger(__name__)
YAML_CONFIG_FILE
=
'
configuration.yaml
'
CONF_CONFIG_FILE
=
'
home-assistant.conf
'
DEFAULT_CONFIG
=
[
# Tuples (attribute, default, auto detect property, description)
(
CONF_NAME
,
'
Home
'
,
None
,
'
Name of the location where Home Assistant is
'
'
running
'
),
(
CONF_LATITUDE
,
None
,
'
latitude
'
,
'
Location required to calculate the time
'
'
the sun rises and sets
'
),
(
CONF_LONGITUDE
,
None
,
'
longitude
'
,
None
),
(
CONF_TEMPERATURE_UNIT
,
'
C
'
,
None
,
'
C for Celcius, F for Fahrenheit
'
),
(
CONF_TIME_ZONE
,
'
UTC
'
,
'
time_zone
'
,
'
Pick yours from here: http://en.wiki
'
'
pedia.org/wiki/List_of_tz_database_time_zones
'
),
]
DEFAULT_COMPONENTS
=
[
'
discovery
'
,
'
frontend
'
,
'
conversation
'
,
'
history
'
,
'
logbook
'
]
'
discovery
'
,
'
frontend
'
,
'
conversation
'
,
'
history
'
,
'
logbook
'
,
'
sun
'
]
def
ensure_config_exists
(
config_dir
,
detect_location
=
True
):
...
...
@@ -41,29 +53,33 @@ def create_default_config(config_dir, detect_location=True):
Returns path to new config file if success, None if failed.
"""
config_path
=
os
.
path
.
join
(
config_dir
,
YAML_CONFIG_FILE
)
info
=
{
attr
:
default
for
attr
,
default
,
*
_
in
DEFAULT_CONFIG
}
location_info
=
detect_location
and
util
.
detect_location_info
()
if
location_info
:
if
location_info
.
use_fahrenheit
:
info
[
CONF_TEMPERATURE_UNIT
]
=
'
F
'
for
attr
,
default
,
prop
,
_
in
DEFAULT_CONFIG
:
if
prop
is
None
:
continue
info
[
attr
]
=
getattr
(
location_info
,
prop
)
or
default
# Writing files with YAML does not create the most human readable results
# So we're hard coding a YAML template.
try
:
with
open
(
config_path
,
'
w
'
)
as
config_file
:
location_info
=
detect_location
and
util
.
detect_location_info
()
if
location_info
:
temp_unit
=
'
F
'
if
location_info
.
use_fahrenheit
else
'
C
'
auto_config
=
{
CONF_NAME
:
'
Home
'
,
CONF_LATITUDE
:
location_info
.
latitude
,
CONF_LONGITUDE
:
location_info
.
longitude
,
CONF_TEMPERATURE_UNIT
:
temp_unit
,
CONF_TIME_ZONE
:
location_info
.
time_zone
,
}
config_file
.
write
(
"
homeassistant:
\n
"
)
config_file
.
write
(
"
homeassistant:
\n
"
)
for
key
,
value
in
auto_config
.
items
():
config_file
.
write
(
"
{}: {}
\n
"
.
format
(
key
,
value
))
for
attr
,
_
,
_
,
description
in
DEFAULT_CONFIG
:
if
info
[
attr
]
is
None
:
continue
elif
description
:
config_file
.
write
(
"
# {}
\n
"
.
format
(
description
))
config_file
.
write
(
"
{}: {}
\n
"
.
format
(
attr
,
info
[
attr
]))
config_file
.
write
(
"
\n
"
)
config_file
.
write
(
"
\n
"
)
for
component
in
DEFAULT_COMPONENTS
:
config_file
.
write
(
"
{}:
\n\n
"
.
format
(
component
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment