Convert rest sensors to async using httpx (#41973)
This commit is contained in:
parent
39adf14079
commit
ad6ce5fa83
12 changed files with 1094 additions and 970 deletions
|
@ -53,7 +53,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the Web scrape sensor."""
|
||||
name = config.get(CONF_NAME)
|
||||
resource = config.get(CONF_RESOURCE)
|
||||
|
@ -79,12 +79,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
else:
|
||||
auth = None
|
||||
rest = RestData(method, resource, auth, headers, payload, verify_ssl)
|
||||
rest.update()
|
||||
await rest.async_update()
|
||||
|
||||
if rest.data is None:
|
||||
raise PlatformNotReady
|
||||
|
||||
add_entities(
|
||||
async_add_entities(
|
||||
[ScrapeSensor(rest, name, select, attr, index, value_template, unit)], True
|
||||
)
|
||||
|
||||
|
@ -118,9 +118,9 @@ class ScrapeSensor(Entity):
|
|||
"""Return the state of the device."""
|
||||
return self._state
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Get the latest data from the source and updates the state."""
|
||||
self.rest.update()
|
||||
await self.rest.async_update()
|
||||
if self.rest.data is None:
|
||||
_LOGGER.error("Unable to retrieve data for %s", self.name)
|
||||
return
|
||||
|
@ -143,8 +143,12 @@ class ScrapeSensor(Entity):
|
|||
return
|
||||
|
||||
if self._value_template is not None:
|
||||
self._state = self._value_template.render_with_possible_json_value(
|
||||
self._state = self._value_template.async_render_with_possible_json_value(
|
||||
value, None
|
||||
)
|
||||
else:
|
||||
self._state = value
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Shutdown the session."""
|
||||
await self.rest.async_remove()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue