1 """Util functions for OpenWeatherMap."""
5 from pyopenweathermap
import RequestError, create_owm_client
9 from .const
import DEFAULT_LANGUAGE, DEFAULT_OWM_MODE
11 OPTION_DEFAULTS = {CONF_LANGUAGE: DEFAULT_LANGUAGE, CONF_MODE: DEFAULT_OWM_MODE}
15 """Validate API key."""
17 errors, description_placeholders = {}, {}
19 owm_client = create_owm_client(api_key, mode)
20 api_key_valid = await owm_client.validate_key()
21 except RequestError
as error:
22 errors[
"base"] =
"cannot_connect"
23 description_placeholders[
"error"] =
str(error)
25 if api_key_valid
is False:
26 errors[
"base"] =
"invalid_api_key"
28 return errors, description_placeholders
32 combined_data: dict[str, Any],
33 ) -> tuple[dict[str, Any], dict[str, Any]]:
34 """Split combined data and options."""
35 data = {k: v
for k, v
in combined_data.items()
if k
not in OPTION_DEFAULTS}
37 option: combined_data.get(option, default)
38 for option, default
in OPTION_DEFAULTS.items()
40 return (data, options)
def validate_api_key(api_key, mode)
tuple[dict[str, Any], dict[str, Any]] build_data_and_options(dict[str, Any] combined_data)