Skip to content

Implement templates for covers#8100

Merged
balloob merged 8 commits into
home-assistant:devfrom
PhracturedBlue:cover_template
Jun 30, 2017
Merged

Implement templates for covers#8100
balloob merged 8 commits into
home-assistant:devfrom
PhracturedBlue:cover_template

Conversation

@PhracturedBlue
Copy link
Copy Markdown
Contributor

@PhracturedBlue PhracturedBlue commented Jun 19, 2017

Description:

Implement templates for cover elements

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#2842

Example entry for configuration.yaml (if applicable):

cover:
  - platform: template
    cover:
      garage_door:
        friendly_name: "Garage Door"
        value_template: "{% raw %}{{is_state('sensor.garage_door > 0'}}{% endraw %}"
        open_cover:
          service: script.open_garage_door
        close_cover:
          service: script.close_garage_door
        stop_cover:
          service: script.stop_garage_door

Checklist:

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

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.

Comment thread tests/components/cover/test_template.py Outdated
self.hass.start()
self.hass.block_till_done()

core.cover.set_cover_tilt_position(self.hass, 42, 'cover.test_template_cover')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (86 > 79 characters)

Comment thread tests/components/cover/test_template.py Outdated
state = self.hass.states.get('cover.test_template_cover')
assert state.state == STATE_OPEN

core.cover.set_cover_position(self.hass, 42, 'cover.test_template_cover')
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

Comment thread tests/components/cover/test_template.py Outdated
'covers': {
'test_template_cover': {
'position_template':
"{{ states.cover.test_state.attributes.position }}",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (84 > 79 characters)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not sure how to best fix this one without breaking the indentation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since it is a string, wondering if you can split it in two lines, like:

                                "{{states.cover.test_state."
                                "attributes.position}}",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can find some other approaches to handle this here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks.
I decided to shorten the state-variable name. I only needed 4 characters.. I think readability is better.


state = self.hass.states.get('cover.test_template_cover')
assert state.state == STATE_CLOSED
def test_template_state_boolean(self):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

expected 1 blank line, found 0

from homeassistant.const import STATE_ON, STATE_OFF, STATE_OPEN, STATE_CLOSED
from homeassistant.helpers.restore_state import DATA_RESTORE_CACHE

from tests.common import (
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'tests.common.mock_component' imported but unused

state = float(self._position_template.async_render())
if state < 0 or state > 100:
self._position = None
_LOGGER.error("Cover position value must be between 0 and 100. Value was: %.2f", state)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (108 > 79 characters)

try:
state = self._template.async_render().lower()
if state in _VALID_STATES:
self._position = 100 if state in ('true', STATE_OPEN) else 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

self._tilt_template.hass = self.hass
if self._icon_template is not None:
self._icon_template.hass = self.hass
@asyncio.coroutine
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

expected 1 blank line, found 0

if ((position_template is None and state_template is None) or
(position_template is not None and state_template is not None)):
_LOGGER.error('Must specify only one of: %s',
', '.join([CONF_VALUE_TEMPLATE, CONF_VALUE_TEMPLATE]))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

tilt_action = device_config.get(TILT_ACTION)

if ((position_template is None and state_template is None) or
(position_template is not None and state_template is not None)):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

state = float(self._position_template.async_render())
if state < 0 or state > 100:
self._position = None
_LOGGER.error("Cover position value must be between 0 and 100."
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (83 > 79 characters)

Comment thread tests/components/cover/test_template.py Outdated
self.hass.stop()

def test_template_state_text(self):
""""Test the state text of a template."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You have an extra double quote here.

Comment thread tests/components/cover/test_template.py Outdated
assert state.state == STATE_CLOSED

def test_template_state_boolean(self):
""""Test the state text of a template."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here

Comment thread tests/components/cover/test_template.py Outdated
assert state.state == STATE_OPEN

def test_template_position(self):
""""Test the state text of a template."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here.

Comment thread tests/components/cover/test_template.py Outdated
assert state.state == STATE_CLOSED

def test_template_tilt(self):
""""Test the state text of a template."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Again, 4 quotes

Comment thread tests/components/cover/test_template.py Outdated
assert state.attributes.get('current_tilt_position') == 42.0

def test_template_out_of_bounds(self):
""""Test the state text of a template."""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same

Comment thread tests/components/cover/test_template.py Outdated
assert self.hass.states.all() == []

def test_template_non_numeric(self):
""""Test the state text of a template."""
Copy link
Copy Markdown
Contributor

@arsaboo arsaboo Jun 20, 2017

Choose a reason for hiding this comment

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

Same. Just search for """" and replace by """ throughout the document

@arsaboo
Copy link
Copy Markdown
Contributor

arsaboo commented Jun 22, 2017

You may have to push another commit to fix the travis timeout error.

if ((position_template is None and state_template is None) or
(position_template is not None and
state_template is not None)):
_LOGGER.error('Must specify only one of: %s',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of testing like this, use the voluptuous Exclusive validator to make sure only 1 is passed in. Example: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/wink.py#L53-L56

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've changed this, however I still need the check because voluptuous doesn't seem to offer one-hot exclusivity.

def async_added_to_hass(self):
"""Register callbacks."""
state = yield from async_get_last_state(self.hass, self.entity_id)
if state:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a weird check. You want to check the value too. Now you just check that a state exists.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't understand the issue. I check the value in the next line:

        if state:
            self._position = 100 if state.state else 0

Do you prefer that I remove the ternary check?

Copy link
Copy Markdown
Member

@balloob balloob Jun 27, 2017

Choose a reason for hiding this comment

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

Sorry, added comment to the wrong line. But state.state is going to be either STATE_OPEN or STATE_CLOSED. Both are string values and thus will always be truthy, setting the position to 100.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for taking the time to explain the issue.

@balloob
Copy link
Copy Markdown
Member

balloob commented Jun 24, 2017

Great work! This is very close to be ready to be merged 💃

@balloob balloob merged commit 17237e9 into home-assistant:dev Jun 30, 2017
@balloob balloob mentioned this pull request Jul 1, 2017
dethpickle pushed a commit to dethpickle/home-assistant that referenced this pull request Aug 18, 2017
* Implement templates for covers

* Fix a few remaining pylint warnings

* Fix hound line-length warnings

* Fix one more hound line-length warning

* Fix quadruple-quotes an line length code-quality issues

* Irrelevant change to retrigger travis due to timeout

* Use volutuous Exclusive to check for mutex condition

* Fix incorrect state check
@home-assistant home-assistant locked and limited conversation to collaborators Oct 20, 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.

6 participants