Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also parse value_templates on scraped data #296

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions custom_components/multiscrape/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@ def __init__(
self._separator = separator
self.reset()

@property
def has_data(self):
return self._data is not None

@property
def name(self):
"""Return scraper name (for logging)."""
return self._config_name

def reset(self):
"""Reset the scraper."""
self._data = None
self._soup = None

async def set_content(self, content):
"""Set the content to be scraped."""
self._data = content

if content[0] in ["{", "["]:
Expand Down Expand Up @@ -71,6 +70,7 @@ async def set_content(self, content):
raise

def scrape(self, selector, sensor, attribute=None):
"""Scrape based on given selector the data."""
# This is required as this function is called separately for sensors and attributes
log_prefix = f"{self._config_name} # {sensor}"
if attribute:
Expand Down Expand Up @@ -124,10 +124,12 @@ def scrape(self, selector, sensor, attribute=None):
if value is not None and selector.value_template is not None:
_LOGGER.debug("%s # Applying value_template on selector result", log_prefix)
value = selector.value_template.async_render(
variables={"value": value}, parse_result=False
variables={"value": value}, parse_result=True
)

_LOGGER.debug("%s # Final selector value: %s", log_prefix, value)
_LOGGER.debug(
"%s # Final selector value: %s of type %s", log_prefix, value, type(value)
)
return value

async def _async_file_log(self, content_name, content):
Expand Down
Loading