mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Add type hints to configurator request_config (#87287)
This commit is contained in:
parent
810367b757
commit
8c6eee7d38
@ -6,10 +6,13 @@ This will return a request id that has to be used for future calls.
|
|||||||
A callback has to be provided to `request_config` which will be called when
|
A callback has to be provided to `request_config` which will be called when
|
||||||
the user has submitted configuration information.
|
the user has submitted configuration information.
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import functools as ft
|
import functools as ft
|
||||||
from typing import Any
|
from typing import Any, cast
|
||||||
|
|
||||||
from homeassistant.const import ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME
|
from homeassistant.const import ATTR_ENTITY_PICTURE, ATTR_FRIENDLY_NAME
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback as async_callback
|
from homeassistant.core import HomeAssistant, ServiceCall, callback as async_callback
|
||||||
@ -40,29 +43,31 @@ SERVICE_CONFIGURE = "configure"
|
|||||||
STATE_CONFIGURE = "configure"
|
STATE_CONFIGURE = "configure"
|
||||||
STATE_CONFIGURED = "configured"
|
STATE_CONFIGURED = "configured"
|
||||||
|
|
||||||
|
ConfiguratorCallback = Callable[[list[dict[str, str]]], None]
|
||||||
|
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
@async_callback
|
@async_callback
|
||||||
def async_request_config(
|
def async_request_config(
|
||||||
hass,
|
hass: HomeAssistant,
|
||||||
name,
|
name: str,
|
||||||
callback=None,
|
callback: ConfiguratorCallback | None = None,
|
||||||
description=None,
|
description: str | None = None,
|
||||||
description_image=None,
|
description_image: str | None = None,
|
||||||
submit_caption=None,
|
submit_caption: str | None = None,
|
||||||
fields=None,
|
fields: list[dict[str, str]] | None = None,
|
||||||
link_name=None,
|
link_name: str | None = None,
|
||||||
link_url=None,
|
link_url: str | None = None,
|
||||||
entity_picture=None,
|
entity_picture: str | None = None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Create a new request for configuration.
|
"""Create a new request for configuration.
|
||||||
|
|
||||||
Will return an ID to be used for sequent calls.
|
Will return an ID to be used for sequent calls.
|
||||||
"""
|
"""
|
||||||
if link_name is not None and link_url is not None:
|
if description and link_name is not None and link_url is not None:
|
||||||
description += f"\n\n[{link_name}]({link_url})"
|
description += f"\n\n[{link_name}]({link_url})"
|
||||||
|
|
||||||
if description_image is not None:
|
if description and description_image is not None:
|
||||||
description += f"\n\n"
|
description += f"\n\n"
|
||||||
|
|
||||||
if (instance := hass.data.get(_KEY_INSTANCE)) is None:
|
if (instance := hass.data.get(_KEY_INSTANCE)) is None:
|
||||||
@ -135,14 +140,22 @@ class Configurator:
|
|||||||
"""Initialize the configurator."""
|
"""Initialize the configurator."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._cur_id = 0
|
self._cur_id = 0
|
||||||
self._requests = {}
|
self._requests: dict[
|
||||||
|
str, tuple[str, list[dict[str, str]], ConfiguratorCallback | None]
|
||||||
|
] = {}
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, SERVICE_CONFIGURE, self.async_handle_service_call
|
DOMAIN, SERVICE_CONFIGURE, self.async_handle_service_call
|
||||||
)
|
)
|
||||||
|
|
||||||
@async_callback
|
@async_callback
|
||||||
def async_request_config(
|
def async_request_config(
|
||||||
self, name, callback, description, submit_caption, fields, entity_picture
|
self,
|
||||||
|
name: str,
|
||||||
|
callback: ConfiguratorCallback | None,
|
||||||
|
description: str | None,
|
||||||
|
submit_caption: str | None,
|
||||||
|
fields: list[dict[str, str]] | None,
|
||||||
|
entity_picture: str | None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Set up a request for configuration."""
|
"""Set up a request for configuration."""
|
||||||
entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, hass=self.hass)
|
entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, hass=self.hass)
|
||||||
@ -219,7 +232,7 @@ class Configurator:
|
|||||||
if not self._validate_request_id(request_id):
|
if not self._validate_request_id(request_id):
|
||||||
return
|
return
|
||||||
|
|
||||||
_, _, callback = self._requests[request_id]
|
_, _, callback = self._requests[cast(str, request_id)]
|
||||||
|
|
||||||
# field validation goes here?
|
# field validation goes here?
|
||||||
if callback:
|
if callback:
|
||||||
|
@ -110,7 +110,7 @@ def _register_new_account(
|
|||||||
url, frob = api.authenticate_desktop()
|
url, frob = api.authenticate_desktop()
|
||||||
_LOGGER.debug("Sent authentication request to server")
|
_LOGGER.debug("Sent authentication request to server")
|
||||||
|
|
||||||
def register_account_callback(_):
|
def register_account_callback(fields: list[dict[str, str]]) -> None:
|
||||||
"""Call for register the configurator."""
|
"""Call for register the configurator."""
|
||||||
api.retrieve_token(frob)
|
api.retrieve_token(frob)
|
||||||
token = api.token
|
token = api.token
|
||||||
|
Loading…
x
Reference in New Issue
Block a user