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
53239387
Commit
53239387
authored
9 years ago
by
Philip Lundrigan
Browse files
Options
Downloads
Patches
Plain Diff
Add support for template
parent
df24a1bf
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
homeassistant/components/automation/numeric_state.py
+26
-9
26 additions, 9 deletions
homeassistant/components/automation/numeric_state.py
with
26 additions
and
9 deletions
homeassistant/components/automation/numeric_state.py
+
26
−
9
View file @
53239387
...
...
@@ -8,13 +8,14 @@ at https://home-assistant.io/components/automation/#numeric-state-trigger
"""
import
logging
from
homeassistant.const
import
CONF_VALUE_TEMPLATE
from
homeassistant.helpers.event
import
track_state_change
from
homeassistant.util
import
template
CONF_ENTITY_ID
=
"
entity_id
"
CONF_BELOW
=
"
below
"
CONF_ABOVE
=
"
above
"
CONF_ATTRIBUTE
=
"
attribute
"
_LOGGER
=
logging
.
getLogger
(
__name__
)
...
...
@@ -29,7 +30,7 @@ def trigger(hass, config, action):
below
=
config
.
get
(
CONF_BELOW
)
above
=
config
.
get
(
CONF_ABOVE
)
attribu
te
=
config
.
get
(
CONF_
ATTRIBU
TE
)
value_templa
te
=
config
.
get
(
CONF_
VALUE_TEMPLA
TE
)
if
below
is
None
and
above
is
None
:
_LOGGER
.
error
(
"
Missing configuration key.
"
...
...
@@ -37,13 +38,18 @@ def trigger(hass, config, action):
CONF_BELOW
,
CONF_ABOVE
)
return
False
if
value_template
is
not
None
:
renderer
=
lambda
value
:
template
.
render
(
hass
,
value_template
,
value
)
else
:
renderer
=
None
# pylint: disable=unused-argument
def
state_automation_listener
(
entity
,
from_s
,
to_s
):
"""
Listens for state changes and calls action.
"""
# Fire action if we go from outside range into range
if
_in_range
(
to_s
,
above
,
below
,
attribute
)
and
\
(
from_s
is
None
or
not
_in_range
(
from_s
,
above
,
below
,
attribute
)):
if
_in_range
(
to_s
,
above
,
below
,
renderer
)
and
\
(
from_s
is
None
or
not
_in_range
(
from_s
,
above
,
below
,
renderer
)):
action
()
track_state_change
(
...
...
@@ -63,7 +69,7 @@ def if_action(hass, config):
below
=
config
.
get
(
CONF_BELOW
)
above
=
config
.
get
(
CONF_ABOVE
)
attribu
te
=
config
.
get
(
CONF_
ATTRIBU
TE
)
value_templa
te
=
config
.
get
(
CONF_
VALUE_TEMPLA
TE
)
if
below
is
None
and
above
is
None
:
_LOGGER
.
error
(
"
Missing configuration key.
"
...
...
@@ -71,18 +77,29 @@ def if_action(hass, config):
CONF_BELOW
,
CONF_ABOVE
)
return
None
if
value_template
is
not
None
:
renderer
=
lambda
value
:
template
.
render
(
hass
,
value_template
,
value
)
else
:
renderer
=
None
def
if_numeric_state
():
"""
Test numeric state condition.
"""
state
=
hass
.
states
.
get
(
entity_id
)
return
state
is
not
None
and
_in_range
(
state
,
above
,
below
,
attribute
)
return
state
is
not
None
and
_in_range
(
state
,
above
,
below
,
renderer
)
return
if_numeric_state
def
_in_range
(
state
,
range_start
,
range_end
,
attribute
):
def
_in_range
(
state
,
range_start
,
range_end
,
renderer
):
"""
Checks if value is inside the range
"""
value
=
(
state
.
state
if
attribute
is
None
else
state
.
attributes
.
get
(
attribute
))
if
renderer
is
not
None
:
value
=
renderer
({
'
value
'
:
state
})
else
:
# If no renderer is provided, just assume they want the state
value
=
state
.
state
try
:
value
=
float
(
value
)
except
ValueError
:
...
...
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