From ee560a792cdc4aa7eb51de37f32f9c6f7c080885 Mon Sep 17 00:00:00 2001 From: dcmartin Date: Wed, 2 Aug 2023 10:03:20 -0700 Subject: [PATCH 1/2] added processing of URL as template --- custom_components/feedparser/sensor.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/custom_components/feedparser/sensor.py b/custom_components/feedparser/sensor.py index 58cb885..c35ad67 100644 --- a/custom_components/feedparser/sensor.py +++ b/custom_components/feedparser/sensor.py @@ -41,7 +41,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_NAME): cv.string, - vol.Required(CONF_FEED_URL): cv.string, + vol.Required(CONF_FEED_URL): cv.template, vol.Required(CONF_DATE_FORMAT, default=DEFAULT_DATE_FORMAT): cv.string, vol.Optional(CONF_LOCAL_TIME, default=False): cv.boolean, vol.Optional(CONF_SHOW_TOPN, default=DEFAULT_TOPN): cv.positive_int, @@ -61,10 +61,16 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the Feedparser sensor.""" + feed=config[CONF_FEED_URL] + + if isinstance(feed, template.Template): + _LOGGER.debug("Feed is template: %s", feed) + template.attach(hass, feed) + async_add_devices( [ FeedParserSensor( - feed=config[CONF_FEED_URL], + feed=feed, name=config[CONF_NAME], date_format=config[CONF_DATE_FORMAT], show_topn=config[CONF_SHOW_TOPN], @@ -106,7 +112,19 @@ def __init__( def update(self: FeedParserSensor) -> None: """Parse the feed and update the state of the sensor.""" - parsed_feed: FeedParserDict = feedparser.parse(self._feed) + if isinstance(self._feed, template.Template): + _LOGGER.debug("Evaluating feed template: %s", self._feed) + tmp = self._feed.async_render(None, limited=False, parse_result=False) + if tmp in ['unknown','none','unavailable','null']: + _LOGGER.warn("Template failure: %s; template: %s", tmp, self._feed) + return False + else: + _LOGGER.debug("Feed URL: %s from template: %s", tmp, self._feed) + feed_url = tmp + else: + feed_url = self._feed + + parsed_feed: FeedParserDict = feedparser.parse(feed_url) if not parsed_feed: self._attr_native_value = None From 36cfe04885e688003bf34775ea687d09ff32993b Mon Sep 17 00:00:00 2001 From: dcmartin Date: Wed, 2 Aug 2023 11:40:56 -0700 Subject: [PATCH 2/2] lgtm --- custom_components/feedparser/sensor.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/custom_components/feedparser/sensor.py b/custom_components/feedparser/sensor.py index c35ad67..9299c93 100644 --- a/custom_components/feedparser/sensor.py +++ b/custom_components/feedparser/sensor.py @@ -14,6 +14,7 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL from homeassistant.util import dt +from homeassistant.helpers import config_validation as cv, template if TYPE_CHECKING: from homeassistant.core import HomeAssistant @@ -61,16 +62,16 @@ async def async_setup_platform( discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the Feedparser sensor.""" - feed=config[CONF_FEED_URL] + feed_url=config[CONF_FEED_URL] - if isinstance(feed, template.Template): - _LOGGER.debug("Feed is template: %s", feed) - template.attach(hass, feed) + if isinstance(feed_url, template.Template): + _LOGGER.debug("Feed is template: %s", feed_url) + template.attach(hass, feed_url) async_add_devices( [ FeedParserSensor( - feed=feed, + feed=feed_url, name=config[CONF_NAME], date_format=config[CONF_DATE_FORMAT], show_topn=config[CONF_SHOW_TOPN],