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

Catch ValueError #2296

Merged
merged 2 commits into from
Jun 18, 2016
Merged

Catch ValueError #2296

merged 2 commits into from
Jun 18, 2016

Conversation

fabaff
Copy link
Member

@fabaff fabaff commented Jun 14, 2016

Description:
The REST binary sensor was only able to handle 0 and 1 so far. This PR catches the ValueError if the return value is something else.

Related issue (if applicable): https://community.home-assistant.io/t/restful-binary-sensor-value-template/1779

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

Example entry for configuration.yaml (if applicable):

binary_sensor:
  - platform: rest
    resource: https://www.hamburg.ccc.de/dooris/status.json
    method: GET
    name: z9
    sensor_class: opening
    value_template: '{{ value_json.state.open }}'

Checklist:

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

If code communicates with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

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.

self._hass, self._value_template, self.rest.data, False)
return bool(int(self.rest.data))
else:
Copy link
Member

Choose a reason for hiding this comment

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

It should not be an else but always happen because it's a binary sensor and thus should be a boolean. Templates always return a string

Copy link
Member Author

Choose a reason for hiding this comment

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

Right

@balloob
Copy link
Member

balloob commented Jun 18, 2016

Looks good! 🐬

@balloob balloob merged commit b646acc into home-assistant:dev Jun 18, 2016
try:
return bool(int(response))
except ValueError:
return {"true": True, "on": True, "open": True,
Copy link
Member

Choose a reason for hiding this comment

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

You could have written this as:

return response.lower() in ('true', 'on', 'open', 'yes')

@fabaff fabaff deleted the binary-sensor-rest branch June 22, 2016 12:07
@home-assistant home-assistant locked and limited conversation to collaborators Mar 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants