From e6522bf0328554fafbcbc38433d91a491248cd7c Mon Sep 17 00:00:00 2001 From: Alex Popoutsis Date: Tue, 13 Oct 2020 21:59:27 -0500 Subject: [PATCH] Added propane sensor support for iGrill v3 --- igrill.py | 17 ++++++++++++++--- utils.py | 4 +++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/igrill.py b/igrill.py index 1d66c0d..055a074 100644 --- a/igrill.py +++ b/igrill.py @@ -29,6 +29,7 @@ class UUIDS(object): PROBE4_TEMPERATURE = btle.UUID('06ef0008-2e06-4b79-9e33-fce2c42805ec') PROBE4_THRESHOLD = btle.UUID('06ef0009-2e06-4b79-9e33-fce2c42805ec') HEATING_ELEMENTS = btle.UUID('6c91000a-58dc-41c7-943f-518b278ceaaa') + PROPANE_LEVEL = btle.UUID('f5d40001-3548-4c22-9947-f3673fce3cd9') class IDevicePeripheral(btle.Peripheral): @@ -36,8 +37,9 @@ class IDevicePeripheral(btle.Peripheral): btle_lock = threading.Lock() has_battery = None has_heating_element = None + has_propane_sensor = None - def __init__(self, address, name, num_probes, has_battery=True, has_heating_element=False): + def __init__(self, address, name, num_probes, has_battery=True, has_heating_element=False, has_propane_sensor=False): """ Connects to the device given by address performing necessary authentication """ @@ -49,6 +51,7 @@ def __init__(self, address, name, num_probes, has_battery=True, has_heating_elem self.name = name self.has_battery = has_battery self.has_heating_element = has_heating_element + self.has_propane_sensor = has_propane_sensor # iDevice devices require bonding. I don't think this will give us bonding # if no bonding exists, so please use bluetoothctl to create a bond first self.setSecurityLevel('medium') @@ -64,6 +67,10 @@ def __init__(self, address, name, num_probes, has_battery=True, has_heating_elem if has_heating_element: self.heating_elements = self.characteristic(UUIDS.HEATING_ELEMENTS) + # Set handle for reading propane level + if has_propane_sensor: + self.propane_level = self.characteristic(UUIDS.PROPANE_LEVEL) + # authenticate with iDevices custom challenge/response protocol if not self.authenticate(): raise RuntimeError('Unable to authenticate with device') @@ -124,6 +131,9 @@ def read_battery(self): def read_heating_elements(self): return bytearray(self.heating_elements.read()) if self.has_heating_element else None + def read_propane_level(self): + return float(bytearray(self.propane_level.read())[0])*25 if self.has_propane_sensor else None + def read_temperature(self, publish_empty, missing_value): empty = False if not publish_empty else missing_value temps = {1: False, 2: False, 3: False, 4: False} @@ -163,7 +173,7 @@ class IGrillV3Peripheral(IDevicePeripheral): def __init__(self, address, name='igrill_v3', num_probes=4): logging.debug("Created new device with name {}".format(name)) - IDevicePeripheral.__init__(self, address, name, num_probes) + IDevicePeripheral.__init__(self, address, name, num_probes, has_propane_sensor=True) class Pulse2000Peripheral(IDevicePeripheral): @@ -215,7 +225,8 @@ def run(self): temperature = device.read_temperature(self.publish_missing_probes, self.missing_probe_value) battery = device.read_battery() heating_element = device.read_heating_elements() - utils.publish(temperature, battery, heating_element, self.mqtt_client, self.topic, device.name) + propane_level = device.read_propane_level() + utils.publish(temperature, battery, heating_element, propane_level, self.mqtt_client, self.topic, device.name) logging.debug("Published temp: {} and battery: {} to topic {}/{}".format(temperature, battery, self.topic, device.name)) logging.debug("Sleeping for {} seconds".format(self.interval)) time.sleep(self.interval) diff --git a/utils.py b/utils.py index c94f9ae..8f86e96 100644 --- a/utils.py +++ b/utils.py @@ -98,7 +98,7 @@ def mqtt_init(mqtt_config): return mqtt_client -def publish(temperatures, battery, heating_element, client, base_topic, device_name): +def publish(temperatures, battery, heating_element, propane_level, client, base_topic, device_name): for i in range(1, 5): if temperatures[i]: client.publish("{0}/{1}/probe{2}".format(base_topic, device_name, i), temperatures[i]) @@ -107,6 +107,8 @@ def publish(temperatures, battery, heating_element, client, base_topic, device_n client.publish("{0}/{1}/battery".format(base_topic, device_name), battery) if heating_element: client.publish("{0}/{1}/heating_element".format(base_topic, device_name), heating_element) + if propane_level: + client.publish("{0}/{1}/propane_level".format(base_topic, device_name), propane_level) def get_devices(device_config):