Add unit translations for github integration (#130538)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Abílio Costa 2024-11-20 17:43:17 +00:00 committed by GitHub
parent 3542bca13d
commit 4776865584
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 58 additions and 19 deletions

View File

@ -37,7 +37,6 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
GitHubSensorEntityDescription( GitHubSensorEntityDescription(
key="discussions_count", key="discussions_count",
translation_key="discussions_count", translation_key="discussions_count",
native_unit_of_measurement="Discussions",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["discussion"]["total"], value_fn=lambda data: data["discussion"]["total"],
@ -45,7 +44,6 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
GitHubSensorEntityDescription( GitHubSensorEntityDescription(
key="stargazers_count", key="stargazers_count",
translation_key="stargazers_count", translation_key="stargazers_count",
native_unit_of_measurement="Stars",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["stargazers_count"], value_fn=lambda data: data["stargazers_count"],
@ -53,7 +51,6 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
GitHubSensorEntityDescription( GitHubSensorEntityDescription(
key="subscribers_count", key="subscribers_count",
translation_key="subscribers_count", translation_key="subscribers_count",
native_unit_of_measurement="Watchers",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["watchers"]["total"], value_fn=lambda data: data["watchers"]["total"],
@ -61,7 +58,6 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
GitHubSensorEntityDescription( GitHubSensorEntityDescription(
key="forks_count", key="forks_count",
translation_key="forks_count", translation_key="forks_count",
native_unit_of_measurement="Forks",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["forks_count"], value_fn=lambda data: data["forks_count"],
@ -69,7 +65,6 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
GitHubSensorEntityDescription( GitHubSensorEntityDescription(
key="issues_count", key="issues_count",
translation_key="issues_count", translation_key="issues_count",
native_unit_of_measurement="Issues",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["issue"]["total"], value_fn=lambda data: data["issue"]["total"],
@ -77,7 +72,6 @@ SENSOR_DESCRIPTIONS: tuple[GitHubSensorEntityDescription, ...] = (
GitHubSensorEntityDescription( GitHubSensorEntityDescription(
key="pulls_count", key="pulls_count",
translation_key="pulls_count", translation_key="pulls_count",
native_unit_of_measurement="Pull Requests",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda data: data["pull_request"]["total"], value_fn=lambda data: data["pull_request"]["total"],

View File

@ -19,22 +19,28 @@
"entity": { "entity": {
"sensor": { "sensor": {
"discussions_count": { "discussions_count": {
"name": "Discussions" "name": "Discussions",
"unit_of_measurement": "discussions"
}, },
"stargazers_count": { "stargazers_count": {
"name": "Stars" "name": "Stars",
"unit_of_measurement": "stars"
}, },
"subscribers_count": { "subscribers_count": {
"name": "Watchers" "name": "Watchers",
"unit_of_measurement": "watchers"
}, },
"forks_count": { "forks_count": {
"name": "Forks" "name": "Forks",
"unit_of_measurement": "forks"
}, },
"issues_count": { "issues_count": {
"name": "Issues" "name": "Issues",
"unit_of_measurement": "issues"
}, },
"pulls_count": { "pulls_count": {
"name": "Pull requests" "name": "Pull requests",
"unit_of_measurement": "pull requests"
}, },
"latest_commit": { "latest_commit": {
"name": "Latest commit" "name": "Latest commit"

View File

@ -368,6 +368,9 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema:
}, },
slug_validator=translation_key_validator, slug_validator=translation_key_validator,
), ),
vol.Optional(
"unit_of_measurement"
): translation_value_validator,
}, },
slug_validator=translation_key_validator, slug_validator=translation_key_validator,
), ),

View File

@ -64,10 +64,16 @@ def test_load_translations_files_by_language(
"test": { "test": {
"entity": { "entity": {
"switch": { "switch": {
"other1": {"name": "Other 1"}, "other1": {
"name": "Other 1",
"unit_of_measurement": "units",
},
"other2": {"name": "Other 2"}, "other2": {"name": "Other 2"},
"other3": {"name": "Other 3"}, "other3": {"name": "Other 3"},
"other4": {"name": "Other 4"}, "other4": {
"name": "Other 4",
"unit_of_measurement": "quantities",
},
"outlet": {"name": "Outlet " "{placeholder}"}, "outlet": {"name": "Outlet " "{placeholder}"},
} }
}, },
@ -87,9 +93,11 @@ def test_load_translations_files_by_language(
"en", "en",
{ {
"component.test.entity.switch.other1.name": "Other 1", "component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Other 2", "component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3", "component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4", "component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}", "component.test.entity.switch.outlet.name": "Outlet {placeholder}",
}, },
[], [],
@ -98,9 +106,11 @@ def test_load_translations_files_by_language(
"es", "es",
{ {
"component.test.entity.switch.other1.name": "Otra 1", "component.test.entity.switch.other1.name": "Otra 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Otra 2", "component.test.entity.switch.other2.name": "Otra 2",
"component.test.entity.switch.other3.name": "Otra 3", "component.test.entity.switch.other3.name": "Otra 3",
"component.test.entity.switch.other4.name": "Otra 4", "component.test.entity.switch.other4.name": "Otra 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Enchufe {placeholder}", "component.test.entity.switch.outlet.name": "Enchufe {placeholder}",
}, },
[], [],
@ -110,12 +120,14 @@ def test_load_translations_files_by_language(
{ {
# Correct # Correct
"component.test.entity.switch.other1.name": "Anderes 1", "component.test.entity.switch.other1.name": "Anderes 1",
"component.test.entity.switch.other1.unit_of_measurement": "einheiten",
# Translation has placeholder missing in English # Translation has placeholder missing in English
"component.test.entity.switch.other2.name": "Other 2", "component.test.entity.switch.other2.name": "Other 2",
# Correct (empty translation) # Correct (empty translation)
"component.test.entity.switch.other3.name": "", "component.test.entity.switch.other3.name": "",
# Translation missing # Translation missing
"component.test.entity.switch.other4.name": "Other 4", "component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
# Mismatch in placeholders # Mismatch in placeholders
"component.test.entity.switch.outlet.name": "Outlet {placeholder}", "component.test.entity.switch.outlet.name": "Outlet {placeholder}",
}, },
@ -166,9 +178,11 @@ async def test_get_translations(hass: HomeAssistant, mock_config_flows) -> None:
assert translations == { assert translations == {
"component.test.entity.switch.other1.name": "Other 1", "component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Other 2", "component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3", "component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4", "component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}", "component.test.entity.switch.outlet.name": "Outlet {placeholder}",
} }
@ -176,24 +190,33 @@ async def test_get_translations(hass: HomeAssistant, mock_config_flows) -> None:
hass, "de", "entity", {"test"} hass, "de", "entity", {"test"}
) )
# Test a partial translation
assert translations == { assert translations == {
# Correct
"component.test.entity.switch.other1.name": "Anderes 1", "component.test.entity.switch.other1.name": "Anderes 1",
"component.test.entity.switch.other1.unit_of_measurement": "einheiten",
# Translation has placeholder missing in English
"component.test.entity.switch.other2.name": "Other 2", "component.test.entity.switch.other2.name": "Other 2",
# Correct (empty translation)
"component.test.entity.switch.other3.name": "", "component.test.entity.switch.other3.name": "",
# Translation missing
"component.test.entity.switch.other4.name": "Other 4", "component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
# Mismatch in placeholders
"component.test.entity.switch.outlet.name": "Outlet {placeholder}", "component.test.entity.switch.outlet.name": "Outlet {placeholder}",
} }
# Test a partial translation
translations = await translation.async_get_translations( translations = await translation.async_get_translations(
hass, "es", "entity", {"test"} hass, "es", "entity", {"test"}
) )
assert translations == { assert translations == {
"component.test.entity.switch.other1.name": "Otra 1", "component.test.entity.switch.other1.name": "Otra 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Otra 2", "component.test.entity.switch.other2.name": "Otra 2",
"component.test.entity.switch.other3.name": "Otra 3", "component.test.entity.switch.other3.name": "Otra 3",
"component.test.entity.switch.other4.name": "Otra 4", "component.test.entity.switch.other4.name": "Otra 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Enchufe {placeholder}", "component.test.entity.switch.outlet.name": "Enchufe {placeholder}",
} }
@ -204,9 +227,11 @@ async def test_get_translations(hass: HomeAssistant, mock_config_flows) -> None:
assert translations == { assert translations == {
"component.test.entity.switch.other1.name": "Other 1", "component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Other 2", "component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3", "component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4", "component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}", "component.test.entity.switch.outlet.name": "Outlet {placeholder}",
} }
@ -507,9 +532,11 @@ async def test_get_cached_translations(hass: HomeAssistant, mock_config_flows) -
) )
assert translations == { assert translations == {
"component.test.entity.switch.other1.name": "Other 1", "component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Other 2", "component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3", "component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4", "component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}", "component.test.entity.switch.outlet.name": "Outlet {placeholder}",
} }
@ -522,9 +549,11 @@ async def test_get_cached_translations(hass: HomeAssistant, mock_config_flows) -
assert translations == { assert translations == {
"component.test.entity.switch.other1.name": "Otra 1", "component.test.entity.switch.other1.name": "Otra 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Otra 2", "component.test.entity.switch.other2.name": "Otra 2",
"component.test.entity.switch.other3.name": "Otra 3", "component.test.entity.switch.other3.name": "Otra 3",
"component.test.entity.switch.other4.name": "Otra 4", "component.test.entity.switch.other4.name": "Otra 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Enchufe {placeholder}", "component.test.entity.switch.outlet.name": "Enchufe {placeholder}",
} }
@ -539,9 +568,11 @@ async def test_get_cached_translations(hass: HomeAssistant, mock_config_flows) -
assert translations == { assert translations == {
"component.test.entity.switch.other1.name": "Other 1", "component.test.entity.switch.other1.name": "Other 1",
"component.test.entity.switch.other1.unit_of_measurement": "units",
"component.test.entity.switch.other2.name": "Other 2", "component.test.entity.switch.other2.name": "Other 2",
"component.test.entity.switch.other3.name": "Other 3", "component.test.entity.switch.other3.name": "Other 3",
"component.test.entity.switch.other4.name": "Other 4", "component.test.entity.switch.other4.name": "Other 4",
"component.test.entity.switch.other4.unit_of_measurement": "quantities",
"component.test.entity.switch.outlet.name": "Outlet {placeholder}", "component.test.entity.switch.outlet.name": "Outlet {placeholder}",
} }
@ -678,7 +709,6 @@ async def test_get_translations_still_has_title_without_translations_files(
) )
assert translations == translations_again assert translations == translations_again
assert translations == { assert translations == {
"component.component1.title": "Component 1", "component.component1.title": "Component 1",
} }

View File

@ -1,7 +1,10 @@
{ {
"entity": { "entity": {
"switch": { "switch": {
"other1": { "name": "Anderes 1" }, "other1": {
"name": "Anderes 1",
"unit_of_measurement": "einheiten"
},
"other2": { "name": "Anderes 2 {placeholder}" }, "other2": { "name": "Anderes 2 {placeholder}" },
"other3": { "name": "" }, "other3": { "name": "" },
"outlet": { "name": "Steckdose {something}" } "outlet": { "name": "Steckdose {something}" }

View File

@ -1,10 +1,13 @@
{ {
"entity": { "entity": {
"switch": { "switch": {
"other1": { "name": "Other 1" }, "other1": { "name": "Other 1", "unit_of_measurement": "units" },
"other2": { "name": "Other 2" }, "other2": { "name": "Other 2" },
"other3": { "name": "Other 3" }, "other3": { "name": "Other 3" },
"other4": { "name": "Other 4" }, "other4": {
"name": "Other 4",
"unit_of_measurement": "quantities"
},
"outlet": { "name": "Outlet {placeholder}" } "outlet": { "name": "Outlet {placeholder}" }
} }
}, },