<s> """<STR_LIT>""" <EOL> import logging <EOL> from datetime import timedelta <EOL> from homeassistant . const import CONF_HOST , CONF_PASSWORD , CONF_USERNAME <EOL> from homeassistant . helpers . entity import Entity <EOL> from homeassistant . util import Throttle <EOL> REQUIREMENTS = [ '<STR_LIT>' ] <EOL> SENSOR_TYPES = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' , None ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> } <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> _THROTTLED_REFRESH = None <EOL> def setup_platform ( hass , config , add_devices , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> import transmissionrpc <EOL> from transmissionrpc . error import TransmissionError <EOL> host = config . get ( CONF_HOST ) <EOL> username = config . get ( CONF_USERNAME , None ) <EOL> password = config . get ( CONF_PASSWORD , None ) <EOL> port = config . get ( '<STR_LIT:port>' , <NUM_LIT> ) <EOL> name = config . get ( "<STR_LIT:name>" , "<STR_LIT>" ) <EOL> if not host : <EOL> _LOGGER . error ( '<STR_LIT>' , CONF_HOST ) <EOL> return False <EOL> transmission_api = transmissionrpc . Client ( <EOL> host , port = port , user = username , password = password ) <EOL> try : <EOL> transmission_api . session_stats ( ) <EOL> except TransmissionError : <EOL> _LOGGER . exception ( "<STR_LIT>" ) <EOL> return False <EOL> global _THROTTLED_REFRESH <EOL> _THROTTLED_REFRESH = Throttle ( timedelta ( seconds = <NUM_LIT:1> ) ) ( <EOL> transmission_api . session_stats ) <EOL> dev = [ ] <EOL> for variable in config [ '<STR_LIT>' ] : <EOL> if variable not in SENSOR_TYPES : <EOL> _LOGGER . error ( '<STR_LIT>' , variable ) <EOL> else : <EOL> dev . append ( TransmissionSensor ( <EOL> variable , transmission_api , name ) ) <EOL> add_devices ( dev ) <EOL> class TransmissionSensor ( Entity ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sensor_type , transmission_client , client_name ) : <EOL> """<STR_LIT>""" <EOL> self . _name = SENSOR_TYPES [ sensor_type ] [ <NUM_LIT:0> ] <EOL> self . transmission_client = transmission_client <EOL> self . type = sensor_type <EOL> self . client_name = client_name <EOL> self . _state = None <EOL> self . _unit_of_measurement = SENSOR_TYPES [ sensor_type ] [ <NUM_LIT:1> ] <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . client_name + '<STR_LIT:U+0020>' + self . _name <EOL> @ property <EOL> def state ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _state <EOL> @ property <EOL> def unit_of_measurement ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _unit_of_measurement <EOL> def refresh_transmission_data ( self ) : <EOL> """<STR_LIT>""" <EOL> from transmissionrpc . error import TransmissionError <EOL> if _THROTTLED_REFRESH is not None : <EOL> try : <EOL> _THROTTLED_REFRESH ( ) <EOL> except TransmissionError : <EOL> _LOGGER . exception ( <EOL> self . name + "<STR_LIT>" <EOL> ) <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> self . refresh_transmission_data ( ) <EOL> if self . type == '<STR_LIT>' : <EOL> if self . transmission_client . session : <EOL> upload = self . transmission_client . session . uploadSpeed <EOL> download = self . transmission_client . session . downloadSpeed <EOL> if upload > <NUM_LIT:0> and download > <NUM_LIT:0> : <EOL> self . _state = '<STR_LIT>' <EOL> elif upload > <NUM_LIT:0> and download == <NUM_LIT:0> : <EOL> self . _state = '<STR_LIT>' <EOL> elif upload == <NUM_LIT:0> and download > <NUM_LIT:0> : <EOL> self . _state = '<STR_LIT>' <EOL> else : <EOL> self . _state = '<STR_LIT>' <EOL> else : <EOL> self . _state = '<STR_LIT>' <EOL> if self . transmission_client . session : <EOL> if self . type == '<STR_LIT>' : <EOL> mb_spd = float ( self . transmission_client . session . downloadSpeed ) <EOL> mb_spd = mb_spd / <NUM_LIT> / <NUM_LIT> <EOL> self . _state = round ( mb_spd , <NUM_LIT:2> if mb_spd < <NUM_LIT:0.1> else <NUM_LIT:1> ) <EOL> elif self . type == '<STR_LIT>' : <EOL> mb_spd = float ( self . transmission_client . session . uploadSpeed ) <EOL> mb_spd = mb_spd / <NUM_LIT> / <NUM_LIT> <EOL> self . _state = round ( mb_spd , <NUM_LIT:2> if mb_spd < <NUM_LIT:0.1> else <NUM_LIT:1> ) </s>
