From e7fbbe5f71fe7452097d8daa3347963cda0838d7 Mon Sep 17 00:00:00 2001
From: Sean Rees <sean@erifax.org>
Date: Sat, 14 Mar 2020 20:29:06 +0000
Subject: [PATCH] Rebase on libpurecool

libpurecoollink hasn't been maintained in 3 years, and in the interim, Dyson
have shutdown the old API endpoint.

libpurecool is a fork and has slightly different conversion behaviour for
temperatures, so adjust the conversions here accordingly.
---
 README.md           |  6 +++---
 prometheus_dyson.py | 13 ++++++++-----
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index 0813096..00461c5 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@ to extend to other fan types (I just don't have one to test).
 ## Dependencies
 
 ```
-pip install libpurecoollink
+pip install libpurecool
 pip install prometheus_client
 ```
 ## Metrics
@@ -57,11 +57,11 @@ optional arguments:
 ### Scrape Frequency
 
 I scrape at 15s intervals. Metrics are updated at approximately 30 second
-intervals by `libpurecoollink`.
+intervals by `libpurecool`.
 
 ### Other Notes
 
-`libpurecoollink` by default uses a flavour of mDNS to automatically discover
+`libpurecool` by default uses a flavour of mDNS to automatically discover
 the Dyson fan. This is overridable (but this script doesn't at the moment).
 The mDNS dependency makes Dockerising this script somewhat challenging at
 the moment.
diff --git a/prometheus_dyson.py b/prometheus_dyson.py
index c7a3393..b61fa80 100755
--- a/prometheus_dyson.py
+++ b/prometheus_dyson.py
@@ -2,7 +2,7 @@
 """Exports Dyson Pure Hot+Cool (DysonLink) statistics as Prometheus metrics.
 
 This module depends on two libraries to function:
-  pip install libpurecoollink
+  pip install libpurecool
   pip install prometheus_client
 """
 
@@ -16,8 +16,8 @@ import time
 
 from typing import Callable
 
-from libpurecoollink import dyson
-from libpurecoollink import dyson_pure_state
+from libpurecool import dyson
+from libpurecool import dyson_pure_state
 import prometheus_client
 
 # Rationale:
@@ -79,7 +79,7 @@ class Metrics():
 
     if isinstance(message, dyson_pure_state.DysonEnvironmentalSensorState):
       self.humidity.labels(name=name, serial=serial).set(message.humidity)
-      self.temperature.labels(name=name, serial=serial).set(message.temperature - 273)
+      self.temperature.labels(name=name, serial=serial).set(message.temperature - 273.2)
       self.voc.labels(name=name, serial=serial).set(message.volatil_organic_compounds)
       self.dust.labels(name=name, serial=serial).set(message.dust)
     elif isinstance(message, dyson_pure_state.DysonPureHotCoolState):
@@ -91,11 +91,14 @@ class Metrics():
         speed = -1
       self.fan_speed.labels(name=name, serial=serial).set(speed)
 
+      # Convert from Decicelsius to Kelvin.
+      heat_target = int(message.heat_target) / 10 - 273.2
+
       self.oscillation.labels(name=name, serial=serial).state(message.oscillation)
       self.focus_mode.labels(name=name, serial=serial).state(message.focus_mode)
       self.heat_mode.labels(name=name, serial=serial).state(message.heat_mode)
       self.heat_state.labels(name=name, serial=serial).state(message.heat_state)
-      self.heat_target.labels(name=name, serial=serial).set(int(message.heat_target)/10 - 273)
+      self.heat_target.labels(name=name, serial=serial).set(heat_target)
       self.quality_target.labels(name=name, serial=serial).set(message.quality_target)
       self.filter_life.labels(name=name, serial=serial).set(message.filter_life)
     else:
-- 
GitLab