From c1139976095296068dd49c1d75b87e36a7925815 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Sun, 7 Feb 2016 19:28:23 +0000 Subject: [PATCH] Tweak mFi switch behavior to avoid false states When we update the mFi server for the state of a switch, the new state is not always reported immediately if we update right after the action (the server is not RESTful). This patch adds some internal target-state handling to report the desired state on the next poll, allowing any subsequent polls to override that state. Also, bump the version requirement for mficlient to 0.2.2 to absorb a bug fix. --- homeassistant/components/sensor/mfi.py | 2 +- homeassistant/components/switch/mfi.py | 10 +++++++--- requirements_all.txt | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/sensor/mfi.py b/homeassistant/components/sensor/mfi.py index 3e68f779dbb..d032279e6dd 100644 --- a/homeassistant/components/sensor/mfi.py +++ b/homeassistant/components/sensor/mfi.py @@ -14,7 +14,7 @@ from homeassistant.components.sensor import DOMAIN from homeassistant.helpers.entity import Entity from homeassistant.helpers import validate_config -REQUIREMENTS = ['mficlient==0.2'] +REQUIREMENTS = ['mficlient==0.2.2'] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/switch/mfi.py b/homeassistant/components/switch/mfi.py index aebaa95c88b..ac03e976e21 100644 --- a/homeassistant/components/switch/mfi.py +++ b/homeassistant/components/switch/mfi.py @@ -12,7 +12,7 @@ from homeassistant.components.switch import DOMAIN, SwitchDevice from homeassistant.const import CONF_USERNAME, CONF_PASSWORD from homeassistant.helpers import validate_config -REQUIREMENTS = ['mficlient==0.2'] +REQUIREMENTS = ['mficlient==0.2.2'] _LOGGER = logging.getLogger(__name__) @@ -56,6 +56,7 @@ class MfiSwitch(SwitchDevice): """ An mFi switch-able device. """ def __init__(self, port): self._port = port + self._target_state = None @property def should_poll(self): @@ -75,14 +76,17 @@ class MfiSwitch(SwitchDevice): def update(self): self._port.refresh() + if self._target_state is not None: + self._port.data['output'] = float(self._target_state) + self._target_state = None def turn_on(self): self._port.control(True) - self._port.data['output'] = 1.0 + self._target_state = True def turn_off(self): self._port.control(False) - self._port.data['output'] = 0.0 + self._target_state = False @property def current_power_mwh(self): diff --git a/requirements_all.txt b/requirements_all.txt index 922bff9cba1..73a53ab7435 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -107,7 +107,7 @@ limitlessled==1.0.0 # homeassistant.components.sensor.mfi # homeassistant.components.switch.mfi -mficlient==0.2 +mficlient==0.2.2 # homeassistant.components.discovery netdisco==0.5.2