Coverage for custom_components/appliance_autonotify/words.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-28 12:19 +0000

1"""Words in a name that say what an appliance is, in English and the language of Home Assistant, from the translations""" 

2 

3from __future__ import annotations 

4 

5from homeassistant.core import HomeAssistant, callback 

6from homeassistant.helpers.translation import async_get_translations 

7 

8from .const import DOMAIN 

9 

10# kept in the translations as the options of a selector that's never shown, the one category with free text, so 

11# whoever translates the integration adds the words for their language, each a comma separated list by type 

12WORDS_SELECTOR = "appliance_words" 

13WORDS_PREFIX = f"component.{DOMAIN}.selector.{WORDS_SELECTOR}.options." 

14DATA_WORDS = f"{DOMAIN}_words" 

15 

16 

17async def async_load_words(hass: HomeAssistant) -> None: 

18 words: dict[str, str] = {} 

19 # English as well, since a plug may well be named in English whatever the language 

20 for language in dict.fromkeys(("en", hass.config.language)): 

21 translations = await async_get_translations(hass, language, "selector", [DOMAIN]) 

22 for key, text in translations.items(): 

23 if key.startswith(WORDS_PREFIX): 

24 kind = key.removeprefix(WORDS_PREFIX) 

25 words.update({word.strip().lower(): kind for word in text.split(",") if word.strip()}) 

26 # longest first, so a dishwasher isn't taken for a washer, nor a microwave oven for an oven 

27 hass.data[DATA_WORDS] = dict(sorted(words.items(), key=lambda item: -len(item[0]))) 

28 

29 

30@callback 

31def name_type(hass: HomeAssistant, name: str) -> str | None: 

32 """The type of appliance a name says it is, such as `washer` for a washing machine plug, None if it says none""" 

33 lowered = name.lower() 

34 return next((kind for word, kind in hass.data.get(DATA_WORDS, {}).items() if word in lowered), None)