<s> """<STR_LIT>""" <EOL> import logging <EOL> import requests <EOL> from homeassistant . components . switch import DOMAIN , SwitchDevice <EOL> from homeassistant . const import CONF_PASSWORD , CONF_USERNAME <EOL> from homeassistant . helpers import validate_config <EOL> REQUIREMENTS = [ '<STR_LIT>' ] <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> SWITCH_MODELS = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> CONF_TLS = '<STR_LIT>' <EOL> CONF_VERIFY_TLS = '<STR_LIT>' <EOL> def setup_platform ( hass , config , add_devices , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> if not validate_config ( { DOMAIN : config } , <EOL> { DOMAIN : [ '<STR_LIT:host>' , <EOL> CONF_USERNAME , <EOL> CONF_PASSWORD ] } , <EOL> _LOGGER ) : <EOL> _LOGGER . error ( '<STR_LIT>' ) <EOL> return False <EOL> host = config . get ( '<STR_LIT:host>' ) <EOL> username = config . get ( '<STR_LIT:username>' ) <EOL> password = config . get ( '<STR_LIT:password>' ) <EOL> use_tls = bool ( config . get ( CONF_TLS , True ) ) <EOL> verify_tls = bool ( config . get ( CONF_VERIFY_TLS , True ) ) <EOL> default_port = use_tls and <NUM_LIT> or <NUM_LIT> <EOL> port = int ( config . get ( '<STR_LIT:port>' , default_port ) ) <EOL> from mficlient . client import FailedToLogin , MFiClient <EOL> try : <EOL> client = MFiClient ( host , username , password , port = port , <EOL> use_tls = use_tls , verify = verify_tls ) <EOL> except ( FailedToLogin , requests . exceptions . ConnectionError ) as ex : <EOL> _LOGGER . error ( '<STR_LIT>' , str ( ex ) ) <EOL> return False <EOL> add_devices ( MfiSwitch ( port ) <EOL> for device in client . get_devices ( ) <EOL> for port in device . ports . values ( ) <EOL> if port . model in SWITCH_MODELS ) <EOL> class MfiSwitch ( SwitchDevice ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , port ) : <EOL> """<STR_LIT>""" <EOL> self . _port = port <EOL> self . _target_state = None <EOL> @ property <EOL> def should_poll ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> @ property <EOL> def unique_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _port . ident <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _port . label <EOL> @ property <EOL> def is_on ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _port . output <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _port . refresh ( ) <EOL> if self . _target_state is not None : <EOL> self . _port . data [ '<STR_LIT>' ] = float ( self . _target_state ) <EOL> self . _target_state = None <EOL> def turn_on ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _port . control ( True ) <EOL> self . _target_state = True <EOL> def turn_off ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _port . control ( False ) <EOL> self . _target_state = False <EOL> @ property <EOL> def current_power_mwh ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . _port . data . get ( '<STR_LIT>' , <NUM_LIT:0> ) * <NUM_LIT:1000> ) <EOL> @ property <EOL> def device_state_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> attr = { } <EOL> attr [ '<STR_LIT>' ] = round ( self . _port . data . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <NUM_LIT:1> ) <EOL> attr [ '<STR_LIT>' ] = round ( self . _port . data . get ( '<STR_LIT>' , <NUM_LIT:0> ) , <NUM_LIT:1> ) <EOL> return attr </s>
