-
-
Notifications
You must be signed in to change notification settings - Fork 29k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add config flow to synology_srm #35412
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,84 @@ | ||
"""The Synology SRM component.""" | ||
"""The Synology SRM integration.""" | ||
import asyncio | ||
|
||
from synology_srm import Client as SynologyClient | ||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import ( | ||
CONF_HOST, | ||
CONF_PASSWORD, | ||
CONF_PORT, | ||
CONF_SSL, | ||
CONF_USERNAME, | ||
CONF_VERIFY_SSL, | ||
) | ||
from homeassistant.core import HomeAssistant | ||
|
||
from .const import DOMAIN | ||
from .router import SynologySrmRouter | ||
|
||
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA) | ||
PLATFORMS = ["device_tracker"] | ||
|
||
|
||
async def async_setup(hass: HomeAssistant, config: dict): | ||
"""Set up the Synology SRM component.""" | ||
return True | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
"""Set up Synology SRM from a config entry.""" | ||
client = get_srm_client_from_user_data(entry.data) | ||
router = SynologySrmRouter(hass, client) | ||
|
||
hass.data.setdefault(DOMAIN, {}) | ||
hass.data[DOMAIN][entry.unique_id] = router | ||
|
||
await router.async_setup() | ||
|
||
for platform in PLATFORMS: | ||
hass.async_create_task( | ||
hass.config_entries.async_forward_entry_setup(entry, platform) | ||
) | ||
|
||
return True | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
"""Unload a config entry.""" | ||
unload_ok = all( | ||
await asyncio.gather( | ||
*[ | ||
hass.config_entries.async_forward_entry_unload(entry, platform) | ||
for platform in PLATFORMS | ||
] | ||
) | ||
) | ||
if unload_ok: | ||
router = hass.data[DOMAIN].pop(entry.unique_id) | ||
await router.async_unload() | ||
|
||
return unload_ok | ||
|
||
|
||
def get_srm_client_from_user_data(data) -> SynologyClient: | ||
"""Get the Synology SRM client from user data.""" | ||
client = SynologyClient( | ||
host=data[CONF_HOST], | ||
port=data[CONF_PORT], | ||
https=data[CONF_SSL], | ||
username=data[CONF_USERNAME], | ||
password=data[CONF_PASSWORD], | ||
) | ||
|
||
if not data[CONF_VERIFY_SSL]: | ||
client.http.disable_https_verify() | ||
|
||
return client | ||
|
||
|
||
def fetch_srm_device_id(client: SynologyClient): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you putting this function here since it's used only in the confug_flow ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the function because it's always mocked in tests to I cannot have 100% coverage in this situation. It's also make sense in the end because I've another function just at the top to generate the client, so both functions handling the |
||
"""Fetch the Synology SRM device ID from the user.""" | ||
info = client.mesh.get_system_info() | ||
return info["nodes"][0]["unique"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""Config flow for Synology SRM integration.""" | ||
import logging | ||
|
||
import requests | ||
from synology_srm.http import SynologyError, SynologyHttpException | ||
import voluptuous as vol | ||
|
||
from homeassistant import config_entries | ||
from homeassistant.const import ( | ||
CONF_HOST, | ||
CONF_PASSWORD, | ||
CONF_PORT, | ||
CONF_SSL, | ||
CONF_USERNAME, | ||
CONF_VERIFY_SSL, | ||
) | ||
|
||
from . import fetch_srm_device_id, get_srm_client_from_user_data | ||
from .const import DEFAULT_PORT, DEFAULT_SSL, DEFAULT_USERNAME, DEFAULT_VERIFY_SSL | ||
from .const import DOMAIN # pylint: disable=unused-import | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class SynologySrmFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): | ||
"""Handle a config flow for Synology SRM.""" | ||
|
||
VERSION = 1 | ||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL | ||
|
||
async def async_step_user(self, user_input=None): | ||
"""Handle the initial step.""" | ||
errors = {} | ||
if user_input is not None: | ||
try: | ||
client = get_srm_client_from_user_data(user_input) | ||
device_id = await self.hass.async_add_executor_job( | ||
fetch_srm_device_id, client | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the A MAC adress, serial number, ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) | ||
|
||
# Check if the device has already been configured | ||
await self.async_set_unique_id(device_id, raise_on_progress=False) | ||
self._abort_if_unique_id_configured() | ||
|
||
return self.async_create_entry(title=device_id, data=user_input) | ||
except (SynologyHttpException, requests.exceptions.ConnectionError) as ex: | ||
errors["base"] = "cannot_connect" | ||
_LOGGER.exception(ex) | ||
except SynologyError as error: | ||
if error.code >= 400: | ||
errors["base"] = "invalid_auth" | ||
else: | ||
errors["base"] = "cannot_connect" | ||
_LOGGER.exception(error) | ||
except Exception as ex: # pylint: disable=broad-except | ||
errors["base"] = "unknown" | ||
_LOGGER.exception(ex) | ||
else: | ||
user_input = {} | ||
|
||
return self.async_show_form( | ||
step_id="user", | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required(CONF_HOST, default=user_input.get(CONF_HOST, "")): str, | ||
vol.Optional( | ||
CONF_PORT, default=user_input.get(CONF_PORT, DEFAULT_PORT) | ||
): int, | ||
vol.Required( | ||
CONF_USERNAME, | ||
default=user_input.get(CONF_USERNAME, DEFAULT_USERNAME), | ||
): str, | ||
vol.Required( | ||
CONF_PASSWORD, default=user_input.get(CONF_PASSWORD, "") | ||
): str, | ||
vol.Optional( | ||
CONF_SSL, default=user_input.get(CONF_SSL, DEFAULT_SSL) | ||
): bool, | ||
vol.Optional( | ||
CONF_VERIFY_SSL, | ||
default=user_input.get(CONF_VERIFY_SSL, DEFAULT_VERIFY_SSL), | ||
): bool, | ||
} | ||
), | ||
errors=errors, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Constants for the Synology SRM integration.""" | ||
|
||
DOMAIN = "synology_srm" | ||
|
||
DEFAULT_USERNAME = "admin" | ||
DEFAULT_PORT = 8001 | ||
DEFAULT_SSL = True | ||
DEFAULT_VERIFY_SSL = False | ||
|
||
DEVICE_ICON = { | ||
"nas": "mdi:nas", | ||
"notebook": "mdi:laptop", | ||
"computer": "mdi:desktop-mac", | ||
"tv": "mdi:television", | ||
"printer": "mdi:printer", | ||
"tablet": "mdi:tablet-ipad", | ||
"gamebox": "mdi:gamepad-variant", | ||
"phone": "mdi:cellphone", | ||
} | ||
|
||
DEVICE_ATTRIBUTE_ALIAS = { | ||
"band": None, | ||
"connection": None, | ||
"current_rate": None, | ||
"dev_type": None, | ||
"hostname": None, | ||
"ip6_addr": None, | ||
"ip_addr": None, | ||
"is_baned": "is_banned", | ||
"is_beamforming_on": None, | ||
"is_guest": None, | ||
"is_high_qos": None, | ||
"is_low_qos": None, | ||
"is_manual_dev_type": None, | ||
"is_manual_hostname": None, | ||
"is_online": None, | ||
"is_parental_controled": "is_parental_controlled", | ||
"is_qos": None, | ||
"is_wireless": None, | ||
"mac": None, | ||
"max_rate": None, | ||
"mesh_node_id": None, | ||
"rate_quality": None, | ||
"signalstrength": "signal_strength", | ||
"transferRXRate": "transfer_rx_rate", | ||
"transferTXRate": "transfer_tx_rate", | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can easily support import config from the
config.yaml
, why not ?See #29927 or #32704
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aerialls
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following the future of YAML, I prefer to delete the YAML integration as soon as possible. I want to add in the near future some dynamic parameters to give the user some control over what is being tracked (and I'm not comfortable or have the time to maintain both system in parallel).