From d076251b189c0a11f2bd528fdfdd0fb3d5dc8f5d Mon Sep 17 00:00:00 2001 From: Harvtronix Date: Thu, 13 Sep 2018 10:38:07 -0400 Subject: [PATCH] Changing z-wave brightness calculation to respect 0x01 and 0x02 byte values (#16420) * Changing z-wave brightness calculation to respect 0x01 and 0x02 byte values * adding additional line breaks to satisfy houndci * - Update comment style for linter - Add additional unit test to increase code coverage * Update zwave.py --- homeassistant/components/light/zwave.py | 12 +++++++++++- tests/components/light/test_zwave.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/light/zwave.py b/homeassistant/components/light/zwave.py index 55feef496f8..1e768eb127a 100644 --- a/homeassistant/components/light/zwave.py +++ b/homeassistant/components/light/zwave.py @@ -63,6 +63,16 @@ def brightness_state(value): return 0, STATE_OFF +def byte_to_zwave_brightness(value): + """Convert brightness in 0-255 scale to 0-99 scale. + + `value` -- (int) Brightness byte value from 0-255. + """ + if value > 0: + return max(1, int((value / 255) * 99)) + return 0 + + def ct_to_hs(temp): """Convert color temperature (mireds) to hs.""" colorlist = list( @@ -187,7 +197,7 @@ class ZwaveDimmer(zwave.ZWaveDeviceEntity, Light): # brightness. Level 255 means to set it to previous value. if ATTR_BRIGHTNESS in kwargs: self._brightness = kwargs[ATTR_BRIGHTNESS] - brightness = int((self._brightness / 255) * 99) + brightness = byte_to_zwave_brightness(self._brightness) else: brightness = 255 diff --git a/tests/components/light/test_zwave.py b/tests/components/light/test_zwave.py index 62bcf834b98..5805c8eb2fb 100644 --- a/tests/components/light/test_zwave.py +++ b/tests/components/light/test_zwave.py @@ -105,6 +105,26 @@ def test_dimmer_turn_on(mock_openzwave): assert entity_id == device.entity_id +def test_dimmer_min_brightness(mock_openzwave): + """Test turning on a dimmable Z-Wave light to its minimum brightness.""" + node = MockNode() + value = MockValue(data=0, node=node) + values = MockLightValues(primary=value) + device = zwave.get_device(node=node, values=values, node_config={}) + + assert not device.is_on + + device.turn_on(**{ATTR_BRIGHTNESS: 1}) + + assert device.is_on + assert device.brightness == 1 + + device.turn_on(**{ATTR_BRIGHTNESS: 0}) + + assert device.is_on + assert device.brightness == 0 + + def test_dimmer_transitions(mock_openzwave): """Test dimming transition on a dimmable Z-Wave light.""" node = MockNode()