From 0a7db98b0e74a99663aab0b4e66591e9c9b2006e Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Sun, 7 Feb 2016 20:32:11 +0000 Subject: [PATCH] Round mFi sensor values to reasonable levels of precision Most of the mFi sensors are able to reasonably provide accurate readings to a tenth of a unit or so. This patch rounds them for better display in the UI. Normally, I would expect this to be a view action instead of altering the actual data emitted, but since these values are reasonable for sensor precision, we're not really losing anything. I followed the model from the openweathermap component, which rounds for readability in the backend. --- homeassistant/components/sensor/mfi.py | 9 ++++++++- homeassistant/components/switch/mfi.py | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/mfi.py b/homeassistant/components/sensor/mfi.py index 3aeb6736819..d361fa9f690 100644 --- a/homeassistant/components/sensor/mfi.py +++ b/homeassistant/components/sensor/mfi.py @@ -20,6 +20,12 @@ _LOGGER = logging.getLogger(__name__) STATE_ON = 'on' STATE_OFF = 'off' +DIGITS = { + 'volts': 1, + 'amps': 1, + 'active_power': 0, + 'temperature': 1, +} SENSOR_MODELS = [ 'Ubiquiti mFi-THS', 'Ubiquiti mFi-CS', @@ -76,7 +82,8 @@ class MfiSensor(Entity): if self._port.model == 'Input Digital': return self._port.value > 0 and STATE_ON or STATE_OFF else: - return self._port.value + digits = DIGITS.get(self._port.tag, 0) + return round(self._port.value, digits) @property def unit_of_measurement(self): diff --git a/homeassistant/components/switch/mfi.py b/homeassistant/components/switch/mfi.py index 1cee23e906b..46cfdb56213 100644 --- a/homeassistant/components/switch/mfi.py +++ b/homeassistant/components/switch/mfi.py @@ -98,6 +98,6 @@ class MfiSwitch(SwitchDevice): @property def device_state_attributes(self): attr = {} - attr['volts'] = self._port.data.get('v_rms', 0) - attr['amps'] = self._port.data.get('i_rms', 0) + attr['volts'] = round(self._port.data.get('v_rms', 0), 1) + attr['amps'] = round(self._port.data.get('i_rms', 0), 1) return attr