diff --git a/homeassistant/components/rest/sensor.py b/homeassistant/components/rest/sensor.py index 41adb855903..b89e96a0f34 100644 --- a/homeassistant/components/rest/sensor.py +++ b/homeassistant/components/rest/sensor.py @@ -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 diff --git a/tests/components/rest/test_binary_sensor.py b/tests/components/rest/test_binary_sensor.py index 63a7d3ff273..8993be6a7a1 100644 --- a/tests/components/rest/test_binary_sensor.py +++ b/tests/components/rest/test_binary_sensor.py @@ -4,7 +4,7 @@ from pytest import raises from unittest.mock import patch, Mock import requests -from requests.exceptions import Timeout, MissingSchema +from requests.exceptions import Timeout import requests_mock from homeassistant.exceptions import PlatformNotReady @@ -47,7 +47,7 @@ class TestRestBinarySensorSetup(unittest.TestCase): def test_setup_missing_schema(self): """Test setup with resource missing schema.""" - with pytest.raises(MissingSchema): + with pytest.raises(PlatformNotReady): rest.setup_platform( self.hass, {"platform": "rest", "resource": "localhost", "method": "GET"}, @@ -60,7 +60,7 @@ class TestRestBinarySensorSetup(unittest.TestCase): with raises(PlatformNotReady): rest.setup_platform( self.hass, - {"platform": "rest", "resource": "http://localhost"}, + {"platform": "rest", "resource": "http://localhost", "method": "GET"}, self.add_devices, None, ) @@ -72,7 +72,7 @@ class TestRestBinarySensorSetup(unittest.TestCase): with raises(PlatformNotReady): rest.setup_platform( self.hass, - {"platform": "rest", "resource": "http://localhost"}, + {"platform": "rest", "resource": "http://localhost", "method": "GET"}, self.add_devices, None, ) diff --git a/tests/components/rest/test_sensor.py b/tests/components/rest/test_sensor.py index 50acb053347..c6cfce90a20 100644 --- a/tests/components/rest/test_sensor.py +++ b/tests/components/rest/test_sensor.py @@ -4,7 +4,7 @@ from pytest import raises from unittest.mock import patch, Mock import requests -from requests.exceptions import Timeout, MissingSchema, RequestException +from requests.exceptions import Timeout, RequestException import requests_mock from homeassistant.exceptions import PlatformNotReady @@ -37,7 +37,7 @@ class TestRestSensorSetup(unittest.TestCase): def test_setup_missing_schema(self): """Test setup with resource missing schema.""" - with pytest.raises(MissingSchema): + with pytest.raises(PlatformNotReady): rest.setup_platform( self.hass, {"platform": "rest", "resource": "localhost", "method": "GET"}, @@ -50,7 +50,7 @@ class TestRestSensorSetup(unittest.TestCase): with raises(PlatformNotReady): rest.setup_platform( self.hass, - {"platform": "rest", "resource": "http://localhost"}, + {"platform": "rest", "resource": "http://localhost", "method": "GET"}, lambda devices, update=True: None, ) @@ -60,7 +60,7 @@ class TestRestSensorSetup(unittest.TestCase): with raises(PlatformNotReady): rest.setup_platform( self.hass, - {"platform": "rest", "resource": "http://localhost"}, + {"platform": "rest", "resource": "http://localhost", "method": "GET"}, lambda devices, update=True: None, ) @@ -397,7 +397,7 @@ class TestRestData(unittest.TestCase): self.rest.update() 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): """Test update when a request exception occurs.""" self.rest.update()