async def test_turn_on_fan_preset_mode(hass: core.HomeAssistant): """Tests that turn on command delegates to breeze on API.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan_with_breeze("name-1"), bond_device_id="test-device-id", props={"max_speed": 6}, ) assert hass.states.get("fan.name_1").attributes[ATTR_PRESET_MODES] == [ PRESET_MODE_BREEZE ] with patch_bond_action() as mock_set_preset_mode, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", preset_mode=PRESET_MODE_BREEZE) mock_set_preset_mode.assert_called_with("test-device-id", Action(Action.BREEZE_ON)) with patch_bond_action() as mock_set_preset_mode, patch_bond_device_state(): await hass.services.async_call( FAN_DOMAIN, SERVICE_SET_PRESET_MODE, service_data={ ATTR_PRESET_MODE: PRESET_MODE_BREEZE, ATTR_ENTITY_ID: "fan.name_1", }, blocking=True, ) mock_set_preset_mode.assert_called_with("test-device-id", Action(Action.BREEZE_ON))
async def test_non_standard_speed_list(hass: core.HomeAssistant): """Tests that the device is registered with custom speed list if number of supported speeds differs form 3.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id", props={"max_speed": 6}, ) with patch_bond_device_state(): with patch_bond_action() as mock_set_speed_low: await turn_fan_on(hass, "fan.name_1", percentage=100 / 6 * 2) mock_set_speed_low.assert_called_once_with( "test-device-id", Action.set_speed(2) ) with patch_bond_action() as mock_set_speed_medium: await turn_fan_on(hass, "fan.name_1", percentage=100 / 6 * 4) mock_set_speed_medium.assert_called_once_with( "test-device-id", Action.set_speed(4) ) with patch_bond_action() as mock_set_speed_high: await turn_fan_on(hass, "fan.name_1", percentage=100) mock_set_speed_high.assert_called_once_with( "test-device-id", Action.set_speed(6) )
async def test_turn_on_fan_with_percentage_6_speeds(hass: core.HomeAssistant): """Tests that turn on command delegates to set speed API.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id", props={"max_speed": 6}, ) with patch_bond_action() as mock_set_speed, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", percentage=10) mock_set_speed.assert_called_with("test-device-id", Action.set_speed(1)) mock_set_speed.reset_mock() with patch_bond_action() as mock_set_speed, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", percentage=50) mock_set_speed.assert_called_with("test-device-id", Action.set_speed(3)) mock_set_speed.reset_mock() with patch_bond_action() as mock_set_speed, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", percentage=100) mock_set_speed.assert_called_with("test-device-id", Action.set_speed(6))
async def async_turn_off(self, **kwargs: Any) -> None: """Turn the fan off.""" if self.preset_mode == PRESET_MODE_BREEZE: await self._hub.bond.action( self._device.device_id, Action(Action.BREEZE_OFF) ) await self._hub.bond.action(self._device.device_id, Action.turn_off())
async def test_press_button_with_argument(hass: core.HomeAssistant): """Tests we can press a button with an argument.""" await setup_platform( hass, BUTTON_DOMAIN, fireplace_increase_decrease_only("name-1"), bond_device_id="test-device-id", ) assert hass.states.get("button.name_1_increase_flame") assert hass.states.get("button.name_1_decrease_flame") with patch_bond_action() as mock_action, patch_bond_device_state(): await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: "button.name_1_increase_flame"}, blocking=True, ) await hass.async_block_till_done() mock_action.assert_called_once_with( "test-device-id", Action(Action.INCREASE_FLAME, STEP_SIZE)) with patch_bond_action() as mock_action, patch_bond_device_state(): await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: "button.name_1_decrease_flame"}, blocking=True, ) await hass.async_block_till_done() mock_action.assert_called_once_with( "test-device-id", Action(Action.DECREASE_FLAME, STEP_SIZE))
async def test_press_button(hass: core.HomeAssistant): """Tests we can press a button.""" await setup_platform( hass, BUTTON_DOMAIN, light_brightness_increase_decrease_only("name-1"), bond_device_id="test-device-id", ) assert hass.states.get("button.name_1_start_increasing_brightness") assert hass.states.get("button.name_1_start_decreasing_brightness") with patch_bond_action() as mock_action, patch_bond_device_state(): await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: "button.name_1_start_increasing_brightness"}, blocking=True, ) await hass.async_block_till_done() mock_action.assert_called_once_with( "test-device-id", Action(Action.START_INCREASING_BRIGHTNESS)) with patch_bond_action() as mock_action, patch_bond_device_state(): await hass.services.async_call( BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: "button.name_1_start_decreasing_brightness"}, blocking=True, ) await hass.async_block_till_done() mock_action.assert_called_once_with( "test-device-id", Action(Action.START_DECREASING_BRIGHTNESS))
async def async_press(self, **kwargs: Any) -> None: """Press the button.""" if self.entity_description.argument: action = Action(self.entity_description.key, self.entity_description.argument) else: action = Action(self.entity_description.key) await self._hub.bond.action(self._device.device_id, action)
async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the light.""" if brightness := kwargs.get(ATTR_BRIGHTNESS): await self._hub.bond.action( self._device.device_id, Action.set_brightness(round((brightness * 100) / 255)), )
async def async_set_preset_mode(self, preset_mode: str) -> None: """Set the preset mode of the fan.""" if preset_mode != PRESET_MODE_BREEZE or not self._device.has_action( Action.BREEZE_ON ): raise ValueError(f"Invalid preset mode: {preset_mode}") await self._hub.bond.action(self._device.device_id, Action(Action.BREEZE_ON))
async def async_set_direction(self, direction: str) -> None: """Set fan rotation direction.""" bond_direction = ( Direction.REVERSE if direction == DIRECTION_REVERSE else Direction.FORWARD ) await self._hub.bond.action( self._device.device_id, Action.set_direction(bond_direction) )
async def async_stop(self) -> None: """Stop all actions and clear the queue.""" _LOGGER.warning( "The bond.stop service is deprecated and has been replaced with a button; Call the button.press service instead" ) self._async_has_action_or_raise(Action.STOP) await self._hub.bond.action(self._device.device_id, Action(Action.STOP))
async def async_turn_on(self, **kwargs: Any) -> None: """Turn the fireplace on.""" _LOGGER.debug("Fireplace async_turn_on called with: %s", kwargs) if brightness := kwargs.get(ATTR_BRIGHTNESS): flame = round((brightness * 100) / 255) await self._hub.bond.action(self._device.device_id, Action.set_flame(flame))
async def async_start_decreasing_brightness(self) -> None: """Start decreasing the light brightness.""" _LOGGER.warning( "The bond.start_decreasing_brightness service is deprecated and has been replaced with a button; Call the button.press service instead" ) self._async_has_action_or_raise(Action.START_DECREASING_BRIGHTNESS) await self._hub.bond.action(self._device.device_id, Action(Action.START_DECREASING_BRIGHTNESS))
async def test_set_speed_belief_speed_100(hass: core.HomeAssistant): """Tests that set power belief service delegates to API.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id" ) with patch_bond_action() as mock_action, patch_bond_device_state(): await hass.services.async_call( BOND_DOMAIN, SERVICE_SET_FAN_SPEED_TRACKED_STATE, {ATTR_ENTITY_ID: "fan.name_1", "speed": 100}, blocking=True, ) await hass.async_block_till_done() mock_action.assert_any_call("test-device-id", Action.set_power_state_belief(True)) mock_action.assert_called_with("test-device-id", Action.set_speed_belief(3))
async def async_set_power_belief(self, power_state: bool) -> None: """Set the believed state to on or off.""" try: await self._hub.bond.action( self._device.device_id, Action.set_power_state_belief(power_state) ) except ClientResponseError as ex: raise HomeAssistantError( f"The bond API returned an error calling set_power_state_belief for {self.entity_id}. Code: {ex.code} Message: {ex.message}" ) from ex
async def test_turn_on_fan_with_off_percentage(hass: core.HomeAssistant): """Tests that turn off command delegates to turn off API.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id" ) with patch_bond_action() as mock_turn_off, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", percentage=0) mock_turn_off.assert_called_with("test-device-id", Action.turn_off())
async def test_turn_on_fan_with_off_with_breeze(hass: core.HomeAssistant): """Tests that turn off command delegates to turn off API.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan_with_breeze("name-1"), bond_device_id="test-device-id", state={"breeze": [1, 0, 0]}, ) assert (hass.states.get("fan.name_1").attributes[ATTR_PRESET_MODE] == PRESET_MODE_BREEZE) with patch_bond_action() as mock_actions, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", percentage=0) assert mock_actions.mock_calls == [ call("test-device-id", Action(Action.BREEZE_OFF)), call("test-device-id", Action.turn_off()), ]
async def async_turn_on( self, percentage: int | None = None, preset_mode: str | None = None, **kwargs: Any, ) -> None: """Turn on the fan.""" _LOGGER.debug("Fan async_turn_on called with percentage %s", percentage) if preset_mode is not None: await self.async_set_preset_mode(preset_mode) elif percentage is not None: await self.async_set_percentage(percentage) else: await self._hub.bond.action(self._device.device_id, Action.turn_on())
async def test_turn_off_switch(hass: core.HomeAssistant): """Tests that turn off command delegates to API.""" await setup_platform( hass, SWITCH_DOMAIN, generic_device("name-1"), bond_device_id="test-device-id" ) with patch_bond_action() as mock_turn_off, patch_bond_device_state(): await hass.services.async_call( SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "switch.name_1"}, blocking=True, ) await hass.async_block_till_done() mock_turn_off.assert_called_once_with("test-device-id", Action.turn_off())
async def test_set_speed_off(hass: core.HomeAssistant): """Tests that set_speed(off) command delegates to turn off API.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id" ) with patch_bond_action() as mock_turn_off, patch_bond_device_state(): await hass.services.async_call( FAN_DOMAIN, SERVICE_SET_PERCENTAGE, service_data={ATTR_ENTITY_ID: "fan.name_1", ATTR_PERCENTAGE: 0}, blocking=True, ) await hass.async_block_till_done() mock_turn_off.assert_called_with("test-device-id", Action.turn_off())
async def test_open_cover(hass: core.HomeAssistant): """Tests that open cover command delegates to API.""" await setup_platform(hass, COVER_DOMAIN, shades("name-1"), bond_device_id="test-device-id") with patch_bond_action() as mock_open, patch_bond_device_state(): await hass.services.async_call( COVER_DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: "cover.name_1"}, blocking=True, ) await hass.async_block_till_done() mock_open.assert_called_once_with("test-device-id", Action.open())
async def test_turn_on_fireplace_without_brightness(hass: core.HomeAssistant): """Tests that turn on command delegates to turn on API.""" await setup_platform(hass, LIGHT_DOMAIN, fireplace("name-1"), bond_device_id="test-device-id") with patch_bond_action() as mock_turn_on, patch_bond_device_state(): await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: "light.name_1"}, blocking=True, ) await hass.async_block_till_done() mock_turn_on.assert_called_once_with("test-device-id", Action.turn_on())
async def async_set_brightness_belief(self, brightness: int) -> None: """Set the belief state of the light.""" if not self._device.supports_set_brightness(): raise HomeAssistantError( "This device does not support setting brightness") if brightness == 0: await self.async_set_power_belief(False) return try: await self._hub.bond.action( self._device.device_id, Action.set_brightness_belief(round((brightness * 100) / 255)), ) except ClientResponseError as ex: raise HomeAssistantError( f"The bond API returned an error calling set_brightness_belief for {self.entity_id}. Code: {ex.code} Message: {ex.message}" ) from ex
async def test_set_fan_direction(hass: core.HomeAssistant): """Tests that set direction command delegates to API.""" await setup_platform( hass, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id" ) with patch_bond_action() as mock_set_direction, patch_bond_device_state(): await hass.services.async_call( FAN_DOMAIN, SERVICE_SET_DIRECTION, {ATTR_ENTITY_ID: "fan.name_1", ATTR_DIRECTION: DIRECTION_FORWARD}, blocking=True, ) await hass.async_block_till_done() mock_set_direction.assert_called_once_with( "test-device-id", Action.set_direction(Direction.FORWARD) )
async def test_tilt_close_cover(hass: core.HomeAssistant): """Tests that tilt close cover command delegates to API.""" await setup_platform(hass, COVER_DOMAIN, tilt_only_shades("name-1"), bond_device_id="test-device-id") with patch_bond_action() as mock_close, patch_bond_device_state(): await hass.services.async_call( COVER_DOMAIN, SERVICE_CLOSE_COVER_TILT, {ATTR_ENTITY_ID: "cover.name_1"}, blocking=True, ) await hass.async_block_till_done() mock_close.assert_called_once_with("test-device-id", Action.tilt_close()) assert hass.states.get("cover.name_1").state == STATE_UNKNOWN
async def test_switch_set_power_belief(hass: core.HomeAssistant): """Tests that the set power belief service delegates to API.""" await setup_platform( hass, SWITCH_DOMAIN, generic_device("name-1"), bond_device_id="test-device-id" ) with patch_bond_action() as mock_bond_action, patch_bond_device_state(): await hass.services.async_call( BOND_DOMAIN, SERVICE_SET_POWER_TRACKED_STATE, {ATTR_ENTITY_ID: "switch.name_1", ATTR_POWER_STATE: False}, blocking=True, ) await hass.async_block_till_done() mock_bond_action.assert_called_once_with( "test-device-id", Action.set_power_state_belief(False) )
async def async_set_percentage(self, percentage: int) -> None: """Set the desired speed for the fan.""" _LOGGER.debug("async_set_percentage called with percentage %s", percentage) if percentage == 0: await self.async_turn_off() return bond_speed = math.ceil( percentage_to_ranged_value(self._speed_range, percentage) ) _LOGGER.debug( "async_set_percentage converted percentage %s to bond speed %s", percentage, bond_speed, ) await self._hub.bond.action( self._device.device_id, Action.set_speed(bond_speed) )
async def test_turn_off_down_light(hass: core.HomeAssistant): """Tests that turn off command, on a down light, delegates to API.""" await setup_platform( hass, LIGHT_DOMAIN, down_light_ceiling_fan("name-1"), bond_device_id="test-device-id", ) with patch_bond_action() as mock_turn_off, patch_bond_device_state(): await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "light.name_1_down_light"}, blocking=True, ) await hass.async_block_till_done() mock_turn_off.assert_called_once_with("test-device-id", Action(Action.TURN_DOWN_LIGHT_OFF))
async def test_light_stop(hass: core.HomeAssistant): """Tests a light that can only increase or decrease brightness delegates to API can stop.""" await setup_platform( hass, LIGHT_DOMAIN, light_brightness_increase_decrease_only("name-1"), bond_device_id="test-device-id", ) with patch_bond_action() as mock_bond_action, patch_bond_device_state(): await hass.services.async_call( DOMAIN, SERVICE_STOP, {ATTR_ENTITY_ID: "light.name_1"}, blocking=True, ) await hass.async_block_till_done() mock_bond_action.assert_called_once_with("test-device-id", Action(Action.STOP))
async def test_tilt_and_open(hass: core.HomeAssistant): """Tests that supports both tilt and open.""" await setup_platform( hass, COVER_DOMAIN, tilt_shades("name-1"), bond_device_id="test-device-id", state={"open": False}, ) with patch_bond_action() as mock_open, patch_bond_device_state(): await hass.services.async_call( COVER_DOMAIN, SERVICE_OPEN_COVER_TILT, {ATTR_ENTITY_ID: "cover.name_1"}, blocking=True, ) await hass.async_block_till_done() mock_open.assert_called_once_with("test-device-id", Action.tilt_open()) assert hass.states.get("cover.name_1").state == STATE_CLOSED