From f11f4510a220acd28f4d9c422b7deb0d11dd758a Mon Sep 17 00:00:00 2001 From: Retha Runolfsson <137745329+zerzhang@users.noreply.github.com> Date: Sat, 19 Apr 2025 17:39:52 +0800 Subject: [PATCH] Add switchot switches unit test with restore state (#143250) --- tests/components/switchbot/test_switch.py | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/components/switchbot/test_switch.py diff --git a/tests/components/switchbot/test_switch.py b/tests/components/switchbot/test_switch.py new file mode 100644 index 00000000000..2d572fd9996 --- /dev/null +++ b/tests/components/switchbot/test_switch.py @@ -0,0 +1,47 @@ +"""Test the switchbot switches.""" + +from collections.abc import Callable +from unittest.mock import patch + +from homeassistant.components.switch import STATE_ON +from homeassistant.core import HomeAssistant, State + +from . import WOHAND_SERVICE_INFO + +from tests.common import MockConfigEntry, mock_restore_cache +from tests.components.bluetooth import inject_bluetooth_service_info + + +async def test_switchbot_switch_with_restore_state( + hass: HomeAssistant, + mock_entry_factory: Callable[[str], MockConfigEntry], +) -> None: + """Test that Switchbot Switch restores state correctly after reboot.""" + inject_bluetooth_service_info(hass, WOHAND_SERVICE_INFO) + + entry = mock_entry_factory(sensor_type="bot") + entity_id = "switch.test_name" + + mock_restore_cache( + hass, + [ + State( + entity_id, + STATE_ON, + {"last_run_success": True}, + ) + ], + ) + + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.switchbot.switch.switchbot.Switchbot.switch_mode", + return_value=False, + ): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state.state == STATE_ON + assert state.attributes["last_run_success"] is True