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
946f6cab
Commit
946f6cab
authored
9 years ago
by
Paulus Schoutsen
Browse files
Options
Downloads
Plain Diff
Merge pull request #743 from philipbl/command_sensor
Command Sensor: Add support for template
parents
9e0d19df
e821b546
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/sensor/command_sensor.py
+11
-17
11 additions, 17 deletions
homeassistant/components/sensor/command_sensor.py
with
11 additions
and
17 deletions
homeassistant/components/sensor/command_sensor.py
+
11
−
17
View file @
946f6cab
...
...
@@ -10,8 +10,9 @@ import logging
import
subprocess
from
datetime
import
timedelta
from
homeassistant.const
import
CONF_VALUE_TEMPLATE
from
homeassistant.helpers.entity
import
Entity
from
homeassistant.util
import
Throttle
from
homeassistant.util
import
template
,
Throttle
_LOGGER
=
logging
.
getLogger
(
__name__
)
...
...
@@ -32,25 +33,24 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
data
=
CommandSensorData
(
config
.
get
(
'
command
'
))
add_devices_callback
([
CommandSensor
(
hass
,
data
,
config
.
get
(
'
name
'
,
DEFAULT_NAME
),
config
.
get
(
'
unit_of_measurement
'
),
config
.
get
(
'
correction_factor
'
,
None
),
config
.
get
(
'
decimal_places
'
,
None
)
config
.
get
(
CONF_VALUE_TEMPLATE
)
)])
# pylint: disable=too-many-arguments
class
CommandSensor
(
Entity
):
"""
Represents a sensor that is returning a value of a shell commands.
"""
def
__init__
(
self
,
data
,
name
,
unit_of_measurement
,
corr_factor
,
decimal_places
):
def
__init__
(
self
,
hass
,
data
,
name
,
unit_of_measurement
,
value_template
):
self
.
_hass
=
hass
self
.
data
=
data
self
.
_name
=
name
self
.
_state
=
False
self
.
_unit_of_measurement
=
unit_of_measurement
self
.
_corr_factor
=
corr_factor
self
.
_decimal_places
=
decimal_places
self
.
_value_template
=
value_template
self
.
update
()
@property
...
...
@@ -73,16 +73,10 @@ class CommandSensor(Entity):
self
.
data
.
update
()
value
=
self
.
data
.
value
try
:
if
value
is
not
None
:
if
self
.
_corr_factor
is
not
None
:
value
=
float
(
value
)
*
float
(
self
.
_corr_factor
)
if
self
.
_decimal_places
is
not
None
:
value
=
round
(
value
,
self
.
_decimal_places
)
if
self
.
_decimal_places
==
0
:
value
=
int
(
value
)
self
.
_state
=
value
except
ValueError
:
if
self
.
_value_template
is
not
None
:
self
.
_state
=
template
.
render_with_possible_json_value
(
self
.
_hass
,
self
.
_value_template
,
value
,
'
N/A
'
)
else
:
self
.
_state
=
value
...
...
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