Parse html in the executor for scrape sensors (#41987)

This commit is contained in:
J. Nick Koston 2020-10-17 11:57:59 -05:00 committed by GitHub
parent c6c617ed31
commit 183f94364a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -118,17 +118,11 @@ class ScrapeSensor(Entity):
"""Return the state of the device.""" """Return the state of the device."""
return self._state return self._state
async def async_update(self): def _extract_value(self):
"""Get the latest data from the source and updates the state.""" """Parse the html extraction in the executor."""
await self.rest.async_update()
if self.rest.data is None:
_LOGGER.error("Unable to retrieve data for %s", self.name)
return
raw_data = BeautifulSoup(self.rest.data, "html.parser") raw_data = BeautifulSoup(self.rest.data, "html.parser")
_LOGGER.debug(raw_data) _LOGGER.debug(raw_data)
try:
if self._attr is not None: if self._attr is not None:
value = raw_data.select(self._select)[self._index][self._attr] value = raw_data.select(self._select)[self._index][self._attr]
else: else:
@ -138,6 +132,17 @@ class ScrapeSensor(Entity):
else: else:
value = tag.text value = tag.text
_LOGGER.debug(value) _LOGGER.debug(value)
return value
async def async_update(self):
"""Get the latest data from the source and updates the state."""
await self.rest.async_update()
if self.rest.data is None:
_LOGGER.error("Unable to retrieve data for %s", self.name)
return
try:
value = await self.hass.async_add_executor_job(self._extract_value)
except IndexError: except IndexError:
_LOGGER.error("Unable to extract data from HTML for %s", self.name) _LOGGER.error("Unable to extract data from HTML for %s", self.name)
return return