update code so it also works in DEBUG=false

This commit is contained in:
Mikhail Epifanov
2024-09-17 22:25:08 +02:00
parent 8c8834e6aa
commit e6eacc48d6
3 changed files with 22 additions and 14 deletions

View File

@@ -31,6 +31,15 @@ class Work:
actionType: ActionType
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
# The way ConnectionManager works is as follows:
# 1. On init, it starts a worker & creates a queue for 'Work'
# 2. Then any time its called, it verifies the type of action (create/update/delete) and if the item is of interest, pushes the Work (non-blocking) to the queue.
@@ -39,7 +48,8 @@ class Work:
# 3.2 If work is of type REGISTERED_CLASSES, it asynchronously fires of all connectors and wait for them to finish (runtime should depend on the 'slowest' connector)
# 4. Work is marked as consumed, and next entry of the queue is consumed.
# Each 'Work' is processed in sequential by the worker, so the throughput is about [workers * the slowest connector]
class ConnectorManager:
# The Singleton class is used for ConnectorManager to have a self-reference and so Python does not garbage collect it
class ConnectorManager(metaclass=Singleton):
_logger: Logger
_queue: queue.Queue
_listening_to_classes = REGISTERED_CLASSES | ConnectorConfig

View File

@@ -13,6 +13,8 @@ class HomeAssistant(Connector):
_config: ConnectorConfig
_logger: Logger
_required_foreign_keys = ("food", "unit", "created_by")
def __init__(self, config: ConnectorConfig):
if not config.token or not config.url or not config.todo_entity:
raise ValueError("config for HomeAssistantConnector in incomplete")
@@ -38,7 +40,7 @@ class HomeAssistant(Connector):
item, description = _format_shopping_list_entry(shopping_list_entry)
self._logger.debug(f"adding {item=}")
self._logger.debug(f"adding {item=} with {description=} to {self._config.todo_entity}")
data = {
"entity_id": self._config.todo_entity,
@@ -60,14 +62,14 @@ class HomeAssistant(Connector):
if not self._config.on_shopping_list_entry_deleted_enabled:
return
if not hasattr(shopping_list_entry._state.fields_cache, "food"):
if not all(k in shopping_list_entry._state.fields_cache for k in self._required_foreign_keys):
# Sometimes the food foreign key is not loaded, and we cant load it from an async process
self._logger.debug("required property was not present in ShoppingListEntry")
return
item, _ = _format_shopping_list_entry(shopping_list_entry)
self._logger.debug(f"removing {item=}")
self._logger.debug(f"removing {item=} from {self._config.todo_entity}")
data = {
"entity_id": self._config.todo_entity,