Repository: home-assistant/core
Pull request: Handle empty zeroconf properties in devolo_home_network
<!--
  You are amazing! Thanks for contributing to our project!
  Please, DO NOT DELETE ANY TEXT from this template! (unless instructed).
-->
## 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.
-->
Since the last firmware update of the devolo Magic LAN devices the properties dict from the zeroconf service info can be empty. I assume it is a firmware issue, but I couldn't convince devolo, yet. Nevertheless I can handle that case to avoid exceptions in the users log. In my tests the devices could still be properly discovered and added. 

## 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)
- [ ] 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 #70477
- 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] 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:

- [x] 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`.

The integration reached or maintains the following [Integration Quality Scale][quality-scale]:
<!--
  The Integration Quality Scale scores an integration on the code quality
  and user experience. Each level of the quality scale consists of a list
  of requirements. We highly recommend getting your integration scored!
-->

- [ ] No score or internal
- [ ] 🥈 Silver
- [ ] 🥇 Gold
- [x] 🏆 Platinum

<!--
  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/en/development_checklist.html
[manifest-docs]: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
[quality-scale]: https://developers.home-assistant.io/docs/en/next/integration_quality_scale_index.html
[docs-repository]: https://github.com/home-assistant/home-assistant.io


Problem statement:
Error doing job: Task exception was never retrieved / devolo_home_network
### The problem

HA 2022.4 is showing an error related to devolo home network even no devolo integration has been configured:

```
Logger: homeassistant
Source: components/devolo_home_network/config_flow.py:81
First occurred: 2:02:48 PM (1 occurrences)
Last logged: 2:02:48 PM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/discovery_flow.py", line 74, in _async_process_pending_flows
    await gather_with_concurrency(
  File "/usr/src/homeassistant/homeassistant/util/async_.py", line 192, in gather_with_concurrency
    return await gather(
  File "/usr/src/homeassistant/homeassistant/util/async_.py", line 190, in sem_task
    return await task
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 205, in async_init
    flow, result = await task
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 232, in _async_init
    result = await self._async_handle_step(flow, flow.init_step, data, init_done)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 335, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/usr/src/homeassistant/homeassistant/components/devolo_home_network/config_flow.py", line 81, in async_step_zeroconf
    if discovery_info.properties["MT"] in ["2600", "2601"]:
KeyError: 'MT'

```

Best regards, Bastian

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

2022.4.6

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

_No response_

### What type of installation are you running?

Home Assistant OS

### Integration causing the issue

devolo_home_network

### 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?

```txt
Logger: homeassistant
Source: components/devolo_home_network/config_flow.py:81
First occurred: 2:02:48 PM (1 occurrences)
Last logged: 2:02:48 PM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/discovery_flow.py", line 74, in _async_process_pending_flows
    await gather_with_concurrency(
  File "/usr/src/homeassistant/homeassistant/util/async_.py", line 192, in gather_with_concurrency
    return await gather(
  File "/usr/src/homeassistant/homeassistant/util/async_.py", line 190, in sem_task
    return await task
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 205, in async_init
    flow, result = await task
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 232, in _async_init
    result = await self._async_handle_step(flow, flow.init_step, data, init_done)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 335, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
  File "/usr/src/homeassistant/homeassistant/components/devolo_home_network/config_flow.py", line 81, in async_step_zeroconf
    if discovery_info.properties["MT"] in ["2600", "2601"]:
KeyError: 'MT'
```


### Additional information

_No response_

Hints from issue discussion:
[devolo_home_network documentation](https://www.home-assistant.io/integrations/devolo_home_network)
[devolo_home_network source](https://github.com/home-assistant/core/tree/dev/homeassistant/components/devolo_home_network)
<sub><sup>(message by IssueLinks)</sup></sub>
Hey there @2fake, @shutgun, mind taking a look at this issue as it has been labeled with an integration (`devolo_home_network`) you are listed as a [code owner](https://github.com/home-assistant/core/blob/dev/CODEOWNERS#L225) for? Thanks!
<sub><sup>(message by CodeOwnersMention)</sup></sub>
Hi @Bastian007 , am I right when I assume, that there are devolo devices in your network even though you are not using the integration? If so, could you give me the device types and firmware versions you are using?
Hi @Shutgun, sorry I missed to mention that I have indeed devolo devices within my network.
Following are currently available:
- Magic 2 LAN DINrail, firmware 7.12.3.112
- Magic 2 WiFi 6, firmware 5.9.5
Best regards, Bastian
I was able to reproduce the issue. I'm pretty sure it's a firmware bug of 7.12.3.112 your DINrail runs. I'll talk to my contacts at devolo and come back to you asap.

Commit message: Handle empty zeroconf properties in devolo_home_network

Patch to review:
diff --git a/homeassistant/components/devolo_home_network/config_flow.py b/homeassistant/components/devolo_home_network/config_flow.py
index c96126f43e2af..3ba870d79d286 100644
--- a/homeassistant/components/devolo_home_network/config_flow.py
+++ b/homeassistant/components/devolo_home_network/config_flow.py
@@ -78,6 +78,12 @@ async def async_step_zeroconf(
         self, discovery_info: zeroconf.ZeroconfServiceInfo
     ) -> FlowResult:
         """Handle zeroconf discovery."""
+        if (
+            "MT" not in discovery_info.properties
+            or "SN" not in discovery_info.properties
+        ):
+            return self.async_abort(reason="incomplete_query")
+
         if discovery_info.properties["MT"] in ["2600", "2601"]:
             return self.async_abort(reason="home_control")
 
@@ -86,7 +92,7 @@ async def async_step_zeroconf(
 
         self.context[CONF_HOST] = discovery_info.host
         self.context["title_placeholders"] = {
-            PRODUCT: discovery_info.properties["Product"],
+            PRODUCT: discovery_info.properties.get("Product", ""),
             CONF_NAME: discovery_info.hostname.split(".")[0],
         }
 
diff --git a/tests/components/devolo_home_network/const.py b/tests/components/devolo_home_network/const.py
index 0e48833a78b1a..cfacc3cd1fb71 100644
--- a/tests/components/devolo_home_network/const.py
+++ b/tests/components/devolo_home_network/const.py
@@ -43,10 +43,21 @@
     hostname="mock_hostname",
     name="mock_name",
     port=None,
-    properties={"MT": "2600"},
+    properties={"MT": "2600", "SN": "1234567890"},
     type="mock_type",
 )
 
+DISCOVERY_INFO_INCOMPLETE_QUERY = zeroconf.ZeroconfServiceInfo(
+    host="mock_host",
+    addresses=["mock_host"],
+    hostname="mock_hostname",
+    name="mock_name",
+    port=None,
+    properties={},
+    type="mock_type",
+)
+
+
 NEIGHBOR_ACCESS_POINTS = {
     "neighbor_aps": [
         {
diff --git a/tests/components/devolo_home_network/test_config_flow.py b/tests/components/devolo_home_network/test_config_flow.py
index 7cd1ba5222c63..6d3ee79c9c2d6 100644
--- a/tests/components/devolo_home_network/test_config_flow.py
+++ b/tests/components/devolo_home_network/test_config_flow.py
@@ -22,7 +22,12 @@
     RESULT_TYPE_FORM,
 )
 
-from .const import DISCOVERY_INFO, DISCOVERY_INFO_WRONG_DEVICE, IP
+from .const import (
+    DISCOVERY_INFO,
+    DISCOVERY_INFO_INCOMPLETE_QUERY,
+    DISCOVERY_INFO_WRONG_DEVICE,
+    IP,
+)
 
 
 async def test_form(hass: HomeAssistant, info: dict[str, Any]):
@@ -118,6 +123,17 @@ async def test_zeroconf(hass: HomeAssistant):
     }
 
 
+async def test_abort_zeroconf_incomplete_query(hass: HomeAssistant):
+    """Test we abort zeroconf on incomplete queries."""
+    result = await hass.config_entries.flow.async_init(
+        DOMAIN,
+        context={"source": config_entries.SOURCE_ZEROCONF},
+        data=DISCOVERY_INFO_INCOMPLETE_QUERY,
+    )
+    assert result["type"] == RESULT_TYPE_ABORT
+    assert result["reason"] == "incomplete_query"
+
+
 async def test_abort_zeroconf_wrong_device(hass: HomeAssistant):
     """Test we abort zeroconf for wrong devices."""
     result = await hass.config_entries.flow.async_init(


Code review comments:
diff_hunk: @@ -78,6 +78,12 @@ async def async_step_zeroconf(
         self, discovery_info: zeroconf.ZeroconfServiceInfo
     ) -> FlowResult:
         """Handle zeroconf discovery."""
+        if (
+            "MT" not in discovery_info.properties
path: homeassistant/components/devolo_home_network/config_flow.py
text: @user1:
It's not totally clear to me but perhaps we can update the manifest to require a match on these keys?

https://developers.home-assistant.io/docs/creating_integration_manifest#zeroconf

https://github.com/home-assistant/core/blob/309d8d70b1d431078d4e1ef03d7deeadc96be90e/homeassistant/components/zeroconf/__init__.py#L341-L347

@author:
I tried it by adding 

```
"zeroconf": [{"type":"_dvl-deviceapi._tcp.local.", "properties":{"MT":"*"}}],
```

but I still run into the case, where the properties are empty.

@user1:
And you ran the hassfest script to update the generated zeroconf.py file before testing?

@author:
don't tell anybody ....

I changed it to that approach. Thanks for the hint!