Module homie.utils
Generic utilities.
Functions
publish_topics (mqtt, list, timeout) | Publishes a list of topics. |
slugify (name) | Turns a string into a valid Homie identifier. |
subscribe_topics (mqtt, list, unsub, timeout) | (un)subscribes to/from a list of topics. |
wait_for (check[, timeout=30[, init=0.010[, max=1]]]) | waits for a task to complete. |
wait_for_task (task[, timeout=30[, cancel]]) | schedules an async task using copas.addthread and waits for completion. |
Functions
- publish_topics (mqtt, list, timeout)
-
Publishes a list of topics. Will not return until done/failed.
Note: the defaults are;
retain = true
,qos = 1
, andcallback
can't be set (will always be overridden).Parameters:
- mqtt mqtt-device the mqtt device
- list
array
table payloads indexed by topics (payload can also be a table with
options see
mqtt_client:publish
) - timeout number timeout in seconds
Returns:
-
true or nil+err
- slugify (name)
-
Turns a string into a valid Homie identifier.
Parameters:
- name string a string, typically a human readable name
Returns:
-
string with Homie ID, or nil+err
- subscribe_topics (mqtt, list, unsub, timeout)
-
(un)subscribes to/from a list of topics. Will not return until completely done/failed.
Parameters:
- mqtt mqtt-device the mqtt device
- list array table with topics (topics can be in the key or value, as long as only one of them is a string)
- unsub bool subcribe or unsubscribe
- timeout number timeout in seconds
Returns:
-
true or nil+err
- wait_for (check[, timeout=30[, init=0.010[, max=1]]])
-
waits for a task to complete. Does exponential backoff while waiting.
Parameters:
- check function should return true on completion
- timeout number time out in seconds (default 30)
- init number wait in seconds (default 0.010)
- max number wait in seconds (each try doubles the wait until it hits this value) (default 1)
Returns:
-
true if finished, or nil+timeout
- wait_for_task (task[, timeout=30[, cancel]])
-
schedules an async task using
copas.addthread
and waits for completion. On a timeout the task will be removed from the Copas scheduler, and the cancel function will be called. The latter one is required when doing socket operations, you must close the socket in case it is in the receiving/sending queuesParameters:
- task function function with the task to execute
- timeout number timeout in seconds for the task (default 30)
- cancel function function to cancel the task (optional)