Fix init of TransmissionData (#20817)

* Fix init of TransmissionData

 Fix in order to avoid null object on first update of Turtle Mode Switch

* Using async functionality

* Various fix

* HoundBot fix

* Removed some async calls

* Fix compilation Error

* Fix

* PEP fix
This commit is contained in:
MatteGary authored and Martin Hjelmare committed 2019-02-08 18:15:14 +01:00
1 parent 6a78ad8ab6
commit faf7ae29b1
3 files changed
+59 -16

No files matched your search

@@ -9,10 +9,11 @@ from datetime import timedelta
import logging
from homeassistant.components.transmission import (
DATA_TRANSMISSION, SENSOR_TYPES)
DATA_TRANSMISSION, SENSOR_TYPES, DATA_UPDATED)
from homeassistant.const import STATE_IDLE
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
DEPENDENCIES = ['transmission']
@@ -23,7 +24,11 @@ DEFAULT_NAME = 'Transmission'
SCAN_INTERVAL = timedelta(seconds=120)
def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(
hass,
config,
async_add_entities,
discovery_info=None):
"""Set up the Transmission sensors."""
if discovery_info is None:
return
@@ -41,7 +46,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
SENSOR_TYPES[sensor_type][0],
SENSOR_TYPES[sensor_type][1]))
add_entities(dev, True)
async_add_entities(dev, True)
class TransmissionSensor(Entity):
@@ -73,6 +78,11 @@ class TransmissionSensor(Entity):
"""Return the state of the sensor."""
return self._state
@property
def should_poll(self):
"""Return the polling requirement for this sensor."""
return False
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
@@ -83,10 +93,18 @@ class TransmissionSensor(Entity):
"""Could the device be accessed during the last update call."""
return self._transmission_api.available
@Throttle(SCAN_INTERVAL)
async def async_added_to_hass(self):
"""Handle entity which will be added."""
async_dispatcher_connect(
self.hass, DATA_UPDATED, self._schedule_immediate_update
)
@callback
def _schedule_immediate_update(self):
self.async_schedule_update_ha_state(True)
def update(self):
"""Get the latest data from Transmission and updates the state."""
self._transmission_api.update()
self._data = self._transmission_api.data
if self.type == 'completed_torrents':