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_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 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}, ) actual_speeds = hass.states.get("fan.name_1").attributes[ATTR_SPEED_LIST] assert actual_speeds == [ fan.SPEED_OFF, fan.SPEED_LOW, fan.SPEED_MEDIUM, fan.SPEED_HIGH, ] with patch_bond_device_state(): with patch_bond_action() as mock_set_speed_low: await turn_fan_on(hass, "fan.name_1", fan.SPEED_LOW) mock_set_speed_low.assert_called_once_with("test-device-id", Action.set_speed(1)) with patch_bond_action() as mock_set_speed_medium: await turn_fan_on(hass, "fan.name_1", fan.SPEED_MEDIUM) mock_set_speed_medium.assert_called_once_with("test-device-id", Action.set_speed(3)) with patch_bond_action() as mock_set_speed_high: await turn_fan_on(hass, "fan.name_1", fan.SPEED_HIGH) mock_set_speed_high.assert_called_once_with("test-device-id", Action.set_speed(6))
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_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_set_position_cover(hass: core.HomeAssistant): """Tests that set position cover command delegates to API.""" await setup_platform( hass, COVER_DOMAIN, shades_with_position("name-1"), bond_device_id="test-device-id", ) with patch_bond_action() as mock_hold, patch_bond_device_state( return_value={"position": 0, "open": 1} ): await hass.services.async_call( COVER_DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.name_1", ATTR_POSITION: 100}, blocking=True, ) async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() mock_hold.assert_called_once_with("test-device-id", Action.set_position(0)) entity_state = hass.states.get("cover.name_1") assert entity_state.state == STATE_OPEN assert entity_state.attributes[ATTR_CURRENT_POSITION] == 100 with patch_bond_action() as mock_hold, patch_bond_device_state( return_value={"position": 100, "open": 0} ): await hass.services.async_call( COVER_DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.name_1", ATTR_POSITION: 0}, blocking=True, ) async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() mock_hold.assert_called_once_with("test-device-id", Action.set_position(100)) entity_state = hass.states.get("cover.name_1") assert entity_state.state == STATE_CLOSED assert entity_state.attributes[ATTR_CURRENT_POSITION] == 0 with patch_bond_action() as mock_hold, patch_bond_device_state( return_value={"position": 40, "open": 1} ): await hass.services.async_call( COVER_DOMAIN, SERVICE_SET_COVER_POSITION, {ATTR_ENTITY_ID: "cover.name_1", ATTR_POSITION: 60}, blocking=True, ) async_fire_time_changed(hass, utcnow() + timedelta(seconds=30)) await hass.async_block_till_done() mock_hold.assert_called_once_with("test-device-id", Action.set_position(40)) entity_state = hass.states.get("cover.name_1") assert entity_state.state == STATE_OPEN assert entity_state.attributes[ATTR_CURRENT_POSITION] == 60
async def async_turn_on(self, **kwargs: Any) -> None: """Turn the fireplace on.""" brightness = kwargs.get(ATTR_BRIGHTNESS) if brightness: flame = round((brightness * 100) / 255) await self._hub.bond.action(self._device.device_id, Action.set_flame(flame)) else: await self._hub.bond.action(self._device.device_id, Action.turn_on())
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.""" brightness = kwargs.get(ATTR_BRIGHTNESS) if brightness: await self._hub.bond.action( self._device.device_id, Action.set_brightness(round((brightness * 100) / 255)), ) else: await self._hub.bond.action(self._device.device_id, Action.turn_light_on())
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_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 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 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_turn_on(self, speed: Optional[str] = None, **kwargs) -> None: """Turn on the fan.""" if speed is not None: await self.async_set_speed(speed) else: await self._hub.bond.action(self._device.device_id, Action.turn_on())
async def test_turn_on_fan_with_speed(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" ) with patch_bond_action() as mock_set_speed, patch_bond_device_state(): await turn_fan_on(hass, "fan.name_1", fan.SPEED_LOW) mock_set_speed.assert_called_with("test-device-id", Action.set_speed(1))
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 async_set_power_belief(self, power_state: bool) -> None: """Set the belief state of the light.""" 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_speed(opp: core.OpenPeerPower): """Tests that turn on command delegates to turn off API.""" await setup_platform(opp, 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(opp, "fan.name_1", fan.SPEED_OFF) mock_turn_off.assert_called_with("test-device-id", Action.turn_off())
async def async_turn_on(self, speed: Optional[str] = None, **kwargs) -> None: """Turn on the fan.""" _LOGGER.debug("Fan async_turn_on called with speed %s", speed) if speed is not None: if speed == SPEED_OFF: await self.async_turn_off() else: await self.async_set_speed(speed) else: await self._hub.bond.action(self._device.device_id, Action.turn_on())
async def test_hold(bond: Bond): """Tests hold action delegates to API.""" with aioresponses() as response: def callback(_url, **kwargs): assert kwargs.get("json") == {} return CallbackResult() response.put("http://test-host/v2/devices/test-device-id/actions/Hold", callback=callback) await bond.action("test-device-id", Action.hold())
async def async_set_speed(self, speed: str) -> None: """Set the desired speed for the fan.""" max_speed = self._device.props.get("max_speed", 3) if speed == SPEED_LOW: bond_speed = 1 elif speed == SPEED_HIGH: bond_speed = max_speed else: bond_speed = math.ceil(max_speed / 2) await self._hub.bond.action(self._device.device_id, Action.set_speed(bond_speed))
async def test_set_flame(bond: Bond): """Tests set_flame action delegates to API.""" with aioresponses() as response: def callback(_url, **kwargs): assert kwargs.get("json") == {"argument": 50} return CallbackResult() response.put( "http://test-host/v2/devices/test-device-id/actions/SetFlame", callback=callback ) await bond.action("test-device-id", Action.set_flame(50))
async def test_set_direction_reverse(bond: Bond): """Tests set_direction action delegates to API with correct value for reverse.""" with aioresponses() as response: def callback(_url, **kwargs): assert kwargs.get("json") == {"argument": -1} return CallbackResult() response.put( "http://test-host/v2/devices/test-device-id/actions/SetDirection", callback=callback ) await bond.action("test-device-id", Action.set_direction(Direction.REVERSE))
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, speed: str | None = None, 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 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_on_fan_with_percentage_3_speeds(opp: core.OpenPeerPower): """Tests that turn on command delegates to set speed API.""" await setup_platform(opp, FAN_DOMAIN, ceiling_fan("name-1"), bond_device_id="test-device-id") with patch_bond_action() as mock_set_speed, patch_bond_device_state(): await turn_fan_on(opp, "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(opp, "fan.name_1", percentage=50) mock_set_speed.assert_called_with("test-device-id", Action.set_speed(2)) mock_set_speed.reset_mock() with patch_bond_action() as mock_set_speed, patch_bond_device_state(): await turn_fan_on(opp, "fan.name_1", percentage=100) mock_set_speed.assert_called_with("test-device-id", Action.set_speed(3))