-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
90 additions
and
648 deletions.
There are no files selected for viewing
This file contains 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 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,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 .const import DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS | ||
|
||
_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, | ||
} | ||
) | ||
from homeassistant.helpers import issue_registry as ir | ||
|
||
DOMAIN = "spider" | ||
|
||
|
||
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 |
This file was deleted.
Oops, something went wrong.
This file contains 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,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) |
This file was deleted.
Oops, something went wrong.
This file contains 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,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": [] | ||
} |
Oops, something went wrong.