Fix digest auth rest sensors (#28153)
* Fix digest auth rest sensors * Refactor to use request() * Fix black complaints * Don't rename data variable Don't rename the data variable, appears several other sensors have been coded to rely on it * Fix tests test_setup_missing_schema: Change the exception to check for to PlatformNotReady. With the change away from prepared statements, we no longer get the MissingSchema error during setup - we get it when we attempt to call the endpoint. Since the result is PlatformNotReady, which is what we want, and the error log clearly contains a note that the schema is incorrect I think this is fine. test_setup_failed_connect & test_setup_timeout: These aren't checking for minimum config, and they're not invoking the correct method to have the default config filled in. Therefore I've just added the correct minimum config so these can continue to test what they're there to test. test_update_request_exception: Testing a request exception with the assert on line 404! Excellent. Anyway, we've moved from using the requests Session object to the requests.request function - so the patch needed to be altered to ensure the RequestException was raised. * Remove no longer needed import * Fix binary sensor test in same waypull/29100/head
parent
b0925e60d7
commit
6c6a5c50a5
|
@ -227,32 +227,33 @@ class RestData:
|
||||||
self, method, resource, auth, headers, data, verify_ssl, timeout=DEFAULT_TIMEOUT
|
self, method, resource, auth, headers, data, verify_ssl, timeout=DEFAULT_TIMEOUT
|
||||||
):
|
):
|
||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self._request = requests.Request(
|
self._method = method
|
||||||
method, resource, headers=headers, auth=auth, data=data
|
self._resource = resource
|
||||||
).prepare()
|
self._auth = auth
|
||||||
|
self._headers = headers
|
||||||
|
self._request_data = data
|
||||||
self._verify_ssl = verify_ssl
|
self._verify_ssl = verify_ssl
|
||||||
self._timeout = timeout
|
self._timeout = timeout
|
||||||
self.data = None
|
self.data = None
|
||||||
|
|
||||||
def set_url(self, url):
|
def set_url(self, url):
|
||||||
"""Set url."""
|
"""Set url."""
|
||||||
self._request.prepare_url(url, None)
|
self._resource = url
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from REST service with provided method."""
|
"""Get the latest data from REST service with provided method."""
|
||||||
_LOGGER.debug("Updating from %s", self._request.url)
|
_LOGGER.debug("Updating from %s", self._resource)
|
||||||
try:
|
try:
|
||||||
with requests.Session() as sess:
|
response = requests.request(
|
||||||
response = sess.send(
|
self._method,
|
||||||
self._request, timeout=self._timeout, verify=self._verify_ssl
|
self._resource,
|
||||||
)
|
headers=self._headers,
|
||||||
|
auth=self._auth,
|
||||||
|
data=self._request_data,
|
||||||
|
timeout=self._timeout,
|
||||||
|
verify=self._verify_ssl,
|
||||||
|
)
|
||||||
self.data = response.text
|
self.data = response.text
|
||||||
except requests.exceptions.RequestException as ex:
|
except requests.exceptions.RequestException as ex:
|
||||||
_LOGGER.error(
|
_LOGGER.error("Error fetching data: %s failed with %s", self._resource, ex)
|
||||||
"Error fetching data: %s from %s failed with %s",
|
|
||||||
self._request,
|
|
||||||
self._request.url,
|
|
||||||
ex,
|
|
||||||
)
|
|
||||||
self.data = None
|
self.data = None
|
||||||
|
|
|
@ -4,7 +4,7 @@ from pytest import raises
|
||||||
from unittest.mock import patch, Mock
|
from unittest.mock import patch, Mock
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from requests.exceptions import Timeout, MissingSchema
|
from requests.exceptions import Timeout
|
||||||
import requests_mock
|
import requests_mock
|
||||||
|
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
|
@ -47,7 +47,7 @@ class TestRestBinarySensorSetup(unittest.TestCase):
|
||||||
|
|
||||||
def test_setup_missing_schema(self):
|
def test_setup_missing_schema(self):
|
||||||
"""Test setup with resource missing schema."""
|
"""Test setup with resource missing schema."""
|
||||||
with pytest.raises(MissingSchema):
|
with pytest.raises(PlatformNotReady):
|
||||||
rest.setup_platform(
|
rest.setup_platform(
|
||||||
self.hass,
|
self.hass,
|
||||||
{"platform": "rest", "resource": "localhost", "method": "GET"},
|
{"platform": "rest", "resource": "localhost", "method": "GET"},
|
||||||
|
@ -60,7 +60,7 @@ class TestRestBinarySensorSetup(unittest.TestCase):
|
||||||
with raises(PlatformNotReady):
|
with raises(PlatformNotReady):
|
||||||
rest.setup_platform(
|
rest.setup_platform(
|
||||||
self.hass,
|
self.hass,
|
||||||
{"platform": "rest", "resource": "http://localhost"},
|
{"platform": "rest", "resource": "http://localhost", "method": "GET"},
|
||||||
self.add_devices,
|
self.add_devices,
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
@ -72,7 +72,7 @@ class TestRestBinarySensorSetup(unittest.TestCase):
|
||||||
with raises(PlatformNotReady):
|
with raises(PlatformNotReady):
|
||||||
rest.setup_platform(
|
rest.setup_platform(
|
||||||
self.hass,
|
self.hass,
|
||||||
{"platform": "rest", "resource": "http://localhost"},
|
{"platform": "rest", "resource": "http://localhost", "method": "GET"},
|
||||||
self.add_devices,
|
self.add_devices,
|
||||||
None,
|
None,
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,7 +4,7 @@ from pytest import raises
|
||||||
from unittest.mock import patch, Mock
|
from unittest.mock import patch, Mock
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from requests.exceptions import Timeout, MissingSchema, RequestException
|
from requests.exceptions import Timeout, RequestException
|
||||||
import requests_mock
|
import requests_mock
|
||||||
|
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
|
@ -37,7 +37,7 @@ class TestRestSensorSetup(unittest.TestCase):
|
||||||
|
|
||||||
def test_setup_missing_schema(self):
|
def test_setup_missing_schema(self):
|
||||||
"""Test setup with resource missing schema."""
|
"""Test setup with resource missing schema."""
|
||||||
with pytest.raises(MissingSchema):
|
with pytest.raises(PlatformNotReady):
|
||||||
rest.setup_platform(
|
rest.setup_platform(
|
||||||
self.hass,
|
self.hass,
|
||||||
{"platform": "rest", "resource": "localhost", "method": "GET"},
|
{"platform": "rest", "resource": "localhost", "method": "GET"},
|
||||||
|
@ -50,7 +50,7 @@ class TestRestSensorSetup(unittest.TestCase):
|
||||||
with raises(PlatformNotReady):
|
with raises(PlatformNotReady):
|
||||||
rest.setup_platform(
|
rest.setup_platform(
|
||||||
self.hass,
|
self.hass,
|
||||||
{"platform": "rest", "resource": "http://localhost"},
|
{"platform": "rest", "resource": "http://localhost", "method": "GET"},
|
||||||
lambda devices, update=True: None,
|
lambda devices, update=True: None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ class TestRestSensorSetup(unittest.TestCase):
|
||||||
with raises(PlatformNotReady):
|
with raises(PlatformNotReady):
|
||||||
rest.setup_platform(
|
rest.setup_platform(
|
||||||
self.hass,
|
self.hass,
|
||||||
{"platform": "rest", "resource": "http://localhost"},
|
{"platform": "rest", "resource": "http://localhost", "method": "GET"},
|
||||||
lambda devices, update=True: None,
|
lambda devices, update=True: None,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -397,7 +397,7 @@ class TestRestData(unittest.TestCase):
|
||||||
self.rest.update()
|
self.rest.update()
|
||||||
assert "test data" == self.rest.data
|
assert "test data" == self.rest.data
|
||||||
|
|
||||||
@patch("requests.Session", side_effect=RequestException)
|
@patch("requests.request", side_effect=RequestException)
|
||||||
def test_update_request_exception(self, mock_req):
|
def test_update_request_exception(self, mock_req):
|
||||||
"""Test update when a request exception occurs."""
|
"""Test update when a request exception occurs."""
|
||||||
self.rest.update()
|
self.rest.update()
|
||||||
|
|
Loading…
Reference in New Issue