2020-09-30 16:27:06 +00:00
|
|
|
"""Support for Synology DSM switch."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-01-31 19:22:46 +00:00
|
|
|
import logging
|
2021-05-09 20:44:55 +00:00
|
|
|
from typing import Any
|
2020-09-30 16:27:06 +00:00
|
|
|
|
|
|
|
from synology_dsm.api.surveillance_station import SynoSurveillanceStation
|
|
|
|
|
|
|
|
from homeassistant.components.switch import ToggleEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-22 14:21:38 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-01 22:37:19 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2021-05-09 20:44:55 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-02-23 22:23:50 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2020-09-30 16:27:06 +00:00
|
|
|
|
2021-02-23 22:23:50 +00:00
|
|
|
from . import SynoApi, SynologyDSMBaseEntity
|
2021-05-09 20:44:55 +00:00
|
|
|
from .const import (
|
|
|
|
COORDINATOR_SWITCHES,
|
|
|
|
DOMAIN,
|
|
|
|
SURVEILLANCE_SWITCH,
|
|
|
|
SYNO_API,
|
|
|
|
EntityInfo,
|
|
|
|
)
|
2020-09-30 16:27:06 +00:00
|
|
|
|
2021-01-31 19:22:46 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-09-30 16:27:06 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-05-09 20:44:55 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-09-30 16:27:06 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Synology NAS switch."""
|
|
|
|
|
2021-02-23 22:23:50 +00:00
|
|
|
data = hass.data[DOMAIN][entry.unique_id]
|
2021-05-09 20:44:55 +00:00
|
|
|
api: SynoApi = data[SYNO_API]
|
2020-09-30 16:27:06 +00:00
|
|
|
|
|
|
|
entities = []
|
|
|
|
|
|
|
|
if SynoSurveillanceStation.INFO_API_KEY in api.dsm.apis:
|
|
|
|
info = await hass.async_add_executor_job(api.dsm.surveillance_station.get_info)
|
|
|
|
version = info["data"]["CMSMinVersion"]
|
2021-02-23 22:23:50 +00:00
|
|
|
|
|
|
|
# initial data fetch
|
2021-05-09 20:44:55 +00:00
|
|
|
coordinator: DataUpdateCoordinator = data[COORDINATOR_SWITCHES]
|
2021-02-23 22:23:50 +00:00
|
|
|
await coordinator.async_refresh()
|
2020-09-30 16:27:06 +00:00
|
|
|
entities += [
|
|
|
|
SynoDSMSurveillanceHomeModeToggle(
|
2021-07-15 04:44:57 +00:00
|
|
|
api, sensor_type, switch, version, coordinator
|
2020-09-30 16:27:06 +00:00
|
|
|
)
|
2021-07-15 04:44:57 +00:00
|
|
|
for sensor_type, switch in SURVEILLANCE_SWITCH.items()
|
2020-09-30 16:27:06 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2021-02-23 22:23:50 +00:00
|
|
|
class SynoDSMSurveillanceHomeModeToggle(SynologyDSMBaseEntity, ToggleEntity):
|
2020-09-30 16:27:06 +00:00
|
|
|
"""Representation a Synology Surveillance Station Home Mode toggle."""
|
|
|
|
|
2021-05-09 20:44:55 +00:00
|
|
|
coordinator: DataUpdateCoordinator[dict[str, dict[str, bool]]]
|
|
|
|
|
2020-09-30 16:27:06 +00:00
|
|
|
def __init__(
|
2021-02-23 22:23:50 +00:00
|
|
|
self,
|
|
|
|
api: SynoApi,
|
|
|
|
entity_type: str,
|
2021-05-09 20:44:55 +00:00
|
|
|
entity_info: EntityInfo,
|
2021-02-23 22:23:50 +00:00
|
|
|
version: str,
|
2021-05-09 20:44:55 +00:00
|
|
|
coordinator: DataUpdateCoordinator[dict[str, dict[str, bool]]],
|
|
|
|
) -> None:
|
2020-09-30 16:27:06 +00:00
|
|
|
"""Initialize a Synology Surveillance Station Home Mode."""
|
|
|
|
super().__init__(
|
|
|
|
api,
|
|
|
|
entity_type,
|
|
|
|
entity_info,
|
2021-02-23 22:23:50 +00:00
|
|
|
coordinator,
|
2020-09-30 16:27:06 +00:00
|
|
|
)
|
|
|
|
self._version = version
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return the state."""
|
2021-02-23 22:23:50 +00:00
|
|
|
return self.coordinator.data["switches"][self.entity_type]
|
2020-09-30 16:27:06 +00:00
|
|
|
|
2021-05-09 20:44:55 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2020-09-30 16:27:06 +00:00
|
|
|
"""Turn on Home mode."""
|
2021-01-31 19:22:46 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"SynoDSMSurveillanceHomeModeToggle.turn_on(%s)",
|
|
|
|
self._api.information.serial,
|
|
|
|
)
|
2021-02-23 22:23:50 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
self._api.dsm.surveillance_station.set_home_mode, True
|
|
|
|
)
|
|
|
|
await self.coordinator.async_request_refresh()
|
2020-09-30 16:27:06 +00:00
|
|
|
|
2021-05-09 20:44:55 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2020-09-30 16:27:06 +00:00
|
|
|
"""Turn off Home mode."""
|
2021-01-31 19:22:46 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"SynoDSMSurveillanceHomeModeToggle.turn_off(%s)",
|
|
|
|
self._api.information.serial,
|
|
|
|
)
|
2021-02-23 22:23:50 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
self._api.dsm.surveillance_station.set_home_mode, False
|
|
|
|
)
|
|
|
|
await self.coordinator.async_request_refresh()
|
2020-09-30 16:27:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return bool(self._api.surveillance_station)
|
|
|
|
|
|
|
|
@property
|
2021-05-01 22:37:19 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2020-09-30 16:27:06 +00:00
|
|
|
"""Return the device information."""
|
|
|
|
return {
|
|
|
|
"identifiers": {
|
|
|
|
(
|
|
|
|
DOMAIN,
|
2021-05-09 20:44:55 +00:00
|
|
|
f"{self._api.information.serial}_{SynoSurveillanceStation.INFO_API_KEY}",
|
2020-09-30 16:27:06 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
"name": "Surveillance Station",
|
|
|
|
"manufacturer": "Synology",
|
|
|
|
"model": self._api.information.model,
|
|
|
|
"sw_version": self._version,
|
|
|
|
"via_device": (DOMAIN, self._api.information.serial),
|
|
|
|
}
|