Skip to content

Commit

Permalink
combining the logic for inserting rows with and without authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
CM000n committed Dec 11, 2023
1 parent 0f13805 commit 6594d59
Showing 1 changed file with 4 additions and 30 deletions.
34 changes: 4 additions & 30 deletions custom_components/qss/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,9 @@
_LOGGER = logging.getLogger(__name__)


def _insert_row_with_auth(host: str, port: int, auth: tuple, event: Event) -> None:
with Sender(host, port, auth=auth, tls=True) as sender:
entity_id = event.data["entity_id"]
state = event.data.get("new_state")
attrs = dict(state.attributes)
sender.row(
"qss",
symbols={
"entity_id": entity_id,
},
columns={
"state": state.state,
"attributes": dumps(attrs, sort_keys=True, default=str),
},
at=event.time_fired,
)

sender.flush()


def _insert_row_without_auth(host: str, port: int, event: Event) -> None:
with Sender(host, port) as sender:
def _insert_row(host: str, port: int, auth: tuple, event: Event) -> None:
tls = bool(auth)
with Sender(host, port, auth=auth, tls=tls) as sender:
entity_id = event.data["entity_id"]
state = event.data.get("new_state")
attrs = dict(state.attributes)
Expand All @@ -52,20 +33,13 @@ def _insert_row_without_auth(host: str, port: int, event: Event) -> None:
sender.flush()


def _insert_row(host: str, port: int, auth: tuple, event: Event) -> None:
if all(auth):
_insert_row_with_auth(host, port, auth, event)
else:
_insert_row_without_auth(host, port, event)


@retry(
stop=stop_after_attempt(RETRY_ATTEMPTS),
wait=wait_fixed(RETRY_WAIT_SECONDS),
retry=retry_if_exception_type(IngressError),
)
def _retry_data_insertion(host: str, port: int, auth: tuple, event: Event) -> None:
"""Usign a retry for inserting event data into QuestDB."""
"""Use a retry for inserting event data into QuestDB."""
_insert_row(host, port, auth, event)


Expand Down

0 comments on commit 6594d59

Please sign in to comment.