diff --git a/README.md b/README.md
index 68b6ce66..7a96ba8e 100644
--- a/README.md
+++ b/README.md
@@ -55,7 +55,7 @@ https://example.com/epg/programme-tv-rss_{{ now().strftime('%d-%m-%Y') }}.xml
Then pick that sensor as the feed's **Feed URL entity**. To switch feeds from an automation or script instead, use an `input_text` helper and set its value with `input_text.set_value`.
- Home Assistant limits entity states to 255 characters, so longer URLs can't be passed this way. `input_text` helpers default to a maximum of 100 characters; raise their maximum length if needed.
-- While the entity is missing, `unknown`, `unavailable` or holds something that is not an `http`, `https` or `file` URL, the feed can't be refreshed and the sensor becomes `unavailable`.
+- While the entity is missing, `unknown`, `unavailable` or holds something that is not an `http`, `https` or `file` URL (or an absolute file path), the feed can't be refreshed and the sensor becomes `unavailable`.
In YAML, use `feed_url_entity` instead of `feed_url`:
@@ -184,7 +184,7 @@ The integration supports both UI and YAML setup. Most options are the same in bo
| :-- | :-- | :-- | :-- | :-- | :-- |
| `platform` (YAML only) | Yes (YAML) | string | - | `feedparser` | Home Assistant platform name used in YAML mode. |
| `name` | Yes | string | - | `Engineering Feed` | Name shown for the sensor entity. |
-| `feed_url` | Yes (unless `feed_url_entity` is set) | URL string | - | `https://www.nu.nl/rss/Algemeen` | RSS/Atom feed URL to fetch and parse. Supports `http`, `https`, and `file` in dev/testing. |
+| `feed_url` | Yes (unless `feed_url_entity` is set) | URL string | - | `https://www.nu.nl/rss/Algemeen` | RSS/Atom feed URL to fetch and parse. Supports `http`, `https`, and `file` URLs. An absolute path such as `/config/www/feed.xml` is read as `file:///config/www/feed.xml`. |
| `feed_url_entity` | Yes (unless `feed_url` is set) | entity ID | - | `sensor.todays_tv_feed_url` | Entity (`input_text`, `text` or `sensor`) whose state is the feed URL, read on every refresh. See [Feed URLs that change](#feed-urls-that-change). |
| `date_format` | No | string (`strftime`) | `%a, %b %d %I:%M %p` | `%a, %d %b %Y %H:%M:%S %Z` | Output format for date fields in feed entries. |
| `local_time` | No | boolean | `false` | `true` | Converts parsed date values from feed timezone to Home Assistant local timezone. |
diff --git a/custom_components/feedparser/config_flow.py b/custom_components/feedparser/config_flow.py
index b6f33349..a92222e8 100644
--- a/custom_components/feedparser/config_flow.py
+++ b/custom_components/feedparser/config_flow.py
@@ -52,6 +52,7 @@
NoFeedUrlError,
feed_url_from_entity,
is_supported_url,
+ normalize_feed_url,
)
from .yaml_import import (
REASON_MANAGED_IN_UI,
@@ -165,7 +166,7 @@ def _source_from_input(
The source is either a typed URL ({feed_url}) or an entity whose state is
the URL ({feed_url_entity}); exactly one of the two must be filled in.
"""
- feed_url = str(user_input.get(CONF_FEED_URL) or "").strip()
+ feed_url = normalize_feed_url(str(user_input.get(CONF_FEED_URL) or ""))
entity_id = str(user_input.get(CONF_FEED_URL_ENTITY) or "").strip()
if bool(feed_url) == bool(entity_id):
return {}, {"base": "url_or_entity"}
@@ -416,7 +417,7 @@ async def async_step_import(
source = (
{CONF_FEED_URL_ENTITY: url_entity}
if url_entity
- else {CONF_FEED_URL: feed_url}
+ else {CONF_FEED_URL: normalize_feed_url(feed_url)}
)
unique_id = yaml_unique_id(name)
data = {CONF_NAME: name, **source, CONF_MANAGED_BY_YAML: True}
diff --git a/custom_components/feedparser/fetch.py b/custom_components/feedparser/fetch.py
index 2097398d..86a1cc3b 100644
--- a/custom_components/feedparser/fetch.py
+++ b/custom_components/feedparser/fetch.py
@@ -85,6 +85,7 @@ def fetch_feed(
session.mount("file://", FileAdapter())
session.headers.update({"User-Agent": user_agent})
proxies = {"http": proxy, "https": proxy} if proxy else None
- response = session.get(url, timeout=REQUEST_TIMEOUT, proxies=proxies)
- response.raise_for_status()
- return FetchedFeed(response.content, response.url or url)
+ # closing the response also closes the file a file:// URL opened
+ with session.get(url, timeout=REQUEST_TIMEOUT, proxies=proxies) as response:
+ response.raise_for_status()
+ return FetchedFeed(response.content, response.url or url)
diff --git a/custom_components/feedparser/source.py b/custom_components/feedparser/source.py
index 6d7f7746..77f637e2 100644
--- a/custom_components/feedparser/source.py
+++ b/custom_components/feedparser/source.py
@@ -8,6 +8,7 @@
from __future__ import annotations
from typing import TYPE_CHECKING
+from urllib.parse import quote
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from yarl import URL
@@ -34,6 +35,18 @@ class InvalidFeedUrlError(FeedUrlError):
"""The URL entity's value is not a supported feed URL."""
+def normalize_feed_url(feed_url: str) -> str:
+ """Return `feed_url` stripped, with an absolute file path made a file:// URL.
+
+ Before 0.2.0 feedparser opened a plain path such as ``/config/www/feed.xml``
+ itself; requests needs the ``file://`` form.
+ """
+ feed_url = feed_url.strip()
+ if feed_url.startswith("/"):
+ return f"file://{quote(feed_url)}"
+ return feed_url
+
+
def is_supported_url(feed_url: str) -> bool:
"""Return whether the URL is well-formed with a supported scheme."""
try:
@@ -47,13 +60,13 @@ def feed_url_from_entity(hass: HomeAssistant, entity_id: str) -> str:
Must run in the event loop. Raises ``NoFeedUrlError`` if the entity is
missing, unknown, unavailable or empty, and ``InvalidFeedUrlError`` if its
- value is not an http(s) or file URL.
+ value is not an http(s) or file URL (or an absolute file path).
"""
state = hass.states.get(entity_id)
if state is None:
msg = f"{entity_id} does not exist"
raise NoFeedUrlError(msg)
- url = state.state.strip()
+ url = normalize_feed_url(state.state)
if not url or url in (STATE_UNKNOWN, STATE_UNAVAILABLE):
msg = f"{entity_id} has no URL (state: {state.state!r})"
raise NoFeedUrlError(msg)
diff --git a/tests/test_hardening.py b/tests/test_hardening.py
index 2d2a67ce..a9c7f416 100644
--- a/tests/test_hardening.py
+++ b/tests/test_hardening.py
@@ -23,6 +23,7 @@
)
from custom_components.feedparser.reader import DEFAULT_THUMBNAIL
from custom_components.feedparser.sensor import FeedParserSensor
+from custom_components.feedparser.source import normalize_feed_url
DATE_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
@@ -86,6 +87,31 @@ def test_non_utf8_feed_is_decoded_from_xml_prolog(tmp_path: Path) -> None:
assert sensor.feed_entries[0]["title"] == "Café crème"
+@pytest.mark.parametrize(
+ ("raw", "expected"),
+ [
+ ("/config/www/feed.xml", "file:///config/www/feed.xml"),
+ (" /my feeds/a#1.xml ", "file:///my%20feeds/a%231.xml"),
+ ("https://example.com/feed", "https://example.com/feed"),
+ ("file:///config/feed.xml", "file:///config/feed.xml"),
+ # relative paths are not guessed at; they stay invalid
+ ("www/feed.xml", "www/feed.xml"),
+ ],
+)
+def test_normalize_feed_url(raw: str, expected: str) -> None:
+ """Only an absolute path is turned into a file:// URL (#187)."""
+ assert normalize_feed_url(raw) == expected
+
+
+def test_plain_path_with_special_characters_is_read(tmp_path: Path) -> None:
+ """The percent-encoded file:// URL still opens the file."""
+ feed = tmp_path / "my feeds" / "a#1.xml"
+ feed.parent.mkdir()
+ feed.write_text(_rss("