From 1571b33e4a12e82700922f5c9a1dcf91ff436dfa Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 13 Feb 2016 00:08:32 -0800 Subject: [PATCH 1/3] Fix: state_as_number always return float --- homeassistant/core.py | 2 +- homeassistant/helpers/state.py | 13 +------------ tests/components/test_graphite.py | 1 + tests/components/test_statsd.py | 5 +++-- tests/helpers/test_state.py | 22 ++++++++-------------- 5 files changed, 14 insertions(+), 29 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 39a10beda8d..37f909e218a 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -309,7 +309,7 @@ class State(object): "Format should be .").format(entity_id)) self.entity_id = entity_id.lower() - self.state = state + self.state = str(state) self.attributes = MappingProxyType(attributes or {}) self.last_updated = dt_util.strip_microseconds( last_updated or dt_util.utcnow()) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index 1a3520d4733..23673e678ca 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -109,16 +109,5 @@ def state_as_number(state): elif state.state in (STATE_OFF, STATE_UNLOCKED, STATE_UNKNOWN, STATE_BELOW_HORIZON, STATE_CLOSED): return 0 - else: - try: - # This distinction is probably not important, - # but in case something downstream cares about - # int vs. float, try to be helpful here. - if '.' in state.state: - return float(state.state) - else: - return int(state.state) - except (ValueError, TypeError): - pass - raise ValueError('State is not a number') + return float(state.state) diff --git a/tests/components/test_graphite.py b/tests/components/test_graphite.py index 720da54930f..b50a7f19630 100644 --- a/tests/components/test_graphite.py +++ b/tests/components/test_graphite.py @@ -89,6 +89,7 @@ class TestGraphite(unittest.TestCase): 'bat': 'NaN', } expected = [ + 'ha.entity.state 0.000000 12345', 'ha.entity.foo 1.000000 12345', 'ha.entity.bar 2.000000 12345', 'ha.entity.baz 1.000000 12345', diff --git a/tests/components/test_statsd.py b/tests/components/test_statsd.py index 72c61a22f54..ad3a96a1a8d 100644 --- a/tests/components/test_statsd.py +++ b/tests/components/test_statsd.py @@ -7,6 +7,7 @@ Tests statsd feeder. import unittest from unittest import mock +import homeassistant.core as ha import homeassistant.components.statsd as statsd from homeassistant.const import STATE_ON, STATE_OFF, EVENT_STATE_CHANGED @@ -78,6 +79,6 @@ class TestStatsd(unittest.TestCase): mock_gauge.return_value.send.reset_mock() for invalid in ('foo', '', object): - state = mock.MagicMock(state=invalid) - handler_method(mock.MagicMock(data={'new_state': state})) + handler_method(mock.MagicMock(data={ + 'new_state': ha.State('domain.test', invalid, {})})) self.assertFalse(mock_gauge.return_value.send.called) diff --git a/tests/helpers/test_state.py b/tests/helpers/test_state.py index e222e72fe4b..27cc5426c3e 100644 --- a/tests/helpers/test_state.py +++ b/tests/helpers/test_state.py @@ -165,24 +165,18 @@ class TestStateHelpers(unittest.TestCase): ha.State('domain.test', _state, {}))) def test_as_number_coercion(self): - for _state in ('0', '0.0'): + for _state in ('0', '0.0', 0, 0.0): self.assertEqual( - 0.0, float(state.state_as_number( - ha.State('domain.test', _state, {})))) - for _state in ('1', '1.0'): + 0.0, state.state_as_number( + ha.State('domain.test', _state, {}))) + for _state in ('1', '1.0', 1, 1.0): self.assertEqual( - 1.0, float(state.state_as_number( - ha.State('domain.test', _state, {})))) - - def test_as_number_tries_to_keep_types(self): - result = state.state_as_number(ha.State('domain.test', '1', {})) - self.assertTrue(isinstance(result, int)) - result = state.state_as_number(ha.State('domain.test', '1.0', {})) - self.assertTrue(isinstance(result, float)) + 1.0, state.state_as_number( + ha.State('domain.test', _state, {}))) def test_as_number_invalid_cases(self): - for _state in ('', 'foo', 'foo.bar', None, False, True, None, - object, object()): + for _state in ('', 'foo', 'foo.bar', None, False, True, object, + object()): self.assertRaises(ValueError, state.state_as_number, ha.State('domain.test', _state, {})) From 77b141a3558f9b7ca78b672b0e225edd46e9434f Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 13 Feb 2016 08:17:38 -0800 Subject: [PATCH 2/3] Update Wink to 0.6 --- homeassistant/components/garage_door/wink.py | 2 +- homeassistant/components/light/wink.py | 2 +- homeassistant/components/lock/wink.py | 2 +- homeassistant/components/sensor/wink.py | 2 +- homeassistant/components/switch/wink.py | 2 +- homeassistant/components/wink.py | 2 +- requirements_all.txt | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/garage_door/wink.py b/homeassistant/components/garage_door/wink.py index aa714a790ca..b32b20dde0a 100644 --- a/homeassistant/components/garage_door/wink.py +++ b/homeassistant/components/garage_door/wink.py @@ -11,7 +11,7 @@ import logging from homeassistant.components.garage_door import GarageDoorDevice from homeassistant.const import CONF_ACCESS_TOKEN -REQUIREMENTS = ['python-wink==0.5.0'] +REQUIREMENTS = ['python-wink==0.6.0'] def setup_platform(hass, config, add_devices, discovery_info=None): diff --git a/homeassistant/components/light/wink.py b/homeassistant/components/light/wink.py index 6b7bb1afcec..2fed8904762 100644 --- a/homeassistant/components/light/wink.py +++ b/homeassistant/components/light/wink.py @@ -11,7 +11,7 @@ import logging from homeassistant.components.light import ATTR_BRIGHTNESS, Light from homeassistant.const import CONF_ACCESS_TOKEN -REQUIREMENTS = ['python-wink==0.5.0'] +REQUIREMENTS = ['python-wink==0.6.0'] def setup_platform(hass, config, add_devices_callback, discovery_info=None): diff --git a/homeassistant/components/lock/wink.py b/homeassistant/components/lock/wink.py index 9c73c35d130..be558dceee7 100644 --- a/homeassistant/components/lock/wink.py +++ b/homeassistant/components/lock/wink.py @@ -11,7 +11,7 @@ import logging from homeassistant.components.lock import LockDevice from homeassistant.const import CONF_ACCESS_TOKEN -REQUIREMENTS = ['python-wink==0.5.0'] +REQUIREMENTS = ['python-wink==0.6.0'] def setup_platform(hass, config, add_devices, discovery_info=None): diff --git a/homeassistant/components/sensor/wink.py b/homeassistant/components/sensor/wink.py index 48583426d72..71fadff157b 100644 --- a/homeassistant/components/sensor/wink.py +++ b/homeassistant/components/sensor/wink.py @@ -11,7 +11,7 @@ import logging from homeassistant.helpers.entity import Entity from homeassistant.const import CONF_ACCESS_TOKEN, STATE_OPEN, STATE_CLOSED -REQUIREMENTS = ['python-wink==0.5.0'] +REQUIREMENTS = ['python-wink==0.6.0'] def setup_platform(hass, config, add_devices, discovery_info=None): diff --git a/homeassistant/components/switch/wink.py b/homeassistant/components/switch/wink.py index 19abc1c4fce..23ba434ad2b 100644 --- a/homeassistant/components/switch/wink.py +++ b/homeassistant/components/switch/wink.py @@ -11,7 +11,7 @@ import logging from homeassistant.components.wink import WinkToggleDevice from homeassistant.const import CONF_ACCESS_TOKEN -REQUIREMENTS = ['python-wink==0.5.0'] +REQUIREMENTS = ['python-wink==0.6.0'] def setup_platform(hass, config, add_devices, discovery_info=None): diff --git a/homeassistant/components/wink.py b/homeassistant/components/wink.py index 6fa2b9287e9..ef97e958dc0 100644 --- a/homeassistant/components/wink.py +++ b/homeassistant/components/wink.py @@ -16,7 +16,7 @@ from homeassistant.const import ( ATTR_SERVICE, ATTR_DISCOVERED) DOMAIN = "wink" -REQUIREMENTS = ['python-wink==0.5.0'] +REQUIREMENTS = ['python-wink==0.6.0'] DISCOVER_LIGHTS = "wink.lights" DISCOVER_SWITCHES = "wink.switches" diff --git a/requirements_all.txt b/requirements_all.txt index b5532905ca3..bfd1e8c7c45 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -199,7 +199,7 @@ python-twitch==1.2.0 # homeassistant.components.lock.wink # homeassistant.components.sensor.wink # homeassistant.components.switch.wink -python-wink==0.5.0 +python-wink==0.6.0 # homeassistant.components.keyboard pyuserinput==0.1.9 From d2df485beac06e9c3c33fe8d4829091634a6b314 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 13 Feb 2016 08:17:47 -0800 Subject: [PATCH 3/3] Version bump to 0.13.1 --- homeassistant/const.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/const.py b/homeassistant/const.py index dbbec17c0dd..0403717b5dd 100644 --- a/homeassistant/const.py +++ b/homeassistant/const.py @@ -1,7 +1,7 @@ # coding: utf-8 """Constants used by Home Assistant components.""" -__version__ = "0.13.0" +__version__ = "0.13.1" # Can be used to specify a catch all when registering state or event listeners. MATCH_ALL = '*'