def pegaDadosSolar(): global gDadosSolar global gEnvios ''' pega dados solar ''' dados_solar = pega_solar(HOYMILES_PLANT_ID) gEnvios['load_time'] = dl.strDateTimeZone( 'now').isoformat() # datetime.today().strftime('%Y-%m-%d %H:%M:%S') gEnvios['load_cnt'] = gEnvios['load_cnt'] + 1 if DEVELOPERS_MODE: print("dados_solar: " + str(dados_solar)) gDadosSolar = dados_solar['data'] capacidade = dl.float2number(gDadosSolar['capacitor']) real_power = dl.float2number(gDadosSolar['real_power']) if real_power == 0: # é igual a 0 printC(Color.B_Red, "REAL_POWER = 0") time.sleep(60) # espera 60 segundos printC(Color.F_Blue, "Getting data again") dados_solar = pega_solar(HOYMILES_PLANT_ID) gDadosSolar = dados_solar['data'] capacidade = dl.float2number(gDadosSolar['capacitor']) real_power = dl.float2number(gDadosSolar['real_power']) if capacidade == 0: # é um erro print(Color.B_Red + "Erro capacitor: " + str(capacidade) + Color.B_Default) else: ajustaDadosSolar() return gDadosSolar
def get_secrets(): ''' GET configuration data ''' global HOYMILES_USER global HOYMILES_PASSWORD global HOYMILES_PLANT_ID global MQTT_HOST global MQTT_PASSWORD global MQTT_USERNAME global DEVELOPERS_MODE config = dl.getConfigParser(SECRETS) printC(Color.F_LightGreen, "Reading secrets.ini") # le os dados HOYMILES_USER = dl.get_config(config, 'secrets', 'HOYMILES_USER', HOYMILES_USER) HOYMILES_PASSWORD = dl.get_config(config, 'secrets', 'HOYMILES_PASSWORD', HOYMILES_PASSWORD) HOYMILES_PLANT_ID = dl.get_config(config, 'secrets', 'HOYMILES_PLANT_ID', HOYMILES_PLANT_ID, getInt=True) MQTT_PASSWORD = dl.get_config(config, 'secrets', 'MQTT_PASS', MQTT_PASSWORD) MQTT_USERNAME = dl.get_config(config, 'secrets', 'MQTT_USER', MQTT_USERNAME) MQTT_HOST = dl.get_config(config, 'secrets', 'MQTT_HOST', MQTT_HOST) dev_mode = dl.get_config(config, 'developers', 'DEVELOPERS_MODE', "") if bool(dev_mode) == True: DEVELOPERS_MODE = True else: DEVELOPERS_MODE = False
def pega_solar(uid): # pega dados da usina ret = False T2 = Template(PAYLOAD_T2) payload_t2 = T2.substitute(sid=uid) header = headers_h2 # header['Cookie'] = COOKIE_UID + "; " + COOKIE_EGG_SESS + "; hm_token=" + token + "; Path=/; Domain=.global.hoymiles.com; Expires=Sat, 19 Mar 2022 22:11:48 GMT;" + "'" header[ 'Cookie'] = COOKIE_UID + "; hm_token=" + token + "; Path=/; Domain=.global.hoymiles.com; Expires=Sat, 30 Mar 2024 22:11:48 GMT;" + "'" solar = pega_url_jsonDic(URL2, payload_t2, header, 2) if 'status' in solar.keys(): solar_status = solar['status'] if solar_status == "0": ret = solar.copy() if solar_status != "0": ret = solar_status if DEVELOPERS_MODE: printC(Color.B_Red, 'Solar Status Error: ' + str(solar_status)) if solar_status == "100": # erro no token # pede um novo if (pega_token()): # chama pega solar novamente ret = pega_solar(uid) else: print(Color.B_Red + "I can't connect!" + Color.B_Default) return ret
def ajustaDadosSolar(): ''' ajusta dados solar ''' global gDadosSolar realPower = dl.float2number(gDadosSolar['real_power'], 0) capacidade = dl.float2number(gDadosSolar['capacitor']) plant_tree = dl.float2number(gDadosSolar['plant_tree'], 0) month_eq = dl.float2number(gDadosSolar['month_eq']) / 1000 month_eq = round(month_eq, 2) total_eq = dl.float2number(gDadosSolar['total_eq']) / 1000000 total_eq = round(total_eq, 2) co2 = dl.float2number(gDadosSolar['co2_emission_reduction']) / 1000000 co2 = round(co2, 2) # corrige escala e digitos if capacidade > 0 and capacidade < 100: capacidade = capacidade * 1000 capacidade = round(capacidade) power = (realPower / capacidade) * 100 power = round(power, 1) if power == 0: printC(Color.F_Magenta, "Power = 0") printC(Color.B_LightMagenta, dl.hoje()) if DEVELOPERS_MODE: printC('parada 1/0', str(1 / 0)) gDadosSolar['real_power'] = str(realPower) gDadosSolar['power_ratio'] = str(power) gDadosSolar['capacitor'] = str(capacidade) gDadosSolar['co2_emission_reduction'] = str(co2) gDadosSolar['plant_tree'] = str(plant_tree) gDadosSolar['total_eq'] = str(total_eq) gDadosSolar['month_eq'] = str(month_eq)
def send_hass(): ''' Envia parametros para incluir device no hass.io ''' global sensor_dic global gDevices_enviados # var comuns varComuns = { 'sw_version': VERSAO, 'model': SOLAR_MODEL, 'manufacturer': MANUFACTURER, 'device_name': APP_NAME, 'identifiers': SHORT_NAME + "_" + str(HOYMILES_PLANT_ID), 'via_device': SOLAR_MODEL, 'sid': SID, 'plant_id': HOYMILES_PLANT_ID, 'last_reset_topic': 'home/$sid/json_$plant_id', 'uniq_id': UUID } if DEVELOPERS_MODE: log().debug('Sensor_dic: ' + str(len(sensor_dic))) if len(sensor_dic) == 0: for k in json_hass.items(): json_file_path = k[0] + '.json' if dl.IN_HASSIO(): json_file_path = '/' + json_file_path # to run on HASS.IO if not os.path.isfile(json_file_path): log().error(json_file_path + " not found!") printC(Color.F_Cyan, json_file_path) json_file = open(json_file_path) if not json_file.readable(): printC(Color.B_Red, "I can't read file") json_str = json_file.read() sensor_dic[k[0]] = json.loads(json_str) if len(sensor_dic) == 0: printC(Color.B_Red, "Sensor_dic error") rc = 0 for k in sensor_dic.items(): # print('Componente:' + k[0]) rc = monta_publica_topico(k[0], sensor_dic[k[0]], varComuns) if not rc == 0: printC(Color.B_LightRed, 'Hass publish error: ' + str(rc)) if rc == 0: gDevices_enviados['b'] = True gDevices_enviados['t'] = datetime.now() log().debug('Hass Sended')
def pega_token(): # pega o token global token global TOKEN global HOYMILES_PASSWORD global HOYMILES_USER pass_hash = hashlib.md5(HOYMILES_PASSWORD.encode()) # b'senhadohoymiles' pass_hex = pass_hash.hexdigest() # print(pass_hex) ret = False T1 = Template(PAYLOAD_T1) payload_T1 = T1.substitute(user=HOYMILES_USER, password=pass_hex) #print(payload_T1) header = headers_h1 header['Cookie'] = "'" + COOKIE_UID + "; " + COOKIE_EGG_SESS + "'" login, sCode = pega_url(URL1, payload_T1, header) if sCode == 200: json_res = json.loads(login) if json_res['status'] == '0': data_body = json_res['data'] token = json_res['data']['token'] TOKEN = token ret = True printC(Color.F_Blue, 'I got the token!! :-)') if token == "": print('erro na resposta') ret = False elif json_res['status'] == '1': TOKEN = '' token = '' print(Color.F_Red + "Wrong user/password" + Color.F_Default) else: TOKEN = '' token = '' print(Color.F_Red + "HTTP Error: " + str(sCode) + Color.F_Default + " " + dl.httpStatusCode(sCode)) return ret
def monta_publica_topico(component, sDict, varComuns): ''' monta e envia topico - sensores''' ret_rc = 0 key_todos = sDict['todos'] newDict = sDict.copy() newDict.pop('todos') for key, dic in newDict.items(): # print(key,dic) if key[:1] != '#': varComuns['uniq_id'] = varComuns['identifiers'] + "_" + key if not ('val_tpl' in dic): dic['val_tpl'] = dic['name'] dic['name'] = varComuns['uniq_id'] dic['device_dict'] = device_dict dic['publish_time'] = datetime.today().strftime( '%Y-%m-%d %H:%M:%S') dic['expire_after'] = int(INTERVALO_EXPIRE) # quando deve expirar dados = Template(json_hass[component]) # sensor dados = Template(dados.safe_substitute(dic)) varComuns_template = Template(json.dumps(varComuns)) varComuns_template = varComuns_template.safe_substitute(varComuns) #dados = Template(dados.safe_substitute(varComuns)) # faz ultimas substituições dados = Template( dados.safe_substitute(json.loads( varComuns_template))) # faz ultimas substituições dados = dados.safe_substitute( key_todos) # remove os não substituidos. topico = MQTT_HASS + "/" + component + "/" + NODE_ID + "/" + varComuns[ 'uniq_id'] + "/config" # print(topico) # print(dados) dados = dl.json_remove_vazio(dados) (rc, mid) = publicaMqtt(topico, dados) if rc == 0: if DEVELOPERS_MODE: topicoResumo = topico.replace( MQTT_HASS + "/" + component + "/" + NODE_ID, '...') topicoResumo = topicoResumo.replace("/config", '') printC(Color.F_Cyan, topicoResumo) printC(Color.F_Red, dados) else: # deu erro na publicação printC(Color.B_Red, "Erro monta_publica_topico") printC(Color.F_Red, topico) ret_rc = ret_rc + rc # print ("rc: ", rc) return rc
def iniciaWebServer(): ''' inicia o webserver ''' printC(Color.B_LightMagenta, "WEB SERVER Starting ...") path_index = comum.PATH_TEMPLATE if IN_HASSIO(): path_index = comum.PATH_TEMPLATE_HAS bl_existe_index = os.path.isfile(path_index + '/index.html') if not bl_existe_index: ''' não existe o index ''' printC(Color.B_Red, "Index not found. I can't start webserver. ") arr = os.listdir(path_index) printC(Color.F_Magenta, path_index) print(arr) else: # existe index p = multiprocessing.Process(target=iniciaWebServerB, args=({ "Something": "SomethingElese" }, )) p.start()
def index(): resposta = dl.loadJsonFile(comum.FILE_COMM) user = {'username': '******'} user = {'username': resposta} posts = [{ 'author': { 'username': '******' }, 'body': 'Beautiful day in Portland!' }, { 'author': { 'username': '******' }, 'body': 'The Avengers movie was so cool!' }] return render_template('index copy.html', title='Home', user=user, posts=posts) if __name__ == "__main__": #app.run() #app.run(debug=True, host="0.0.0.0") dl.printC(dl.Color.B_Green, 'Iniciando WEBSERVER __main__') app.run(debug=True, host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 4444)))
def ajustaDadosSolar(): ''' ajusta dados solar ''' global gDadosSolar global gLastReset realPower = dl.float2number(gDadosSolar['real_power'], 0) capacidade = dl.float2number(gDadosSolar['capacitor']) plant_tree = dl.float2number(gDadosSolar['plant_tree'], 0) today_eqW = dl.float2number(gDadosSolar['today_eq']) today_eq = dl.float2number(gDadosSolar['today_eq']) / 1000 today_eq = round(today_eq, 2) month_eq = dl.float2number(gDadosSolar['month_eq']) / 1000 month_eq = round(month_eq, 2) total_eq = dl.float2number(gDadosSolar['total_eq']) / 1000 total_eq = round(total_eq, 2) co2 = dl.float2number(gDadosSolar['co2_emission_reduction']) / 1000000 co2 = round(co2, 2) last_data_time = gDadosSolar['last_data_time'] # corrige escala e digitos if capacidade > 0 and capacidade < 100: capacidade = capacidade * 1000 capacidade = round(capacidade) power = (realPower / capacidade) * 100 power = round(power, 1) if realPower == 0: printC(Color.F_Magenta, "realPower = 0") printC(Color.B_LightMagenta, dl.hoje()) if DEVELOPERS_MODE: #printC ('parada 1/0', str(1/0)) printC(Color.B_Red, 'parada') gDadosSolar['real_power'] = str(realPower) gDadosSolar['real_power_measurement'] = str(realPower) gDadosSolar['real_power_total_increasing'] = str(realPower) gDadosSolar['power_ratio'] = str(power) gDadosSolar['capacitor'] = str(capacidade) gDadosSolar['capacitor_kW'] = str(capacidade / 1000) gDadosSolar['co2_emission_reduction'] = str(co2) gDadosSolar['plant_tree'] = str(plant_tree) gDadosSolar['today_eq'] = str(today_eq) gDadosSolar['today_eq_Wh'] = str(today_eqW) gDadosSolar['month_eq'] = str(month_eq) gDadosSolar['total_eq'] = str(total_eq) #last_data_time = datetime.strptime(last_data_time, '%Y-%m-%d %H:%M:%S') #last_data_time = last_data_time.replace(tzinfo=LOCAL_TIMEZONE) last_data_time = dl.strDateTimeZone(last_data_time) gDadosSolar['last_data_time'] = last_data_time.isoformat() print(last_data_time.isoformat()) print(last_data_time.tzname()) # dados solar reset gLastReset['valMes'] = month_eq #gLastReset['dtMes']=datetime.today().strftime('%Y-%m-01T00:00:00+00:00') dtMes = datetime.today().strftime('%Y-%m-%d %H:%M:%S') gLastReset['dtMes'] = dl.strDateTimeZone(dtMes).isoformat() gLastReset['valDia'] = month_eq #gLastReset['dtDia']=datetime.today().strftime('%Y-%m-%dT00:00:00+00:00') gLastReset['dtDia'] = gLastReset['dtMes'] part1 = "reset_" # TODO: Need to check values source gDadosSolar[part1 + "today_eq"] = gLastReset['dtDia'] gDadosSolar[part1 + "month_eq"] = gLastReset['dtMes'] # v0.23. gDadosSolar[part1 + "real_power_measurement"] = gLastReset['dtMes'] gDadosSolar[part1 + "real_power_total_increasing"] = gLastReset['dtMes'] gDadosSolar[part1 + "today_eq_Wh"] = gLastReset['dtMes'] gDadosSolar[part1 + "total_eq"] = gLastReset['dtMes']
def mqttStart(): ''' Start MQTT ''' global client global clientOk # MQTT Start # client = mqtt.Client(transport="tcp") # "websockets" client = mqtt.Client(client_id='', clean_session=True, userdata=None, protocol=MQTTversion, transport="tcp") # mqtt.MQTTv31 log().info("Starting MQTT " + MQTT_HOST) printC(Color.B_Blue, "Starting MQTT Client " + MQTT_HOST) if DEVELOPERS_MODE: printC(Color.F_Blue, "SSL: " + ssl.OPENSSL_VERSION) log().debug("mqttStart External: " + str(External_MQTT_Server) + ' ' + str(type(External_MQTT_Server))) log().debug("mqttStart TLS: " + str(External_MQTT_TLS) + ' ' + str(type(External_MQTT_TLS))) log().debug("mqttStart MQTT_USERNAME: "******"mqttStart MQTT_PASSWORD: "******"mqttStart MQTT_PORT: " + str(MQTT_PORT) + ' ' + str(type(MQTT_PORT))) client.username_pw_set(username=MQTT_USERNAME, password=MQTT_PASSWORD) client.on_connect = on_connect # client.on_message = on_message client.on_disconnect = on_disconnect client.on_publish = on_publish if DEVELOPERS_MODE: client.on_log = on_log #v.0.22 TLS if (External_MQTT_TLS): log().debug("Trying TLS: " + str(MQTT_PORT)) printC(Color.F_Green, "Trying TLS: " + str(MQTT_PORT) + " " + str(type(MQTT_PORT))) # ssl._create_default_https_context = ssl._create_unverified_context if DEVELOPERS_MODE: printC(Color.F_Magenta, "TLS_protocol_version: " + str(TLS_protocol_version)) context = ssl.SSLContext( protocol=TLS_protocol_version ) # ssl.PROTOCOL_TLSv1 , ssl.PROTOCOL_TLS_CLIENT #context.check_hostname = False client.tls_set_context(context) #ssl._create_default_https_context = ssl._create_unverified_context #ssl.create_default_context() #client.tls_set() #client.tls_set_context(ssl._create_unverified_context) #client.tls_insecure_set(True) #import certifi #client.tls_set(certifi.where()) try: clientOk = True #rc = client.connect(MQTT_HOST, MQTT_PORT, 60) # 1883 rc = client.connect(host=MQTT_HOST, port=int(MQTT_PORT), keepalive=60) # 1883 except Exception as e: # OSError if e.__class__.__name__ == 'OSError': clientOk = False log().warning("Can't start MQTT") print(Color.F_Red + "Can't start MQTT" + Color.F_Default) # e.errno = 51 - 'Network is unreachable' mostraErro(e, 20, "MQTT Start") else: clientOk = False mostraErro(e, 30, "MQTT Start") if clientOk: client.loop_start() # start the loop
def substitui_secrets(): "No HASS.IO ADD-ON substitui os dados do secrets.ini pelos do options.json" global HOYMILES_USER global HOYMILES_PASSWORD global HOYMILES_PLANT_ID global MQTT_HOST global MQTT_PASSWORD global MQTT_USERNAME global MQTT_PORT global DEVELOPERS_MODE global FILE_COMM global WEB_SERVER global Use_kW_instead_W global External_MQTT_Server global External_MQTT_Host global External_MQTT_User global External_MQTT_Pass global External_MQTT_TLS global External_MQTT_TLS_PORT global HASS_TIMEZONE log().debug("Loading env data....") HOYMILES_USER = dl.pegaEnv("HOYMILES_USER") HOYMILES_PASSWORD = dl.pegaEnv("HOYMILES_PASSWORD") HOYMILES_PLANT_ID = dl.pegaEnv("HOYMILES_PLANT_ID") MQTT_HOST = dl.pegaEnv("MQTT_HOST") MQTT_PASSWORD = dl.pegaEnv("MQTT_PASSWORD") MQTT_USERNAME = dl.pegaEnv("MQTT_USER") DEVELOPERS_MODE = dl.pegaEnv("DEVELOPERS_MODE") DEVELOPERS_MODE = dl.onOff(DEVELOPERS_MODE, True, False) if dl.IN_HASSIO(): WEB_SERVER = True FILE_COMM = '/data/' + comum.FILE_COMM #Use_kW_instead_W = dl.pegaEnv("Use_kW_instead_W") External_MQTT_Server = dl.pegaEnv("External_MQTT_Server") External_MQTT_Server = dl.onOff(External_MQTT_Server, True, False) External_MQTT_Host = dl.pegaEnv("External_MQTT_Host") External_MQTT_User = dl.pegaEnv("External_MQTT_User") External_MQTT_Pass = dl.pegaEnv("External_MQTT_Pass") #v0.22 External_MQTT_TLS = dl.pegaEnv("External_MQTT_TLS") External_MQTT_TLS = dl.onOff(External_MQTT_TLS, True, False) External_MQTT_TLS_PORT = dl.pegaEnv("External_MQTT_TLS_PORT") printC(Color.B_Red, "MQTT Server: " + str(MQTT_HOST)) printC(Color.B_Green, "Using External MQTT Server: " + str(External_MQTT_Server == True)) HASS_TIMEZONE = dl.pegaEnv("HASS_TIMEZONE") if (External_MQTT_Server): MQTT_HOST = External_MQTT_Host MQTT_PASSWORD = External_MQTT_Pass MQTT_USERNAME = External_MQTT_User printC(Color.B_Green, "Using External MQTT Server: " + str(MQTT_HOST)) if (External_MQTT_TLS): MQTT_PORT = External_MQTT_TLS_PORT printC(Color.B_Green, "Using External MQTT TLS PORT: " + str(MQTT_PORT)) #printC(Color.B_Red, "mqtt_extenal: " + str(External_MQTT_Host)) #printC(Color.B_Red, "Use_kW_instead_W: " + str(Use_kW_instead_W)) #printC(Color.B_Red, "External_MQTT: " + str(External_MQTT_User)) log().debug("Env data loaded.")
def get_secrets(): ''' GET configuration data ''' global HOYMILES_USER global HOYMILES_PASSWORD global HOYMILES_PLANT_ID global MQTT_HOST global MQTT_PASSWORD global MQTT_USERNAME global MQTT_PORT global DEVELOPERS_MODE global WEB_SERVER global Use_kW_instead_W global External_MQTT_Server global External_MQTT_Host global External_MQTT_User global External_MQTT_Pass global External_MQTT_TLS global External_MQTT_TLS_PORT config = dl.getConfigParser(SECRETS) printC(Color.F_LightGreen, "Reading secrets.ini") # le os dados HOYMILES_USER = dl.get_config(config, 'secrets', 'HOYMILES_USER', HOYMILES_USER) HOYMILES_PASSWORD = dl.get_config(config, 'secrets', 'HOYMILES_PASSWORD', HOYMILES_PASSWORD) HOYMILES_PLANT_ID = dl.get_config(config, 'secrets', 'HOYMILES_PLANT_ID', HOYMILES_PLANT_ID, getInt=True) MQTT_PASSWORD = dl.get_config(config, 'secrets', 'MQTT_PASS', MQTT_PASSWORD) MQTT_USERNAME = dl.get_config(config, 'secrets', 'MQTT_USER', MQTT_USERNAME) MQTT_HOST = dl.get_config(config, 'secrets', 'MQTT_HOST', MQTT_HOST) dev_mode = dl.get_config(config, 'developers', 'DEVELOPERS_MODE', "") if bool(dev_mode) == True: DEVELOPERS_MODE = True else: DEVELOPERS_MODE = False WEB_SERVER = dl.get_config(config, 'secrets', 'WEB_SERVER', WEB_SERVER) # external server External_MQTT_Server = dl.get_config(config, 'external', 'External_MQTT_Server', External_MQTT_Server, getBool=True) if (External_MQTT_Server): External_MQTT_Host = dl.get_config(config, 'external', 'External_MQTT_Host', External_MQTT_Host) External_MQTT_User = dl.get_config(config, 'external', 'External_MQTT_User', External_MQTT_User) External_MQTT_Pass = dl.get_config(config, 'external', 'External_MQTT_Pass', External_MQTT_Pass) External_MQTT_TLS = dl.get_config(config, 'external', 'External_MQTT_TLS', External_MQTT_TLS, getBool=True) External_MQTT_TLS_PORT = dl.get_config(config, 'external', 'External_MQTT_TLS_PORT', External_MQTT_TLS_PORT, getInt=True) if (External_MQTT_Server): MQTT_HOST = External_MQTT_Host MQTT_PASSWORD = External_MQTT_Pass MQTT_USERNAME = External_MQTT_User printC(Color.B_Green, "Using External MQTT Server: " + str(MQTT_HOST)) if (External_MQTT_TLS): MQTT_PORT = External_MQTT_TLS_PORT printC(Color.B_Green, "Using External MQTT TLS PORT: " + str(MQTT_PORT)) else: External_MQTT_TLS = False
"Something": "SomethingElese" }, )) p.start() # INICIO, START print(Color.B_Blue + "********** " + MANUFACTURER + " " + APP_NAME + " v." + VERSAO + Color.B_Default) print(Color.B_Green + "Starting up... " + datetime.today().strftime('%Y-%m-%d %H:%M:%S') + ' ' + Color.B_Default) # version check if int(dl.version()) < int(version_expected['dmslibs']): printC(Color.B_Red, "Wrong dmslibs version! ") printC(Color.F_Blue, "Current: " + dl.version()) printC(Color.F_Blue, "Expected: " + version_expected['dmslibs']) dl.inicia_log(logFile='/var/tmp/hass.hoymiles.log', logName='hass.hoymiles', stdOut=True) # info dl.dadosOS() status['ip'] = dl.get_ip() print(Color.B_Cyan + "IP: " + Color.B_Default + Color.F_Magenta + status['ip'] + Color.F_Default) if DEVELOPERS_MODE: print(Color.B_Red, "DEVELOPERS_MODE", Color.B_Default)