1
mirror of https://github.com/home-assistant/core synced 2024-07-12 07:21:24 +02:00

Scrape HTML attributes that are not key/val pairs (#58247)

This commit is contained in:
MizterB 2022-01-21 05:17:48 -05:00 committed by GitHub
parent 5954ca2b1f
commit 604a900658
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -160,14 +160,23 @@ class ScrapeSensor(SensorEntity):
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
try:
if self._attr is not None:
value = raw_data.select(self._select)[self._index][self._attr]
else:
value = tag.text
tag = raw_data.select(self._select)[self._index]
if tag.name in ("style", "script", "template"):
value = tag.string
else:
value = tag.text
except IndexError:
_LOGGER.warning("Index '%s' not found in %s", self._attr, self.entity_id)
value = None
except KeyError:
_LOGGER.warning(
"Attribute '%s' not found in %s", self._attr, self.entity_id
)
value = None
_LOGGER.debug(value)
return value