From 03e0c7c71cbc912d15543c417223c935b14a74d1 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 26 Nov 2016 10:10:29 -0800 Subject: [PATCH] Prevent edimax from doing I/O in event loop (#4584) --- homeassistant/components/switch/edimax.py | 34 ++++++++++++++--------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/switch/edimax.py b/homeassistant/components/switch/edimax.py index 41746f9a0ef..0e2183920f3 100644 --- a/homeassistant/components/switch/edimax.py +++ b/homeassistant/components/switch/edimax.py @@ -49,6 +49,9 @@ class SmartPlugSwitch(SwitchDevice): """Initialize the switch.""" self.smartplug = smartplug self._name = name + self._now_power = None + self._now_energy_day = None + self._state = False @property def name(self): @@ -58,27 +61,17 @@ class SmartPlugSwitch(SwitchDevice): @property def current_power_mwh(self): """Return the current power usage in mWh.""" - try: - return float(self.smartplug.now_power) / 1000000.0 - except ValueError: - return None - except TypeError: - return None + return self._now_power @property def today_power_mw(self): """Return the today total power usage in mW.""" - try: - return float(self.smartplug.now_energy_day) / 1000.0 - except ValueError: - return None - except TypeError: - return None + return self._now_energy_day @property def is_on(self): """Return true if switch is on.""" - return self.smartplug.state == 'ON' + return self._state def turn_on(self, **kwargs): """Turn the switch on.""" @@ -87,3 +80,18 @@ class SmartPlugSwitch(SwitchDevice): def turn_off(self): """Turn the switch off.""" self.smartplug.state = 'OFF' + + def update(self): + """Update edimax switch.""" + try: + self._now_power = float(self.smartplug.now_power) / 1000000.0 + except (TypeError, ValueError): + self._now_power = None + + try: + self._now_energy_day = (float(self.smartplug.now_energy_day) / + 1000.0) + except (TypeError, ValueError): + self._now_energy_day = None + + self._state = self.smartplug.state == 'ON'