Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from liaan/feature/pienv_tests
Browse files Browse the repository at this point in the history
Feature/pienv tests
  • Loading branch information
liaan authored Jun 12, 2020
2 parents 91b8038 + 2752604 commit 847c345
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
73 changes: 29 additions & 44 deletions broadlink_ac_mqtt/AcToMqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
import paho.mqtt.client as mqtt
import tempfile
import json
import classes.broadlink.ac_db as broadlink

sys.path.insert(1, os.path.join(os.path.dirname(os.path.realpath(__file__)),'classes','broadlink'))
import ac_db as broadlink

logger = logging.getLogger(__name__)

debug = False
logger = logging.getLogger(__name__)


config = {}
device_objects = {}



pid = str(os.getpid())
pidfile = tempfile.gettempdir() + "/ac_to_mqtt.pid"
pid_stale_time = 60
Expand Down Expand Up @@ -50,18 +47,9 @@ def discover(self):
##Make sure correct device id
for device in discovered_devices:
if device.devtype == 0x4E2a:
devices[device.status['macaddress']] = device

devices[device.status['macaddress']] = device

return devices



#device = self.devices["a"]
#self.device = broadlink.ac_db(host=device.host, mac=device.mac,debug=False)
#logger.debug(self.device.host)
#logger.debug( "Device type detected: " +self.device.type)
#logger.debug( "Starting device test()")

def check_if_running(self):
##Check if there is pid, if there is, then check if valid or stale .. probably should add process id for race conditions but damn, this is a simple scripte.....
Expand Down Expand Up @@ -100,8 +88,7 @@ def make_device_objects(self,device_list = None):
device_objects = {}
if device_list == [] or device_list == None:
error_msg = " Cannot make device objects, empty list given"
logger.error(error_msg)
print (error_msg)
logger.error(error_msg)
sys.exit()

for device in device_list:
Expand All @@ -112,7 +99,7 @@ def make_device_objects(self,device_list = None):
def stop(self):

try:
self._mqtt.disconnect();
self._mqtt.disconnect()
except:
""
##clean pid file
Expand Down Expand Up @@ -145,24 +132,22 @@ def start (self,config, devices = None):
continue
else:
""
#print "timeout done"


#print "timeout done"

##Get the status, the global update interval is used as well to reduce requests to aircons as they slow
status = device.get_ac_status()
#print status
if status:
##Update last time checked
self.last_update[key] = time.time()

self.publish_mqtt_info(status)

self.last_update[key] = time.time()
self.publish_mqtt_info(status)

else:
logger.debug("No status")


except Exception as e:
except Exception as e:

logger.critical(e)
##Something went wrong, so just exit and let system restart
continue
Expand Down Expand Up @@ -199,14 +184,14 @@ def make_devices_array_from_devices(self,devices):

for device in devices.values():
##topic = self.config["mqtt_auto_discovery_topic"]+"/climate/"+device.status["macaddress"]+"/config"
name = ""
if not device.name :
name = device.status["macaddress"]
else:
name = device.name.encode('ascii','ignore')

name = device.name.encode('ascii','ignore')

device_array = {
"name": name
"name": name.decode("utf-8")
#,"power_command_topic" : self.config["mqtt_topic_prefix"]+ device.status["macaddress"]+"/power/set"
,"mode_command_topic" : self.config["mqtt_topic_prefix"]+ device.status["macaddress"]+"/mode_homeassistant/set"
,"temperature_command_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/temp/set"
Expand Down Expand Up @@ -243,33 +228,33 @@ def publish_mqtt_auto_discovery(self,devices):
for key in devices_array:
device = devices_array[key]
topic = self.config["mqtt_auto_discovery_topic"]+"/climate/"+key+"/config"
##Publish
self._publish(topic,json.dumps(device))


#sys.exit();


##Publish
self._publish(topic,json.dumps(device))

def publish_mqtt_info(self,status):

def publish_mqtt_info(self,status):
##Publish all values in status
for key in status:
value = status[key]
##Make sure its a string
value = status[key]



##check if device already in previous_status
if status['macaddress'] in self.previous_status:
##Check if key in state
if key in self.previous_status[status['macaddress']]:
##If the values are same, skip it to make mqtt less chatty #17

if self.previous_status[status['macaddress']][key] == value:
#print ("value same key:%s, value:%s vs : %s" % (key,value,self.previous_status[status['macaddress']][key]))
continue
else:
""
#print ("value NOT Same key:%s, value:%s vs : %s" % (key,value,self.previous_status[status['macaddress']][key]))

pubResult = self._publish(self.config["mqtt_topic_prefix"] + status['macaddress']+'/'+key+ '/value',bytes(status[key]))
#print ("value NOT Same key:%s, value:%s vs : %s" % (key,value,self.previous_status[status['macaddress']][key]))

pubResult = self._publish(self.config["mqtt_topic_prefix"] + status['macaddress']+'/'+key+ '/value',value)


if pubResult != None:
logger.warning('Publishing Result: "%s"' % mqtt.error_string(pubResult))
Expand Down
2 changes: 1 addition & 1 deletion broadlink_ac_mqtt/classes/broadlink/ac_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def make_nice_status(self,status):

def get_key(self,list,search_value):

for key,value in list.iteritems():
for key,value in list.items():
if value == search_value:
return key
##Not found so return value;
Expand Down
1 change: 1 addition & 0 deletions monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import time
import broadlink_ac_mqtt.AcToMqtt as AcToMqtt
import broadlink_ac_mqtt.classes.broadlink.ac_db as ac_db_version
import signal


logger = logging.getLogger(__name__)
Expand Down

0 comments on commit 847c345

Please sign in to comment.