-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prepare-for-shutdown: publish config for home assistant to enable aut…
…omatic discovery
- Loading branch information
1 parent
59e043a
commit 56dc5ba
Showing
12 changed files
with
333 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+43.3 KB
docs/homeassistant/preparing-for-shutdown/settings/discovery/2020-06-21.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import re | ||
|
||
import systemctl_mqtt._utils | ||
|
||
NODE_ID_ALLOWED_CHARS = r"a-zA-Z0-9_-" | ||
|
||
|
||
def get_default_node_id() -> str: | ||
return re.sub( | ||
r"[^{}]".format(NODE_ID_ALLOWED_CHARS), | ||
"", | ||
# pylint: disable=protected-access | ||
systemctl_mqtt._utils.get_hostname(), | ||
) | ||
|
||
|
||
def validate_node_id(node_id: str) -> bool: | ||
return re.match(r"^[{}]+$".format(NODE_ID_ALLOWED_CHARS), node_id) is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import json | ||
|
||
|
||
def encode_bool(value: bool) -> str: | ||
return json.dumps(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import socket | ||
|
||
|
||
def get_hostname() -> str: | ||
return socket.gethostname() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import unittest.mock | ||
|
||
import pytest | ||
|
||
import systemctl_mqtt._homeassistant | ||
|
||
# pylint: disable=protected-access | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("hostname", "expected_node_id"), | ||
[ | ||
("raspberrypi", "raspberrypi"), | ||
("da-sh", "da-sh"), | ||
("under_score", "under_score"), | ||
("someone evil mocked the hostname", "someoneevilmockedthehostname"), | ||
], | ||
) | ||
def test_get_default_node_id(hostname, expected_node_id): | ||
with unittest.mock.patch( | ||
"systemctl_mqtt._utils.get_hostname", return_value=hostname | ||
): | ||
assert systemctl_mqtt._homeassistant.get_default_node_id() == expected_node_id | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("node_id", "valid"), | ||
[ | ||
("raspberrypi", True), | ||
("da-sh", True), | ||
("under_score", True), | ||
('" or ""="', False), | ||
("", False), | ||
], | ||
) | ||
def test_validate_node_id(node_id, valid): | ||
assert systemctl_mqtt._homeassistant.validate_node_id(node_id) == valid |
Oops, something went wrong.