diff --git a/CODEOWNERS b/CODEOWNERS index 40e37ec4697..46ffd1196f7 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -306,7 +306,7 @@ homeassistant/components/tplink/* @rytilahti homeassistant/components/traccar/* @ludeeus homeassistant/components/tradfri/* @ggravlingen homeassistant/components/trafikverket_train/* @endor-force -homeassistant/components/transmission/* @engrbm87 +homeassistant/components/transmission/* @engrbm87 @JPHutchins homeassistant/components/tts/* @robbiet480 homeassistant/components/twentemilieu/* @frenck homeassistant/components/twilio_call/* @robbiet480 diff --git a/homeassistant/components/transmission/__init__.py b/homeassistant/components/transmission/__init__.py index 6cfd6bf640a..be41ca85998 100644 --- a/homeassistant/components/transmission/__init__.py +++ b/homeassistant/components/transmission/__init__.py @@ -232,6 +232,7 @@ class TransmissionData: self._api = api self.completed_torrents = [] self.started_torrents = [] + self.started_torrent_dict = {} @property def host(self): @@ -250,6 +251,7 @@ class TransmissionData: self.torrents = self._api.get_torrents() self.session = self._api.get_session() + self.check_started_torrent_info() self.check_completed_torrent() self.check_started_torrent() _LOGGER.debug("Torrent Data for %s Updated", self.host) @@ -301,6 +303,31 @@ class TransmissionData: self.hass.bus.fire("transmission_started_torrent", {"name": var}) self.started_torrents = actual_started_torrents + def check_started_torrent_info(self): + """Get started torrent info functionality.""" + all_torrents = self._api.get_torrents() + current_down = {} + + for torrent in all_torrents: + if torrent.status == "downloading": + info = self.started_torrent_dict[torrent.name] = { + "added_date": torrent.addedDate, + "percent_done": f"{torrent.percentDone * 100:.2f}", + } + try: + info["eta"] = str(torrent.eta) + except ValueError: + info["eta"] = "unknown" + + current_down[torrent.name] = True + + elif torrent.name in self.started_torrent_dict: + self.started_torrent_dict.pop(torrent.name) + + for torrent in list(self.started_torrent_dict): + if torrent not in current_down: + self.started_torrent_dict.pop(torrent) + def get_started_torrent_count(self): """Get the number of started torrents.""" return len(self.started_torrents) diff --git a/homeassistant/components/transmission/const.py b/homeassistant/components/transmission/const.py index 472bb32a391..5540f718ba1 100644 --- a/homeassistant/components/transmission/const.py +++ b/homeassistant/components/transmission/const.py @@ -17,6 +17,7 @@ DEFAULT_NAME = "Transmission" DEFAULT_PORT = 9091 DEFAULT_SCAN_INTERVAL = 120 +STATE_ATTR_TORRENT_INFO = "torrent_info" ATTR_TORRENT = "torrent" SERVICE_ADD_TORRENT = "add_torrent" diff --git a/homeassistant/components/transmission/manifest.json b/homeassistant/components/transmission/manifest.json index c2fa31d7b50..9618a5677ad 100644 --- a/homeassistant/components/transmission/manifest.json +++ b/homeassistant/components/transmission/manifest.json @@ -8,6 +8,7 @@ ], "dependencies": [], "codeowners": [ - "@engrbm87" + "@engrbm87", + "@JPHutchins" ] -} \ No newline at end of file +} diff --git a/homeassistant/components/transmission/sensor.py b/homeassistant/components/transmission/sensor.py index d9fd2b51144..489582de157 100644 --- a/homeassistant/components/transmission/sensor.py +++ b/homeassistant/components/transmission/sensor.py @@ -6,7 +6,8 @@ from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity -from .const import DOMAIN, SENSOR_TYPES +from .const import DOMAIN, SENSOR_TYPES, STATE_ATTR_TORRENT_INFO + _LOGGER = logging.getLogger(__name__) @@ -82,6 +83,13 @@ class TransmissionSensor(Entity): """Could the device be accessed during the last update call.""" return self._tm_client.api.available + @property + def device_state_attributes(self): + """Return the state attributes, if any.""" + if self._tm_client.api.started_torrent_dict and self.type == "started_torrents": + return {STATE_ATTR_TORRENT_INFO: self._tm_client.api.started_torrent_dict} + return None + async def async_added_to_hass(self): """Handle entity which will be added.""" async_dispatcher_connect(