-
Notifications
You must be signed in to change notification settings - Fork 39
Add config flow & config entry support #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iantrich
wants to merge
8
commits into
master
Choose a base branch
from
modernization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8f1720a
Add config flow & config entry support
iantrich c658333
Add platform-only CONFIG_SCHEMA and tidy manifest
iantrich af84c31
Fix manifest.json by removing duplicate domain and name entries
iantrich 4a9ba7e
Update workflow actions and dependencies for modernization
iantrich d051557
Refactor config flow and sensor code to improve type handling and cla…
iantrich c6d8b5f
Refactor config flow type hints and ensure JSON files end with a newline
iantrich df8e48c
Fix JSON files by adding missing newlines and refactor sensor entry h…
iantrich 1323e8c
Refactor ruff configuration and clean up feedsource.py comments for c…
iantrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| { | ||
| "name": "custom-components/feedparser", | ||
| "image": "mcr.microsoft.com/devcontainers/python:3.13", | ||
| "postCreateCommand": "bash scripts/setup", | ||
| "forwardPorts": [8123], | ||
| "portsAttributes": { | ||
| "8123": { | ||
| "label": "Home Assistant", | ||
| "onAutoForward": "notify" | ||
| } | ||
| }, | ||
| "customizations": { | ||
| "vscode": { | ||
| "extensions": [ | ||
| "charliermarsh.ruff", | ||
| "github.vscode-pull-request-github", | ||
| "ms-python.python", | ||
| "ms-python.vscode-pylance", | ||
| "ryanluker.vscode-coverage-gutters" | ||
| ], | ||
| "settings": { | ||
| "files.eol": "\n", | ||
| "editor.tabSize": 4, | ||
| "editor.formatOnPaste": true, | ||
| "editor.formatOnSave": true, | ||
| "editor.formatOnType": false, | ||
| "files.trimTrailingWhitespace": true, | ||
| "python.analysis.typeCheckingMode": "basic", | ||
| "python.analysis.autoImportCompletions": true, | ||
| "python.defaultInterpreterPath": "/usr/local/bin/python", | ||
| "[python]": { | ||
| "editor.defaultFormatter": "charliermarsh.ruff" | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "remoteUser": "vscode", | ||
| "features": { | ||
| "ghcr.io/devcontainers/features/python:1": { | ||
| "version": "3.13" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,133 @@ | ||
| """A component which allows you to parse an RSS feed into a sensor. | ||
| """The Feedparser integration.""" | ||
|
|
||
| For more details about this component, please refer to the documentation at | ||
| https://github.com/custom-components/sensor.feedparser | ||
| from __future__ import annotations | ||
|
|
||
| Following spec from https://validator.w3.org/feed/docs/rss2.html | ||
| """ | ||
| from datetime import timedelta | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
| from .const import ( | ||
| CONF_DATE_FORMAT, | ||
| CONF_EXCLUSIONS, | ||
| CONF_INCLUSIONS, | ||
| CONF_LOCAL_TIME, | ||
| CONF_REMOVE_SUMMARY_IMAGE, | ||
| CONF_SCAN_INTERVAL, | ||
| CONF_SHOW_TOPN, | ||
| DEFAULT_DATE_FORMAT, | ||
| DEFAULT_LOCAL_TIME, | ||
| DEFAULT_REMOVE_SUMMARY_IMAGE, | ||
| DEFAULT_SCAN_INTERVAL, | ||
| DEFAULT_TOPN, | ||
| DOMAIN, | ||
| ENTRY_VERSION, | ||
| OPTION_KEYS, | ||
| PLATFORMS, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant | ||
|
|
||
|
|
||
| CONFIG_SCHEMA = cv.platform_only_config_schema(DOMAIN) | ||
|
|
||
|
|
||
| def _normalize_list(value: object) -> list[str]: | ||
| """Normalize option values to list[str].""" | ||
| if isinstance(value, list): | ||
| return [item for item in value if isinstance(item, str)] | ||
| if isinstance(value, str): | ||
| return [item.strip() for item in value.split(",") if item.strip()] | ||
| return [] | ||
|
|
||
|
|
||
| def _normalize_scan_interval(value: object) -> dict[str, int]: | ||
| """Normalize scan_interval to {'hours': int, 'minutes': int}.""" | ||
| if isinstance(value, timedelta): | ||
| total_minutes = max(1, int(value.total_seconds() // 60)) | ||
| return { | ||
| "hours": total_minutes // 60, | ||
| "minutes": total_minutes % 60, | ||
| } | ||
|
|
||
| if isinstance(value, dict): | ||
| raw_hours = value.get("hours") | ||
| raw_minutes = value.get("minutes") | ||
|
|
||
| hours = raw_hours if isinstance(raw_hours, int) else 0 | ||
| minutes = raw_minutes if isinstance(raw_minutes, int) else 0 | ||
| total_minutes = max(1, (hours * 60) + minutes) | ||
| return { | ||
| "hours": total_minutes // 60, | ||
| "minutes": total_minutes % 60, | ||
| } | ||
|
|
||
| default_minutes = int(DEFAULT_SCAN_INTERVAL.total_seconds() // 60) | ||
| return { | ||
| "hours": default_minutes // 60, | ||
| "minutes": default_minutes % 60, | ||
| } | ||
|
|
||
|
|
||
| async def async_setup(hass: HomeAssistant, config: dict) -> bool: # noqa: ARG001 | ||
| """Set up Feedparser from YAML (legacy path).""" | ||
| return True | ||
|
|
||
|
|
||
| async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Set up Feedparser from a config entry.""" | ||
| await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) | ||
| return True | ||
|
|
||
|
|
||
| async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Unload a config entry.""" | ||
| return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) | ||
|
|
||
|
|
||
| async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: | ||
| """Reload config entry.""" | ||
| await async_unload_entry(hass, entry) | ||
| await async_setup_entry(hass, entry) | ||
|
|
||
|
|
||
| async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
| """Migrate old config entries to the latest schema.""" | ||
| if entry.version > ENTRY_VERSION: | ||
| return False | ||
|
|
||
| if entry.version < ENTRY_VERSION: | ||
| data = dict(entry.data) | ||
| options = dict(entry.options) | ||
|
|
||
| merged = {**data, **options} | ||
| normalized_options = { | ||
| CONF_DATE_FORMAT: str(merged.get(CONF_DATE_FORMAT, DEFAULT_DATE_FORMAT)), | ||
| CONF_LOCAL_TIME: bool(merged.get(CONF_LOCAL_TIME, DEFAULT_LOCAL_TIME)), | ||
| CONF_SCAN_INTERVAL: _normalize_scan_interval( | ||
| merged.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL), | ||
| ), | ||
| CONF_SHOW_TOPN: int(merged.get(CONF_SHOW_TOPN, DEFAULT_TOPN)), | ||
| CONF_REMOVE_SUMMARY_IMAGE: bool( | ||
| merged.get( | ||
| CONF_REMOVE_SUMMARY_IMAGE, | ||
| DEFAULT_REMOVE_SUMMARY_IMAGE, | ||
| ), | ||
| ), | ||
| CONF_INCLUSIONS: _normalize_list(merged.get(CONF_INCLUSIONS, [])), | ||
| CONF_EXCLUSIONS: _normalize_list(merged.get(CONF_EXCLUSIONS, [])), | ||
| } | ||
|
|
||
| for key in OPTION_KEYS: | ||
| data.pop(key, None) | ||
|
|
||
| hass.config_entries.async_update_entry( | ||
| entry, | ||
| data=data, | ||
| options=normalized_options, | ||
| version=ENTRY_VERSION, | ||
| ) | ||
|
|
||
| return True | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the migration function worth covering by a test?