init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
"""Tests for the feedreader component."""
from typing import Any
from unittest.mock import patch
from homeassistant.components.feedreader.const import CONF_MAX_ENTRIES, DOMAIN
from homeassistant.const import CONF_URL
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
def load_fixture_bytes(src: str) -> bytes:
"""Return byte stream of fixture."""
feed_data = load_fixture(src, DOMAIN)
return bytes(feed_data, "utf-8")
def create_mock_entry(
data: dict[str, Any],
) -> MockConfigEntry:
"""Create config entry mock from data."""
return MockConfigEntry(
domain=DOMAIN,
data={CONF_URL: data[CONF_URL]},
options={CONF_MAX_ENTRIES: data[CONF_MAX_ENTRIES]},
)
async def async_setup_config_entry(
hass: HomeAssistant,
data: dict[str, Any],
return_value: bytes | None = None,
side_effect: bytes | None = None,
) -> bool:
"""Do setup of a MockConfigEntry."""
entry = create_mock_entry(data)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get",
) as feedparser:
if return_value:
feedparser.return_value = return_value
if side_effect:
feedparser.side_effect = side_effect
result = await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return result

View File

@@ -0,0 +1,82 @@
"""Fixtures for the tests for the feedreader component."""
import pytest
from homeassistant.components.feedreader.coordinator import EVENT_FEEDREADER
from homeassistant.core import Event, HomeAssistant
from . import load_fixture_bytes
from tests.common import async_capture_events
@pytest.fixture(name="feed_one_event")
def fixture_feed_one_event(hass: HomeAssistant) -> bytes:
"""Load test feed data for one event."""
return load_fixture_bytes("feedreader.xml")
@pytest.fixture(name="feed_two_event")
def fixture_feed_two_events(hass: HomeAssistant) -> bytes:
"""Load test feed data for two event."""
return load_fixture_bytes("feedreader1.xml")
@pytest.fixture(name="feed_21_events")
def fixture_feed_21_events(hass: HomeAssistant) -> bytes:
"""Load test feed data for twenty one events."""
return load_fixture_bytes("feedreader2.xml")
@pytest.fixture(name="feed_three_events")
def fixture_feed_three_events(hass: HomeAssistant) -> bytes:
"""Load test feed data for three events."""
return load_fixture_bytes("feedreader3.xml")
@pytest.fixture(name="feed_four_events")
def fixture_feed_four_events(hass: HomeAssistant) -> bytes:
"""Load test feed data for three events."""
return load_fixture_bytes("feedreader4.xml")
@pytest.fixture(name="feed_atom_event")
def fixture_feed_atom_event(hass: HomeAssistant) -> bytes:
"""Load test feed data for atom event."""
return load_fixture_bytes("feedreader5.xml")
@pytest.fixture(name="feed_identically_timed_events")
def fixture_feed_identically_timed_events(hass: HomeAssistant) -> bytes:
"""Load test feed data for two events published at the exact same time."""
return load_fixture_bytes("feedreader6.xml")
@pytest.fixture(name="feed_without_items")
def fixture_feed_without_items(hass: HomeAssistant) -> bytes:
"""Load test feed without any items."""
return load_fixture_bytes("feedreader7.xml")
@pytest.fixture(name="feed_only_summary")
def fixture_feed_only_summary(hass: HomeAssistant) -> bytes:
"""Load test feed data with one event containing only a summary, no content."""
return load_fixture_bytes("feedreader8.xml")
@pytest.fixture(name="feed_htmlentities")
def fixture_feed_htmlentities(hass: HomeAssistant) -> bytes:
"""Load test feed data with HTML Entities."""
return load_fixture_bytes("feedreader9.xml")
@pytest.fixture(name="feed_atom_htmlentities")
def fixture_feed_atom_htmlentities(hass: HomeAssistant) -> bytes:
"""Load test ATOM feed data with HTML Entities."""
return load_fixture_bytes("feedreader10.xml")
@pytest.fixture(name="events")
async def fixture_events(hass: HomeAssistant) -> list[Event]:
"""Fixture that catches alexa events."""
return async_capture_events(hass, EVENT_FEEDREADER)

View File

@@ -0,0 +1,14 @@
"""Constants for the tests for the feedreader component."""
from homeassistant.components.feedreader.const import (
CONF_MAX_ENTRIES,
DEFAULT_MAX_ENTRIES,
)
from homeassistant.const import CONF_URL
URL = "http://some.rss.local/rss_feed.xml"
FEED_TITLE = "RSS Sample"
VALID_CONFIG_DEFAULT = {CONF_URL: URL, CONF_MAX_ENTRIES: DEFAULT_MAX_ENTRIES}
VALID_CONFIG_100 = {CONF_URL: URL, CONF_MAX_ENTRIES: 100}
VALID_CONFIG_5 = {CONF_URL: URL, CONF_MAX_ENTRIES: 5}
VALID_CONFIG_1 = {CONF_URL: URL, CONF_MAX_ENTRIES: 1}

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 1</title>
<description>Description 1</description>
<link>http://www.example.com/link/1</link>
<guid isPermaLink="false">GUID 1</guid>
<pubDate>Mon, 30 Apr 2018 15:10:00 +1000</pubDate>
<content>Content 1</content>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 2</title>
<description>Description 2</description>
<link>http://www.example.com/link/2</link>
<guid isPermaLink="false">GUID 2</guid>
<pubDate>Mon, 30 Apr 2018 15:11:00 +1000</pubDate>
<content>Content 2</content>
</item>
<item>
<title>Title 1</title>
<description>Description 1</description>
<link>http://www.example.com/link/1</link>
<guid isPermaLink="false">GUID 1</guid>
<pubDate>Mon, 30 Apr 2018 15:10:00 +1000</pubDate>
<content>Content 1</content>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<feed
xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[ATOM RSS en espa&ntilde;ol]]></title>
<link href="http://example.org/"/>
<updated>2024-11-18T14:00:00Z</updated>
<author>
<name><![CDATA[Juan P&eacute;rez]]></name>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<entry>
<title><![CDATA[T&iacute;tulo]]></title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2024-11-18T14:00:00Z</updated>
<summary><![CDATA[Resumen en espa&ntilde;ol]]></summary>
<content><![CDATA[Contenido en espa&ntilde;ol]]></content>
</entry>
</feed>

View File

@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 21</title>
<pubDate>Mon, 30 Apr 2018 15:20:00 +1000</pubDate>
<content>Content 21</content>
</item>
<item>
<title>Title 20</title>
<pubDate>Mon, 30 Apr 2018 15:19:00 +1000</pubDate>
<content>Content 20</content>
</item>
<item>
<title>Title 19</title>
<pubDate>Mon, 30 Apr 2018 15:18:00 +1000</pubDate>
<content>Content 19</content>
</item>
<item>
<title>Title 18</title>
<pubDate>Mon, 30 Apr 2018 15:17:00 +1000</pubDate>
<content>Content 18</content>
</item>
<item>
<title>Title 17</title>
<pubDate>Mon, 30 Apr 2018 15:16:00 +1000</pubDate>
<content>Content 17</content>
</item>
<item>
<title>Title 16</title>
<pubDate>Mon, 30 Apr 2018 15:15:00 +1000</pubDate>
<content>Content 16</content>
</item>
<item>
<title>Title 15</title>
<pubDate>Mon, 30 Apr 2018 15:14:00 +1000</pubDate>
<content>Content 15</content>
</item>
<item>
<title>Title 14</title>
<pubDate>Mon, 30 Apr 2018 15:13:00 +1000</pubDate>
<content>Content 14</content>
</item>
<item>
<title>Title 13</title>
<pubDate>Mon, 30 Apr 2018 15:12:00 +1000</pubDate>
<content>Content 13</content>
</item>
<item>
<title>Title 12</title>
<pubDate>Mon, 30 Apr 2018 15:11:00 +1000</pubDate>
<content>Content 12</content>
</item>
<item>
<title>Title 11</title>
<pubDate>Mon, 30 Apr 2018 15:10:00 +1000</pubDate>
<content>Content 11</content>
</item>
<item>
<title>Title 10</title>
<pubDate>Mon, 30 Apr 2018 15:09:00 +1000</pubDate>
<content>Content 10</content>
</item>
<item>
<title>Title 9</title>
<pubDate>Mon, 30 Apr 2018 15:08:00 +1000</pubDate>
<content>Content 9</content>
</item>
<item>
<title>Title 8</title>
<pubDate>Mon, 30 Apr 2018 15:07:00 +1000</pubDate>
<content>Content 8</content>
</item>
<item>
<title>Title 7</title>
<pubDate>Mon, 30 Apr 2018 15:06:00 +1000</pubDate>
<content>Content 7</content>
</item>
<item>
<title>Title 6</title>
<pubDate>Mon, 30 Apr 2018 15:05:00 +1000</pubDate>
<content>Content 6</content>
</item>
<item>
<title>Title 5</title>
<pubDate>Mon, 30 Apr 2018 15:04:00 +1000</pubDate>
<content>Content 5</content>
</item>
<item>
<title>Title 4</title>
<pubDate>Mon, 30 Apr 2018 15:03:00 +1000</pubDate>
<content>Content 4</content>
</item>
<item>
<title>Title 3</title>
<pubDate>Mon, 30 Apr 2018 15:02:00 +1000</pubDate>
<content>Content 3</content>
</item>
<item>
<title>Title 1</title>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<content>Content 1</content>
</item>
<item>
<title>Title 2</title>
<pubDate>Mon, 30 Apr 2018 15:01:00 +1000</pubDate>
<content>Content 2</content>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 1</title>
<description>Description 1</description>
<link>http://www.example.com/link/1</link>
<guid isPermaLink="false">GUID 1</guid>
<pubDate>Mon, 30 Apr 2018 15:10:00 +1000</pubDate>
<content>Content 1</content>
</item>
<item>
<title>Title 2</title>
<description>Description 2</description>
<link>http://www.example.com/link/2</link>
<guid isPermaLink="false">GUID 2</guid>
<content>Content 2</content>
</item>
<item>
<description>Description 3</description>
<link>http://www.example.com/link/3</link>
<guid isPermaLink="false">GUID 3</guid>
<content>Content 3</content>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 26 Oct 2019 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 26 Oct 2019 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 1</title>
<description>Description 1</description>
<link>http://www.example.com/link/1</link>
<guid isPermaLink="false">GUID 1</guid>
<pubDate>26.10.2019 - 12:06:24</pubDate>
<content>Content 1</content>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<feed
xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<link href="http://example.org/"/>
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
<content>Content 1</content>
</entry>
</feed>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +0000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +0000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 1</title>
<description>Description 1</description>
<link>http://www.example.com/link/1</link>
<guid isPermaLink="false">GUID 1</guid>
<pubDate>Mon, 30 Apr 2018 15:10:00 +0000</pubDate>
<content>Content 1</content>
</item>
<item>
<title>Title 2</title>
<description>Description 2</description>
<link>http://www.example.com/link/2</link>
<guid isPermaLink="false">GUID 2</guid>
<pubDate>Mon, 30 Apr 2018 15:10:00 +0000</pubDate>
<content>Content 2</content>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
</channel>
</rss>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 1</title>
<description>Description 1</description>
<link>http://www.example.com/link/1</link>
<guid isPermaLink="false">GUID 1</guid>
<pubDate>Mon, 30 Apr 2018 15:10:00 +1000</pubDate>
<summary>This is a summary</summary>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[RSS en espa&ntilde;ol]]></title>
<description><![CDATA[Esto es un ejemplo de un feed RSS en espa&ntilde;ol]]></description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 18 Nov 2024 15:00:00 +1000</lastBuildDate>
<pubDate>Mon, 18 Nov 2024 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title><![CDATA[T&iacute;tulo 1]]></title>
<description><![CDATA[Descripci&oacute;n 1]]></description>
<link>http://www.example.com/link/1</link>
<guid isPermaLink="false">GUID 1</guid>
<pubDate>Mon, 18 Nov 2024 15:00:00 +1000</pubDate>
<content><![CDATA[Contenido 1 en espa&ntilde;ol]]></content>
</item>
</channel>
</rss>

View File

@@ -0,0 +1,27 @@
# serializer version: 1
# name: test_event_htmlentities[feed_atom_htmlentities]
ReadOnlyDict({
'content': 'Contenido en español',
'description': 'Resumen en español',
'event_type': 'feedreader',
'event_types': list([
'feedreader',
]),
'friendly_name': 'Mock Title',
'link': 'http://example.org/2003/12/13/atom03',
'title': 'Título',
})
# ---
# name: test_event_htmlentities[feed_htmlentities]
ReadOnlyDict({
'content': 'Contenido 1 en español',
'description': 'Descripción 1',
'event_type': 'feedreader',
'event_types': list([
'feedreader',
]),
'friendly_name': 'Mock Title',
'link': 'http://www.example.com/link/1',
'title': 'Título 1',
})
# ---

View File

@@ -0,0 +1,221 @@
"""The tests for the feedreader config flow."""
from unittest.mock import Mock, patch
import urllib
import pytest
from homeassistant.components.feedreader.const import (
CONF_MAX_ENTRIES,
DEFAULT_MAX_ENTRIES,
DOMAIN,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import create_mock_entry
from .const import FEED_TITLE, URL, VALID_CONFIG_DEFAULT
@pytest.fixture(name="feedparser")
def feedparser_fixture(feed_one_event: bytes) -> Mock:
"""Patch libraries."""
with (
patch(
"homeassistant.components.feedreader.config_flow.feedparser.http.get",
return_value=feed_one_event,
) as feedparser,
):
yield feedparser
@pytest.fixture(name="setup_entry")
def setup_entry_fixture(feed_one_event: bytes) -> Mock:
"""Patch libraries."""
with (
patch("homeassistant.components.feedreader.async_setup_entry") as setup_entry,
):
yield setup_entry
async def test_user(hass: HomeAssistant, feedparser, setup_entry) -> None:
"""Test starting a flow by user."""
# init user flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
# success
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_URL: URL}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == FEED_TITLE
assert result["data"][CONF_URL] == URL
assert result["options"][CONF_MAX_ENTRIES] == DEFAULT_MAX_ENTRIES
async def test_user_errors(
hass: HomeAssistant, feedparser, setup_entry, feed_one_event
) -> None:
"""Test starting a flow by user which results in an URL error."""
# init user flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
# raise URLError
feedparser.side_effect = urllib.error.URLError("Test")
feedparser.return_value = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_URL: URL}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "url_error"}
# success
feedparser.side_effect = None
feedparser.return_value = feed_one_event
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_URL: URL}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == FEED_TITLE
assert result["data"][CONF_URL] == URL
assert result["options"][CONF_MAX_ENTRIES] == DEFAULT_MAX_ENTRIES
async def test_reconfigure(hass: HomeAssistant, feedparser) -> None:
"""Test starting a reconfigure flow."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
# init user flow
result = await entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
# success
with patch(
"homeassistant.config_entries.ConfigEntries.async_reload"
) as mock_async_reload:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_URL: "http://other.rss.local/rss_feed.xml",
},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == {
CONF_URL: "http://other.rss.local/rss_feed.xml",
}
await hass.async_block_till_done()
assert mock_async_reload.call_count == 1
async def test_reconfigure_errors(
hass: HomeAssistant, feedparser, setup_entry, feed_one_event
) -> None:
"""Test starting a reconfigure flow by user which results in an URL error."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
# init user flow
result = await entry.start_reconfigure_flow(hass)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
# raise URLError
feedparser.side_effect = urllib.error.URLError("Test")
feedparser.return_value = None
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_URL: "http://other.rss.local/rss_feed.xml",
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "reconfigure"
assert result["errors"] == {"base": "url_error"}
# success
feedparser.side_effect = None
feedparser.return_value = feed_one_event
# success
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_URL: "http://other.rss.local/rss_feed.xml",
},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "reconfigure_successful"
assert entry.data == {
CONF_URL: "http://other.rss.local/rss_feed.xml",
}
async def test_options_flow(hass: HomeAssistant) -> None:
"""Test options flow."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
result = await hass.config_entries.options.async_init(entry.entry_id)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_MAX_ENTRIES: 10,
},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_MAX_ENTRIES: 10,
}
@pytest.mark.parametrize(
("fixture_name", "expected_title"),
[
("feed_htmlentities", "RSS en español"),
("feed_atom_htmlentities", "ATOM RSS en español"),
],
)
async def test_feed_htmlentities(
hass: HomeAssistant,
feedparser,
setup_entry,
fixture_name,
expected_title,
request: pytest.FixtureRequest,
) -> None:
"""Test starting a flow by user from a feed with HTML Entities in the title."""
with patch(
"homeassistant.components.feedreader.config_flow.feedparser.http.get",
side_effect=[request.getfixturevalue(fixture_name)],
):
# init user flow
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
# success
result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_URL: URL}
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == expected_title

View File

@@ -0,0 +1,92 @@
"""The tests for the feedreader event entity."""
from datetime import timedelta
from unittest.mock import patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.feedreader.event import (
ATTR_CONTENT,
ATTR_DESCRIPTION,
ATTR_LINK,
ATTR_TITLE,
)
from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util
from . import create_mock_entry
from .const import VALID_CONFIG_DEFAULT
from tests.common import async_fire_time_changed
async def test_event_entity(
hass: HomeAssistant, feed_one_event, feed_two_event, feed_only_summary
) -> None:
"""Test feed event entity."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get",
side_effect=[feed_one_event, feed_two_event, feed_only_summary],
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("event.mock_title")
assert state
assert state.attributes[ATTR_TITLE] == "Title 1"
assert state.attributes[ATTR_LINK] == "http://www.example.com/link/1"
assert state.attributes[ATTR_CONTENT] == "Content 1"
assert state.attributes[ATTR_DESCRIPTION] == "Description 1"
future = dt_util.utcnow() + timedelta(hours=1, seconds=1)
async_fire_time_changed(hass, future)
await hass.async_block_till_done(wait_background_tasks=True)
state = hass.states.get("event.mock_title")
assert state
assert state.attributes[ATTR_TITLE] == "Title 2"
assert state.attributes[ATTR_LINK] == "http://www.example.com/link/2"
assert state.attributes[ATTR_CONTENT] == "Content 2"
assert state.attributes[ATTR_DESCRIPTION] == "Description 2"
future = dt_util.utcnow() + timedelta(hours=2, seconds=2)
async_fire_time_changed(hass, future)
await hass.async_block_till_done(wait_background_tasks=True)
state = hass.states.get("event.mock_title")
assert state
assert state.attributes[ATTR_TITLE] == "Title 1"
assert state.attributes[ATTR_LINK] == "http://www.example.com/link/1"
assert state.attributes[ATTR_CONTENT] == "This is a summary"
assert state.attributes[ATTR_DESCRIPTION] == "Description 1"
@pytest.mark.parametrize(
("fixture_name"),
[
("feed_htmlentities"),
("feed_atom_htmlentities"),
],
)
async def test_event_htmlentities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
fixture_name,
request: pytest.FixtureRequest,
) -> None:
"""Test feed event entity with HTML Entities."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get",
side_effect=[request.getfixturevalue(fixture_name)],
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("event.mock_title")
assert state
assert state.attributes == snapshot

View File

@@ -0,0 +1,398 @@
"""The tests for the feedreader component."""
from datetime import datetime, timedelta
from time import gmtime
from typing import Any
from unittest.mock import patch
import urllib
import urllib.error
from freezegun.api import FrozenDateTimeFactory
import pytest
from homeassistant.components.feedreader.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import Event, HomeAssistant
from homeassistant.helpers import device_registry as dr
import homeassistant.util.dt as dt_util
from . import async_setup_config_entry, create_mock_entry
from .const import (
URL,
VALID_CONFIG_1,
VALID_CONFIG_5,
VALID_CONFIG_100,
VALID_CONFIG_DEFAULT,
)
from tests.common import async_fire_time_changed
@pytest.mark.parametrize(
"config",
[VALID_CONFIG_DEFAULT, VALID_CONFIG_1, VALID_CONFIG_100, VALID_CONFIG_5],
)
async def test_setup(
hass: HomeAssistant,
events: list[Event],
feed_one_event: bytes,
hass_storage: dict[str, Any],
config: dict[str, Any],
) -> None:
"""Test loading existing storage data."""
storage_data: dict[str, str] = {URL: "2018-04-30T05:10:00+00:00"}
hass_storage[DOMAIN] = {
"version": 1,
"minor_version": 1,
"key": DOMAIN,
"data": storage_data,
}
assert await async_setup_config_entry(hass, config, return_value=feed_one_event)
# no new events
assert not events
async def test_setup_error(
hass: HomeAssistant,
feed_one_event,
) -> None:
"""Test setup error."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get"
) as feedreader:
feedreader.side_effect = urllib.error.URLError("Test")
feedreader.return_value = feed_one_event
await hass.config_entries.async_setup(entry.entry_id)
assert entry.state is ConfigEntryState.SETUP_RETRY
async def test_storage_data_writing(
hass: HomeAssistant,
events: list[Event],
feed_one_event: bytes,
hass_storage: dict[str, Any],
) -> None:
"""Test writing to storage."""
storage_data: dict[str, str] = {URL: "2018-04-30T05:10:00+00:00"}
with (
patch("homeassistant.components.feedreader.coordinator.DELAY_SAVE", new=0),
):
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_one_event
)
# one new event
assert len(events) == 1
# storage data updated
assert hass_storage[DOMAIN]["data"] == storage_data
async def test_feed(hass: HomeAssistant, events, feed_one_event) -> None:
"""Test simple rss feed with valid data."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_one_event
)
assert len(events) == 1
assert events[0].data.title == "Title 1"
assert events[0].data.description == "Description 1"
assert events[0].data.link == "http://www.example.com/link/1"
assert events[0].data.id == "GUID 1"
assert events[0].data.published_parsed.tm_year == 2018
assert events[0].data.published_parsed.tm_mon == 4
assert events[0].data.published_parsed.tm_mday == 30
assert events[0].data.published_parsed.tm_hour == 5
assert events[0].data.published_parsed.tm_min == 10
async def test_atom_feed(hass: HomeAssistant, events, feed_atom_event) -> None:
"""Test simple atom feed with valid data."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_atom_event
)
assert len(events) == 1
assert events[0].data.title == "Atom-Powered Robots Run Amok"
assert events[0].data.description == "Some text."
assert events[0].data.link == "http://example.org/2003/12/13/atom03"
assert events[0].data.id == "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a"
assert events[0].data.updated_parsed.tm_year == 2003
assert events[0].data.updated_parsed.tm_mon == 12
assert events[0].data.updated_parsed.tm_mday == 13
assert events[0].data.updated_parsed.tm_hour == 18
assert events[0].data.updated_parsed.tm_min == 30
async def test_feed_identical_timestamps(
hass: HomeAssistant, events, feed_identically_timed_events
) -> None:
"""Test feed with 2 entries with identical timestamps."""
with (
patch(
"homeassistant.components.feedreader.coordinator.StoredData.get_timestamp",
return_value=gmtime(
datetime.fromisoformat("1970-01-01T00:00:00.0+0000").timestamp()
),
),
):
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_identically_timed_events
)
assert len(events) == 2
assert events[0].data.title == "Title 1"
assert events[1].data.title == "Title 2"
assert events[0].data.link == "http://www.example.com/link/1"
assert events[1].data.link == "http://www.example.com/link/2"
assert events[0].data.id == "GUID 1"
assert events[1].data.id == "GUID 2"
assert (
events[0].data.updated_parsed.tm_year
== events[1].data.updated_parsed.tm_year
== 2018
)
assert (
events[0].data.updated_parsed.tm_mon
== events[1].data.updated_parsed.tm_mon
== 4
)
assert (
events[0].data.updated_parsed.tm_mday
== events[1].data.updated_parsed.tm_mday
== 30
)
assert (
events[0].data.updated_parsed.tm_hour
== events[1].data.updated_parsed.tm_hour
== 15
)
assert (
events[0].data.updated_parsed.tm_min
== events[1].data.updated_parsed.tm_min
== 10
)
assert (
events[0].data.updated_parsed.tm_sec
== events[1].data.updated_parsed.tm_sec
== 0
)
async def test_feed_with_only_summary(
hass: HomeAssistant, events, feed_only_summary
) -> None:
"""Test simple feed with only summary, no content."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_only_summary
)
await hass.async_block_till_done()
assert len(events) == 1
assert events[0].data.title == "Title 1"
assert events[0].data.description == "Description 1"
assert events[0].data.content[0].value == "This is a summary"
async def test_feed_updates(
hass: HomeAssistant, events, feed_one_event, feed_two_event
) -> None:
"""Test feed updates."""
side_effect = [
feed_one_event,
feed_two_event,
feed_two_event,
]
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get",
side_effect=side_effect,
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(events) == 1
# Change time and fetch more entries
future = dt_util.utcnow() + timedelta(hours=1, seconds=1)
async_fire_time_changed(hass, future)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(events) == 2
# Change time but no new entries
future = dt_util.utcnow() + timedelta(hours=2, seconds=2)
async_fire_time_changed(hass, future)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(events) == 2
async def test_feed_default_max_length(
hass: HomeAssistant, events, feed_21_events
) -> None:
"""Test long feed beyond the default 20 entry limit."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_21_events
)
await hass.async_block_till_done()
assert len(events) == 20
async def test_feed_max_length(hass: HomeAssistant, events, feed_21_events) -> None:
"""Test long feed beyond a configured 5 entry limit."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_5, return_value=feed_21_events
)
await hass.async_block_till_done()
assert len(events) == 5
async def test_feed_without_publication_date_and_title(
hass: HomeAssistant, events, feed_three_events
) -> None:
"""Test simple feed with entry without publication date and title."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_three_events
)
await hass.async_block_till_done()
assert len(events) == 3
async def test_feed_with_unrecognized_publication_date(
hass: HomeAssistant, events, feed_four_events
) -> None:
"""Test simple feed with entry with unrecognized publication date."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_four_events
)
await hass.async_block_till_done()
assert len(events) == 1
async def test_feed_without_items(
hass: HomeAssistant, events, feed_without_items, caplog: pytest.LogCaptureFixture
) -> None:
"""Test simple feed without any items."""
assert "No new entries to be published in feed" not in caplog.text
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_without_items
)
await hass.async_block_till_done()
assert "No new entries to be published in feed" in caplog.text
assert len(events) == 0
async def test_feed_invalid_data(hass: HomeAssistant, events) -> None:
"""Test feed with invalid data."""
assert await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=bytes("INVALID DATA", "utf-8")
)
await hass.async_block_till_done()
assert len(events) == 0
async def test_feed_parsing_failed(
hass: HomeAssistant, events, feed_one_event, caplog: pytest.LogCaptureFixture
) -> None:
"""Test feed where parsing fails."""
assert "Error fetching feed data" not in caplog.text
with patch("feedparser.parse", return_value=None):
assert not await async_setup_config_entry(
hass, VALID_CONFIG_DEFAULT, return_value=feed_one_event
)
await hass.async_block_till_done()
assert "Error fetching feed data" in caplog.text
assert not events
async def test_feed_errors(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
feed_one_event,
) -> None:
"""Test feed errors."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get"
) as feedreader:
# success setup
feedreader.return_value = feed_one_event
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
# raise URL error
feedreader.side_effect = urllib.error.URLError("Test")
freezer.tick(timedelta(hours=1, seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert (
"Error fetching feed data from http://some.rss.local/rss_feed.xml : <urlopen error Test>"
in caplog.text
)
# success
feedreader.side_effect = None
feedreader.return_value = feed_one_event
freezer.tick(timedelta(hours=1, seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
caplog.clear()
# no feed returned
freezer.tick(timedelta(hours=1, seconds=1))
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.parse",
return_value=None,
):
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert (
"Error fetching feed data from http://some.rss.local/rss_feed.xml"
in caplog.text
)
caplog.clear()
# success
feedreader.side_effect = None
feedreader.return_value = feed_one_event
freezer.tick(timedelta(hours=1, seconds=1))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
async def test_feed_atom_htmlentities(
hass: HomeAssistant, feed_atom_htmlentities, device_registry: dr.DeviceRegistry
) -> None:
"""Test ATOM feed author with HTML Entities."""
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get",
side_effect=[feed_atom_htmlentities],
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
device_entry = device_registry.async_get_device(
identifiers={(DOMAIN, entry.entry_id)}
)
assert device_entry.manufacturer == "Juan Pérez"