Skip to content

Commit

Permalink
Revert "Disable sky connect config entry if USB stick is not plugged …
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Jan 4, 2023
1 parent 0239938 commit 22dbbd4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
get_addon_manager,
get_zigbee_socket,
)
from homeassistant.config_entries import ConfigEntry, ConfigEntryDisabler
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady

Expand Down Expand Up @@ -75,12 +75,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

if not usb.async_is_plugged_in(hass, matcher):
# The USB dongle is not plugged in
hass.async_create_task(
hass.config_entries.async_set_disabled_by(
entry.entry_id, ConfigEntryDisabler.INTEGRATION
)
)
return False
raise ConfigEntryNotReady

addon_info = await _multi_pan_addon_info(hass, entry)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from homeassistant.components import usb
from homeassistant.components.homeassistant_hardware import silabs_multiprotocol_addon
from homeassistant.config_entries import ConfigEntry, ConfigEntryDisabler, ConfigFlow
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult

Expand Down Expand Up @@ -35,12 +35,7 @@ async def async_step_usb(self, discovery_info: usb.UsbServiceInfo) -> FlowResult
manufacturer = discovery_info.manufacturer
description = discovery_info.description
unique_id = f"{vid}:{pid}_{serial_number}_{manufacturer}_{description}"
if existing_entry := await self.async_set_unique_id(unique_id):
# Re-enable existing config entry which was disabled by USB unplug
if existing_entry.disabled_by == ConfigEntryDisabler.INTEGRATION:
await self.hass.config_entries.async_set_disabled_by(
existing_entry.entry_id, None
)
if await self.async_set_unique_id(unique_id):
self._abort_if_unique_id_configured(updates={"device": device})
return self.async_create_entry(
title="Home Assistant Sky Connect",
Expand Down
1 change: 0 additions & 1 deletion homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ class ConfigEntryChange(StrEnum):
class ConfigEntryDisabler(StrEnum):
"""What disabled a config entry."""

INTEGRATION = "integration"
USER = "user"


Expand Down
41 changes: 4 additions & 37 deletions tests/components/homeassistant_sky_connect/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

import pytest

from homeassistant.components import usb, zha
from homeassistant.components import zha
from homeassistant.components.hassio.handler import HassioAPIError
from homeassistant.components.homeassistant_sky_connect.const import DOMAIN
from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from tests.common import MockConfigEntry

Expand Down Expand Up @@ -245,22 +244,14 @@ async def test_setup_zha_multipan_other_device(
assert config_entry.title == CONFIG_ENTRY_DATA["description"]


async def test_setup_entry_wait_usb(
mock_zha_config_flow_setup, hass: HomeAssistant
) -> None:
async def test_setup_entry_wait_usb(hass: HomeAssistant) -> None:
"""Test setup of a config entry when the dongle is not plugged in."""
# Setup the config entry
vid = CONFIG_ENTRY_DATA["vid"]
pid = CONFIG_ENTRY_DATA["device"]
serial_number = CONFIG_ENTRY_DATA["serial_number"]
manufacturer = CONFIG_ENTRY_DATA["manufacturer"]
description = CONFIG_ENTRY_DATA["description"]
config_entry = MockConfigEntry(
data=CONFIG_ENTRY_DATA,
domain=DOMAIN,
options={},
title="Home Assistant Sky Connect",
unique_id=f"{vid}:{pid}_{serial_number}_{manufacturer}_{description}",
)
config_entry.add_to_hass(hass)
with patch(
Expand All @@ -270,31 +261,7 @@ async def test_setup_entry_wait_usb(
assert not await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(mock_is_plugged_in.mock_calls) == 1
assert config_entry.disabled_by == ConfigEntryDisabler.INTEGRATION
assert config_entry.state == ConfigEntryState.NOT_LOADED

# USB dongle plugged in
usb_data = usb.UsbServiceInfo(
device=CONFIG_ENTRY_DATA["device"],
vid=CONFIG_ENTRY_DATA["vid"],
pid=CONFIG_ENTRY_DATA["device"],
serial_number=CONFIG_ENTRY_DATA["serial_number"],
manufacturer=CONFIG_ENTRY_DATA["manufacturer"],
description=CONFIG_ENTRY_DATA["description"],
)
with patch(
"homeassistant.components.homeassistant_sky_connect.usb.async_is_plugged_in",
return_value=True,
) as mock_is_plugged_in, patch(
"homeassistant.components.onboarding.async_is_onboarded", return_value=True
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": "usb"}, data=usb_data
)
assert result["type"] == FlowResultType.ABORT

assert config_entry.disabled_by is None
assert config_entry.state == ConfigEntryState.LOADED
assert config_entry.state == ConfigEntryState.SETUP_RETRY


async def test_setup_entry_addon_info_fails(
Expand Down

0 comments on commit 22dbbd4

Please sign in to comment.