-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
Added possibilities to use template in the command_line sensor #8505
Added possibilities to use template in the command_line sensor #8505
Conversation
Hi @mar-schmidt, It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
@mar-schmidt, thanks for your PR! By analyzing the history of the files in this pull request, we identified @devdelay, @fabaff and @rmkraus to be potential reviewers. |
|
||
if rendered_args == args: | ||
# No template used. default behavior | ||
shell=True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
local variable 'shell' is assigned to but never used
missing whitespace around operator
|
||
if args_compiled: | ||
try: | ||
a = {"arguments":args} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing whitespace after ':'
|
||
def update(self): | ||
"""Get the latest data with a shell command.""" | ||
_LOGGER.info("Running command: %s", self.command) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line contains whitespace
Hi @mar-schmidt, It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
else: | ||
# Template used. Construct the string used in the shell | ||
command = str(' '.join([prog] + shlex.split(rendered_args))) | ||
shell=True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing whitespace around operator
|
||
if args_compiled: | ||
try: | ||
a = { "arguments": args } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whitespace after '{'
whitespace before '}'
args_compiled = template.Template(args, self.hass) | ||
cache[command] = prog, args, args_compiled | ||
|
||
if args_compiled: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing whitespace
Hi @mar-schmidt, It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
Can you add a test |
def test_template_render(self): | ||
"""Ensure command with templates get rendered properly.""" | ||
self.hass.states.set('sensor.test_state', 'Works') | ||
data = command_line.CommandSensorData(self.hass, 'echo {{ states.sensor.test_state.state }}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line too long (101 > 79 characters)
"""Ensure command with templates get rendered properly.""" | ||
self.hass.states.set('sensor.test_state', 'Works') | ||
data = command_line.CommandSensorData(self.hass, | ||
'echo {{ states.sensor.test_state.state }}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
continuation line under-indented for visual indent
def test_template_render(self): | ||
"""Ensure command with templates get rendered properly.""" | ||
self.hass.states.set('sensor.test_state', 'Works') | ||
data = command_line.CommandSensorData(self.hass, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trailing whitespace
self.value = None | ||
self.hass = hass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not necessary. By the time update
gets called, self.hass
will be set by Home Assistant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is not the entity class, hass
needs to be passed in. I think @balloob was thinking about the entity class where it could be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok to merge when 1 comment addressed
@balloob removing this pointer will essentially throw some exceptions and break tests. Shall I keep it? 2017-07-24 18:23:33 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform command_line
3 Traceback (most recent call last):
4 File "/usr/local/lib/python3.4/dist-packages/homeassistant/helpers/entity_component.py", line 164, in _async_setup_platform
5 SLOW_SETUP_MAX_WAIT, loop=self.hass.loop)
6 File "/usr/lib/python3.4/asyncio/tasks.py", line 372, in wait_for
7 return fut.result()
8 File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
9 raise self._exception
10 File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
11 result = self.fn(*self.args, **self.kwargs)
12 File "/usr/local/lib/python3.4/dist-packages/homeassistant/components/sensor/command_line.py", line 48, in setup_platform
13 add_devices([CommandSensor(hass, data, name, unit, value_template)])
14 File "/usr/local/lib/python3.4/dist-packages/homeassistant/components/sensor/command_line.py", line 62, in __init__
15 self.update()
16 File "/usr/local/lib/python3.4/dist-packages/homeassistant/components/sensor/command_line.py", line 81, in update
17 self.data.update()
18 File "/usr/local/lib/python3.4/dist-packages/homeassistant/components/sensor/command_line.py", line 115, in update
19 args_compiled = template.Template(args, self.hass)
20 AttributeError: 'CommandSensorData' object has no attribute 'hass'
|
The code should not call |
@mar-schmidt: You should also send a PR to add a usage example in the docs! |
@mad-ady: here you go home-assistant/home-assistant.io#3157 |
…assistant#8505) * Added possibilities to use template in the command_line sensor * Minor style guideline conforms * Minor style guideline conforms * Added new test for template rendering * Minor style guideline conforms * Minor style guideline conforms * Fixed failing testcases * Fix style violations * fix code pretty
Description:
Added possibilities to use template in the command_line sensor
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#3157
Example entry for
configuration.yaml
(if applicable):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
tox
run successfully. Your PR cannot be merged unless tests passscript/gen_requirements_all.py
.If the code does not interact with devices:
tox
run successfully. Your PR cannot be merged unless tests pass