Skip to content

Commit d093610

Browse files
committed
feat: remove image from the description if configured
1 parent 122c043 commit d093610

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

custom_components/feedparser/sensor.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535
CONF_INCLUSIONS = "inclusions"
3636
CONF_EXCLUSIONS = "exclusions"
3737
CONF_SHOW_TOPN = "show_topn"
38+
CONF_REMOVE_SUMMARY_IMG = "remove_summary_image"
3839

3940
DEFAULT_DATE_FORMAT = "%a, %b %d %I:%M %p"
4041
DEFAULT_SCAN_INTERVAL = timedelta(hours=1)
4142
DEFAULT_THUMBNAIL = "https://www.home-assistant.io/images/favicon-192x192-full.png"
4243
DEFAULT_TOPN = 9999
4344
USER_AGENT = f"Home Assistant Feed-parser Integration {__version__}"
45+
IMAGE_REGEX = r"<img.+?src=\"(.+?)\".+?>"
4446

4547
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
4648
{
@@ -49,6 +51,7 @@
4951
vol.Required(CONF_DATE_FORMAT, default=DEFAULT_DATE_FORMAT): cv.string,
5052
vol.Optional(CONF_LOCAL_TIME, default=False): cv.boolean,
5153
vol.Optional(CONF_SHOW_TOPN, default=DEFAULT_TOPN): cv.positive_int,
54+
vol.Optional(CONF_REMOVE_SUMMARY_IMG, default=False): cv.boolean,
5255
vol.Optional(CONF_INCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
5356
vol.Optional(CONF_EXCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
5457
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
@@ -72,6 +75,7 @@ async def async_setup_platform(
7275
name=config[CONF_NAME],
7376
date_format=config[CONF_DATE_FORMAT],
7477
show_topn=config[CONF_SHOW_TOPN],
78+
remove_summary_image=config[CONF_REMOVE_SUMMARY_IMG],
7579
inclusions=config[CONF_INCLUSIONS],
7680
exclusions=config[CONF_EXCLUSIONS],
7781
scan_interval=config[CONF_SCAN_INTERVAL],
@@ -95,6 +99,7 @@ def __init__(
9599
name: str,
96100
date_format: str,
97101
show_topn: int,
102+
remove_summary_image: bool,
98103
exclusions: list[str | None],
99104
inclusions: list[str | None],
100105
scan_interval: timedelta,
@@ -106,6 +111,7 @@ def __init__(
106111
self._attr_icon = "mdi:rss"
107112
self._date_format = date_format
108113
self._show_topn: int = show_topn
114+
self._remove_summary_image = remove_summary_image
109115
self._inclusions = inclusions
110116
self._exclusions = exclusions
111117
self._scan_interval = scan_interval
@@ -119,7 +125,9 @@ def __repr__(self: FeedParserSensor) -> str:
119125
"""Return the representation."""
120126
return (
121127
f'FeedParserSensor(name="{self.name}", feed="{self._feed}", '
122-
f"show_topn={self._show_topn}, inclusions={self._inclusions}, "
128+
f"show_topn={self._show_topn}, "
129+
f"remove_summary_image={self._remove_summary_image}, "
130+
f"inclusions={self._inclusions}, "
123131
f"exclusions={self._exclusions}, scan_interval={self._scan_interval}, "
124132
f'local_time={self._local_time}, date_format="{self._date_format}")'
125133
)
@@ -199,6 +207,12 @@ def _generate_sensor_entry(
199207
and (processed_link := self._process_link(feed_entry))
200208
):
201209
sensor_entry["link"] = processed_link
210+
if self._remove_summary_image and "summary" in sensor_entry:
211+
sensor_entry["summary"] = re.sub(
212+
IMAGE_REGEX,
213+
"",
214+
sensor_entry["summary"],
215+
)
202216
_LOGGER.debug("Feed %s: Generated sensor entry: %s", self.name, sensor_entry)
203217
return sensor_entry
204218

@@ -248,7 +262,7 @@ def _process_image(self: FeedParserSensor, feed_entry: FeedParserDict) -> str:
248262
return images[0]["href"]
249263
elif "summary" in feed_entry:
250264
images = re.findall(
251-
r"<img.+?src=\"(.+?)\".+?>",
265+
IMAGE_REGEX,
252266
feed_entry["summary"],
253267
)
254268
if images:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ target-version = "py311"
150150
"tests/**" = ["S101"]
151151

152152
[tool.ruff.pylint]
153-
max-args = 9
153+
max-args = 10
154154

155155
[[tool.mypy.overrides]]
156156
module = "feedparser.*"

tests/feedsource.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def _common_config(self: "FeedSource") -> dict[str, str | int | bool | list[str]
111111
"name": self.name,
112112
"date_format": self.sensor_config.date_format,
113113
"show_topn": self.sensor_config.show_topn,
114+
"remove_summary_image": self.sensor_config.remove_summary_image,
114115
"inclusions": self.sensor_config.inclusions,
115116
"exclusions": self.sensor_config.exclusions,
116117
"local_time": self.sensor_config.local_time,
@@ -191,6 +192,11 @@ def show_topn(self: "FeedConfig") -> int:
191192
"""Return show_topn."""
192193
return self.raw.get("show_topn", 9999)
193194

195+
@property
196+
def remove_summary_image(self: "FeedConfig") -> bool:
197+
"""Return remove_summary_image."""
198+
return self.raw.get("remove_summary_image", False)
199+
194200
@property
195201
def scan_interval(self: "FeedConfig") -> int:
196202
"""Return scan_interval in seconds."""

tests/test_sensors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def test_update_sensor(feed: FeedSource) -> None:
3333
date_format=DATE_FORMAT,
3434
local_time=False,
3535
show_topn=9999,
36+
remove_summary_image=feed.sensor_config.remove_summary_image,
3637
inclusions=["image", "title", "link", "published"],
3738
exclusions=[],
3839
scan_interval=DEFAULT_SCAN_INTERVAL,
@@ -96,6 +97,7 @@ def test_update_sensor_with_topn(feed: FeedSource) -> None:
9697
date_format=DATE_FORMAT,
9798
local_time=False,
9899
show_topn=show_topn,
100+
remove_summary_image=False,
99101
inclusions=["image", "title", "link", "published"],
100102
exclusions=[],
101103
scan_interval=DEFAULT_SCAN_INTERVAL,
@@ -172,6 +174,7 @@ def test_fetch_data_headers_required(online_feed: dict) -> None:
172174
date_format=DATE_FORMAT,
173175
local_time=False,
174176
show_topn=9999,
177+
remove_summary_image=False,
175178
inclusions=["image", "title", "link", "published"],
176179
exclusions=[],
177180
scan_interval=DEFAULT_SCAN_INTERVAL,

0 commit comments

Comments
 (0)