Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions homeassistant/components/synology_dsm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
SynologyDSMLoginFailedException,
SynologyDSMRequestException,
)
import voluptuous as vol

from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ATTRIBUTION,
CONF_DISKS,
Expand All @@ -37,6 +36,7 @@
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import entity_registry
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import async_get_registry
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
Expand All @@ -46,10 +46,10 @@
from homeassistant.helpers.typing import HomeAssistantType

from .const import (
CONF_FILTER_STORAGE,
CONF_SERIAL,
CONF_VOLUMES,
DEFAULT_SCAN_INTERVAL,
DEFAULT_USE_SSL,
DEFAULT_VERIFY_SSL,
DOMAIN,
ENTITY_CLASS,
Expand All @@ -70,23 +70,8 @@
UTILISATION_SENSORS,
)

CONFIG_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT): cv.port,
vol.Optional(CONF_SSL, default=DEFAULT_USE_SSL): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_DISKS): cv.ensure_list,
vol.Optional(CONF_VOLUMES): cv.ensure_list,
}
)
CONFIG_SCHEMA = cv.deprecated(DOMAIN, invalidation_version="0.120")

CONFIG_SCHEMA = vol.Schema(
{DOMAIN: vol.Schema(vol.All(cv.ensure_list, [CONFIG_SCHEMA]))},
extra=vol.ALLOW_EXTRA,
)

ATTRIBUTION = "Data provided by Synology"

Expand All @@ -96,20 +81,6 @@

async def async_setup(hass, config):
"""Set up Synology DSM sensors from legacy config file."""

conf = config.get(DOMAIN)
if conf is None:
return True

for dsm_conf in conf:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=dsm_conf,
)
)

return True


Expand Down Expand Up @@ -180,6 +151,25 @@ def _async_migrator(entity_entry: entity_registry.RegistryEntry):
entry, data={**entry.data, CONF_VERIFY_SSL: DEFAULT_VERIFY_SSL}
)

# Consider if filter storage devices is true
if entry.options.get(CONF_FILTER_STORAGE):
hass.config_entries.async_update_entry(
entry,
data={
**entry.data,
CONF_DISKS: entry.options[CONF_DISKS],
CONF_VOLUMES: entry.options[CONF_VOLUMES],
},
)
else:
data = dict(entry.data)
data.pop(CONF_DISKS, None)
data.pop(CONF_VOLUMES, None)
hass.config_entries.async_update_entry(
entry,
data=data,
)

# Continue setup
api = SynoApi(hass, entry)
try:
Expand Down Expand Up @@ -230,6 +220,8 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
entry_data[UNDO_UPDATE_LISTENER]()
await entry_data[SYNO_API].async_unload()
hass.data[DOMAIN].pop(entry.unique_id)
dev_reg = await async_get_registry(hass)
dev_reg.async_clear_config_entry(entry.entry_id)

return unload_ok

Expand Down
42 changes: 33 additions & 9 deletions homeassistant/components/synology_dsm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@
import homeassistant.helpers.config_validation as cv

from .const import (
CONF_FILTER_STORAGE,
CONF_VOLUMES,
DEFAULT_FILTER_STORAGE,
DEFAULT_PORT,
DEFAULT_PORT_SSL,
DEFAULT_SCAN_INTERVAL,
DEFAULT_TIMEOUT,
DEFAULT_USE_SSL,
DEFAULT_VERIFY_SSL,
SYNO_API,
)
from .const import DOMAIN # pylint: disable=unused-import

Expand Down Expand Up @@ -90,6 +93,7 @@ def __init__(self):
"""Initialize the synology_dsm config flow."""
self.saved_user_input = {}
self.discovered_conf = {}
self.syno_info = {}

async def _show_setup_form(self, user_input=None, errors=None):
"""Show the setup form to the user."""
Expand Down Expand Up @@ -140,7 +144,7 @@ async def async_step_user(self, user_input=None):
)

try:
serial = await self.hass.async_add_executor_job(
self.syno_info = await self.hass.async_add_executor_job(
_login_and_fetch_syno_info, api, otp_code
)
except SynologyDSMLogin2SARequiredException:
Expand All @@ -164,10 +168,10 @@ async def async_step_user(self, user_input=None):
if errors:
return await self._show_setup_form(user_input, errors)

# unique_id should be serial for services purpose
await self.async_set_unique_id(serial, raise_on_progress=False)

# Check if already configured
await self.async_set_unique_id(
self.syno_info["serial"], raise_on_progress=False
)
self._abort_if_unique_id_configured()

config_data = {
Expand Down Expand Up @@ -212,10 +216,6 @@ async def async_step_ssdp(self, discovery_info):
self.context["title_placeholders"] = self.discovered_conf
return await self.async_step_user()

async def async_step_import(self, user_input=None):
"""Import a config entry."""
return await self.async_step_user(user_input)

async def async_step_link(self, user_input):
"""Link a config entry from discovery."""
return await self.async_step_user(user_input)
Expand Down Expand Up @@ -259,6 +259,12 @@ async def async_step_init(self, user_input=None):
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

disks_ids = self.hass.data[DOMAIN][self.config_entry.unique_id][
SYNO_API
].storage.disks_ids
volumes_ids = self.hass.data[DOMAIN][self.config_entry.unique_id][
SYNO_API
].storage.volumes_ids
data_schema = vol.Schema(
{
vol.Optional(
Expand All @@ -273,6 +279,20 @@ async def async_step_init(self, user_input=None):
CONF_TIMEOUT, DEFAULT_TIMEOUT
),
): cv.positive_int,
vol.Optional(
CONF_FILTER_STORAGE,
default=self.config_entry.options.get(
CONF_FILTER_STORAGE, DEFAULT_FILTER_STORAGE
),
): bool,
vol.Required(
CONF_DISKS,
default=self.config_entry.options.get(CONF_DISKS, disks_ids),
): cv.multi_select(disks_ids),
vol.Required(
CONF_VOLUMES,
default=self.config_entry.options.get(CONF_VOLUMES, volumes_ids),
): cv.multi_select(volumes_ids),
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)
Expand All @@ -294,7 +314,11 @@ def _login_and_fetch_syno_info(api, otp_code):
):
raise InvalidData

return api.information.serial
return {
"serial": api.information.serial,
CONF_DISKS: api.storage.disks_ids,
CONF_VOLUMES: api.storage.volumes_ids,
}


class InvalidData(exceptions.HomeAssistantError):
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/synology_dsm/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
# Configuration
CONF_SERIAL = "serial"
CONF_VOLUMES = "volumes"
CONF_FILTER_STORAGE = "filter_storage"

DEFAULT_USE_SSL = True
DEFAULT_VERIFY_SSL = False
DEFAULT_PORT = 5000
DEFAULT_PORT_SSL = 5001
DEFAULT_FILTER_STORAGE = False
# Options
DEFAULT_SCAN_INTERVAL = 15 # min
DEFAULT_TIMEOUT = 10 # sec
Expand Down
11 changes: 10 additions & 1 deletion homeassistant/components/synology_dsm/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
"password": "[%key:common::config_flow::data::password%]",
"port": "[%key:common::config_flow::data::port%]"
}
},
"filter_storage": {
"title": "Synology DSM: filter disks and volumes",
"description": "Select the disks and volumes you want to add",
"data": {
"disks": "Disks",
"volumes": "Volumes"
}
}
},
"error": {
Expand All @@ -47,7 +55,8 @@
"init": {
"data": {
"scan_interval": "Minutes between scans",
"timeout": "Timeout (seconds)"
"timeout": "Timeout (seconds)",
"filter_storage": "Filter disks and volumes to add"
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/synology_dsm/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
"init": {
"data": {
"scan_interval": "Minutes between scans",
"timeout": "Timeout (seconds)"
"timeout": "Timeout (seconds)",
"filter_storage": "Filter disks and volumes to add",
"disks": "Disks",
"volumes": "Volumes"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/components/synology_dsm/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
PASSWORD = "password"
DEVICE_TOKEN = "Dév!cè_T0k€ñ"

DISKS = ["sda", "sdb", "sdc"]
VOLUMES = ["volume_1"]

MACS = ["00-11-32-XX-XX-59", "00-11-32-XX-XX-5A"]
Loading