diff --git a/plugin.py b/plugin.py index 3ad2bd7..aa8a717 100644 --- a/plugin.py +++ b/plugin.py @@ -27,7 +27,7 @@ - @@ -111,6 +111,12 @@ def __init__(self): self.versionsupported = False return + @staticmethod + def fahrenheitToCelsius(fahrenheit): + """Convert Fahrenheit to Celsius and round to the first decimal point.""" + celsius = (fahrenheit - 32) * 5.0 / 9.0 + return round(celsius, 1) + def onStart(self): @@ -138,6 +144,15 @@ def onStart(self): Domoticz.Error("Minimum domoticz version is 2023.2") return + # Fetch the system settings from the API to determine temperature unit + settingsAPI = DomoticzAPI("type=command¶m=getsettings") + if settingsAPI and "status" in settingsAPI and settingsAPI["status"] == "OK" and "TempUnit" in settingsAPI: + self.systemTempUnit = "F" if settingsAPI["TempUnit"] == 1 else "C" + Domoticz.Debug("System temperature unit is: {}".format(self.systemTempUnit)) + else: + self.systemTempUnit = "C" # Default to Celsius + Domoticz.Error("Failed to determine system temperature unit, defaulting to Celsius") + # create the child devices if these do not exist yet devicecreated = [] if 1 not in Devices: @@ -244,6 +259,30 @@ def onCommand(self, Unit, Command, Level, Color): nvalue = 1 if Level > 0 else 0 svalue = str(Level) + # Check if the unit is a setpoint device + if Unit in (4, 5): + # Fetch the system settings from the API to determine temperature unit + settingsAPI = DomoticzAPI("type=command¶m=getsettings") + if settingsAPI and "status" in settingsAPI and settingsAPI["status"] == "OK" and "TempUnit" in settingsAPI: + currentSystemTempUnit = "F" if settingsAPI["TempUnit"] == 1 else "C" + Domoticz.Debug("Current system temperature unit is: {}".format(currentSystemTempUnit)) + else: + currentSystemTempUnit = "C" # Default to Celsius + Domoticz.Error("Failed to determine current system temperature unit, defaulting to Celsius") + + # Only convert level from Fahrenheit to Celsius if system is set to Fahrenheit + if currentSystemTempUnit == "F": + # Convert level from Fahrenheit to Celsius + Level = self.fahrenheitToCelsius(Level) + # Update svalue with the converted and clamped level + svalue = str(Level) + else: + # System is set to Celsius, no conversion needed + sValue = str(Level) + + # Update the system temperature unit to the current setting + self.systemTempUnit = currentSystemTempUnit + Devices[Unit].Update(nValue=nvalue, sValue=svalue) if Unit in (1, 2, 4, 5): # force recalculation if control or mode or a setpoint changed @@ -505,6 +544,11 @@ def readTemps(self): else: Domoticz.Error("device: {}-{} is not a Temperature sensor".format(device["idx"], device["Name"])) + # Convert temperatures from Fahrenheit to Celsius if needed + if self.systemTempUnit == "F": + listintemps = [self.fahrenheitToCelsius(temp) for temp in listintemps] + listouttemps = [self.fahrenheitToCelsius(temp) for temp in listouttemps] + # calculate the average inside temperature nbtemps = len(listintemps) if nbtemps > 0: