clean code
This commit is contained in:
		
							parent
							
								
									6679f2b22b
								
							
						
					
					
						commit
						0d8db4077d
					
				@ -1,24 +1,18 @@
 | 
				
			|||||||
import json
 | 
					 | 
				
			||||||
import sys
 | 
					 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# Homematic IP
 | 
					# Homematic IP
 | 
				
			||||||
import homematicip
 | 
					import homematicip
 | 
				
			||||||
from homematicip.home import Home
 | 
					from homematicip.home import Home
 | 
				
			||||||
from homematicip.device import ShutterContact,HeatingThermostat,PlugableSwitchMeasuring,WallMountedThermostatPro
 | 
					from homematicip.device import ShutterContact, WallMountedThermostatPro
 | 
				
			||||||
from homematicip.group import HeatingCoolingProfile,HeatingGroup
 | 
					 | 
				
			||||||
# Import MQTT client.
 | 
					# Import MQTT client.
 | 
				
			||||||
import paho.mqtt.publish as publish
 | 
					# import paho.mqtt.publish as publish
 | 
				
			||||||
import paho.mqtt.client as paho
 | 
					import paho.mqtt.client as paho
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# MQTT credentials
 | 
					# MQTT credentials
 | 
				
			||||||
# mqtt_username  = 'homeassistant' # MQTT client username
 | 
					# mqtt_username  = 'homeassistant' # MQTT client username
 | 
				
			||||||
# mqtt_password = '3355' # MQTT client password
 | 
					# mqtt_password = '3355' # MQTT client password
 | 
				
			||||||
# client_id = 'temperature' # unique client_id
 | 
					# client_id = 'temperature' # unique client_id
 | 
				
			||||||
mqtt_broker = "raspberrypi" # broker address, usually your HASS IP address
 | 
					mqtt_broker = "raspberrypi"  # broker address, usually your HASS IP address
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# create the MQTT client
 | 
					# create the MQTT client
 | 
				
			||||||
global client
 | 
					 | 
				
			||||||
client = paho.Client(protocol=paho.MQTTv31)  # * set a random string (max 23 chars)
 | 
					client = paho.Client(protocol=paho.MQTTv31)  # * set a random string (max 23 chars)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# client connection
 | 
					# client connection
 | 
				
			||||||
@ -32,44 +26,29 @@ home = Home()
 | 
				
			|||||||
home.set_auth_token(config.auth_token)
 | 
					home.set_auth_token(config.auth_token)
 | 
				
			||||||
home.init(config.access_point)
 | 
					home.init(config.access_point)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def write_shutter(room,device):
 | 
					 | 
				
			||||||
        print(room, " ", device.label, " ", device.lastStatusUpdate, " ", device.windowState)
 | 
					 | 
				
			||||||
        #print(device)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def write_plugableswitchmeasuring(room,device):
 | 
					def write_shutter(room, device):
 | 
				
			||||||
        print(room, " ", device.label, " ", device.lastStatusUpdate, " ", device.currentPowerConsumption, " ", device.energyCounter)
 | 
					    print(room, " ", device.label, " ", device.lastStatusUpdate, " ", device.windowState)
 | 
				
			||||||
 | 
					    # print(device)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def write_heatingthermostat(room,device):
 | 
					 | 
				
			||||||
        print(room, " ", device.label, " ", device.lastStatusUpdate)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def write_wallmountedthermostatpro(room,device):
 | 
					def write_wallmountedthermostatpro(room, device):
 | 
				
			||||||
    global time
 | 
					    topic = 'weathersensors/' + room
 | 
				
			||||||
    global deviceName
 | 
					 | 
				
			||||||
    global actualTemperature
 | 
					 | 
				
			||||||
    global setPointTemperature
 | 
					 | 
				
			||||||
    global humidity
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    time = device.lastStatusUpdate
 | 
					    client.publish(topic + "/time", "" + str(device.lastStatusUpdate))
 | 
				
			||||||
    deviceName = device.label
 | 
					    client.publish(topic + "/actualTemperature", "" + str(device.actualTemperature))
 | 
				
			||||||
    actualTemperature = device.actualTemperature
 | 
					    client.publish(topic + "/setPointTemperature", "" + str(device.setPointTemperature))
 | 
				
			||||||
    setPointTemperature = device.setPointTemperature
 | 
					    client.publish(topic + "/humidity", "" + str(device.humidity))
 | 
				
			||||||
    humidity = device.humidity
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    topic  = 'weathersensors/'+room
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    #print(topic)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    client.publish(topic + "/time", "" + str(time))
 | 
					 | 
				
			||||||
    client.publish(topic + "/actualTemperature", "" + str(actualTemperature))
 | 
					 | 
				
			||||||
    client.publish(topic + "/setPointTemperature", "" + str(setPointTemperature))
 | 
					 | 
				
			||||||
    client.publish(topic + "/humidity", "" + str(humidity))
 | 
					 | 
				
			||||||
    client.publish(topic + "/availability", "ON")
 | 
					    client.publish(topic + "/availability", "ON")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print({"Room":room, "Device Name":device.label, "Last Status Update" : device.lastStatusUpdate, "Actual Temperature":device.actualTemperature, "Set Point Temperature":device.setPointTemperature,"Humidity":device.humidity})
 | 
					    print({"Room": room, "Device Name": device.label, "Last Status Update": device.lastStatusUpdate,
 | 
				
			||||||
 | 
					           "Actual Temperature": device.actualTemperature, "Set Point Temperature": device.setPointTemperature,
 | 
				
			||||||
 | 
					           "Humidity": device.humidity})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def heating_profile(group):
 | 
					def heating_profile(group):
 | 
				
			||||||
    #heatingGroup=homematicip.group.HeatingGroup(group)
 | 
					    # heatingGroup=homematicip.group.HeatingGroup(group)
 | 
				
			||||||
    #heatingGroup.__init__(home)
 | 
					    # heatingGroup.__init__(home)
 | 
				
			||||||
    print("Heating Profiles")
 | 
					    print("Heating Profiles")
 | 
				
			||||||
    print("Group Label: " + group.label)
 | 
					    print("Group Label: " + group.label)
 | 
				
			||||||
    print("Group Type: " + group.groupType)
 | 
					    print("Group Type: " + group.groupType)
 | 
				
			||||||
@ -77,31 +56,27 @@ def heating_profile(group):
 | 
				
			|||||||
    for profile in group.profiles:
 | 
					    for profile in group.profiles:
 | 
				
			||||||
        if profile.name != "":
 | 
					        if profile.name != "":
 | 
				
			||||||
            print("Group Profile Name: " + profile.name)
 | 
					            print("Group Profile Name: " + profile.name)
 | 
				
			||||||
            #print("Profile ID: " + profile.id)
 | 
					            # print("Profile ID: " + profile.id)
 | 
				
			||||||
            #print("Profile Index: " + profile.index)
 | 
					            # print("Profile Index: " + profile.index)
 | 
				
			||||||
            if profile.name == "Home Office":
 | 
					            if profile.name == "Home Office":
 | 
				
			||||||
                print("Profile Index" + profile.index)
 | 
					                print("Profile Index" + profile.index)
 | 
				
			||||||
                print(group)
 | 
					                print(group)
 | 
				
			||||||
                heatingGroup=HeatingGroup(group)
 | 
					 | 
				
			||||||
#                print(heatingGroup.profiles[0])
 | 
					 | 
				
			||||||
                group.set_active_profile(profile.index)
 | 
					                group.set_active_profile(profile.index)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
    global home
 | 
					    global home
 | 
				
			||||||
    home.get_current_state()
 | 
					    home.get_current_state()
 | 
				
			||||||
    for group in home.groups:
 | 
					    for group in home.groups:
 | 
				
			||||||
#        if group.groupType=="HEATING":
 | 
					        #        if group.groupType=="HEATING":
 | 
				
			||||||
#            heating_profile(group)
 | 
					        #            heating_profile(group)
 | 
				
			||||||
        if group.groupType=="META":
 | 
					        if group.groupType == "META":
 | 
				
			||||||
            for device in group.devices:
 | 
					            for device in group.devices:
 | 
				
			||||||
                if isinstance(device,ShutterContact):
 | 
					                if isinstance(device, ShutterContact):
 | 
				
			||||||
                    write_shutter(group.label,device)
 | 
					                    write_shutter(group.label, device)
 | 
				
			||||||
                elif isinstance(device,HeatingThermostat):
 | 
					                elif isinstance(device, WallMountedThermostatPro):
 | 
				
			||||||
                    write_heatingthermostat(group.label,device)
 | 
					                    write_wallmountedthermostatpro(group.label, device)
 | 
				
			||||||
                elif isinstance(device,PlugableSwitchMeasuring):
 | 
					
 | 
				
			||||||
                    write_plugableswitchmeasuring(group.label,device)
 | 
					 | 
				
			||||||
                elif isinstance(device,WallMountedThermostatPro):
 | 
					 | 
				
			||||||
                    write_wallmountedthermostatpro(group.label,device)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    main()
 | 
					    main()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user