"""Tests for scrape component."""
from __future__ import annotations
from typing import Any
def return_integration_config(
*,
authentication=None,
username=None,
password=None,
headers=None,
sensors=None,
) -> dict[str, dict[str, Any]]:
"""Return config."""
config = {
"resource": "https://www.home-assistant.io",
"verify_ssl": True,
"sensor": sensors,
}
if authentication:
config["authentication"] = authentication
if username:
config["username"] = username
config["password"] = password
if headers:
config["headers"] = headers
return config
class MockRestData:
"""Mock RestData."""
def __init__(
self,
payload,
) -> None:
"""Init RestDataMock."""
self.data: str | None = None
self.headers: dict[str, str] | None = None
self.payload = payload
self.count = 0
async def async_update(self, data: bool | None = True) -> None:
"""Update."""
self.count += 1
self.headers = {}
if self.payload == "test_scrape_sensor":
self.data = (
# Default
"
"
"
Current Version: 2021.12.10
Released:
January 17, 2022"
"
"
"Trying to get"
""
"
Current Time:
2022-12-22T13:15:30Z"
""
)
if self.payload == "test_scrape_sensor2":
self.data = (
# Hidden version
""
"
Hidden Version: 2021.12.10
Released:
January 17, 2022"
"
"
"Trying to get"
)
if self.payload == "test_scrape_uom_and_classes":
self.data = (
""
"
Current Temperature: 22.1
"
"
"
)
if self.payload == "test_scrape_sensor_authentication":
self.data = "secret text
"
if self.payload == "test_scrape_sensor_no_data":
self.data = None
if self.payload == "test_scrape_xml":
# XML/RSS content for testing XML parser detection via Content-Type
self.headers = {"Content-Type": "application/rss+xml"}
self.data = (
''
"Test RSS Feed"
"- Test Itemhttps://example.com/item
"
""
)
if self.payload == "test_scrape_xml_fallback":
# XML/RSS content with non-XML Content-Type for testing content-based detection
self.headers = {"Content-Type": "text/html"}
self.data = (
''
"Test RSS Feed"
"- Test Itemhttps://example.com/item
"
""
)
if self.payload == "test_scrape_html5_with_xml_declaration":
# HTML5 with XML declaration, no Content-Type header, and uppercase tags
# Tests: XML stripping, content detection, case-insensitive selectors
self.data = (
'\n'
"\n"
"Test Page"
""
"
Current Version: 2021.12.10
"
)
if self.count == 3:
self.data = None