Skip to content

Commit

Permalink
Prevent edimax from doing I/O in event loop (home-assistant#4584)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Nov 26, 2016
1 parent 32ffd00 commit 03e0c7c
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions homeassistant/components/switch/edimax.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def __init__(self, smartplug, name):
"""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):
Expand All @@ -58,27 +61,17 @@ def name(self):
@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."""
Expand All @@ -87,3 +80,18 @@ def turn_on(self, **kwargs):
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'

0 comments on commit 03e0c7c

Please sign in to comment.