Update zwave to use new fan entity model (#45541)

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
pull/45663/head
J. Nick Koston 2021-01-28 03:35:01 -06:00 committed by GitHub
parent ee592350b3
commit 22e44e4ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 44 deletions

View File

@ -1,28 +1,19 @@
"""Support for Z-Wave fans."""
import math
from homeassistant.components.fan import (
DOMAIN,
SPEED_HIGH,
SPEED_LOW,
SPEED_MEDIUM,
SPEED_OFF,
SUPPORT_SET_SPEED,
FanEntity,
)
from homeassistant.components.fan import DOMAIN, SUPPORT_SET_SPEED, FanEntity
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.util.percentage import (
percentage_to_ranged_value,
ranged_value_to_percentage,
)
from . import ZWaveDeviceEntity
SPEED_LIST = [SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]
SUPPORTED_FEATURES = SUPPORT_SET_SPEED
# Value will first be divided to an integer
VALUE_TO_SPEED = {0: SPEED_OFF, 1: SPEED_LOW, 2: SPEED_MEDIUM, 3: SPEED_HIGH}
SPEED_TO_VALUE = {SPEED_OFF: 0, SPEED_LOW: 1, SPEED_MEDIUM: 50, SPEED_HIGH: 99}
SPEED_RANGE = (1, 99) # off is not included
async def async_setup_entry(hass, config_entry, async_add_entities):
@ -51,41 +42,31 @@ class ZwaveFan(ZWaveDeviceEntity, FanEntity):
def update_properties(self):
"""Handle data changes for node values."""
value = math.ceil(self.values.primary.data * 3 / 100)
self._state = VALUE_TO_SPEED[value]
self._state = self.values.primary.data
def set_speed(self, speed):
"""Set the speed of the fan."""
self.node.set_dimmer(self.values.primary.value_id, SPEED_TO_VALUE[speed])
def set_percentage(self, percentage):
"""Set the speed percentage of the fan."""
if percentage is None:
# Value 255 tells device to return to previous value
zwave_speed = 255
elif percentage == 0:
zwave_speed = 0
else:
zwave_speed = math.ceil(percentage_to_ranged_value(SPEED_RANGE, percentage))
self.node.set_dimmer(self.values.primary.value_id, zwave_speed)
#
# The fan entity model has changed to use percentages and preset_modes
# instead of speeds.
#
# Please review
# https://developers.home-assistant.io/docs/core/entity/fan/
#
def turn_on(self, speed=None, percentage=None, preset_mode=None, **kwargs):
"""Turn the device on."""
if speed is None:
# Value 255 tells device to return to previous value
self.node.set_dimmer(self.values.primary.value_id, 255)
else:
self.set_speed(speed)
self.set_percentage(percentage)
def turn_off(self, **kwargs):
"""Turn the device off."""
self.node.set_dimmer(self.values.primary.value_id, 0)
@property
def speed(self):
"""Return the current speed."""
return self._state
@property
def speed_list(self):
"""Get the list of available speeds."""
return SPEED_LIST
def percentage(self):
"""Return the current speed percentage."""
return ranged_value_to_percentage(SPEED_RANGE, self._state)
@property
def supported_features(self):

View File

@ -39,7 +39,7 @@ def test_fan_turn_on(mock_openzwave):
node.reset_mock()
device.turn_on(speed=SPEED_OFF)
device.turn_on(percentage=0)
assert node.set_dimmer.called
value_id, brightness = node.set_dimmer.mock_calls[0][1]
@ -49,7 +49,7 @@ def test_fan_turn_on(mock_openzwave):
node.reset_mock()
device.turn_on(speed=SPEED_LOW)
device.turn_on(percentage=1)
assert node.set_dimmer.called
value_id, brightness = node.set_dimmer.mock_calls[0][1]
@ -59,7 +59,7 @@ def test_fan_turn_on(mock_openzwave):
node.reset_mock()
device.turn_on(speed=SPEED_MEDIUM)
device.turn_on(percentage=50)
assert node.set_dimmer.called
value_id, brightness = node.set_dimmer.mock_calls[0][1]
@ -69,7 +69,7 @@ def test_fan_turn_on(mock_openzwave):
node.reset_mock()
device.turn_on(speed=SPEED_HIGH)
device.turn_on(percentage=100)
assert node.set_dimmer.called
value_id, brightness = node.set_dimmer.mock_calls[0][1]