Add config flow to transmission (#26434)
* Add config flow to transmission * Reworked code to add all sensors and switches * applied fixes * final touches * Add tests * fixed tests * fix get_api errors and entities availabilty update * update config_flows.py * fix pylint error * update .coveragerc * add codeowner * add test_options * fixed test_options
This commit is contained in:
1 parent
9b204ad162
commit
82b77c2d29
17 files changed
+723
-128
No files matched your search
@@ -1,43 +1,50 @@
|
||||
"""Support for setting the Transmission BitTorrent client Turtle Mode."""
|
||||
import logging
|
||||
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
|
||||
from . import DATA_TRANSMISSION, DATA_UPDATED
|
||||
from .const import DATA_TRANSMISSION, DATA_UPDATED, DOMAIN, SWITCH_TYPES
|
||||
|
||||
_LOGGING = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME = "Transmission Turtle Mode"
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Import config from configuration.yaml."""
|
||||
pass
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up the Transmission switch."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
component_name = DATA_TRANSMISSION
|
||||
transmission_api = hass.data[component_name]
|
||||
name = discovery_info["client_name"]
|
||||
transmission_api = hass.data[DOMAIN][DATA_TRANSMISSION]
|
||||
name = config_entry.data[CONF_NAME]
|
||||
|
||||
async_add_entities([TransmissionSwitch(transmission_api, name)], True)
|
||||
dev = []
|
||||
for switch_type, switch_name in SWITCH_TYPES.items():
|
||||
dev.append(TransmissionSwitch(switch_type, switch_name, transmission_api, name))
|
||||
|
||||
async_add_entities(dev, True)
|
||||
|
||||
|
||||
class TransmissionSwitch(ToggleEntity):
|
||||
"""Representation of a Transmission switch."""
|
||||
|
||||
def __init__(self, transmission_client, name):
|
||||
def __init__(self, switch_type, switch_name, transmission_api, name):
|
||||
"""Initialize the Transmission switch."""
|
||||
self._name = name
|
||||
self.transmission_client = transmission_client
|
||||
self._name = switch_name
|
||||
self.client_name = name
|
||||
self.type = switch_type
|
||||
self._transmission_api = transmission_api
|
||||
self._state = STATE_OFF
|
||||
self._data = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the switch."""
|
||||
return self._name
|
||||
return f"{self.client_name} {self._name}"
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
@@ -54,15 +61,30 @@ class TransmissionSwitch(ToggleEntity):
|
||||
"""Return true if device is on."""
|
||||
return self._state == STATE_ON
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Could the device be accessed during the last update call."""
|
||||
return self._transmission_api.available
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the device on."""
|
||||
_LOGGING.debug("Turning Turtle Mode of Transmission on")
|
||||
self.transmission_client.set_alt_speed_enabled(True)
|
||||
if self.type == "on_off":
|
||||
_LOGGING.debug("Starting all torrents")
|
||||
self._transmission_api.start_torrents()
|
||||
elif self.type == "turtle_mode":
|
||||
_LOGGING.debug("Turning Turtle Mode of Transmission on")
|
||||
self._transmission_api.set_alt_speed_enabled(True)
|
||||
self._transmission_api.update()
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the device off."""
|
||||
_LOGGING.debug("Turning Turtle Mode of Transmission off")
|
||||
self.transmission_client.set_alt_speed_enabled(False)
|
||||
if self.type == "on_off":
|
||||
_LOGGING.debug("Stoping all torrents")
|
||||
self._transmission_api.stop_torrents()
|
||||
if self.type == "turtle_mode":
|
||||
_LOGGING.debug("Turning Turtle Mode of Transmission off")
|
||||
self._transmission_api.set_alt_speed_enabled(False)
|
||||
self._transmission_api.update()
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Handle entity which will be added."""
|
||||
@@ -76,7 +98,14 @@ class TransmissionSwitch(ToggleEntity):
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data from Transmission and updates the state."""
|
||||
active = self.transmission_client.get_alt_speed_enabled()
|
||||
active = None
|
||||
if self.type == "on_off":
|
||||
self._data = self._transmission_api.data
|
||||
if self._data:
|
||||
active = self._data.activeTorrentCount > 0
|
||||
|
||||
elif self.type == "turtle_mode":
|
||||
active = self._transmission_api.get_alt_speed_enabled()
|
||||
|
||||
if active is None:
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user