-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Unpacking RESTful sensor JSON results into attributes. #10753
Merged
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
dbaa626
Added support for extracting JSON attributes from RESTful values
nickovs fb19f87
Added support for extracting JSON attributes from RESTful values
nickovs 0cb9def
Merge branch 'rest-json-attrs' of https://github.com/nickovs/home-ass…
nickovs e08f649
Added requirement that RESTful JSON results used as attributes must be
nickovs de07ae5
Expanded test coverage to test REFTful JSON attributes with and
nickovs 227a5ad
Added support for extracting JSON attributes from RESTful values
nickovs 769c7ff
Added requirement that RESTful JSON results used as attributes must be
nickovs 0f25f6d
Expanded test coverage to test REFTful JSON attributes with and
nickovs a22a6d4
Merge branch 'rest-json-attrs' of github.com:nickovs/home-assistant i…
nickovs 6bd57cc
sensor.envirophat: add missing requirement (#7451)
imrehg d635e26
PyPI Openzwave (#7415)
JshWright 44cc762
Add datadog component (#7158)
nunofgs 67e8f52
Fix object type for default KNX port
JshWright d23ccce
Added support for extracting JSON attributes from RESTful values
nickovs f5bbf6b
Added requirement that RESTful JSON results used as attributes must be
nickovs a281132
Expanded test coverage to test REFTful JSON attributes with and
nickovs dda455d
Added support for extracting JSON attributes from RESTful values
nickovs 3a99f4e
Added requirement that RESTful JSON results used as attributes must be
nickovs 22bc0fc
Expanded test coverage to test REFTful JSON attributes with and
nickovs d19a81b
Added support for extracting JSON attributes from RESTful values
nickovs 4ef54a1
Added requirement that RESTful JSON results used as attributes must be
nickovs 0b9711c
Expanded test coverage to test REFTful JSON attributes with and
nickovs 8c5548e
Merge branch 'rest-json-attrs' of github.com:nickovs/home-assistant i…
nickovs 0795823
Brought up to date with most recent upstream master
nickovs 19eab69
Fixed breaks cause by manual upstream merge.
nickovs 9dd2a5a
Added one extra blank line to make PyLint happy.
nickovs 2dbcff5
Switched json_attributes to be a list of keys rather than a boolean.
nickovs 1d36270
Added an explicit default value to the json_attributes config entry.
nickovs 02158b0
Removed self.update() from __init__() body.
nickovs a537b45
Merge branch 'rest-json-attrs' of github.com:nickovs/home-assistant i…
nickovs 3a9650c
Expended unit tests for error cases of json_attributes processing.
nickovs 0bf5ea3
Align quotes
fabaff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Added support for extracting JSON attributes from RESTful values
Setting the json_attributes configuration option to true on the RESTful sensor will cause the result of the REST request to be parsed as a JSON string and if successful the resulting dictionary will be used for the attributes of the sensor.
- Loading branch information
commit dbaa626880e9f79160c70afe256ef0847188504a
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
https://home-assistant.io/components/sensor.rest/ | ||
""" | ||
import logging | ||
import json | ||
|
||
import voluptuous as vol | ||
import requests | ||
|
@@ -25,6 +26,8 @@ | |
DEFAULT_NAME = 'REST Sensor' | ||
DEFAULT_VERIFY_SSL = True | ||
|
||
CONF_JSON_ATTRS = "json_attributes" | ||
|
||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
vol.Required(CONF_RESOURCE): cv.url, | ||
vol.Optional(CONF_AUTHENTICATION): | ||
|
@@ -38,6 +41,7 @@ | |
vol.Optional(CONF_USERNAME): cv.string, | ||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template, | ||
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, | ||
vol.Optional(CONF_JSON_ATTRS): cv.boolean, | ||
}) | ||
|
||
|
||
|
@@ -53,6 +57,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |
headers = config.get(CONF_HEADERS) | ||
unit = config.get(CONF_UNIT_OF_MEASUREMENT) | ||
value_template = config.get(CONF_VALUE_TEMPLATE) | ||
json_attrs = config.get(CONF_JSON_ATTRS) | ||
if value_template is not None: | ||
value_template.hass = hass | ||
|
||
|
@@ -70,20 +75,24 @@ def setup_platform(hass, config, add_devices, discovery_info=None): | |
_LOGGER.error("Unable to fetch REST data") | ||
return False | ||
|
||
add_devices([RestSensor(hass, rest, name, unit, value_template)]) | ||
add_devices([RestSensor(hass, rest, name, unit, | ||
value_template, json_attrs)]) | ||
|
||
|
||
class RestSensor(Entity): | ||
"""Implementation of a REST sensor.""" | ||
|
||
def __init__(self, hass, rest, name, unit_of_measurement, value_template): | ||
def __init__(self, hass, rest, name, | ||
unit_of_measurement, value_template, json_attrs): | ||
"""Initialize the REST sensor.""" | ||
self._hass = hass | ||
self.rest = rest | ||
self._name = name | ||
self._state = STATE_UNKNOWN | ||
self._unit_of_measurement = unit_of_measurement | ||
self._value_template = value_template | ||
self._json_attrs = json_attrs | ||
self._attributes = None | ||
self.update() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you say "this", presumably you mean the |
||
|
||
@property | ||
|
@@ -112,8 +121,21 @@ def update(self): | |
value = self._value_template.render_with_possible_json_value( | ||
value, STATE_UNKNOWN) | ||
|
||
if self._json_attrs: | ||
self._attributes = None | ||
try: | ||
self._attributes = json.loads(value) | ||
except ValueError: | ||
_LOGGER.warning('REST result could not be parsed as JSON') | ||
_LOGGER.debug('Erroneous JSON: %s', value) | ||
|
||
self._state = value | ||
|
||
@property | ||
def device_state_attributes(self): | ||
"""Return the state attributes.""" | ||
return self._attributes | ||
|
||
|
||
class RestData(object): | ||
"""Class for handling the data retrieval.""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Revert the True at the end
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.
Sorry about that. The flag was added after I forked the code and I missed that in the merge.