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
49 changes: 49 additions & 0 deletions homeassistant/components/synology_dsm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def _async_migrator(entity_entry: entity_registry.RegistryEntry):
)

# Continue setup
_LOGGER.debug("async_setup_entry - unique_id:%s - setup SynoApi", entry.unique_id)
api = SynoApi(hass, entry)
try:
await api.async_setup()
Expand All @@ -190,11 +191,21 @@ def _async_migrator(entity_entry: entity_registry.RegistryEntry):
# For SSDP compat
if not entry.data.get(CONF_MAC):
network = await hass.async_add_executor_job(getattr, api.dsm, "network")
_LOGGER.debug(
"async_setup_entry - unique_id:%s - add macs %s",
entry.unique_id,
network.macs,
)
hass.config_entries.async_update_entry(
entry, data={**entry.data, CONF_MAC: network.macs}
)

for platform in PLATFORMS:
_LOGGER.debug(
"async_setup_entry - unique_id:%s - setup platform %s",
entry.unique_id,
platform,
)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, platform)
)
Expand All @@ -204,6 +215,7 @@ def _async_migrator(entity_entry: entity_registry.RegistryEntry):

async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
"""Unload Synology DSM sensors."""
_LOGGER.debug("async_unload_entry - unique_id:%s - start unload", entry.unique_id)
unload_ok = all(
await asyncio.gather(
*[
Expand All @@ -214,6 +226,7 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
)

if unload_ok:
_LOGGER.debug("async_unload_entry - unique_id:%s - unload ok", entry.unique_id)
entry_data = hass.data[DOMAIN][entry.unique_id]
entry_data[UNDO_UPDATE_LISTENER]()
await entry_data[SYNO_API].async_unload()
Expand All @@ -224,6 +237,9 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):

async def _async_update_listener(hass: HomeAssistantType, entry: ConfigEntry):
"""Handle options update."""
_LOGGER.debug(
"_async_update_listener - unique_id:%s - reload entity", entry.unique_id
)
await hass.config_entries.async_reload(entry.entry_id)


Expand All @@ -232,6 +248,7 @@ class SynoApi:

def __init__(self, hass: HomeAssistantType, entry: ConfigEntry):
"""Initialize the API wrapper class."""
_LOGGER.debug("SynoApi - unique_id:%s - init", entry.unique_id)
self._hass = hass
self._entry = entry

Expand Down Expand Up @@ -263,6 +280,10 @@ def signal_sensor_update(self) -> str:

async def async_setup(self):
"""Start interacting with the NAS."""
_LOGGER.debug(
"SynoApi.async_setup - unique_id:%s - setup SynologyDSM",
self._entry.unique_id,
)
self.dsm = SynologyDSM(
self._entry.data[CONF_HOST],
self._entry.data[CONF_PORT],
Expand All @@ -273,11 +294,21 @@ async def async_setup(self):
timeout=self._entry.options.get(CONF_TIMEOUT),
device_token=self._entry.data.get("device_token"),
)

_LOGGER.debug(
"SynoApi.async_setup - unique_id:%s - SynologyDSM.login()",
self._entry.unique_id,
)
await self._hass.async_add_executor_job(self.dsm.login)

self._with_surveillance_station = bool(
self.dsm.apis.get(SynoSurveillanceStation.CAMERA_API_KEY)
)
_LOGGER.debug(
"SynoApi.async_setup - unique_id:%s - _with_surveillance_station:%s",
self._entry.unique_id,
self._with_surveillance_station,
)

self._async_setup_api_requests()

Expand Down Expand Up @@ -311,8 +342,15 @@ def unsubscribe() -> None:
@callback
def _async_setup_api_requests(self):
"""Determine if we should fetch each API, if one entity needs it."""
_LOGGER.debug(
"SynoApi._async_setup_api_requests - unique_id:%s", self._entry.unique_id
)
# Entities not added yet, fetch all
if not self._fetching_entities:
_LOGGER.debug(
"SynoApi._async_setup_api_requests - unique_id:%s - fetch all",
self._entry.unique_id,
)
return

# Determine if we should fetch an API
Expand Down Expand Up @@ -356,6 +394,9 @@ def _async_setup_api_requests(self):

def _fetch_device_configuration(self):
"""Fetch initial device config."""
_LOGGER.debug(
"SynoApi._fetch_device_configuration - unique_id:%s", self._entry.unique_id
)
self.information = self.dsm.information
self.network = self.dsm.network
self.network.update()
Expand All @@ -377,10 +418,12 @@ def _fetch_device_configuration(self):

async def async_unload(self):
"""Stop interacting with the NAS and prepare for removal from hass."""
_LOGGER.debug("SynoApi.async_unload - unique_id:%s", self._entry.unique_id)
self._unsub_dispatcher()

async def async_update(self, now=None):
"""Update function for updating API information."""
_LOGGER.debug("SynoApi.async_update - unique_id:%s", self._entry.unique_id)
self._async_setup_api_requests()
await self._hass.async_add_executor_job(self.dsm.update, self._with_information)
async_dispatcher_send(self._hass, self.signal_sensor_update)
Expand All @@ -407,6 +450,7 @@ def __init__(
self._icon = entity_info[ENTITY_ICON]
self._unit = entity_info[ENTITY_UNIT]
self._unique_id = f"{self._api.information.serial}_{entity_type}"
_LOGGER.debug("SynologyDSMEntity - unique_id:%s - init", self._unique_id)

@property
def unique_id(self) -> str:
Expand Down Expand Up @@ -491,6 +535,11 @@ def __init__(
):
"""Initialize the Synology DSM disk or volume entity."""
super().__init__(api, entity_type, entity_info)
_LOGGER.debug(
"SynologyDSMDeviceEntity - device_id:%s type:%s - init",
device_id,
entity_type,
)
self._device_id = device_id
self._device_name = None
self._device_manufacturer = None
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/synology_dsm/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Support for Synology DSM binary sensors."""
import logging
from typing import Dict

from homeassistant.components.binary_sensor import BinarySensorEntity
Expand All @@ -15,6 +16,8 @@
UPGRADE_BINARY_SENSORS,
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
Expand Down Expand Up @@ -47,6 +50,7 @@ async def async_setup_entry(
for sensor_type in STORAGE_DISK_BINARY_SENSORS
]

_LOGGER.debug("async_setup_entry - %s", entities)
async_add_entities(entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/synology_dsm/camera.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Support for Synology DSM cameras."""
import logging
from typing import Dict

from synology_dsm.api.surveillance_station import SynoSurveillanceStation
Expand All @@ -19,6 +20,8 @@
SYNO_API,
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
Expand All @@ -35,6 +38,7 @@ async def async_setup_entry(
cameras = surveillance_station.get_all_cameras()
entities = [SynoDSMCamera(api, camera) for camera in cameras]

_LOGGER.debug("async_setup_entry - %s", entities)
async_add_entities(entities)


Expand Down
26 changes: 26 additions & 0 deletions homeassistant/components/synology_dsm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def _ordered_shared_schema(schema_input):
class SynologyDSMFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow."""

_LOGGER.debug("SynologyDSMFlowHandler")

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL

Expand All @@ -103,6 +105,7 @@ async def _show_setup_form(self, user_input=None, errors=None):
else:
step_id = "user"
data_schema = _user_schema_with_defaults(user_input)
_LOGGER.debug("SynologyDSMFlowHandler._show_setup_form - step_id:%s", step_id)

return self.async_show_form(
step_id=step_id,
Expand All @@ -113,6 +116,7 @@ async def _show_setup_form(self, user_input=None, errors=None):

async def async_step_user(self, user_input=None):
"""Handle a flow initiated by the user."""
_LOGGER.debug("SynologyDSMFlowHandler.async_step_user")
errors = {}

if user_input is None:
Expand Down Expand Up @@ -165,7 +169,13 @@ async def async_step_user(self, user_input=None):
return await self._show_setup_form(user_input, errors)

# Check if already configured
_LOGGER.debug(
"SynologyDSMFlowHandler.async_step_user - async_set_unique_id(%s)", serial
)
await self.async_set_unique_id(serial, raise_on_progress=False)
_LOGGER.debug(
"SynologyDSMFlowHandler.async_step_user - check if already configured"
)
self._abort_if_unique_id_configured()

config_data = {
Expand All @@ -184,10 +194,15 @@ async def async_step_user(self, user_input=None):
if user_input.get(CONF_VOLUMES):
config_data[CONF_VOLUMES] = user_input[CONF_VOLUMES]

_LOGGER.debug("SynologyDSMFlowHandler.async_step_user - finish config flow")
return self.async_create_entry(title=host, data=config_data)

async def async_step_ssdp(self, discovery_info):
"""Handle a discovered synology_dsm."""
_LOGGER.debug(
"SynologyDSMFlowHandler.async_step_ssdp - discovery_info: %s",
discovery_info,
)
parsed_url = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION])
friendly_name = (
discovery_info[ssdp.ATTR_UPNP_FRIENDLY_NAME].split("(", 1)[0].strip()
Expand All @@ -197,6 +212,10 @@ async def async_step_ssdp(self, discovery_info):
# Synology NAS can broadcast on multiple IP addresses, since they can be connected to multiple ethernets.
# The serial of the NAS is actually its MAC address.
if self._mac_already_configured(mac):
_LOGGER.debug(
"SynologyDSMFlowHandler.async_step_ssdp - mac %s is already configured",
mac,
)
return self.async_abort(reason="already_configured")

await self.async_set_unique_id(mac)
Expand Down Expand Up @@ -237,11 +256,18 @@ async def async_step_2sa(self, user_input, errors=None):

def _mac_already_configured(self, mac):
"""See if we already have configured a NAS with this MAC address."""
_LOGGER.debug(
"SynologyDSMFlowHandler._mac_already_configured - check mac %s", mac
)
existing_macs = [
mac.replace("-", "")
for entry in self._async_current_entries()
for mac in entry.data.get(CONF_MAC, [])
]
_LOGGER.debug(
"SynologyDSMFlowHandler._mac_already_configured - existing macs %s",
existing_macs,
)
return mac in existing_macs


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/synology_dsm/sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Synology DSM sensors."""
from datetime import timedelta
import logging
from typing import Dict

from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -27,6 +28,8 @@
UTILISATION_SENSORS,
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
Expand Down Expand Up @@ -65,6 +68,7 @@ async def async_setup_entry(
for sensor_type in INFORMATION_SENSORS
]

_LOGGER.debug("async_setup_entry - %s", entities)
async_add_entities(entities)


Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/synology_dsm/switch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Support for Synology DSM switch."""
import logging
from typing import Dict

from synology_dsm.api.surveillance_station import SynoSurveillanceStation
Expand All @@ -10,6 +11,8 @@
from . import SynoApi, SynologyDSMEntity
from .const import DOMAIN, SURVEILLANCE_SWITCH, SYNO_API

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
Expand All @@ -30,6 +33,7 @@ async def async_setup_entry(
for sensor_type in SURVEILLANCE_SWITCH
]

_LOGGER.debug("async_setup_entry - %s", entities)
async_add_entities(entities, True)


Expand Down