Skip to content

Commit

Permalink
Add script to compare alexa locales with upstream (home-assistant#114247
Browse files Browse the repository at this point in the history
)

* Add script to compare alexa locales with upstream

* Use a function in script

* Add test base

* Complete output assertion

* Add type annotation

* Add note to docstring

* Update script call example

Co-authored-by: Jan Bouwhuis <[email protected]>

---------

Co-authored-by: Jan Bouwhuis <[email protected]>
  • Loading branch information
MartinHjelmare and jbouwh authored Apr 18, 2024
1 parent aee620b commit 47f0d5e
Show file tree
Hide file tree
Showing 6 changed files with 813 additions and 0 deletions.
4 changes: 4 additions & 0 deletions homeassistant/components/alexa/capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ class Alexa(AlexaCapability):
The API suggests you should explicitly include this interface.
https://developer.amazon.com/docs/device-apis/alexa-interface.html
To compare current supported locales in Home Assistant
with Alexa supported locales, run the following script:
python -m script.alexa_locales
"""

supported_locales = {
Expand Down
67 changes: 67 additions & 0 deletions script/alexa_locales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Check if upstream Alexa locales are subset of the core Alexa supported locales."""

from pprint import pprint
import re

from bs4 import BeautifulSoup
import requests

from homeassistant.components.alexa import capabilities

SITE = (
"https://developer.amazon.com/en-GB/docs/alexa/device-apis/list-of-interfaces.html"
)


def run_script() -> None:
"""Run the script."""
response = requests.get(SITE, timeout=10)
soup = BeautifulSoup(response.text, "html.parser")

table = soup.find("table")
table_body = table.find_all("tbody")[-1]
rows = table_body.find_all("tr")
data = [[ele.text.strip() for ele in row.find_all("td") if ele] for row in rows]
upstream_locales_raw = {row[0]: row[3] for row in data}
language_pattern = re.compile(r"^[a-z]{2}-[A-Z]{2}$")
upstream_locales = {
upstream_interface: {
name
for word in upstream_locale.split(" ")
if (name := word.strip(",")) and language_pattern.match(name) is not None
}
for upstream_interface, upstream_locale in upstream_locales_raw.items()
if upstream_interface.count(".") == 1 # Skip sub-interfaces
}

interfaces_missing = {}
interfaces_nok = {}
interfaces_ok = {}

for upstream_interface, upstream_locale in upstream_locales.items():
core_interface_name = upstream_interface.replace(".", "")
core_interface = getattr(capabilities, core_interface_name, None)

if core_interface is None:
interfaces_missing[upstream_interface] = upstream_locale
continue

core_locale = core_interface.supported_locales

if not upstream_locale.issubset(core_locale):
interfaces_nok[core_interface_name] = core_locale
else:
interfaces_ok[core_interface_name] = core_locale

print("Missing interfaces:")
pprint(list(interfaces_missing))
print("\n")
print("Interfaces where upstream locales are not subsets of the core locales:")
pprint(list(interfaces_nok))
print("\n")
print("Interfaces checked ok:")
pprint(list(interfaces_ok))


if __name__ == "__main__":
run_script()
Loading

0 comments on commit 47f0d5e

Please sign in to comment.