diff --git a/homeassistant/components/bring/__init__.py b/homeassistant/components/bring/__init__.py index e9501fc64b3d5d..aec3cd53c945e4 100644 --- a/homeassistant/components/bring/__init__.py +++ b/homeassistant/components/bring/__init__.py @@ -14,6 +14,7 @@ from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryError, ConfigEntryNotReady +from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN from .coordinator import BringDataUpdateCoordinator @@ -29,14 +30,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: email = entry.data[CONF_EMAIL] password = entry.data[CONF_PASSWORD] - bring = Bring(email, password) - - def login_and_load_lists() -> None: - bring.login() - bring.loadLists() + session = async_get_clientsession(hass) + bring = Bring(email, password, sessionAsync=session) try: - await hass.async_add_executor_job(login_and_load_lists) + await bring.loginAsync() + await bring.loadListsAsync() except BringRequestException as e: raise ConfigEntryNotReady( f"Timeout while connecting for email '{email}'" diff --git a/homeassistant/components/bring/config_flow.py b/homeassistant/components/bring/config_flow.py index 21774117ff6467..122e71feea61b3 100644 --- a/homeassistant/components/bring/config_flow.py +++ b/homeassistant/components/bring/config_flow.py @@ -11,6 +11,7 @@ from homeassistant import config_entries from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.data_entry_flow import FlowResult +from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.selector import ( TextSelector, TextSelectorConfig, @@ -48,14 +49,14 @@ async def async_step_user( """Handle the initial step.""" errors: dict[str, str] = {} if user_input is not None: - bring = Bring(user_input[CONF_EMAIL], user_input[CONF_PASSWORD]) - - def login_and_load_lists() -> None: - bring.login() - bring.loadLists() + session = async_get_clientsession(self.hass) + bring = Bring( + user_input[CONF_EMAIL], user_input[CONF_PASSWORD], sessionAsync=session + ) try: - await self.hass.async_add_executor_job(login_and_load_lists) + await bring.loginAsync() + await bring.loadListsAsync() except BringRequestException: errors["base"] = "cannot_connect" except BringAuthException: diff --git a/homeassistant/components/bring/coordinator.py b/homeassistant/components/bring/coordinator.py index a7bd4a35f43d68..eb28f24e085725 100644 --- a/homeassistant/components/bring/coordinator.py +++ b/homeassistant/components/bring/coordinator.py @@ -40,9 +40,7 @@ def __init__(self, hass: HomeAssistant, bring: Bring) -> None: async def _async_update_data(self) -> dict[str, BringData]: try: - lists_response = await self.hass.async_add_executor_job( - self.bring.loadLists - ) + lists_response = await self.bring.loadListsAsync() except BringRequestException as e: raise UpdateFailed("Unable to connect and retrieve data from bring") from e except BringParseException as e: @@ -51,9 +49,7 @@ async def _async_update_data(self) -> dict[str, BringData]: list_dict = {} for lst in lists_response["lists"]: try: - items = await self.hass.async_add_executor_job( - self.bring.getItems, lst["listUuid"] - ) + items = await self.bring.getItemsAsync(lst["listUuid"]) except BringRequestException as e: raise UpdateFailed( "Unable to connect and retrieve data from bring" diff --git a/homeassistant/components/bring/manifest.json b/homeassistant/components/bring/manifest.json index bc249ecea98081..e7d23bfc3df6f9 100644 --- a/homeassistant/components/bring/manifest.json +++ b/homeassistant/components/bring/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/bring", "integration_type": "service", "iot_class": "cloud_polling", - "requirements": ["python-bring-api==2.0.0"] + "requirements": ["python-bring-api==3.0.0"] } diff --git a/homeassistant/components/bring/todo.py b/homeassistant/components/bring/todo.py index bd87a2d18de1f5..14279c894af733 100644 --- a/homeassistant/components/bring/todo.py +++ b/homeassistant/components/bring/todo.py @@ -91,11 +91,8 @@ def bring_list(self) -> BringData: async def async_create_todo_item(self, item: TodoItem) -> None: """Add an item to the To-do list.""" try: - await self.hass.async_add_executor_job( - self.coordinator.bring.saveItem, - self.bring_list["listUuid"], - item.summary, - item.description or "", + await self.coordinator.bring.saveItemAsync( + self.bring_list["listUuid"], item.summary, item.description or "" ) except BringRequestException as e: raise HomeAssistantError("Unable to save todo item for bring") from e @@ -126,16 +123,14 @@ async def async_update_todo_item(self, item: TodoItem) -> None: assert item.uid if item.status == TodoItemStatus.COMPLETED: - await self.hass.async_add_executor_job( - self.coordinator.bring.removeItem, + await self.coordinator.bring.removeItemAsync( bring_list["listUuid"], item.uid, ) elif item.summary == item.uid: try: - await self.hass.async_add_executor_job( - self.coordinator.bring.updateItem, + await self.coordinator.bring.updateItemAsync( bring_list["listUuid"], item.uid, item.description or "", @@ -144,13 +139,11 @@ async def async_update_todo_item(self, item: TodoItem) -> None: raise HomeAssistantError("Unable to update todo item for bring") from e else: try: - await self.hass.async_add_executor_job( - self.coordinator.bring.removeItem, + await self.coordinator.bring.removeItemAsync( bring_list["listUuid"], item.uid, ) - await self.hass.async_add_executor_job( - self.coordinator.bring.saveItem, + await self.coordinator.bring.saveItemAsync( bring_list["listUuid"], item.summary, item.description or "", @@ -164,8 +157,8 @@ async def async_delete_todo_items(self, uids: list[str]) -> None: """Delete an item from the To-do list.""" for uid in uids: try: - await self.hass.async_add_executor_job( - self.coordinator.bring.removeItem, self.bring_list["listUuid"], uid + await self.coordinator.bring.removeItemAsync( + self.bring_list["listUuid"], uid ) except BringRequestException as e: raise HomeAssistantError("Unable to delete todo item for bring") from e diff --git a/requirements_all.txt b/requirements_all.txt index fad05d0a6d7c3c..36e916f9fba00c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2181,7 +2181,7 @@ python-awair==0.2.4 python-blockchain-api==0.0.2 # homeassistant.components.bring -python-bring-api==2.0.0 +python-bring-api==3.0.0 # homeassistant.components.bsblan python-bsblan==0.5.18 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7d36ebdf88a94e..401e47232dcc07 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1684,7 +1684,7 @@ python-MotionMount==0.3.1 python-awair==0.2.4 # homeassistant.components.bring -python-bring-api==2.0.0 +python-bring-api==3.0.0 # homeassistant.components.bsblan python-bsblan==0.5.18 diff --git a/tests/components/bring/conftest.py b/tests/components/bring/conftest.py index f8749d3dea9ba4..81a76c9ee3e9ef 100644 --- a/tests/components/bring/conftest.py +++ b/tests/components/bring/conftest.py @@ -1,6 +1,6 @@ """Common fixtures for the Bring! tests.""" from collections.abc import Generator -from unittest.mock import Mock, patch +from unittest.mock import AsyncMock, patch import pytest @@ -16,7 +16,7 @@ @pytest.fixture -def mock_setup_entry() -> Generator[Mock, None, None]: +def mock_setup_entry() -> Generator[AsyncMock, None, None]: """Override async_setup_entry.""" with patch( "homeassistant.components.bring.async_setup_entry", return_value=True @@ -25,7 +25,7 @@ def mock_setup_entry() -> Generator[Mock, None, None]: @pytest.fixture -def mock_bring_client() -> Generator[Mock, None, None]: +def mock_bring_client() -> Generator[AsyncMock, None, None]: """Mock a Bring client.""" with patch( "homeassistant.components.bring.Bring", @@ -36,8 +36,8 @@ def mock_bring_client() -> Generator[Mock, None, None]: ): client = mock_client.return_value client.uuid = UUID - client.login.return_value = True - client.loadLists.return_value = {"lists": []} + client.loginAsync.return_value = True + client.loadListsAsync.return_value = {"lists": []} yield client diff --git a/tests/components/bring/test_config_flow.py b/tests/components/bring/test_config_flow.py index 063d84a0e9754c..531554d584e839 100644 --- a/tests/components/bring/test_config_flow.py +++ b/tests/components/bring/test_config_flow.py @@ -1,5 +1,5 @@ """Test the Bring! config flow.""" -from unittest.mock import AsyncMock, Mock +from unittest.mock import AsyncMock import pytest from python_bring_api.exceptions import ( @@ -25,7 +25,7 @@ async def test_form( - hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bring_client: Mock + hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_bring_client: AsyncMock ) -> None: """Test we get the form.""" result = await hass.config_entries.flow.async_init( @@ -59,10 +59,10 @@ async def test_form( ], ) async def test_flow_user_init_data_unknown_error_and_recover( - hass: HomeAssistant, mock_bring_client: Mock, raise_error, text_error + hass: HomeAssistant, mock_bring_client: AsyncMock, raise_error, text_error ) -> None: """Test unknown errors.""" - mock_bring_client.login.side_effect = raise_error + mock_bring_client.loginAsync.side_effect = raise_error result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "user"} @@ -76,7 +76,7 @@ async def test_flow_user_init_data_unknown_error_and_recover( assert result["errors"]["base"] == text_error # Recover - mock_bring_client.login.side_effect = None + mock_bring_client.loginAsync.side_effect = None result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "user"} ) @@ -92,7 +92,9 @@ async def test_flow_user_init_data_unknown_error_and_recover( async def test_flow_user_init_data_already_configured( - hass: HomeAssistant, mock_bring_client: Mock, bring_config_entry: MockConfigEntry + hass: HomeAssistant, + mock_bring_client: AsyncMock, + bring_config_entry: MockConfigEntry, ) -> None: """Test we abort user data set when entry is already configured.""" diff --git a/tests/components/bring/test_init.py b/tests/components/bring/test_init.py index 3c605143ba0b6a..59628fa59b7478 100644 --- a/tests/components/bring/test_init.py +++ b/tests/components/bring/test_init.py @@ -1,5 +1,5 @@ """Unit tests for the bring integration.""" -from unittest.mock import Mock +from unittest.mock import AsyncMock import pytest @@ -27,7 +27,7 @@ async def setup_integration( async def test_load_unload( hass: HomeAssistant, - mock_bring_client: Mock, + mock_bring_client: AsyncMock, bring_config_entry: MockConfigEntry, ) -> None: """Test loading and unloading of the config entry.""" @@ -52,12 +52,12 @@ async def test_load_unload( ) async def test_init_failure( hass: HomeAssistant, - mock_bring_client: Mock, + mock_bring_client: AsyncMock, status: ConfigEntryState, exception: Exception, bring_config_entry: MockConfigEntry | None, ) -> None: """Test an initialization error on integration load.""" - mock_bring_client.login.side_effect = exception + mock_bring_client.loginAsync.side_effect = exception await setup_integration(hass, bring_config_entry) assert bring_config_entry.state == status