diff --git a/homeassistant/components/pvoutput/sensor.py b/homeassistant/components/pvoutput/sensor.py index 188c49ca6ee..0bc2a6f6ca8 100644 --- a/homeassistant/components/pvoutput/sensor.py +++ b/homeassistant/components/pvoutput/sensor.py @@ -5,7 +5,7 @@ import logging import voluptuous as vol -from homeassistant.components.rest.sensor import RestData +from homeassistant.components.rest.data import RestData from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( ATTR_DATE, diff --git a/homeassistant/components/rest/binary_sensor.py b/homeassistant/components/rest/binary_sensor.py index 5d185889e24..c25750e2a2e 100644 --- a/homeassistant/components/rest/binary_sensor.py +++ b/homeassistant/components/rest/binary_sensor.py @@ -30,13 +30,12 @@ import homeassistant.helpers.config_validation as cv from homeassistant.helpers.reload import async_setup_reload_service from . import DOMAIN, PLATFORMS -from .sensor import RestData +from .data import DEFAULT_TIMEOUT, RestData DEFAULT_METHOD = "GET" DEFAULT_NAME = "REST Binary Sensor" DEFAULT_VERIFY_SSL = True DEFAULT_FORCE_UPDATE = False -DEFAULT_TIMEOUT = 10 PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { diff --git a/homeassistant/components/rest/data.py b/homeassistant/components/rest/data.py new file mode 100644 index 00000000000..9d9e802c2a0 --- /dev/null +++ b/homeassistant/components/rest/data.py @@ -0,0 +1,65 @@ +"""Support for RESTful API.""" +import logging + +import httpx + +DEFAULT_TIMEOUT = 10 + +_LOGGER = logging.getLogger(__name__) + + +class RestData: + """Class for handling the data retrieval.""" + + def __init__( + self, + method, + resource, + auth, + headers, + data, + verify_ssl, + timeout=DEFAULT_TIMEOUT, + ): + """Initialize the data object.""" + self._method = method + self._resource = resource + self._auth = auth + self._headers = headers + self._request_data = data + self._timeout = timeout + self._verify_ssl = verify_ssl + self._async_client = None + self.data = None + self.headers = None + + async def async_remove(self): + """Destroy the http session on destroy.""" + if self._async_client: + await self._async_client.aclose() + + def set_url(self, url): + """Set url.""" + self._resource = url + + async def async_update(self): + """Get the latest data from REST service with provided method.""" + if not self._async_client: + self._async_client = httpx.AsyncClient(verify=self._verify_ssl) + + _LOGGER.debug("Updating from %s", self._resource) + try: + response = await self._async_client.request( + self._method, + self._resource, + headers=self._headers, + auth=self._auth, + data=self._request_data, + timeout=self._timeout, + ) + self.data = response.text + self.headers = response.headers + except httpx.RequestError as ex: + _LOGGER.error("Error fetching data: %s failed with %s", self._resource, ex) + self.data = None + self.headers = None diff --git a/homeassistant/components/rest/sensor.py b/homeassistant/components/rest/sensor.py index 0a7fa6ef90f..e9783e3fea3 100644 --- a/homeassistant/components/rest/sensor.py +++ b/homeassistant/components/rest/sensor.py @@ -34,6 +34,7 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.reload import async_setup_reload_service from . import DOMAIN, PLATFORMS +from .data import DEFAULT_TIMEOUT, RestData _LOGGER = logging.getLogger(__name__) @@ -41,7 +42,6 @@ DEFAULT_METHOD = "GET" DEFAULT_NAME = "REST Sensor" DEFAULT_VERIFY_SSL = True DEFAULT_FORCE_UPDATE = False -DEFAULT_TIMEOUT = 10 CONF_JSON_ATTRS = "json_attributes" @@ -269,60 +269,3 @@ class RestSensor(Entity): def device_state_attributes(self): """Return the state attributes.""" return self._attributes - - -class RestData: - """Class for handling the data retrieval.""" - - def __init__( - self, - method, - resource, - auth, - headers, - data, - verify_ssl, - timeout=DEFAULT_TIMEOUT, - ): - """Initialize the data object.""" - self._method = method - self._resource = resource - self._auth = auth - self._headers = headers - self._request_data = data - self._timeout = timeout - self._verify_ssl = verify_ssl - self._async_client = None - self.data = None - self.headers = None - - async def async_remove(self): - """Destroy the http session on destroy.""" - if self._async_client: - await self._async_client.aclose() - - def set_url(self, url): - """Set url.""" - self._resource = url - - async def async_update(self): - """Get the latest data from REST service with provided method.""" - if not self._async_client: - self._async_client = httpx.AsyncClient(verify=self._verify_ssl) - - _LOGGER.debug("Updating from %s", self._resource) - try: - response = await self._async_client.request( - self._method, - self._resource, - headers=self._headers, - auth=self._auth, - data=self._request_data, - timeout=self._timeout, - ) - self.data = response.text - self.headers = response.headers - except httpx.RequestError as ex: - _LOGGER.error("Error fetching data: %s failed with %s", self._resource, ex) - self.data = None - self.headers = None diff --git a/homeassistant/components/scrape/sensor.py b/homeassistant/components/scrape/sensor.py index 1979f6f744d..1b55d75eded 100644 --- a/homeassistant/components/scrape/sensor.py +++ b/homeassistant/components/scrape/sensor.py @@ -5,7 +5,7 @@ from bs4 import BeautifulSoup from requests.auth import HTTPBasicAuth, HTTPDigestAuth import voluptuous as vol -from homeassistant.components.rest.sensor import RestData +from homeassistant.components.rest.data import RestData from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.const import ( CONF_AUTHENTICATION,