def __init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs): indigo.PluginBase.__init__(self, pluginId, pluginDisplayName, pluginVersion, pluginPrefs) self.debug = DEBUG self.active_remote_sensors = [] self.active_thermostats = [] self.active_smart_thermostats = [] logHandler = IndigoLoggingHandler(self) pyecobeeLogger = logging.getLogger('pyecobee') pyecobeeLogger.addHandler(logHandler) self.log = logging.getLogger('indigo.ecobee.plugin') self.log.addHandler(logHandler) try: self.logLevel = int(self.pluginPrefs[u"logLevel"]) except: self.logLevel = logging.INFO pyecobeeLogger.setLevel(self.logLevel) self.log.setLevel(self.logLevel) self.log.debug(u"logLevel = " + str(self.logLevel)) if TEMPERATURE_SCALE_PLUGIN_PREF in pluginPrefs: self._setTemperatureScale( pluginPrefs[TEMPERATURE_SCALE_PLUGIN_PREF][0]) else: self._setTemperatureScale('F') tmpconfig = {'API_KEY': API_KEY} if ACCESS_TOKEN_PLUGIN_PREF in pluginPrefs: tmpconfig['ACCESS_TOKEN'] = pluginPrefs[ACCESS_TOKEN_PLUGIN_PREF] if AUTHORIZATION_CODE_PLUGIN_PREF in pluginPrefs: tmpconfig['AUTHORIZATION_CODE'] = pluginPrefs[ AUTHORIZATION_CODE_PLUGIN_PREF] if REFRESH_TOKEN_PLUGIN_PREF in pluginPrefs: tmpconfig['REFRESH_TOKEN'] = pluginPrefs[REFRESH_TOKEN_PLUGIN_PREF] self.log.debug(u"constructed pyecobee config: %s" % json.dumps(tmpconfig)) # Create an ecobee object with the config dictionary self.ecobee = pyecobee.Ecobee(config=tmpconfig) self.pluginPrefs["pin"] = self.ecobee.pin if self.ecobee.authenticated: self.pluginPrefs[ ACCESS_TOKEN_PLUGIN_PREF] = self.ecobee.access_token self.pluginPrefs[ AUTHORIZATION_CODE_PLUGIN_PREF] = self.ecobee.authorization_code self.pluginPrefs[ REFRESH_TOKEN_PLUGIN_PREF] = self.ecobee.refresh_token if self.ecobee.refresh_token == '': self.pluginPrefs[ACCESS_TOKEN_PLUGIN_PREF] = '' self.pluginPrefs[AUTHORIZATION_CODE_PLUGIN_PREF] = '' self.pluginPrefs[REFRESH_TOKEN_PLUGIN_PREF] = '' self.errorLog( 'Ecobee device requires authentication; open plugin configuration page for info' )
def connect_app_and_account(optiondict): # first create logging for what we are donig logger.info("Connecting application to account") logger.info("Have user get prepared by logging into the consumerportal") # STEP1 - check to see if the configuration file exists that the user passed in if optiondict['config_filename']: config_filename = optiondict['config_filename'] if not os.path.isfile(config_filename): logger.info( 'Config_filename specified but does not exist, creating it') jsonconfig = {"API_KEY": optiondict['api_key']} pyecobee.config_from_file(config_filename, jsonconfig) # STEP2 - display message to the user print("Connecting this application to an account") print( "Please login to the account that we will be connecting to by going to:" ) print("https://www.ecobee.com/consumerportal/index.html") # get the input when the user is ready for us to move on name = input("\nPress [Enter] when this is done ==> ") # logging logger.info("Consumer has the portal ready to go") # STEP3 - create the ecobee object with no configuration file - we are starting new here logger.info("Building ecobee object") ecobee = pyecobee.Ecobee(api_key=optiondict['api_key'], config_filename=optiondict['config_filename']) # STEP4 - should have failed, but if it did not - then no work to do - exit if ecobee.authenticated: # logging logger.info( "App already connected to account - no additional action required - terminating program" ) print( "App already connected to account - no additional action required - terminating program" ) sys.exit(1) # STEP 4 - did not fail - we need to request a pin logger.info('App is now requesting a pin be generated') print('App requesting a pin') ecobee.request_pin() logger.info('Delivered pin:' + ecobee.pin) if not ecobee.pin: logger.info('A pin was not created - failing') print('A pin was not generated - there is a problem - EXITTING') sys.exit(1) # logging logger.info( "Request user to enter PIN into consumer portal My Apps, Add application" ) # STEP5 - we have the PIN code that the user needs to authenticate - have them go do it. print("Go to the consumerportal web page and click") print('My Apps, Add application, and when prompted enter PIN: [', ecobee.pin, '] and click Authorize.') print('Once authorized, click Add application') # get the input when the user is ready for us to move on name = input("\nPress [Enter] when this is done ==> ") # logging logger.info( "User entered PIN [ %s ] - application will now call request_tokens", ecobee.pin) # STEP6 - call the request_tokens if ecobee.request_tokens(): print('Request_tokens delivered true and autenticated is:', ecobee.authenticated) # STEP7 - validate we got what we wanted if ecobee.pin is not None: logger.error( "Request_tokens call did not work - you are still not connected - please try again" ) print( "Request_tokens call did not work - you are still not connected - please try again" ) sys.exit(1) else: logger.info( 'Request_tokens now has the thermostat connected and a configuration file generated for future runs' ) print( 'Request_tokens now has the thermostat connected and a configuration file generated for future runs' ) # STEP8 - make sure this delivers a working solution - get thermostats logger.info('Validating we can authenticate') ecobee.get_thermostats() print('We should be authenticated:', ecobee.authenticated) if not ecobee.authenticated: logger.info('failed to authenticate') else: logger.info('All worked as expected - we are authenticated') sys.exit()
# set variables based on what came form command line debug = optiondict['debug'] # print header to show what is going on (convert this to a kvutil function: kvutil.loggingStart(logger,optiondict)) kvutil.loggingAppStart(logger, optiondict, kvutil.scriptinfo()['name']) # OPTIONAL ACTION - determine if we are connecting and if so do that - the called routine will exit - no other work to do. if optiondict['connect']: connect_app_and_account(optiondict) # create the ecobee object logger.info( "Building ecobee object - it may refresh the tokens and update config file:%s", optiondict['config_filename']) ecobee = pyecobee.Ecobee(api_key=optiondict['api_key'], config_filename=optiondict['config_filename']) # read in therms, save data, and get therm values logger.info('Fetch ecobee thermostat data - save temp readings to file:%s', optiondict['temperature_filename']) therms = readSave_thermoSensor_rtn_therms( ecobee, optiondict['temperature_filename'], debug=debug) # validate that we were authenticated after we called in if not ecobee.authenticated: logger.error("Did not authenticate - program terminated!") print("Did not authenticate - program terminated!") sys.exit(1) # check to see if system holds are on systemHoldOn = systemHoldOn_chk(therms, holdSetting, debug=debug)