Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down Expand Up @@ -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. |
Expand Down
5 changes: 3 additions & 2 deletions custom_components/feedparser/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
NoFeedUrlError,
feed_url_from_entity,
is_supported_url,
normalize_feed_url,
)
from .yaml_import import (
REASON_MANAGED_IN_UI,
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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}
Expand Down
7 changes: 4 additions & 3 deletions custom_components/feedparser/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
17 changes: 15 additions & 2 deletions custom_components/feedparser/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_hardening.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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("<item><title>a</title></item>"), encoding="utf-8")
fetched = fetch.fetch_feed(normalize_feed_url(str(feed)))
assert b"<title>a</title>" in fetched.content


RELATIVE_FEED = _rss(
"<item><title>a</title><link>/news/1</link>"
'<description>&lt;img src="/img/1.jpg"&gt; hi</description></item>'
Expand Down
36 changes: 36 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,42 @@ async def test_yaml_is_imported(hass: HomeAssistant, tmp_path: Path) -> None:
assert not [domain for domain, _ in ir.async_get(hass).issues if domain == DOMAIN]


async def test_yaml_feed_url_can_be_a_plain_path(
hass: HomeAssistant,
tmp_path: Path,
) -> None:
"""An absolute path, accepted before 0.2.0, is stored as a file:// URL (#187)."""
url = write_feed(tmp_path / "feed.xml", ["one"])
await setup_yaml(
hass,
{"name": "Local Feed", "feed_url": str(tmp_path / "feed.xml")},
)

entry = hass.config_entries.async_entries(DOMAIN)[0]
assert entry.data[CONF_FEED_URL] == url
assert get_state(hass, "sensor.local_feed").state == "1"


async def test_user_flow_accepts_plain_path(
hass: HomeAssistant,
tmp_path: Path,
) -> None:
"""The setup form also takes an absolute path and stores it as a URL."""
url = write_feed(tmp_path / "feed.xml", ["one"])
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input("Test Feed", f" {tmp_path / 'feed.xml'} "),
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"][CONF_FEED_URL] == url
await hass.async_block_till_done()
assert get_state(hass).state == "1"


async def test_yaml_import_is_idempotent(hass: HomeAssistant, tmp_path: Path) -> None:
"""Importing again on the next start does not duplicate the entry."""
url = write_feed(tmp_path / "feed.xml", ["one"])
Expand Down
11 changes: 11 additions & 0 deletions tests/test_url_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ async def test_user_flow_with_url_entity(hass: HomeAssistant, tmp_path: Path) ->
assert titles(hass) == ["one", "two"]


async def test_url_entity_can_hold_a_plain_path(
hass: HomeAssistant,
tmp_path: Path,
) -> None:
"""An entity holding an absolute path reads it as a file:// URL."""
write_feed(tmp_path / "a.xml", ["one"])
hass.states.async_set(URL_ENTITY, str(tmp_path / "a.xml"))
await add_entity_entry(hass)
assert titles(hass) == ["one"]


async def test_url_change_refreshes_feed(hass: HomeAssistant, tmp_path: Path) -> None:
"""Changing the entity's value fetches the new feed right away."""
hass.states.async_set(URL_ENTITY, write_feed(tmp_path / "a.xml", ["old"]))
Expand Down
Loading