core/homeassistant/components/bring/util.py

41 lines
1008 B
Python

"""Utility functions for Bring."""
from __future__ import annotations
from bring_api import BringUserSettingsResponse
from .coordinator import BringData
def list_language(
list_uuid: str,
user_settings: BringUserSettingsResponse,
) -> str | None:
"""Get the lists language setting."""
try:
list_settings = next(
filter(
lambda x: x["listUuid"] == list_uuid,
user_settings["userlistsettings"],
)
)
return next(
filter(
lambda x: x["key"] == "listArticleLanguage",
list_settings["usersettings"],
)
)["value"]
except (StopIteration, KeyError):
return None
def sum_attributes(bring_list: BringData, attribute: str) -> int:
"""Count items with given attribute set."""
return sum(
item["attributes"][0]["content"][attribute]
for item in bring_list["purchase"]
if len(item.get("attributes", []))
)