Create httpx helper to wrap a shared httpx.AsyncClient (#43877)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
J. Nick Koston 2020-12-09 10:18:57 -06:00 committed by GitHub
parent 12903f9c8c
commit dd0afc3b66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 284 additions and 30 deletions

View file

@ -78,7 +78,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
auth = HTTPBasicAuth(username, password)
else:
auth = None
rest = RestData(method, resource, auth, headers, None, payload, verify_ssl)
rest = RestData(hass, method, resource, auth, headers, None, payload, verify_ssl)
await rest.async_update()
if rest.data is None:
@ -137,6 +137,14 @@ class ScrapeSensor(Entity):
async def async_update(self):
"""Get the latest data from the source and updates the state."""
await self.rest.async_update()
await self._async_update_from_rest_data()
async def async_added_to_hass(self):
"""Ensure the data from the initial update is reflected in the state."""
await self._async_update_from_rest_data()
async def _async_update_from_rest_data(self):
"""Update state from the rest data."""
if self.rest.data is None:
_LOGGER.error("Unable to retrieve data for %s", self.name)
return
@ -153,7 +161,3 @@ class ScrapeSensor(Entity):
)
else:
self._state = value
async def async_will_remove_from_hass(self):
"""Shutdown the session."""
await self.rest.async_remove()