Bump switchbot-api to 2.4.0 (#145786)

* update switchbot-api version to 2.4.0

* debug for test code
This commit is contained in:
Samuel Xiao 2025-05-31 02:22:40 +08:00 committed by GitHub
parent 66bb638dd0
commit 6d11c0395f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 35 additions and 19 deletions

View File

@ -7,7 +7,13 @@ from dataclasses import dataclass, field
from logging import getLogger
from aiohttp import web
from switchbot_api import CannotConnect, Device, InvalidAuth, Remote, SwitchBotAPI
from switchbot_api import (
Device,
Remote,
SwitchBotAPI,
SwitchBotAuthenticationError,
SwitchBotConnectionError,
)
from homeassistant.components import webhook
from homeassistant.config_entries import ConfigEntry
@ -175,12 +181,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
api = SwitchBotAPI(token=token, secret=secret)
try:
devices = await api.list_devices()
except InvalidAuth as ex:
except SwitchBotAuthenticationError as ex:
_LOGGER.error(
"Invalid authentication while connecting to SwitchBot API: %s", ex
)
return False
except CannotConnect as ex:
except SwitchBotConnectionError as ex:
raise ConfigEntryNotReady from ex
_LOGGER.debug("Devices: %s", devices)
coordinators_by_id: dict[str, SwitchBotCoordinator] = {}

View File

@ -3,7 +3,11 @@
from logging import getLogger
from typing import Any
from switchbot_api import CannotConnect, InvalidAuth, SwitchBotAPI
from switchbot_api import (
SwitchBotAPI,
SwitchBotAuthenticationError,
SwitchBotConnectionError,
)
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
@ -36,9 +40,9 @@ class SwitchBotCloudConfigFlow(ConfigFlow, domain=DOMAIN):
await SwitchBotAPI(
token=user_input[CONF_API_TOKEN], secret=user_input[CONF_API_KEY]
).list_devices()
except CannotConnect:
except SwitchBotConnectionError:
errors["base"] = "cannot_connect"
except InvalidAuth:
except SwitchBotAuthenticationError:
errors["base"] = "invalid_auth"
except Exception:
_LOGGER.exception("Unexpected exception")

View File

@ -4,7 +4,7 @@ from asyncio import timeout
from logging import getLogger
from typing import Any
from switchbot_api import CannotConnect, Device, Remote, SwitchBotAPI
from switchbot_api import Device, Remote, SwitchBotAPI, SwitchBotConnectionError
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@ -70,5 +70,5 @@ class SwitchBotCoordinator(DataUpdateCoordinator[Status]):
status: Status = await self._api.get_status(self._device_id)
_LOGGER.debug("Refreshing %s with %s", self._device_id, status)
return status
except CannotConnect as err:
except SwitchBotConnectionError as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err

View File

@ -8,5 +8,5 @@
"integration_type": "hub",
"iot_class": "cloud_polling",
"loggers": ["switchbot_api"],
"requirements": ["switchbot-api==2.3.1"]
"requirements": ["switchbot-api==2.4.0"]
}

2
requirements_all.txt generated
View File

@ -2859,7 +2859,7 @@ surepy==0.9.0
swisshydrodata==0.1.0
# homeassistant.components.switchbot_cloud
switchbot-api==2.3.1
switchbot-api==2.4.0
# homeassistant.components.synology_srm
synology-srm==0.2.0

View File

@ -2354,7 +2354,7 @@ subarulink==0.7.13
surepy==0.9.0
# homeassistant.components.switchbot_cloud
switchbot-api==2.3.1
switchbot-api==2.4.0
# homeassistant.components.system_bridge
systembridgeconnector==4.1.5

View File

@ -6,8 +6,8 @@ import pytest
from homeassistant import config_entries
from homeassistant.components.switchbot_cloud.config_flow import (
CannotConnect,
InvalidAuth,
SwitchBotAuthenticationError,
SwitchBotConnectionError,
)
from homeassistant.components.switchbot_cloud.const import DOMAIN, ENTRY_TITLE
from homeassistant.const import CONF_API_KEY, CONF_API_TOKEN
@ -57,8 +57,8 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
@pytest.mark.parametrize(
("error", "message"),
[
(InvalidAuth, "invalid_auth"),
(CannotConnect, "cannot_connect"),
(SwitchBotAuthenticationError, "invalid_auth"),
(SwitchBotConnectionError, "cannot_connect"),
(Exception, "unknown"),
],
)

View File

@ -3,7 +3,13 @@
from unittest.mock import patch
import pytest
from switchbot_api import CannotConnect, Device, InvalidAuth, PowerState, Remote
from switchbot_api import (
Device,
PowerState,
Remote,
SwitchBotAuthenticationError,
SwitchBotConnectionError,
)
from homeassistant.components.switchbot_cloud import SwitchBotAPI
from homeassistant.config_entries import ConfigEntryState
@ -127,8 +133,8 @@ async def test_setup_entry_success(
@pytest.mark.parametrize(
("error", "state"),
[
(InvalidAuth, ConfigEntryState.SETUP_ERROR),
(CannotConnect, ConfigEntryState.SETUP_RETRY),
(SwitchBotAuthenticationError, ConfigEntryState.SETUP_ERROR),
(SwitchBotConnectionError, ConfigEntryState.SETUP_RETRY),
],
)
async def test_setup_entry_fails_when_listing_devices(
@ -162,7 +168,7 @@ async def test_setup_entry_fails_when_refreshing(
hubDeviceId="test-hub-id",
)
]
mock_get_status.side_effect = CannotConnect
mock_get_status.side_effect = SwitchBotConnectionError
entry = await configure_integration(hass)
assert entry.state is ConfigEntryState.SETUP_RETRY