diff --git a/CODEOWNERS b/CODEOWNERS index 36ed63175f2..64a8ef5abfa 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1384,8 +1384,6 @@ build.json @home-assistant/supervisor /tests/components/spaceapi/ @fabaff /homeassistant/components/speedtestdotnet/ @rohankapoorcom @engrbm87 /tests/components/speedtestdotnet/ @rohankapoorcom @engrbm87 -/homeassistant/components/spider/ @peternijssen -/tests/components/spider/ @peternijssen /homeassistant/components/splunk/ @Bre77 /homeassistant/components/spotify/ @frenck @joostlek /tests/components/spotify/ @frenck @joostlek diff --git a/homeassistant/components/spider/__init__.py b/homeassistant/components/spider/__init__.py index 782486de2d8..4b138ec77a8 100644 --- a/homeassistant/components/spider/__init__.py +++ b/homeassistant/components/spider/__init__.py @@ -1,87 +1,39 @@ -"""Support for Spider Smart devices.""" +"""The Spider integration.""" -import logging +from __future__ import annotations -from spiderpy.spiderapi import SpiderApi, SpiderApiException, UnauthorizedException -import voluptuous as vol - -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry -from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME +from homeassistant.config_entries import ConfigEntry, ConfigEntryState from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryNotReady -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.typing import ConfigType +from homeassistant.helpers import issue_registry as ir -from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS +DOMAIN = "spider" -_LOGGER = logging.getLogger(__name__) -CONFIG_SCHEMA = vol.Schema( - vol.All( - cv.deprecated(DOMAIN), - { - DOMAIN: vol.Schema( - { - vol.Required(CONF_PASSWORD): cv.string, - vol.Required(CONF_USERNAME): cv.string, - vol.Optional( - CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL - ): cv.time_period, - } - ) +async def async_setup_entry(hass: HomeAssistant, _: ConfigEntry) -> bool: + """Set up Spider from a config entry.""" + ir.async_create_issue( + hass, + DOMAIN, + DOMAIN, + is_fixable=False, + severity=ir.IssueSeverity.ERROR, + translation_key="integration_removed", + translation_placeholders={ + "link": "https://www.ithodaalderop.nl/additionelespiderproducten", + "entries": "/config/integrations/integration/spider", }, - ), - extra=vol.ALLOW_EXTRA, -) - - -async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: - """Set up a config entry.""" - hass.data[DOMAIN] = {} - if DOMAIN not in config: - return True - - conf = config[DOMAIN] - - if not hass.config_entries.async_entries(DOMAIN): - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_IMPORT}, data=conf - ) - ) - - return True - - -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: - """Set up Spider via config entry.""" - try: - api = await hass.async_add_executor_job( - SpiderApi, - entry.data[CONF_USERNAME], - entry.data[CONF_PASSWORD], - entry.data[CONF_SCAN_INTERVAL], - ) - except UnauthorizedException: - _LOGGER.error("Authorization failed") - return False - except SpiderApiException as err: - _LOGGER.error("Can't connect to the Spider API: %s", err) - raise ConfigEntryNotReady from err - - hass.data[DOMAIN][entry.entry_id] = api - - await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + ) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: - """Unload Spider entry.""" - unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - if not unload_ok: - return False - - hass.data[DOMAIN].pop(entry.entry_id) + """Unload a config entry.""" + if all( + config_entry.state is ConfigEntryState.NOT_LOADED + for config_entry in hass.config_entries.async_entries(DOMAIN) + if config_entry.entry_id != entry.entry_id + ): + ir.async_delete_issue(hass, DOMAIN, DOMAIN) return True diff --git a/homeassistant/components/spider/climate.py b/homeassistant/components/spider/climate.py deleted file mode 100644 index 11e84a942f4..00000000000 --- a/homeassistant/components/spider/climate.py +++ /dev/null @@ -1,144 +0,0 @@ -"""Support for Spider thermostats.""" - -from typing import Any - -from homeassistant.components.climate import ( - ClimateEntity, - ClimateEntityFeature, - HVACMode, -) -from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature -from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.entity_platform import AddEntitiesCallback - -from .const import DOMAIN - -HA_STATE_TO_SPIDER = { - HVACMode.COOL: "Cool", - HVACMode.HEAT: "Heat", - HVACMode.OFF: "Idle", -} - -SPIDER_STATE_TO_HA = {value: key for key, value in HA_STATE_TO_SPIDER.items()} - - -async def async_setup_entry( - hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback -) -> None: - """Initialize a Spider thermostat.""" - api = hass.data[DOMAIN][config.entry_id] - - async_add_entities( - [ - SpiderThermostat(api, entity) - for entity in await hass.async_add_executor_job(api.get_thermostats) - ] - ) - - -class SpiderThermostat(ClimateEntity): - """Representation of a thermostat.""" - - _attr_has_entity_name = True - _attr_name = None - _attr_temperature_unit = UnitOfTemperature.CELSIUS - _enable_turn_on_off_backwards_compatibility = False - - def __init__(self, api, thermostat): - """Initialize the thermostat.""" - self.api = api - self.thermostat = thermostat - self.support_fan = thermostat.fan_speed_values - self.support_hvac = [] - for operation_value in thermostat.operation_values: - if operation_value in SPIDER_STATE_TO_HA: - self.support_hvac.append(SPIDER_STATE_TO_HA[operation_value]) - self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE - if len(self.hvac_modes) > 1 and HVACMode.OFF in self.hvac_modes: - self._attr_supported_features |= ( - ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON - ) - if thermostat.has_fan_mode: - self._attr_supported_features |= ClimateEntityFeature.FAN_MODE - - @property - def device_info(self) -> DeviceInfo: - """Return the device_info of the device.""" - return DeviceInfo( - configuration_url="https://mijn.ithodaalderop.nl/", - identifiers={(DOMAIN, self.thermostat.id)}, - manufacturer=self.thermostat.manufacturer, - model=self.thermostat.model, - name=self.thermostat.name, - ) - - @property - def unique_id(self): - """Return the id of the thermostat, if any.""" - return self.thermostat.id - - @property - def current_temperature(self): - """Return the current temperature.""" - return self.thermostat.current_temperature - - @property - def target_temperature(self): - """Return the temperature we try to reach.""" - return self.thermostat.target_temperature - - @property - def target_temperature_step(self): - """Return the supported step of target temperature.""" - return self.thermostat.temperature_steps - - @property - def min_temp(self): - """Return the minimum temperature.""" - return self.thermostat.minimum_temperature - - @property - def max_temp(self): - """Return the maximum temperature.""" - return self.thermostat.maximum_temperature - - @property - def hvac_mode(self) -> HVACMode: - """Return current operation ie. heat, cool, idle.""" - return SPIDER_STATE_TO_HA[self.thermostat.operation_mode] - - @property - def hvac_modes(self) -> list[HVACMode]: - """Return the list of available operation modes.""" - return self.support_hvac - - def set_temperature(self, **kwargs: Any) -> None: - """Set new target temperature.""" - if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None: - return - - self.thermostat.set_temperature(temperature) - - def set_hvac_mode(self, hvac_mode: HVACMode) -> None: - """Set new target operation mode.""" - self.thermostat.set_operation_mode(HA_STATE_TO_SPIDER.get(hvac_mode)) - - @property - def fan_mode(self): - """Return the fan setting.""" - return self.thermostat.current_fan_speed - - def set_fan_mode(self, fan_mode: str) -> None: - """Set fan mode.""" - self.thermostat.set_fan_speed(fan_mode) - - @property - def fan_modes(self): - """List of available fan modes.""" - return self.support_fan - - def update(self) -> None: - """Get the latest data.""" - self.thermostat = self.api.get_thermostat(self.unique_id) diff --git a/homeassistant/components/spider/config_flow.py b/homeassistant/components/spider/config_flow.py index 0c305adbc39..d96fb9e88b6 100644 --- a/homeassistant/components/spider/config_flow.py +++ b/homeassistant/components/spider/config_flow.py @@ -1,87 +1,11 @@ -"""Config flow for Spider.""" +"""Config flow for Spider integration.""" -import logging -from typing import Any +from homeassistant.config_entries import ConfigFlow -from spiderpy.spiderapi import SpiderApi, SpiderApiException, UnauthorizedException -import voluptuous as vol - -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult -from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME - -from .const import DEFAULT_SCAN_INTERVAL, DOMAIN - -_LOGGER = logging.getLogger(__name__) - -DATA_SCHEMA_USER = vol.Schema( - {vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str} -) - -RESULT_AUTH_FAILED = "auth_failed" -RESULT_CONN_ERROR = "conn_error" -RESULT_SUCCESS = "success" +from . import DOMAIN class SpiderConfigFlow(ConfigFlow, domain=DOMAIN): - """Handle a Spider config flow.""" + """Handle a config flow for Spider.""" VERSION = 1 - - def __init__(self) -> None: - """Initialize the Spider flow.""" - self.data = { - CONF_USERNAME: "", - CONF_PASSWORD: "", - CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL, - } - - def _try_connect(self): - """Try to connect and check auth.""" - try: - SpiderApi( - self.data[CONF_USERNAME], - self.data[CONF_PASSWORD], - self.data[CONF_SCAN_INTERVAL], - ) - except SpiderApiException: - return RESULT_CONN_ERROR - except UnauthorizedException: - return RESULT_AUTH_FAILED - - return RESULT_SUCCESS - - async def async_step_user( - self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: - """Handle a flow initiated by the user.""" - if self._async_current_entries(): - return self.async_abort(reason="single_instance_allowed") - - errors = {} - if user_input is not None: - self.data[CONF_USERNAME] = user_input["username"] - self.data[CONF_PASSWORD] = user_input["password"] - - result = await self.hass.async_add_executor_job(self._try_connect) - - if result == RESULT_SUCCESS: - return self.async_create_entry( - title=DOMAIN, - data=self.data, - ) - if result != RESULT_AUTH_FAILED: - _LOGGER.exception("Unexpected exception") - errors["base"] = "unknown" - return self.async_abort(reason=result) - - errors["base"] = "invalid_auth" - - return self.async_show_form( - step_id="user", - data_schema=DATA_SCHEMA_USER, - errors=errors, - ) - - async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: - """Import spider config from configuration.yaml.""" - return await self.async_step_user(import_data) diff --git a/homeassistant/components/spider/const.py b/homeassistant/components/spider/const.py deleted file mode 100644 index 189763f4e98..00000000000 --- a/homeassistant/components/spider/const.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Constants for the Spider integration.""" - -from homeassistant.const import Platform - -DOMAIN = "spider" -DEFAULT_SCAN_INTERVAL = 300 - -PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.SWITCH] diff --git a/homeassistant/components/spider/manifest.json b/homeassistant/components/spider/manifest.json index a80fd178898..76d148954f2 100644 --- a/homeassistant/components/spider/manifest.json +++ b/homeassistant/components/spider/manifest.json @@ -1,10 +1,9 @@ { "domain": "spider", "name": "Itho Daalderop Spider", - "codeowners": ["@peternijssen"], - "config_flow": true, + "codeowners": [], "documentation": "https://www.home-assistant.io/integrations/spider", + "integration_type": "system", "iot_class": "cloud_polling", - "loggers": ["spiderpy"], - "requirements": ["spiderpy==1.6.1"] + "requirements": [] } diff --git a/homeassistant/components/spider/sensor.py b/homeassistant/components/spider/sensor.py deleted file mode 100644 index 70c38a40e15..00000000000 --- a/homeassistant/components/spider/sensor.py +++ /dev/null @@ -1,108 +0,0 @@ -"""Support for Spider Powerplugs (energy & power).""" - -from __future__ import annotations - -from homeassistant.components.sensor import ( - SensorDeviceClass, - SensorEntity, - SensorStateClass, -) -from homeassistant.config_entries import ConfigEntry -from homeassistant.const import UnitOfEnergy, UnitOfPower -from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.entity_platform import AddEntitiesCallback - -from .const import DOMAIN - - -async def async_setup_entry( - hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback -) -> None: - """Initialize a Spider Power Plug.""" - api = hass.data[DOMAIN][config.entry_id] - entities: list[SensorEntity] = [] - - for entity in await hass.async_add_executor_job(api.get_power_plugs): - entities.append(SpiderPowerPlugEnergy(api, entity)) - entities.append(SpiderPowerPlugPower(api, entity)) - - async_add_entities(entities) - - -class SpiderPowerPlugEnergy(SensorEntity): - """Representation of a Spider Power Plug (energy).""" - - _attr_has_entity_name = True - _attr_translation_key = "total_energy_today" - _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR - _attr_device_class = SensorDeviceClass.ENERGY - _attr_state_class = SensorStateClass.TOTAL_INCREASING - - def __init__(self, api, power_plug) -> None: - """Initialize the Spider Power Plug.""" - self.api = api - self.power_plug = power_plug - - @property - def device_info(self) -> DeviceInfo: - """Return the device_info of the device.""" - return DeviceInfo( - identifiers={(DOMAIN, self.power_plug.id)}, - manufacturer=self.power_plug.manufacturer, - model=self.power_plug.model, - name=self.power_plug.name, - ) - - @property - def unique_id(self) -> str: - """Return the ID of this sensor.""" - return f"{self.power_plug.id}_total_energy_today" - - @property - def native_value(self) -> float: - """Return todays energy usage in Kwh.""" - return round(self.power_plug.today_energy_consumption / 1000, 2) - - def update(self) -> None: - """Get the latest data.""" - self.power_plug = self.api.get_power_plug(self.power_plug.id) - - -class SpiderPowerPlugPower(SensorEntity): - """Representation of a Spider Power Plug (power).""" - - _attr_has_entity_name = True - _attr_translation_key = "power_consumption" - _attr_device_class = SensorDeviceClass.POWER - _attr_state_class = SensorStateClass.MEASUREMENT - _attr_native_unit_of_measurement = UnitOfPower.WATT - - def __init__(self, api, power_plug) -> None: - """Initialize the Spider Power Plug.""" - self.api = api - self.power_plug = power_plug - - @property - def device_info(self) -> DeviceInfo: - """Return the device_info of the device.""" - return DeviceInfo( - identifiers={(DOMAIN, self.power_plug.id)}, - manufacturer=self.power_plug.manufacturer, - model=self.power_plug.model, - name=self.power_plug.name, - ) - - @property - def unique_id(self) -> str: - """Return the ID of this sensor.""" - return f"{self.power_plug.id}_power_consumption" - - @property - def native_value(self) -> float: - """Return the current power usage in W.""" - return round(self.power_plug.current_energy_consumption) - - def update(self) -> None: - """Get the latest data.""" - self.power_plug = self.api.get_power_plug(self.power_plug.id) diff --git a/homeassistant/components/spider/strings.json b/homeassistant/components/spider/strings.json index c8d67be36ae..338ae3aa762 100644 --- a/homeassistant/components/spider/strings.json +++ b/homeassistant/components/spider/strings.json @@ -1,30 +1,8 @@ { - "config": { - "step": { - "user": { - "title": "Sign-in with mijn.ithodaalderop.nl account", - "data": { - "username": "[%key:common::config_flow::data::username%]", - "password": "[%key:common::config_flow::data::password%]" - } - } - }, - "error": { - "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", - "unknown": "[%key:common::config_flow::error::unknown%]" - }, - "abort": { - "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]" - } - }, - "entity": { - "sensor": { - "power_consumption": { - "name": "Power consumption" - }, - "total_energy_today": { - "name": "Total energy today" - } + "issues": { + "integration_removed": { + "title": "The Spider integration has been removed", + "description": "The Spider integration has been removed from Home Assistant.\n\nItho daalderop has [discontinued]({link}) the Spider Connect System.\n\nTo resolve this issue, please remove the (now defunct) integration entries from your Home Assistant setup. [Click here to see your existing Spider integration entries]({entries})." } } } diff --git a/homeassistant/components/spider/switch.py b/homeassistant/components/spider/switch.py deleted file mode 100644 index 63f0ec6cb69..00000000000 --- a/homeassistant/components/spider/switch.py +++ /dev/null @@ -1,74 +0,0 @@ -"""Support for Spider switches.""" - -from typing import Any - -from homeassistant.components.switch import SwitchEntity -from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.entity_platform import AddEntitiesCallback - -from .const import DOMAIN - - -async def async_setup_entry( - hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback -) -> None: - """Initialize a Spider Power Plug.""" - api = hass.data[DOMAIN][config.entry_id] - async_add_entities( - [ - SpiderPowerPlug(api, entity) - for entity in await hass.async_add_executor_job(api.get_power_plugs) - ] - ) - - -class SpiderPowerPlug(SwitchEntity): - """Representation of a Spider Power Plug.""" - - _attr_has_entity_name = True - _attr_name = None - - def __init__(self, api, power_plug): - """Initialize the Spider Power Plug.""" - self.api = api - self.power_plug = power_plug - - @property - def device_info(self) -> DeviceInfo: - """Return the device_info of the device.""" - return DeviceInfo( - configuration_url="https://mijn.ithodaalderop.nl/", - identifiers={(DOMAIN, self.power_plug.id)}, - manufacturer=self.power_plug.manufacturer, - model=self.power_plug.model, - name=self.power_plug.name, - ) - - @property - def unique_id(self): - """Return the ID of this switch.""" - return self.power_plug.id - - @property - def is_on(self): - """Return true if switch is on. Standby is on.""" - return self.power_plug.is_on - - @property - def available(self) -> bool: - """Return true if switch is available.""" - return self.power_plug.is_available - - def turn_on(self, **kwargs: Any) -> None: - """Turn device on.""" - self.power_plug.turn_on() - - def turn_off(self, **kwargs: Any) -> None: - """Turn device off.""" - self.power_plug.turn_off() - - def update(self) -> None: - """Get the latest data.""" - self.power_plug = self.api.get_power_plug(self.power_plug.id) diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index 10e27ff2c97..f399b0922f1 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -554,7 +554,6 @@ FLOWS = { "sonos", "soundtouch", "speedtestdotnet", - "spider", "spotify", "sql", "squeezebox", diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 7b1cb045041..3243d1677ae 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -5821,12 +5821,6 @@ "config_flow": true, "iot_class": "cloud_polling" }, - "spider": { - "name": "Itho Daalderop Spider", - "integration_type": "hub", - "config_flow": true, - "iot_class": "cloud_polling" - }, "splunk": { "name": "Splunk", "integration_type": "hub", diff --git a/requirements_all.txt b/requirements_all.txt index ec20231cb66..cfc60887612 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2696,9 +2696,6 @@ speak2mary==1.4.0 # homeassistant.components.speedtestdotnet speedtest-cli==2.1.3 -# homeassistant.components.spider -spiderpy==1.6.1 - # homeassistant.components.spotify spotipy==2.23.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index d6566897887..bdf63ae70d2 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -2142,9 +2142,6 @@ speak2mary==1.4.0 # homeassistant.components.speedtestdotnet speedtest-cli==2.1.3 -# homeassistant.components.spider -spiderpy==1.6.1 - # homeassistant.components.spotify spotipy==2.23.0 diff --git a/tests/components/spider/__init__.py b/tests/components/spider/__init__.py index d145f4efc09..4d9139a501e 100644 --- a/tests/components/spider/__init__.py +++ b/tests/components/spider/__init__.py @@ -1 +1 @@ -"""Tests for the Spider component.""" +"""Tests for the Spider integration.""" diff --git a/tests/components/spider/test_config_flow.py b/tests/components/spider/test_config_flow.py deleted file mode 100644 index 69f97130f8c..00000000000 --- a/tests/components/spider/test_config_flow.py +++ /dev/null @@ -1,112 +0,0 @@ -"""Tests for the Spider config flow.""" - -from unittest.mock import Mock, patch - -import pytest - -from homeassistant import config_entries -from homeassistant.components.spider.const import DOMAIN -from homeassistant.const import CONF_PASSWORD, CONF_USERNAME -from homeassistant.core import HomeAssistant -from homeassistant.data_entry_flow import FlowResultType - -from tests.common import MockConfigEntry - -USERNAME = "spider-username" -PASSWORD = "spider-password" - -SPIDER_USER_DATA = { - CONF_USERNAME: USERNAME, - CONF_PASSWORD: PASSWORD, -} - - -@pytest.fixture(name="spider") -def spider_fixture() -> Mock: - """Patch libraries.""" - with patch("homeassistant.components.spider.config_flow.SpiderApi") as spider: - yield spider - - -async def test_user(hass: HomeAssistant, spider) -> None: - """Test user config.""" - - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "user" - - with ( - patch( - "homeassistant.components.spider.async_setup", return_value=True - ) as mock_setup, - patch( - "homeassistant.components.spider.async_setup_entry", return_value=True - ) as mock_setup_entry, - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=SPIDER_USER_DATA - ) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == DOMAIN - assert result["data"][CONF_USERNAME] == USERNAME - assert result["data"][CONF_PASSWORD] == PASSWORD - assert not result["result"].unique_id - - assert len(mock_setup.mock_calls) == 1 - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_import(hass: HomeAssistant, spider) -> None: - """Test import step.""" - - with ( - patch( - "homeassistant.components.spider.async_setup", - return_value=True, - ) as mock_setup, - patch( - "homeassistant.components.spider.async_setup_entry", - return_value=True, - ) as mock_setup_entry, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_IMPORT}, - data=SPIDER_USER_DATA, - ) - await hass.async_block_till_done() - - assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == DOMAIN - assert result["data"][CONF_USERNAME] == USERNAME - assert result["data"][CONF_PASSWORD] == PASSWORD - assert not result["result"].unique_id - - assert len(mock_setup.mock_calls) == 1 - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_abort_if_already_setup(hass: HomeAssistant, spider) -> None: - """Test we abort if Spider is already setup.""" - MockConfigEntry(domain=DOMAIN, data=SPIDER_USER_DATA).add_to_hass(hass) - - # Should fail, config exist (import) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER}, data=SPIDER_USER_DATA - ) - - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "single_instance_allowed" - - # Should fail, config exist (flow) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=SPIDER_USER_DATA - ) - - assert result["type"] is FlowResultType.ABORT - assert result["reason"] == "single_instance_allowed" diff --git a/tests/components/spider/test_init.py b/tests/components/spider/test_init.py new file mode 100644 index 00000000000..6d1d87cfa6a --- /dev/null +++ b/tests/components/spider/test_init.py @@ -0,0 +1,50 @@ +"""Tests for the Spider integration.""" + +from homeassistant.components.spider import DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.helpers import issue_registry as ir + +from tests.common import MockConfigEntry + + +async def test_spider_repair_issue( + hass: HomeAssistant, issue_registry: ir.IssueRegistry +) -> None: + """Test the Spider configuration entry loading/unloading handles the repair.""" + config_entry_1 = MockConfigEntry( + title="Example 1", + domain=DOMAIN, + ) + config_entry_1.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry_1.entry_id) + await hass.async_block_till_done() + assert config_entry_1.state is ConfigEntryState.LOADED + + # Add a second one + config_entry_2 = MockConfigEntry( + title="Example 2", + domain=DOMAIN, + ) + config_entry_2.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry_2.entry_id) + await hass.async_block_till_done() + + assert config_entry_2.state is ConfigEntryState.LOADED + assert issue_registry.async_get_issue(DOMAIN, DOMAIN) + + # Remove the first one + await hass.config_entries.async_remove(config_entry_1.entry_id) + await hass.async_block_till_done() + + assert config_entry_1.state is ConfigEntryState.NOT_LOADED + assert config_entry_2.state is ConfigEntryState.LOADED + assert issue_registry.async_get_issue(DOMAIN, DOMAIN) + + # Remove the second one + await hass.config_entries.async_remove(config_entry_2.entry_id) + await hass.async_block_till_done() + + assert config_entry_1.state is ConfigEntryState.NOT_LOADED + assert config_entry_2.state is ConfigEntryState.NOT_LOADED + assert issue_registry.async_get_issue(DOMAIN, DOMAIN) is None