-
-
Notifications
You must be signed in to change notification settings - Fork 38.2k
Deprecate imap_content_sensor #90429
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
Merged
MartinHjelmare
merged 13 commits into
home-assistant:dev
from
jbouwh:deprecate-imap_email_content
Apr 3, 2023
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
370aed8
Deprecate imap_content_sensor
jbouwh 35341ae
Rename unique_id to issue_id
jbouwh 294f1f6
Migrate config to imap entry
jbouwh f6ff0b1
Improve dialogs
jbouwh 2bf0577
Improve dialog texts
jbouwh 272a312
Add repairs.py to .coveragerc
jbouwh 7a8cb68
Test the integration component setup
jbouwh aad44d1
Text tweak
jbouwh 5e6ea33
Use flow for creating entries
jbouwh f69c430
Rename schema add tests
jbouwh 7a55de1
Patch client instead
jbouwh 07b61b8
Add tests repairs - refactor async_step_confirm
jbouwh 2e6a2af
Comments test, correct calling next step
jbouwh 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
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 +1,12 @@ | ||
| """The imap_email_content component.""" | ||
|
|
||
| from homeassistant.const import Platform | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.typing import ConfigType | ||
|
|
||
| PLATFORMS = [Platform.SENSOR] | ||
|
|
||
|
|
||
| async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: | ||
| """Set up imap_email_content.""" | ||
| return True |
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,13 @@ | ||
| """Constants for the imap email content integration.""" | ||
|
|
||
| DOMAIN = "imap_email_content" | ||
|
|
||
| CONF_SERVER = "server" | ||
| CONF_SENDERS = "senders" | ||
| CONF_FOLDER = "folder" | ||
|
|
||
| ATTR_FROM = "from" | ||
| ATTR_BODY = "body" | ||
| ATTR_SUBJECT = "subject" | ||
|
|
||
| DEFAULT_PORT = 993 |
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 |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| """Repair flow for imap email content integration.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| import voluptuous as vol | ||
| import yaml | ||
|
|
||
| from homeassistant import data_entry_flow | ||
| from homeassistant.components.imap import DOMAIN as IMAP_DOMAIN | ||
| from homeassistant.components.repairs import RepairsFlow | ||
| from homeassistant.config_entries import SOURCE_IMPORT | ||
| from homeassistant.const import ( | ||
| CONF_NAME, | ||
| CONF_PASSWORD, | ||
| CONF_PORT, | ||
| CONF_USERNAME, | ||
| CONF_VALUE_TEMPLATE, | ||
| ) | ||
| from homeassistant.core import HomeAssistant, callback | ||
| from homeassistant.data_entry_flow import FlowResultType | ||
| from homeassistant.helpers import issue_registry as ir | ||
| from homeassistant.helpers.typing import ConfigType | ||
|
|
||
| from .const import CONF_FOLDER, CONF_SENDERS, CONF_SERVER, DOMAIN | ||
|
|
||
|
|
||
| async def async_process_issue(hass: HomeAssistant, config: ConfigType) -> None: | ||
| """Register an issue and suggest new config.""" | ||
|
|
||
| name: str = config.get(CONF_NAME) or config[CONF_USERNAME] | ||
|
|
||
| issue_id = ( | ||
| f"{name}_{config[CONF_USERNAME]}_{config[CONF_SERVER]}_{config[CONF_FOLDER]}" | ||
| ) | ||
|
|
||
| if CONF_VALUE_TEMPLATE in config: | ||
| template: str = config[CONF_VALUE_TEMPLATE].template | ||
| template = template.replace("subject", 'trigger.event.data["subject"]') | ||
| template = template.replace("from", 'trigger.event.data["sender"]') | ||
| template = template.replace("date", 'trigger.event.data["date"]') | ||
| template = template.replace("body", 'trigger.event.data["text"]') | ||
| else: | ||
| template = '{{ trigger.event.data["subject"] }}' | ||
|
|
||
| template_sensor_config: ConfigType = { | ||
| "template": [ | ||
| { | ||
| "trigger": [ | ||
| { | ||
| "id": "custom_event", | ||
| "platform": "event", | ||
| "event_type": "imap_content", | ||
| "event_data": {"sender": config[CONF_SENDERS][0]}, | ||
| } | ||
| ], | ||
| "sensor": [ | ||
| { | ||
| "state": template, | ||
| "name": name, | ||
| } | ||
| ], | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| data = { | ||
| CONF_SERVER: config[CONF_SERVER], | ||
| CONF_PORT: config[CONF_PORT], | ||
| CONF_USERNAME: config[CONF_USERNAME], | ||
| CONF_PASSWORD: config[CONF_PASSWORD], | ||
| CONF_FOLDER: config[CONF_FOLDER], | ||
| } | ||
| data[CONF_VALUE_TEMPLATE] = template | ||
| data[CONF_NAME] = name | ||
| placeholders = {"yaml_example": yaml.dump(template_sensor_config)} | ||
| placeholders.update(data) | ||
|
|
||
| ir.async_create_issue( | ||
| hass, | ||
| DOMAIN, | ||
| issue_id, | ||
| breaks_in_ha_version="2023.10.0", | ||
| is_fixable=True, | ||
| severity=ir.IssueSeverity.WARNING, | ||
| translation_key="migration", | ||
| translation_placeholders=placeholders, | ||
| data=data, | ||
| ) | ||
|
|
||
|
|
||
| class DeprecationRepairFlow(RepairsFlow): | ||
| """Handler for an issue fixing flow.""" | ||
|
|
||
| def __init__(self, issue_id: str, config: ConfigType) -> None: | ||
| """Create flow.""" | ||
| self._name: str = config[CONF_NAME] | ||
| self._config: dict[str, Any] = config | ||
| self._issue_id = issue_id | ||
| super().__init__() | ||
|
|
||
| async def async_step_init( | ||
| self, user_input: dict[str, str] | None = None | ||
| ) -> data_entry_flow.FlowResult: | ||
| """Handle the first step of a fix flow.""" | ||
| return await self.async_step_start() | ||
|
|
||
| @callback | ||
| def _async_get_placeholders(self) -> dict[str, str] | None: | ||
| issue_registry = ir.async_get(self.hass) | ||
| description_placeholders = None | ||
| if issue := issue_registry.async_get_issue(self.handler, self.issue_id): | ||
| description_placeholders = issue.translation_placeholders | ||
|
|
||
| return description_placeholders | ||
|
|
||
| async def async_step_start( | ||
| self, user_input: dict[str, str] | None = None | ||
| ) -> data_entry_flow.FlowResult: | ||
| """Wait for the user to start the config migration.""" | ||
| placeholders = self._async_get_placeholders() | ||
| if user_input is None: | ||
| return self.async_show_form( | ||
| step_id="start", | ||
| data_schema=vol.Schema({}), | ||
| description_placeholders=placeholders, | ||
| ) | ||
|
|
||
| return self.async_show_form( | ||
| step_id="confirm", | ||
| data_schema=vol.Schema({}), | ||
| description_placeholders=placeholders, | ||
| ) | ||
|
|
||
| async def async_step_confirm( | ||
| self, user_input: dict[str, str] | None = None | ||
| ) -> data_entry_flow.FlowResult: | ||
| """Handle the confirm step of a fix flow.""" | ||
| if user_input is not None: | ||
| placeholders = self._async_get_placeholders() | ||
| user_input[CONF_NAME] = self._name | ||
| result = await self.hass.config_entries.flow.async_init( | ||
| IMAP_DOMAIN, context={"source": SOURCE_IMPORT}, data=self._config | ||
| ) | ||
| if result["type"] == FlowResultType.ABORT: | ||
| ir.async_delete_issue(self.hass, DOMAIN, self._issue_id) | ||
| ir.async_create_issue( | ||
| self.hass, | ||
| DOMAIN, | ||
| self._issue_id, | ||
| breaks_in_ha_version="2023.10.0", | ||
| is_fixable=False, | ||
| severity=ir.IssueSeverity.WARNING, | ||
| translation_key="deprecation", | ||
| translation_placeholders=placeholders, | ||
| data=self._config, | ||
| learn_more_url="https://www.home-assistant.io/integrations/imap/#using-events", | ||
| ) | ||
| return self.async_abort(reason=result["reason"]) | ||
| return self.async_create_entry( | ||
| title="", | ||
| data={}, | ||
| ) | ||
|
|
||
| return self.async_show_form( | ||
| step_id="confirm", | ||
| data_schema=vol.Schema({}), | ||
| description_placeholders=self._config, | ||
| ) | ||
|
|
||
|
|
||
| async def async_create_fix_flow( | ||
| hass: HomeAssistant, | ||
| issue_id: str, | ||
| data: dict[str, str | int | float | None], | ||
| ) -> RepairsFlow: | ||
| """Create flow.""" | ||
| return DeprecationRepairFlow(issue_id, data) | ||
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| { | ||
| "issues": { | ||
| "deprecation": { | ||
| "title": "The IMAP email content integration is deprecated", | ||
| "description": "The IMAP email content integration is deprecated. Your IMAP server configuration was already migrated to to the [imap integration](https://my.home-assistant.io/redirect/config_flow_start?domain=imap). To set up a sensor for the IMAP email content, set up a template sensor with the config:\n\n```yaml\n{yaml_example}```\n\nPlease remove the deprecated `imap_email_plaform` sensor configuration from your `configuration.yaml`.\n\nNote that the event filter only filters on the first of the configured allowed senders, customize the filter if needed.\n\nYou can skip this part if you have already set up a template sensor." | ||
| }, | ||
| "migration": { | ||
| "title": "The IMAP email content integration needs attention", | ||
| "fix_flow": { | ||
| "step": { | ||
| "start": { | ||
| "title": "Migrate your IMAP email configuration", | ||
| "description": "The IMAP email content integration is deprecated. Your IMAP server configuration can be migrated automatically to the [imap integration](https://my.home-assistant.io/redirect/config_flow_start?domain=imap), this will enable using a custom `imap` event trigger. To set up a sensor that has an IMAP content state, a template sensor can be used. Remove the `imap_email_plaform` sensor configuration from your `configuration.yaml` after migration.\n\nSubmit to start migration of your IMAP server configuration to the `imap` integration." | ||
| }, | ||
| "confirm": { | ||
| "title": "Your IMAP server settings will be migrated", | ||
| "description": "In this step an `imap` config entry will be set up with the following configuration:\n\n```text\nServer\t{server}\nPort\t{port}\nUsername\t{username}\nPassword\t*****\nFolder\t{folder}\n```\n\nSee also: (https://www.home-assistant.io/integrations/imap/)\n\nFitering configuration on allowed `sender` is part of the template sensor config that can copied and placed in your `configuration.yaml.\n\nNote that the event filter only filters on the first of the configured allowed senders, customize the filter if needed.\n\n```yaml\n{yaml_example}```\nDo not forget to cleanup the your `configuration.yaml` after migration.\n\nSubmit to migrate your IMAP server configuration to an `imap` configuration entry." | ||
| } | ||
| }, | ||
| "abort": { | ||
| "already_configured": "The IMAP server config was already migrated to the imap integration. Remove the `imap_email_plaform` sensor configuration from your `configuration.yaml`.", | ||
| "cannot_connect": "Migration failed. Failed to connect to the IMAP server. Perform a manual migration." | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.