hass-core/homeassistant/components/deluge/switch.py

91 lines
2.9 KiB
Python
Raw Normal View History

"""Support for setting the Deluge BitTorrent client in Pause."""
from __future__ import annotations
2022-03-23 00:01:24 -04:00
from typing import Any
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
2022-03-23 00:01:24 -04:00
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
2019-07-31 12:25:30 -07:00
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
2019-07-31 12:25:30 -07:00
CONF_USERNAME,
2022-03-23 00:01:24 -04:00
Platform,
2019-07-31 12:25:30 -07:00
)
from homeassistant.core import HomeAssistant
2022-03-23 00:01:24 -04:00
from homeassistant.helpers import entity_platform
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2022-03-23 00:01:24 -04:00
from . import DelugeEntity
from .const import DEFAULT_RPC_PORT, DOMAIN
from .coordinator import DelugeDataUpdateCoordinator
2022-03-23 00:01:24 -04:00
# Deprecated in Home Assistant 2022.3
2019-07-31 12:25:30 -07:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
2022-03-23 00:01:24 -04:00
vol.Optional(CONF_PORT, default=DEFAULT_RPC_PORT): cv.port,
2019-07-31 12:25:30 -07:00
vol.Required(CONF_USERNAME): cv.string,
2022-03-23 00:01:24 -04:00
vol.Optional(CONF_NAME, default="Deluge Switch"): cv.string,
2019-07-31 12:25:30 -07:00
}
)
2022-03-23 00:01:24 -04:00
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
2022-03-23 00:01:24 -04:00
async_add_entities: entity_platform.AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2022-03-23 00:01:24 -04:00
"""Set up the Deluge sensor component."""
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
)
)
2022-03-23 00:01:24 -04:00
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: entity_platform.AddEntitiesCallback,
) -> None:
"""Set up the Deluge switch."""
async_add_entities([DelugeSwitch(hass.data[DOMAIN][entry.entry_id])])
2022-03-23 00:01:24 -04:00
class DelugeSwitch(DelugeEntity, SwitchEntity):
"""Representation of a Deluge switch."""
2022-03-23 00:01:24 -04:00
def __init__(self, coordinator: DelugeDataUpdateCoordinator) -> None:
"""Initialize the Deluge switch."""
2022-03-23 00:01:24 -04:00
super().__init__(coordinator)
self._attr_name = coordinator.config_entry.title
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_enabled"
2022-03-23 00:01:24 -04:00
def turn_on(self, **kwargs: Any) -> None:
"""Turn the device on."""
2022-03-23 00:01:24 -04:00
torrent_ids = self.coordinator.api.call("core.get_session_state")
self.coordinator.api.call("core.resume_torrent", torrent_ids)
2022-03-23 00:01:24 -04:00
def turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
2022-03-23 00:01:24 -04:00
torrent_ids = self.coordinator.api.call("core.get_session_state")
self.coordinator.api.call("core.pause_torrent", torrent_ids)
2022-03-23 00:01:24 -04:00
@property
def is_on(self) -> bool:
"""Return state of the switch."""
if self.coordinator.data:
data: dict = self.coordinator.data[Platform.SWITCH]
for torrent in data.values():
item = torrent.popitem()
if not item[1]:
return True
return False