generated from ludeeus/integration_blueprint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from shadow578/refactor/entity_id_normalization
refactor sensor entity id creation
- Loading branch information
Showing
4 changed files
with
170 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""tvxml_epg helper functions.""" | ||
|
||
|
||
def normalize_for_entity_id(s: str) -> str: | ||
"""Normalize a string for usage in an entity_id. | ||
Example: | ||
- s = 'DE: WDR (Münster)' | ||
=> "de_wdr_muenster" | ||
:param s: The string to normalize. | ||
:return: The normalized string. | ||
""" | ||
|
||
# lower case | ||
s = s.lower() | ||
|
||
# special replacement rules | ||
replacements = { | ||
# replace umlauts with their respective digraphs | ||
"ä": "ae", | ||
"ö": "oe", | ||
"ü": "ue", | ||
"ß": "ss", | ||
# '+' is replaced with ' plus '. | ||
"+": " plus ", | ||
} | ||
for umlaut, replacement in replacements.items(): | ||
s = s.replace(umlaut, replacement) | ||
|
||
# replace "delimiting characters" and spaces with underscores | ||
for c in " -.:": | ||
s = s.replace(c, "_") | ||
|
||
# remove all non-alphanumeric characters except underscores | ||
s = "".join(c if c.isalnum() or c == "_" else "" for c in s) | ||
|
||
# trim underscores from start and end | ||
s = s.strip("_") | ||
|
||
# remove all occurrences of multiple underscores | ||
while "__" in s: | ||
s = s.replace("__", "_") | ||
|
||
return s |
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,30 @@ | ||
"""Test cases for the helper module.""" | ||
|
||
from custom_components.xmltv_epg.helper import normalize_for_entity_id | ||
|
||
|
||
def test_normalize_for_entity_id(): | ||
"""Test the normalize_for_entity_id function.""" | ||
|
||
assert normalize_for_entity_id("WDR") == "wdr" | ||
assert normalize_for_entity_id("DE: My Channel 1") == "de_my_channel_1" | ||
assert normalize_for_entity_id("DE: WDR (Münster)") == "de_wdr_muenster" | ||
|
||
# test umlauts | ||
assert normalize_for_entity_id("ÄÖÜß") == "aeoeuess" | ||
|
||
# test special characters | ||
assert ( | ||
normalize_for_entity_id('Special Characters: !"§$%&/()=?') | ||
== "special_characters" | ||
) | ||
|
||
# test leading and trailing spaces | ||
assert ( | ||
normalize_for_entity_id(" Leading and Trailing Spaces ") | ||
== "leading_and_trailing_spaces" | ||
) | ||
|
||
# test special replacement rules | ||
# required because both "Sport1" and "Sport1+" are channels that exists in the wild... | ||
assert normalize_for_entity_id("A+B-C") == "a_plus_b_c" |
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