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 way
This commit is contained in:
parent
b0925e60d7
commit
6c6a5c50a5
3 changed files with 26 additions and 25 deletions
|
@ -227,32 +227,33 @@ class RestData:
|
|||
self, method, resource, auth, headers, data, verify_ssl, timeout=DEFAULT_TIMEOUT
|
||||
):
|
||||
"""Initialize the data object."""
|
||||
self._request = requests.Request(
|
||||
method, resource, headers=headers, auth=auth, data=data
|
||||
).prepare()
|
||||
self._method = method
|
||||
self._resource = resource
|
||||
self._auth = auth
|
||||
self._headers = headers
|
||||
self._request_data = data
|
||||
self._verify_ssl = verify_ssl
|
||||
self._timeout = timeout
|
||||
self.data = None
|
||||
|
||||
def set_url(self, url):
|
||||
"""Set url."""
|
||||
self._request.prepare_url(url, None)
|
||||
self._resource = url
|
||||
|
||||
def update(self):
|
||||
"""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:
|
||||
with requests.Session() as sess:
|
||||
response = sess.send(
|
||||
self._request, timeout=self._timeout, verify=self._verify_ssl
|
||||
)
|
||||
|
||||
response = requests.request(
|
||||
self._method,
|
||||
self._resource,
|
||||
headers=self._headers,
|
||||
auth=self._auth,
|
||||
data=self._request_data,
|
||||
timeout=self._timeout,
|
||||
verify=self._verify_ssl,
|
||||
)
|
||||
self.data = response.text
|
||||
except requests.exceptions.RequestException as ex:
|
||||
_LOGGER.error(
|
||||
"Error fetching data: %s from %s failed with %s",
|
||||
self._request,
|
||||
self._request.url,
|
||||
ex,
|
||||
)
|
||||
_LOGGER.error("Error fetching data: %s failed with %s", self._resource, ex)
|
||||
self.data = None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue