1
mirror of https://github.com/home-assistant/core synced 2024-10-04 07:58:43 +02:00

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,6 +118,22 @@ class ScrapeSensor(Entity):
"""Return the state of the device."""
return self._state
def _extract_value(self):
"""Parse the html extraction in the executor."""
raw_data = BeautifulSoup(self.rest.data, "html.parser")
_LOGGER.debug(raw_data)
if self._attr is not None:
value = raw_data.select(self._select)[self._index][self._attr]
else:
tag = raw_data.select(self._select)[self._index]
if tag.name in ("style", "script", "template"):
value = tag.string
else:
value = tag.text
_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()
@ -125,19 +141,8 @@ class ScrapeSensor(Entity):
_LOGGER.error("Unable to retrieve data for %s", self.name)
return
raw_data = BeautifulSoup(self.rest.data, "html.parser")
_LOGGER.debug(raw_data)
try:
if self._attr is not None:
value = raw_data.select(self._select)[self._index][self._attr]
else:
tag = raw_data.select(self._select)[self._index]
if tag.name in ("style", "script", "template"):
value = tag.string
else:
value = tag.text
_LOGGER.debug(value)
value = await self.hass.async_add_executor_job(self._extract_value)
except IndexError:
_LOGGER.error("Unable to extract data from HTML for %s", self.name)
return