aboutsummaryrefslogblamecommitdiffstats
path: root/scripts/neohub.py
blob: 7ef8b77b5d2f56a1c2998346a162a1df892d7cd7 (plain) (tree)
1
2
3
4
5
6
7
8
9
                  
                                                                                                            

         
           
              
                             

                                 
             


                                          
                                
 
                        
                              






                                                         
                                                              










                                             
                                    










                                                               
                                             

                                       
                                   
                                     

                                          



                                                        
                                             
 



                              
#!/usr/bin/python3
''' Get stuff from neohub! This is mostly the usage-example from https://gitlab.com/neohubapi/neohubapi/ '''

import os
import time
import asyncio
from datetime import datetime
import neohubapi.neohub as neohub

import common

neohub_ip = os.environ['el_neohub_ip']
neohub_port = os.environ['el_neohub_port']
SLEEP = 120 # Sleep between runs

async def call_neohub():
    ''' async runner! w00p '''
    # Legacy connection
    hub = neohub.NeoHub(neohub_ip, int(neohub_port))
    # Or, for a websocket connection:
    # hub = neohub.Neohub(port=4243, token='xxx-xxxxxxx')
    system = await hub.get_system()
    hub_data, devices = await hub.get_live_data()
    for device in devices['thermostats']:
        #print(f"Temperature in zone {device.name}: {device}")
        values = (
            datetime.utcnow(),
            device.time,
            device.device_id,
            device.away,
            device.heat_mode,
            device.heat_on,
            device.current_floor_temperature,
            device.target_temperature,
            device.temperature)

        sql =  """INSERT INTO neohub
                        (timestamp,
                         time,
                         device_id,
                         away,
                         heat_mode,
                         heat_on,
                         current_floor_temperature,
                         target_temperature,
                         temperature)
                  VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)"""

        common.dbi(sql, values, verbose=True)

        sql = """INSERT INTO mqtt_temps
                        (sensor_id,
                         temperature,
                         time)
                 SELECT sensors.id, %s, %s
                    FROM sensors
                    WHERE sensors.name = 'neohub'"""
        values = (device.temperature, datetime.utcnow())

        common.dbi(sql, values, verbose=True)

# Loop it forever
while True:
    asyncio.run(call_neohub())
    time.sleep(SLEEP)