Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fahrenheit compatibility #67

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<param field="Mode3" label="Heating Switches (csv list of idx)" width="100px" required="true" default="0"/>
<param field="Mode4" label="Apply minimum heating per cycle" width="200px">
<options>
<option label="only when heating required" value="Normal" default="true" />
<option label="only when heating required" value="Normal" default="true" />
<option label="always" value="Forced"/>
</options>
</param>
Expand Down Expand Up @@ -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):

Expand Down Expand Up @@ -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&param=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:
Expand Down Expand Up @@ -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&param=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
Expand Down Expand Up @@ -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:
Expand Down