2017-08-08 04:52:27 +00:00
|
|
|
"""The tests for the REST sensor platform."""
|
2016-10-31 04:51:03 +00:00
|
|
|
import unittest
|
2018-09-21 13:54:50 +00:00
|
|
|
from pytest import raises
|
2016-10-31 04:51:03 +00:00
|
|
|
from unittest.mock import patch, Mock
|
|
|
|
|
|
|
|
import requests
|
|
|
|
from requests.exceptions import Timeout, MissingSchema, RequestException
|
|
|
|
import requests_mock
|
|
|
|
|
2018-09-21 13:54:50 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2017-03-05 09:41:54 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2017-01-23 15:17:29 +00:00
|
|
|
import homeassistant.components.sensor as sensor
|
2019-03-19 06:07:39 +00:00
|
|
|
import homeassistant.components.rest.sensor as rest
|
2016-10-31 04:51:03 +00:00
|
|
|
from homeassistant.helpers.config_validation import template
|
2017-01-23 15:17:29 +00:00
|
|
|
|
2016-10-31 04:51:03 +00:00
|
|
|
from tests.common import get_test_home_assistant, assert_setup_component
|
2018-10-24 10:10:05 +00:00
|
|
|
import pytest
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
|
2017-08-08 04:52:27 +00:00
|
|
|
class TestRestSensorSetup(unittest.TestCase):
|
|
|
|
"""Tests for setting up the REST sensor platform."""
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-31 04:51:03 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def test_setup_missing_config(self):
|
|
|
|
"""Test setup with configuration missing required entries."""
|
2017-01-23 15:17:29 +00:00
|
|
|
with assert_setup_component(0):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(
|
|
|
|
self.hass, sensor.DOMAIN, {"sensor": {"platform": "rest"}}
|
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
def test_setup_missing_schema(self):
|
|
|
|
"""Test setup with resource missing schema."""
|
2018-10-24 10:10:05 +00:00
|
|
|
with pytest.raises(MissingSchema):
|
2019-07-31 19:25:30 +00:00
|
|
|
rest.setup_platform(
|
|
|
|
self.hass,
|
|
|
|
{"platform": "rest", "resource": "localhost", "method": "GET"},
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
|
|
|
|
@patch("requests.Session.send", side_effect=requests.exceptions.ConnectionError())
|
2016-10-31 04:51:03 +00:00
|
|
|
def test_setup_failed_connect(self, mock_req):
|
|
|
|
"""Test setup when connection error occurs."""
|
2018-09-21 13:54:50 +00:00
|
|
|
with raises(PlatformNotReady):
|
2019-07-31 19:25:30 +00:00
|
|
|
rest.setup_platform(
|
|
|
|
self.hass,
|
|
|
|
{"platform": "rest", "resource": "http://localhost"},
|
|
|
|
lambda devices, update=True: None,
|
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("requests.Session.send", side_effect=Timeout())
|
2016-10-31 04:51:03 +00:00
|
|
|
def test_setup_timeout(self, mock_req):
|
|
|
|
"""Test setup when connection timeout occurs."""
|
2018-09-21 13:54:50 +00:00
|
|
|
with raises(PlatformNotReady):
|
2019-07-31 19:25:30 +00:00
|
|
|
rest.setup_platform(
|
|
|
|
self.hass,
|
|
|
|
{"platform": "rest", "resource": "http://localhost"},
|
|
|
|
lambda devices, update=True: None,
|
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
@requests_mock.Mocker()
|
|
|
|
def test_setup_minimum(self, mock_req):
|
|
|
|
"""Test setup with minimum configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_req.get("http://localhost", status_code=200)
|
|
|
|
with assert_setup_component(1, "sensor"):
|
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"sensor",
|
|
|
|
{"sensor": {"platform": "rest", "resource": "http://localhost"}},
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == mock_req.call_count
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
@requests_mock.Mocker()
|
|
|
|
def test_setup_get(self, mock_req):
|
|
|
|
"""Test setup with valid configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_req.get("http://localhost", status_code=200)
|
|
|
|
with assert_setup_component(1, "sensor"):
|
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"sensor",
|
|
|
|
{
|
|
|
|
"sensor": {
|
|
|
|
"platform": "rest",
|
|
|
|
"resource": "http://localhost",
|
|
|
|
"method": "GET",
|
|
|
|
"value_template": "{{ value_json.key }}",
|
|
|
|
"name": "foo",
|
|
|
|
"unit_of_measurement": "MB",
|
|
|
|
"verify_ssl": "true",
|
|
|
|
"timeout": 30,
|
|
|
|
"authentication": "basic",
|
|
|
|
"username": "my username",
|
|
|
|
"password": "my password",
|
|
|
|
"headers": {"Accept": "application/json"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == mock_req.call_count
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
@requests_mock.Mocker()
|
|
|
|
def test_setup_post(self, mock_req):
|
|
|
|
"""Test setup with valid configuration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_req.post("http://localhost", status_code=200)
|
|
|
|
with assert_setup_component(1, "sensor"):
|
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
"sensor",
|
|
|
|
{
|
|
|
|
"sensor": {
|
|
|
|
"platform": "rest",
|
|
|
|
"resource": "http://localhost",
|
|
|
|
"method": "POST",
|
|
|
|
"value_template": "{{ value_json.key }}",
|
|
|
|
"payload": '{ "device": "toaster"}',
|
|
|
|
"name": "foo",
|
|
|
|
"unit_of_measurement": "MB",
|
|
|
|
"verify_ssl": "true",
|
|
|
|
"timeout": 30,
|
|
|
|
"authentication": "basic",
|
|
|
|
"username": "my username",
|
|
|
|
"password": "my password",
|
|
|
|
"headers": {"Accept": "application/json"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-10-24 10:10:05 +00:00
|
|
|
assert 2 == mock_req.call_count
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestRestSensor(unittest.TestCase):
|
|
|
|
"""Tests for REST sensor platform."""
|
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-31 04:51:03 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2019-07-31 19:25:30 +00:00
|
|
|
self.initial_state = "initial_state"
|
|
|
|
self.rest = Mock("rest.RestData")
|
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update",
|
|
|
|
side_effect=self.update_side_effect(
|
|
|
|
'{ "key": "' + self.initial_state + '" }'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.name = "foo"
|
|
|
|
self.unit_of_measurement = "MB"
|
2019-01-16 18:03:53 +00:00
|
|
|
self.device_class = None
|
2019-07-31 19:25:30 +00:00
|
|
|
self.value_template = template("{{ value_json.key }}")
|
2016-10-31 04:51:03 +00:00
|
|
|
self.value_template.hass = self.hass
|
2017-12-09 07:18:45 +00:00
|
|
|
self.force_update = False
|
2016-10-31 04:51:03 +00:00
|
|
|
|
2017-12-09 07:18:45 +00:00
|
|
|
self.sensor = rest.RestSensor(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
self.rest,
|
|
|
|
self.name,
|
|
|
|
self.unit_of_measurement,
|
|
|
|
self.device_class,
|
|
|
|
self.value_template,
|
|
|
|
[],
|
|
|
|
self.force_update,
|
2017-12-09 07:18:45 +00:00
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
def update_side_effect(self, data):
|
|
|
|
"""Side effect function for mocking RestData.update()."""
|
|
|
|
self.rest.data = data
|
|
|
|
|
|
|
|
def test_name(self):
|
|
|
|
"""Test the name."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.name == self.sensor.name
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
def test_unit_of_measurement(self):
|
|
|
|
"""Test the unit of measurement."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.unit_of_measurement == self.sensor.unit_of_measurement
|
2016-10-31 04:51:03 +00:00
|
|
|
|
2017-12-09 07:18:45 +00:00
|
|
|
def test_force_update(self):
|
|
|
|
"""Test the unit of measurement."""
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.force_update == self.sensor.force_update
|
2017-12-09 07:18:45 +00:00
|
|
|
|
2016-10-31 04:51:03 +00:00
|
|
|
def test_state(self):
|
|
|
|
"""Test the initial state."""
|
2017-08-08 04:52:27 +00:00
|
|
|
self.sensor.update()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.initial_state == self.sensor.state
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
def test_update_when_value_is_none(self):
|
|
|
|
"""Test state gets updated to unknown when sensor returns no data."""
|
2017-08-08 04:52:27 +00:00
|
|
|
self.rest.update = Mock(
|
2019-07-31 19:25:30 +00:00
|
|
|
"rest.RestData.update", side_effect=self.update_side_effect(None)
|
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
self.sensor.update()
|
2019-01-24 07:20:20 +00:00
|
|
|
assert self.sensor.state is None
|
2018-10-24 10:10:05 +00:00
|
|
|
assert not self.sensor.available
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
def test_update_when_value_changed(self):
|
|
|
|
"""Test state gets updated when sensor returns a new status."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update",
|
|
|
|
side_effect=self.update_side_effect('{ "key": "updated_state" }'),
|
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
self.sensor.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "updated_state" == self.sensor.state
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.sensor.available
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
def test_update_with_no_template(self):
|
|
|
|
"""Test update when there is no value template."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update", side_effect=self.update_side_effect("plain_state")
|
|
|
|
)
|
|
|
|
self.sensor = rest.RestSensor(
|
|
|
|
self.hass,
|
|
|
|
self.rest,
|
|
|
|
self.name,
|
|
|
|
self.unit_of_measurement,
|
|
|
|
self.device_class,
|
|
|
|
None,
|
|
|
|
[],
|
|
|
|
self.force_update,
|
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
self.sensor.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "plain_state" == self.sensor.state
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.sensor.available
|
2016-10-31 04:51:03 +00:00
|
|
|
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
def test_update_with_json_attrs(self):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update",
|
|
|
|
side_effect=self.update_side_effect('{ "key": "some_json_value" }'),
|
|
|
|
)
|
|
|
|
self.sensor = rest.RestSensor(
|
|
|
|
self.hass,
|
|
|
|
self.rest,
|
|
|
|
self.name,
|
|
|
|
self.unit_of_measurement,
|
|
|
|
self.device_class,
|
|
|
|
None,
|
|
|
|
["key"],
|
|
|
|
self.force_update,
|
|
|
|
)
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
self.sensor.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "some_json_value" == self.sensor.device_state_attributes["key"]
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("homeassistant.components.rest.sensor._LOGGER")
|
2018-03-05 23:30:28 +00:00
|
|
|
def test_update_with_json_attrs_no_data(self, mock_logger):
|
|
|
|
"""Test attributes when no JSON result fetched."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update", side_effect=self.update_side_effect(None)
|
|
|
|
)
|
|
|
|
self.sensor = rest.RestSensor(
|
|
|
|
self.hass,
|
|
|
|
self.rest,
|
|
|
|
self.name,
|
|
|
|
self.unit_of_measurement,
|
|
|
|
self.device_class,
|
|
|
|
None,
|
|
|
|
["key"],
|
|
|
|
self.force_update,
|
|
|
|
)
|
2018-03-05 23:30:28 +00:00
|
|
|
self.sensor.update()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert {} == self.sensor.device_state_attributes
|
|
|
|
assert mock_logger.warning.called
|
2018-03-05 23:30:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("homeassistant.components.rest.sensor._LOGGER")
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
def test_update_with_json_attrs_not_dict(self, mock_logger):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update",
|
|
|
|
side_effect=self.update_side_effect('["list", "of", "things"]'),
|
|
|
|
)
|
|
|
|
self.sensor = rest.RestSensor(
|
|
|
|
self.hass,
|
|
|
|
self.rest,
|
|
|
|
self.name,
|
|
|
|
self.unit_of_measurement,
|
|
|
|
self.device_class,
|
|
|
|
None,
|
|
|
|
["key"],
|
|
|
|
self.force_update,
|
|
|
|
)
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
self.sensor.update()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert {} == self.sensor.device_state_attributes
|
|
|
|
assert mock_logger.warning.called
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("homeassistant.components.rest.sensor._LOGGER")
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update",
|
|
|
|
side_effect=self.update_side_effect("This is text rather than JSON data."),
|
|
|
|
)
|
|
|
|
self.sensor = rest.RestSensor(
|
|
|
|
self.hass,
|
|
|
|
self.rest,
|
|
|
|
self.name,
|
|
|
|
self.unit_of_measurement,
|
|
|
|
self.device_class,
|
|
|
|
None,
|
|
|
|
["key"],
|
|
|
|
self.force_update,
|
|
|
|
)
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
self.sensor.update()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert {} == self.sensor.device_state_attributes
|
|
|
|
assert mock_logger.warning.called
|
|
|
|
assert mock_logger.debug.called
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
|
|
|
|
def test_update_with_json_attrs_and_template(self):
|
|
|
|
"""Test attributes get extracted from a JSON result."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.rest.update = Mock(
|
|
|
|
"rest.RestData.update",
|
|
|
|
side_effect=self.update_side_effect(
|
|
|
|
'{ "key": "json_state_updated_value" }'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
self.sensor = rest.RestSensor(
|
|
|
|
self.hass,
|
|
|
|
self.rest,
|
|
|
|
self.name,
|
|
|
|
self.unit_of_measurement,
|
|
|
|
self.device_class,
|
|
|
|
self.value_template,
|
|
|
|
["key"],
|
|
|
|
self.force_update,
|
|
|
|
)
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
self.sensor.update()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "json_state_updated_value" == self.sensor.state
|
|
|
|
assert (
|
|
|
|
"json_state_updated_value" == self.sensor.device_state_attributes["key"]
|
|
|
|
), self.force_update
|
Unpacking RESTful sensor JSON results into attributes. (#10753)
* 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.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* sensor.envirophat: add missing requirement (#7451)
Adding requirements that is not explicitly pulled in by the library
that manages the Enviro pHAT.
* PyPI Openzwave (#7415)
* Remove default zwave config path
PYOZW now has much more comprehensive default handling for the config
path (in src-lib/libopenzwave/libopenzwave.pyx:getConfig()). It looks in
the same place we were looking, plus _many_ more. It will certainly do a
much better job of finding the config files than we will (and will be
updated as the library is changed, so we don't end up chasing it). The
getConfig() method has been there for a while, but was subsntially
improved recently.
This change simply leaves the config_path as None if it is not
specified, which will trigger the default handling in PYOZW.
* Install python-openzwave from PyPI
As of version 0.4, python-openzwave supports installation from PyPI,
which means we can use our 'normal' dependency management tooling to
install it. Yay.
This uses the default 'embed' build (which goes and downloads
statically sources to avoid having to compile anything locally). Check
out the python-openzwave readme for more details.
* Add python-openzwave deps to .travis.yml
Python OpenZwave require the libudev headers to build. This adds the
libudev-dev package to Travis runs via the 'apt' addon for Travis.
Thanks to @MartinHjelmare for this fix.
* Update docker build for PyPI openzwave
Now that PYOZW can be install from PyPI, the docker image build process
can be simplified to remove the explicit compilation of PYOZW.
* Add datadog component (#7158)
* Add datadog component
* Improve test_invalid_config datadog test
* Use assert_setup_component for test setup
* Fix object type for default KNX port
#7429 describes a TypeError that is raised if the port is omitted in the config for the KNX component (integer is required (got type str)). This commit changes the default port from a string to an integer. I expect this will resolve that issue...
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* 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.
* Added requirement that RESTful JSON results used as attributes must be
objects, not lists.
* Expanded test coverage to test REFTful JSON attributes with and
without a value template.
* Fixed breaks cause by manual upstream merge.
* Added one extra blank line to make PyLint happy.
* Switched json_attributes to be a list of keys rather than a boolean.
The value of json_attributes can now be either a comma sepaated list
of key names or a YAML list of key names. Only matching keys in a
retuned JSON dictionary will be mapped to sensor attributes.
Updated test cases to handle json_attributes being a list.
Also fixed two minor issues arrising from manual merge with 0.58 master.
* Added an explicit default value to the json_attributes config entry.
* Removed self.update() from __init__() body.
* Expended unit tests for error cases of json_attributes processing.
* Align quotes
2017-12-03 15:30:25 +00:00
|
|
|
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
class TestRestData(unittest.TestCase):
|
|
|
|
"""Tests for RestData."""
|
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-10-31 04:51:03 +00:00
|
|
|
self.method = "GET"
|
|
|
|
self.resource = "http://localhost"
|
|
|
|
self.verify_ssl = True
|
2019-02-26 18:23:46 +00:00
|
|
|
self.timeout = 10
|
2017-08-08 04:52:27 +00:00
|
|
|
self.rest = rest.RestData(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.method, self.resource, None, None, None, self.verify_ssl, self.timeout
|
|
|
|
)
|
2016-10-31 04:51:03 +00:00
|
|
|
|
|
|
|
@requests_mock.Mocker()
|
|
|
|
def test_update(self, mock_req):
|
|
|
|
"""Test update."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_req.get("http://localhost", text="test data")
|
2016-10-31 04:51:03 +00:00
|
|
|
self.rest.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert "test data" == self.rest.data
|
2016-10-31 04:51:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("requests.Session", side_effect=RequestException)
|
2016-10-31 04:51:03 +00:00
|
|
|
def test_update_request_exception(self, mock_req):
|
|
|
|
"""Test update when a request exception occurs."""
|
|
|
|
self.rest.update()
|
2018-10-24 10:10:05 +00:00
|
|
|
assert self.rest.data is None
|