Skip to content

Commit

Permalink
Bump python-bring-api to 3.0.0 (home-assistant#109720)
Browse files Browse the repository at this point in the history
  • Loading branch information
miaucl authored Feb 5, 2024
1 parent ed7307c commit 53d46ac
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 51 deletions.
11 changes: 5 additions & 6 deletions homeassistant/components/bring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}'"
Expand Down
13 changes: 7 additions & 6 deletions homeassistant/components/bring/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 2 additions & 6 deletions homeassistant/components/bring/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/bring/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
23 changes: 8 additions & 15 deletions homeassistant/components/bring/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "",
Expand All @@ -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 "",
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/components/bring/conftest.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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


Expand Down
14 changes: 8 additions & 6 deletions tests/components/bring/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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(
Expand Down Expand Up @@ -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"}
Expand All @@ -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"}
)
Expand All @@ -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."""

Expand Down
8 changes: 4 additions & 4 deletions tests/components/bring/test_init.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Unit tests for the bring integration."""
from unittest.mock import Mock
from unittest.mock import AsyncMock

import pytest

Expand Down Expand Up @@ -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."""
Expand All @@ -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

0 comments on commit 53d46ac

Please sign in to comment.