Skip to content
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

Merged
merged 10 commits into from
Aug 10, 2017

Conversation

mar-schmidt
Copy link
Contributor

@mar-schmidt mar-schmidt commented Jul 16, 2017

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):

- platform: command_line
    name: yr wind direction cmd
    command: 'sh /home/pi/.homeassistant/scripts/wind_direction.sh {{ states.sensor.yr_wind_direction_template.state }}'
    unit_of_measurement: "Vind"

Checklist:

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
    script/gen_requirements_all.py.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@homeassistant
Copy link
Contributor

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!

@mention-bot
Copy link

@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

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}

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

@homeassistant
Copy link
Contributor

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

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 }

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:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing whitespace

@homeassistant
Copy link
Contributor

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!

@balloob
Copy link
Member

balloob commented Jul 16, 2017

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 }}')

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 }}')

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,

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
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

@balloob balloob left a 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

@mar-schmidt
Copy link
Contributor Author

@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'

tests/components/sensor/test_command_line.py ✓⨯✓⨯⨯

@balloob
Copy link
Member

balloob commented Jul 26, 2017

The code should not call self.update() in the constructor of the class. Instead, pass True as second argument to add_devices callback.

@pvizeli pvizeli merged commit d7e8616 into home-assistant:dev Aug 10, 2017
@mar-schmidt mar-schmidt deleted the sensor.command_line-improv branch August 10, 2017 18:52
@fabaff fabaff mentioned this pull request Aug 12, 2017
@mad-ady
Copy link

mad-ady commented Aug 13, 2017

@mar-schmidt: You should also send a PR to add a usage example in the docs!

@mar-schmidt
Copy link
Contributor Author

@mad-ady: here you go home-assistant/home-assistant.io#3157

dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
…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
@home-assistant home-assistant locked and limited conversation to collaborators Dec 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants