change formatting a bit, and add async close method

This commit is contained in:
Mikhail Epifanov
2024-01-13 13:43:08 +01:00
parent c7dd61e239
commit 87ede4b9cc
3 changed files with 36 additions and 11 deletions

View File

@ -1,7 +1,5 @@
import logging
from collections import defaultdict
from logging import Logger
from typing import Dict, Any, Optional
from homeassistant_api import Client, HomeassistantAPIError, Domain
@ -58,16 +56,20 @@ class HomeAssistant(Connector):
except HomeassistantAPIError as err:
self._logger.warning(f"[HomeAssistant {self._config.name}] Received an exception from the api: {err=}, {type(err)=}")
async def close(self) -> None:
await self._client.async_cache_session.close()
def _format_shopping_list_entry(shopping_list_entry: ShoppingListEntry):
item = shopping_list_entry.food.name
if shopping_list_entry.amount > 0:
item += f" ({shopping_list_entry.amount:.2f}".rstrip('0').rstrip('.')
if shopping_list_entry.unit and shopping_list_entry.unit.base_unit and len(shopping_list_entry.unit.base_unit) > 0:
item += f" ({shopping_list_entry.amount} {shopping_list_entry.unit.base_unit})"
item += f" {shopping_list_entry.unit.base_unit})"
elif shopping_list_entry.unit and shopping_list_entry.unit.name and len(shopping_list_entry.unit.name) > 0:
item += f" ({shopping_list_entry.amount} {shopping_list_entry.unit.name})"
item += f" {shopping_list_entry.unit.name})"
else:
item += f" ({shopping_list_entry.amount})"
item += ")"
description = "Imported by TandoorRecipes"
if shopping_list_entry.created_by.first_name and len(shopping_list_entry.created_by.first_name) > 0: