Repository: home-assistant/core
Pull request: Add username to Reauth flow in Honeywell
<!--
  You are amazing! Thanks for contributing to our project!
  Please, DO NOT DELETE ANY TEXT from this template! (unless instructed).
-->
## Breaking change
<!--
  If your PR contains a breaking change for existing users, it is important
  to tell them what breaks, how to make it work again and why we did this.
  This piece of text is published with the release notes, so it helps if you
  write it towards our users, not us.
  Note: Remove this section if this PR is NOT a breaking change.
-->


## Proposed change
<!--
  Describe the big picture of your changes here to communicate to the
  maintainers why we should accept this pull request. If it fixes a bug
  or resolves a feature request, be sure to link to that issue in the
  additional information section.
-->
Allow user to change both username and password in rauth flow for Honeywell integration

## Type of change
<!--
  What type of change does your PR introduce to Home Assistant?
  NOTE: Please, check only 1! box!
  If your PR requires multiple boxes to be checked, you'll most likely need to
  split it into multiple PRs. This makes things easier and faster to code review.
-->

- [ ] Dependency upgrade
- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [ ] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests

## Additional information
<!--
  Details are important, and help maintainers processing your PR.
  Please be sure to fill out additional details, if applicable.
-->

- This PR fixes or closes issue: fixes #92896
- This PR is related to issue: 
- Link to documentation pull request: 

## Checklist
<!--
  Put an `x` in the boxes that apply. You can also fill these out after
  creating the PR. If you're unsure about any of them, don't hesitate to ask.
  We're here to help! This is simply a reminder of what we are going to look
  for before merging your code.
-->

- [x] The code change is tested and works locally.
- [x] Local tests pass. **Your PR cannot be merged unless tests pass**
- [x] There is no commented out code in this PR.
- [x] I have followed the [development checklist][dev-checklist]
- [x] I have followed the [perfect PR recommendations][perfect-pr]
- [x] The code has been formatted using Black (`black --fast homeassistant tests`)
- [x] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

- [ ] Documentation added/updated for [www.home-assistant.io][docs-repository]

If the code communicates with devices, web services, or third-party tools:

- [ ] The [manifest file][manifest-docs] has all fields filled out correctly.  
      Updated and included derived files by running: `python3 -m script.hassfest`.
- [ ] New or updated dependencies have been added to `requirements_all.txt`.  
      Updated by running `python3 -m script.gen_requirements_all`.
- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
- [ ] Untested files have been added to `.coveragerc`.

<!--
  This project is very active and we have a high turnover of pull requests.

  Unfortunately, the number of incoming pull requests is higher than what our
  reviewers can review and merge so there is a long backlog of pull requests
  waiting for review. You can help here!
  
  By reviewing another pull request, you will help raise the code quality of
  that pull request and the final review will be faster. This way the general
  pace of pull request reviews will go up and your wait time will go down.
  
  When picking a pull request to review, try to choose one that hasn't yet
  been reviewed.

  Thanks for helping out!
-->

To help with the load of incoming pull requests:

- [ ] I have reviewed two other [open pull requests][prs] in this repository.

[prs]: https://github.com/home-assistant/core/pulls?q=is%3Aopen+is%3Apr+-author%3A%40me+-draft%3Atrue+-label%3Awaiting-for-upstream+sort%3Acreated-desc+review%3Anone+-status%3Afailure

<!--
  Thank you for contributing <3

  Below, some useful links you could explore:
-->
[dev-checklist]: https://developers.home-assistant.io/docs/development_checklist/
[manifest-docs]: https://developers.home-assistant.io/docs/creating_integration_manifest/
[quality-scale]: https://developers.home-assistant.io/docs/integration_quality_scale_index/
[docs-repository]: https://github.com/home-assistant/home-assistant.io
[perfect-pr]: https://developers.home-assistant.io/docs/review-process/#creating-the-perfect-pr


Problem statement:
Honeywell TCC - Inability to Update Username / Only Password
### The problem

Trying to change the email address for my Honeywell TCC, but cannot find a way to do this via the GUI nor config files. Should be an option via the integration GUI.

### What version of Home Assistant Core has the issue?

2023.4.6

### What was the last working version of Home Assistant Core?

_No response_

### What type of installation are you running?

Home Assistant Container

### Integration causing the issue

Honeywell TCC

### Link to integration documentation on our website

_No response_

### Diagnostics information

_No response_

### Example YAML snippet

_No response_

### Anything in the logs that might be useful for us?

_No response_

### Additional information

_No response_

Hints from issue discussion:
Hey there @rdfurman, @mkmer, mind taking a look at this issue as it has been labeled with an integration (`honeywell`) you are listed as a [code owner](https://github.com/home-assistant/core/blob/dev/CODEOWNERS#L538) for? Thanks!

<details>
<summary>Code owner commands</summary>


Code owners of `honeywell` can trigger bot actions by commenting:

- `@home-assistant close` Closes the issue.
- `@home-assistant rename Awesome new title` Renames the issue.
- `@home-assistant reopen` Reopen the issue.
- `@home-assistant unassign honeywell` Removes the current integration label and assignees on the issue, add the integration domain after the command.

</details>


<sub><sup>(message by CodeOwnersMention)</sup></sub>

---

[honeywell documentation](https://www.home-assistant.io/integrations/honeywell)
[honeywell source](https://github.com/home-assistant/core/tree/dev/homeassistant/components/honeywell)
<sub><sup>(message by IssueLinks)</sup></sub>
If you need to change the user name, remove the integration and re-add it.  I was told this is "normal" behavior for HA when I added the reauth flow.
Not all integrations work this way. You should be able to update
credentials, including a username/email. Having this possible in the GUI
would be very helpful.

On Mon, Jul 17, 2023 at 9:38 AM mkmer ***@***.***> wrote:

> If you need to change the user name, remove the integration and re-add it.
> I was told this is "normal" behavior for HA when I added the reauth flow.
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/home-assistant/core/issues/92896#issuecomment-1638165229>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AKAGRDBBG3TH3IUK777W7B3XQU55VANCNFSM6AAAAAAX435DGQ>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>

When you set up an integration, devices and configuration are made for your account (or device), also things like unique ids. When you want to change that, you would need to change every reference to that old account and add it again. And that can become a very fragile part of the application. (What if some devices don't remove correctly, or if your references don't change) You would end up with an integration with half account A and half account B. So with this in mind, it's always consistent to remove and re-add it again.
Turns out the username is not part of the unique ID, changing will not pose an issue for this integration.
Excellent news. Pretty sure I resolved this myself by modifying the
underlying json file where the username is stored. The use case I have is
that for testing, I use one email address. However, for home automation, I
use another email address. With many migrating from cable to fiber in the
next few years, there may be additional email changes for individuals. So
having the ability to update an email address for an integration, generally
the username for login, is a good idea. In many cases, the actual 3rd
party, Honeywell lets you update the email address via the web GUI,
Airthings requires a support request and support does it for you, and
Ecobee just doesnt appear to support it. Better to be able to do this via
the GUI than having to muck around with underlying files ;)

On Tue, Jul 18, 2023 at 10:21 AM mkmer ***@***.***> wrote:

> Turns out the username is not part of the unique ID, changing will not
> pose an issue for this integration.
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/home-assistant/core/issues/92896#issuecomment-1640323985>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AKAGRDDLWWWE7PIGXQFRZLLXQ2LWVANCNFSM6AAAAAAX435DGQ>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>

Commit message: Use add_suggested_value_to_schema

Patch to review:
diff --git a/homeassistant/components/honeywell/config_flow.py b/homeassistant/components/honeywell/config_flow.py
index 8b24fc912f1796..af0f217a923a3f 100644
--- a/homeassistant/components/honeywell/config_flow.py
+++ b/homeassistant/components/honeywell/config_flow.py
@@ -22,7 +22,12 @@
     DOMAIN,
 )
 
-REAUTH_SCHEMA = vol.Schema({vol.Required(CONF_PASSWORD): str})
+REAUTH_SCHEMA = vol.Schema(
+    {
+        vol.Required(CONF_USERNAME): str,
+        vol.Required(CONF_PASSWORD): str,
+    }
+)
 
 
 class HoneywellConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
@@ -42,12 +47,12 @@ async def async_step_reauth_confirm(
     ) -> FlowResult:
         """Confirm re-authentication with Honeywell."""
         errors: dict[str, str] = {}
-
+        assert self.entry is not None
         if user_input:
-            assert self.entry is not None
+            username = user_input[CONF_USERNAME]
             password = user_input[CONF_PASSWORD]
             data = {
-                CONF_USERNAME: self.entry.data[CONF_USERNAME],
+                CONF_USERNAME: username,
                 CONF_PASSWORD: password,
             }
 
@@ -71,6 +76,7 @@ async def async_step_reauth_confirm(
                     self.entry,
                     data={
                         **self.entry.data,
+                        CONF_USERNAME: username,
                         CONF_PASSWORD: password,
                     },
                 )
@@ -79,7 +85,9 @@ async def async_step_reauth_confirm(
 
         return self.async_show_form(
             step_id="reauth_confirm",
-            data_schema=REAUTH_SCHEMA,
+            data_schema=self.add_suggested_values_to_schema(
+                REAUTH_SCHEMA, self.entry.data
+            ),
             errors=errors,
         )
 
diff --git a/tests/components/honeywell/test_config_flow.py b/tests/components/honeywell/test_config_flow.py
index a416f030a05a60..25ffa0a60936f7 100644
--- a/tests/components/honeywell/test_config_flow.py
+++ b/tests/components/honeywell/test_config_flow.py
@@ -156,14 +156,14 @@ async def test_reauth_flow(hass: HomeAssistant) -> None:
     ):
         result2 = await hass.config_entries.flow.async_configure(
             result["flow_id"],
-            {CONF_PASSWORD: "new-password"},
+            {CONF_USERNAME: "new-username", CONF_PASSWORD: "new-password"},
         )
         await hass.async_block_till_done()
 
     assert result2["type"] == FlowResultType.ABORT
     assert result2["reason"] == "reauth_successful"
     assert mock_entry.data == {
-        CONF_USERNAME: "test-username",
+        CONF_USERNAME: "new-username",
         CONF_PASSWORD: "new-password",
     }
 
@@ -200,7 +200,7 @@ async def test_reauth_flow_auth_error(hass: HomeAssistant, client: MagicMock) ->
     ):
         result2 = await hass.config_entries.flow.async_configure(
             result["flow_id"],
-            {CONF_PASSWORD: "new-password"},
+            {CONF_USERNAME: "new-username", CONF_PASSWORD: "new-password"},
         )
         await hass.async_block_till_done()
 
@@ -246,7 +246,7 @@ async def test_reauth_flow_connnection_error(
 
     result2 = await hass.config_entries.flow.async_configure(
         result["flow_id"],
-        {CONF_PASSWORD: "new-password"},
+        {CONF_USERNAME: "new-username", CONF_PASSWORD: "new-password"},
     )
     await hass.async_block_till_done()
 


Code review comments:
diff_hunk: @@ -71,6 +76,7 @@ async def async_step_reauth_confirm(
                     self.entry,
                     data={
                         **self.entry.data,
+                        CONF_USERNAME: username,
                         CONF_PASSWORD: password,
path: homeassistant/components/honeywell/config_flow.py
text: @user1:
```suggestion
                        **user_input,
```

diff_hunk: @@ -42,12 +47,12 @@ async def async_step_reauth_confirm(
     ) -> FlowResult:
         """Confirm re-authentication with Honeywell."""
         errors: dict[str, str] = {}
-
+        assert self.entry is not None
         if user_input:
-            assert self.entry is not None
+            username = user_input[CONF_USERNAME]
             password = user_input[CONF_PASSWORD]
             data = {
-                CONF_USERNAME: self.entry.data[CONF_USERNAME],
+                CONF_USERNAME: username,
                 CONF_PASSWORD: password,
             }
path: homeassistant/components/honeywell/config_flow.py
text: @user1:
We want data to be exactly like user input now right? So I think we can remove the creation of an intermediary object.

When I look at the rest of the code, we only use it in the `self.is_valid` call and not even at the async update entry. 

@author:
Makes sense.