Search is not available for this dataset
identifier
stringlengths 1
155
| parameters
stringlengths 2
6.09k
| docstring
stringlengths 11
63.4k
| docstring_summary
stringlengths 0
63.4k
| function
stringlengths 29
99.8k
| function_tokens
list | start_point
list | end_point
list | language
stringclasses 1
value | docstring_language
stringlengths 2
7
| docstring_language_predictions
stringlengths 18
23
| is_langid_reliable
stringclasses 2
values |
---|---|---|---|---|---|---|---|---|---|---|---|
ConfigFlow.async_step_pick_device | (self, user_input=None) | Handle the step to pick discovered device. | Handle the step to pick discovered device. | async def async_step_pick_device(self, user_input=None):
"""Handle the step to pick discovered device."""
if user_input is not None:
unique_id = user_input[CONF_DEVICE]
capabilities = self._discovered_devices[unique_id]
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=_async_unique_name(capabilities),
data={CONF_ID: unique_id},
)
configured_devices = {
entry.data[CONF_ID]
for entry in self._async_current_entries()
if entry.data[CONF_ID]
}
devices_name = {}
# Run 3 times as packets can get lost
for _ in range(3):
devices = await self.hass.async_add_executor_job(yeelight.discover_bulbs)
for device in devices:
capabilities = device["capabilities"]
unique_id = capabilities["id"]
if unique_id in configured_devices:
continue # ignore configured devices
model = capabilities["model"]
host = device["ip"]
name = f"{host} {model} {unique_id}"
self._discovered_devices[unique_id] = capabilities
devices_name[unique_id] = name
# Check if there is at least one device
if not devices_name:
return self.async_abort(reason="no_devices_found")
return self.async_show_form(
step_id="pick_device",
data_schema=vol.Schema({vol.Required(CONF_DEVICE): vol.In(devices_name)}),
) | [
"async",
"def",
"async_step_pick_device",
"(",
"self",
",",
"user_input",
"=",
"None",
")",
":",
"if",
"user_input",
"is",
"not",
"None",
":",
"unique_id",
"=",
"user_input",
"[",
"CONF_DEVICE",
"]",
"capabilities",
"=",
"self",
".",
"_discovered_devices",
"[",
"unique_id",
"]",
"await",
"self",
".",
"async_set_unique_id",
"(",
"unique_id",
")",
"self",
".",
"_abort_if_unique_id_configured",
"(",
")",
"return",
"self",
".",
"async_create_entry",
"(",
"title",
"=",
"_async_unique_name",
"(",
"capabilities",
")",
",",
"data",
"=",
"{",
"CONF_ID",
":",
"unique_id",
"}",
",",
")",
"configured_devices",
"=",
"{",
"entry",
".",
"data",
"[",
"CONF_ID",
"]",
"for",
"entry",
"in",
"self",
".",
"_async_current_entries",
"(",
")",
"if",
"entry",
".",
"data",
"[",
"CONF_ID",
"]",
"}",
"devices_name",
"=",
"{",
"}",
"# Run 3 times as packets can get lost",
"for",
"_",
"in",
"range",
"(",
"3",
")",
":",
"devices",
"=",
"await",
"self",
".",
"hass",
".",
"async_add_executor_job",
"(",
"yeelight",
".",
"discover_bulbs",
")",
"for",
"device",
"in",
"devices",
":",
"capabilities",
"=",
"device",
"[",
"\"capabilities\"",
"]",
"unique_id",
"=",
"capabilities",
"[",
"\"id\"",
"]",
"if",
"unique_id",
"in",
"configured_devices",
":",
"continue",
"# ignore configured devices",
"model",
"=",
"capabilities",
"[",
"\"model\"",
"]",
"host",
"=",
"device",
"[",
"\"ip\"",
"]",
"name",
"=",
"f\"{host} {model} {unique_id}\"",
"self",
".",
"_discovered_devices",
"[",
"unique_id",
"]",
"=",
"capabilities",
"devices_name",
"[",
"unique_id",
"]",
"=",
"name",
"# Check if there is at least one device",
"if",
"not",
"devices_name",
":",
"return",
"self",
".",
"async_abort",
"(",
"reason",
"=",
"\"no_devices_found\"",
")",
"return",
"self",
".",
"async_show_form",
"(",
"step_id",
"=",
"\"pick_device\"",
",",
"data_schema",
"=",
"vol",
".",
"Schema",
"(",
"{",
"vol",
".",
"Required",
"(",
"CONF_DEVICE",
")",
":",
"vol",
".",
"In",
"(",
"devices_name",
")",
"}",
")",
",",
")"
] | [
70,
4
] | [
108,
9
] | python | en | ['en', 'en', 'en'] | True |
ConfigFlow.async_step_import | (self, user_input=None) | Handle import step. | Handle import step. | async def async_step_import(self, user_input=None):
"""Handle import step."""
host = user_input[CONF_HOST]
try:
await self._async_try_connect(host)
except CannotConnect:
_LOGGER.error("Failed to import %s: cannot connect", host)
return self.async_abort(reason="cannot_connect")
except AlreadyConfigured:
return self.async_abort(reason="already_configured")
if CONF_NIGHTLIGHT_SWITCH_TYPE in user_input:
user_input[CONF_NIGHTLIGHT_SWITCH] = (
user_input.pop(CONF_NIGHTLIGHT_SWITCH_TYPE)
== NIGHTLIGHT_SWITCH_TYPE_LIGHT
)
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input) | [
"async",
"def",
"async_step_import",
"(",
"self",
",",
"user_input",
"=",
"None",
")",
":",
"host",
"=",
"user_input",
"[",
"CONF_HOST",
"]",
"try",
":",
"await",
"self",
".",
"_async_try_connect",
"(",
"host",
")",
"except",
"CannotConnect",
":",
"_LOGGER",
".",
"error",
"(",
"\"Failed to import %s: cannot connect\"",
",",
"host",
")",
"return",
"self",
".",
"async_abort",
"(",
"reason",
"=",
"\"cannot_connect\"",
")",
"except",
"AlreadyConfigured",
":",
"return",
"self",
".",
"async_abort",
"(",
"reason",
"=",
"\"already_configured\"",
")",
"if",
"CONF_NIGHTLIGHT_SWITCH_TYPE",
"in",
"user_input",
":",
"user_input",
"[",
"CONF_NIGHTLIGHT_SWITCH",
"]",
"=",
"(",
"user_input",
".",
"pop",
"(",
"CONF_NIGHTLIGHT_SWITCH_TYPE",
")",
"==",
"NIGHTLIGHT_SWITCH_TYPE_LIGHT",
")",
"return",
"self",
".",
"async_create_entry",
"(",
"title",
"=",
"user_input",
"[",
"CONF_NAME",
"]",
",",
"data",
"=",
"user_input",
")"
] | [
110,
4
] | [
125,
84
] | python | en | ['en', 'en', 'en'] | True |
ConfigFlow._async_try_connect | (self, host) | Set up with options. | Set up with options. | async def _async_try_connect(self, host):
"""Set up with options."""
for entry in self._async_current_entries():
if entry.data.get(CONF_HOST) == host:
raise AlreadyConfigured
bulb = yeelight.Bulb(host)
try:
capabilities = await self.hass.async_add_executor_job(bulb.get_capabilities)
if capabilities is None: # timeout
_LOGGER.debug("Failed to get capabilities from %s: timeout", host)
else:
_LOGGER.debug("Get capabilities: %s", capabilities)
await self.async_set_unique_id(capabilities["id"])
self._abort_if_unique_id_configured()
return
except OSError as err:
_LOGGER.debug("Failed to get capabilities from %s: %s", host, err)
# Ignore the error since get_capabilities uses UDP discovery packet
# which does not work in all network environments
# Fallback to get properties
try:
await self.hass.async_add_executor_job(bulb.get_properties)
except yeelight.BulbException as err:
_LOGGER.error("Failed to get properties from %s: %s", host, err)
raise CannotConnect from err
_LOGGER.debug("Get properties: %s", bulb.last_properties) | [
"async",
"def",
"_async_try_connect",
"(",
"self",
",",
"host",
")",
":",
"for",
"entry",
"in",
"self",
".",
"_async_current_entries",
"(",
")",
":",
"if",
"entry",
".",
"data",
".",
"get",
"(",
"CONF_HOST",
")",
"==",
"host",
":",
"raise",
"AlreadyConfigured",
"bulb",
"=",
"yeelight",
".",
"Bulb",
"(",
"host",
")",
"try",
":",
"capabilities",
"=",
"await",
"self",
".",
"hass",
".",
"async_add_executor_job",
"(",
"bulb",
".",
"get_capabilities",
")",
"if",
"capabilities",
"is",
"None",
":",
"# timeout",
"_LOGGER",
".",
"debug",
"(",
"\"Failed to get capabilities from %s: timeout\"",
",",
"host",
")",
"else",
":",
"_LOGGER",
".",
"debug",
"(",
"\"Get capabilities: %s\"",
",",
"capabilities",
")",
"await",
"self",
".",
"async_set_unique_id",
"(",
"capabilities",
"[",
"\"id\"",
"]",
")",
"self",
".",
"_abort_if_unique_id_configured",
"(",
")",
"return",
"except",
"OSError",
"as",
"err",
":",
"_LOGGER",
".",
"debug",
"(",
"\"Failed to get capabilities from %s: %s\"",
",",
"host",
",",
"err",
")",
"# Ignore the error since get_capabilities uses UDP discovery packet",
"# which does not work in all network environments",
"# Fallback to get properties",
"try",
":",
"await",
"self",
".",
"hass",
".",
"async_add_executor_job",
"(",
"bulb",
".",
"get_properties",
")",
"except",
"yeelight",
".",
"BulbException",
"as",
"err",
":",
"_LOGGER",
".",
"error",
"(",
"\"Failed to get properties from %s: %s\"",
",",
"host",
",",
"err",
")",
"raise",
"CannotConnect",
"from",
"err",
"_LOGGER",
".",
"debug",
"(",
"\"Get properties: %s\"",
",",
"bulb",
".",
"last_properties",
")"
] | [
127,
4
] | [
154,
65
] | python | en | ['en', 'en', 'en'] | True |
OptionsFlowHandler.__init__ | (self, config_entry) | Initialize the option flow. | Initialize the option flow. | def __init__(self, config_entry):
"""Initialize the option flow."""
self._config_entry = config_entry | [
"def",
"__init__",
"(",
"self",
",",
"config_entry",
")",
":",
"self",
".",
"_config_entry",
"=",
"config_entry"
] | [
160,
4
] | [
162,
41
] | python | en | ['en', 'en', 'en'] | True |
OptionsFlowHandler.async_step_init | (self, user_input=None) | Handle the initial step. | Handle the initial step. | async def async_step_init(self, user_input=None):
"""Handle the initial step."""
if user_input is not None:
options = {**self._config_entry.options}
options.update(user_input)
return self.async_create_entry(title="", data=options)
options = self._config_entry.options
return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Optional(CONF_MODEL, default=options[CONF_MODEL]): str,
vol.Required(
CONF_TRANSITION,
default=options[CONF_TRANSITION],
): cv.positive_int,
vol.Required(
CONF_MODE_MUSIC, default=options[CONF_MODE_MUSIC]
): bool,
vol.Required(
CONF_SAVE_ON_CHANGE,
default=options[CONF_SAVE_ON_CHANGE],
): bool,
vol.Required(
CONF_NIGHTLIGHT_SWITCH,
default=options[CONF_NIGHTLIGHT_SWITCH],
): bool,
}
),
) | [
"async",
"def",
"async_step_init",
"(",
"self",
",",
"user_input",
"=",
"None",
")",
":",
"if",
"user_input",
"is",
"not",
"None",
":",
"options",
"=",
"{",
"*",
"*",
"self",
".",
"_config_entry",
".",
"options",
"}",
"options",
".",
"update",
"(",
"user_input",
")",
"return",
"self",
".",
"async_create_entry",
"(",
"title",
"=",
"\"\"",
",",
"data",
"=",
"options",
")",
"options",
"=",
"self",
".",
"_config_entry",
".",
"options",
"return",
"self",
".",
"async_show_form",
"(",
"step_id",
"=",
"\"init\"",
",",
"data_schema",
"=",
"vol",
".",
"Schema",
"(",
"{",
"vol",
".",
"Optional",
"(",
"CONF_MODEL",
",",
"default",
"=",
"options",
"[",
"CONF_MODEL",
"]",
")",
":",
"str",
",",
"vol",
".",
"Required",
"(",
"CONF_TRANSITION",
",",
"default",
"=",
"options",
"[",
"CONF_TRANSITION",
"]",
",",
")",
":",
"cv",
".",
"positive_int",
",",
"vol",
".",
"Required",
"(",
"CONF_MODE_MUSIC",
",",
"default",
"=",
"options",
"[",
"CONF_MODE_MUSIC",
"]",
")",
":",
"bool",
",",
"vol",
".",
"Required",
"(",
"CONF_SAVE_ON_CHANGE",
",",
"default",
"=",
"options",
"[",
"CONF_SAVE_ON_CHANGE",
"]",
",",
")",
":",
"bool",
",",
"vol",
".",
"Required",
"(",
"CONF_NIGHTLIGHT_SWITCH",
",",
"default",
"=",
"options",
"[",
"CONF_NIGHTLIGHT_SWITCH",
"]",
",",
")",
":",
"bool",
",",
"}",
")",
",",
")"
] | [
164,
4
] | [
194,
9
] | python | en | ['en', 'en', 'en'] | True |
get_imei_from_config | (hass: core.HomeAssistant, data) | Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
| Validate the user input allows us to connect. | async def get_imei_from_config(hass: core.HomeAssistant, data):
"""Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
device = data[CONF_DEVICE]
config = {"Device": device, "Connection": "at"}
gateway = await create_sms_gateway(config, hass)
if not gateway:
raise CannotConnect
try:
imei = await gateway.get_imei_async()
except gammu.GSMError as err: # pylint: disable=no-member
raise CannotConnect from err
finally:
await gateway.terminate_async()
# Return info that you want to store in the config entry.
return imei | [
"async",
"def",
"get_imei_from_config",
"(",
"hass",
":",
"core",
".",
"HomeAssistant",
",",
"data",
")",
":",
"device",
"=",
"data",
"[",
"CONF_DEVICE",
"]",
"config",
"=",
"{",
"\"Device\"",
":",
"device",
",",
"\"Connection\"",
":",
"\"at\"",
"}",
"gateway",
"=",
"await",
"create_sms_gateway",
"(",
"config",
",",
"hass",
")",
"if",
"not",
"gateway",
":",
"raise",
"CannotConnect",
"try",
":",
"imei",
"=",
"await",
"gateway",
".",
"get_imei_async",
"(",
")",
"except",
"gammu",
".",
"GSMError",
"as",
"err",
":",
"# pylint: disable=no-member",
"raise",
"CannotConnect",
"from",
"err",
"finally",
":",
"await",
"gateway",
".",
"terminate_async",
"(",
")",
"# Return info that you want to store in the config entry.",
"return",
"imei"
] | [
17,
0
] | [
35,
15
] | python | en | ['en', 'en', 'en'] | True |
SMSFlowHandler.async_step_user | (self, user_input=None) | Handle the initial step. | Handle the initial step. | async def async_step_user(self, user_input=None):
"""Handle the initial step."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
errors = {}
if user_input is not None:
try:
imei = await get_imei_from_config(self.hass, user_input)
except CannotConnect:
errors["base"] = "cannot_connect"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
if not errors:
await self.async_set_unique_id(imei)
self._abort_if_unique_id_configured()
return self.async_create_entry(title=imei, data=user_input)
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
) | [
"async",
"def",
"async_step_user",
"(",
"self",
",",
"user_input",
"=",
"None",
")",
":",
"if",
"self",
".",
"_async_current_entries",
"(",
")",
":",
"return",
"self",
".",
"async_abort",
"(",
"reason",
"=",
"\"single_instance_allowed\"",
")",
"errors",
"=",
"{",
"}",
"if",
"user_input",
"is",
"not",
"None",
":",
"try",
":",
"imei",
"=",
"await",
"get_imei_from_config",
"(",
"self",
".",
"hass",
",",
"user_input",
")",
"except",
"CannotConnect",
":",
"errors",
"[",
"\"base\"",
"]",
"=",
"\"cannot_connect\"",
"except",
"Exception",
":",
"# pylint: disable=broad-except",
"_LOGGER",
".",
"exception",
"(",
"\"Unexpected exception\"",
")",
"errors",
"[",
"\"base\"",
"]",
"=",
"\"unknown\"",
"if",
"not",
"errors",
":",
"await",
"self",
".",
"async_set_unique_id",
"(",
"imei",
")",
"self",
".",
"_abort_if_unique_id_configured",
"(",
")",
"return",
"self",
".",
"async_create_entry",
"(",
"title",
"=",
"imei",
",",
"data",
"=",
"user_input",
")",
"return",
"self",
".",
"async_show_form",
"(",
"step_id",
"=",
"\"user\"",
",",
"data_schema",
"=",
"DATA_SCHEMA",
",",
"errors",
"=",
"errors",
")"
] | [
44,
4
] | [
65,
9
] | python | en | ['en', 'en', 'en'] | True |
SMSFlowHandler.async_step_import | (self, user_input) | Handle import. | Handle import. | async def async_step_import(self, user_input):
"""Handle import."""
return await self.async_step_user(user_input) | [
"async",
"def",
"async_step_import",
"(",
"self",
",",
"user_input",
")",
":",
"return",
"await",
"self",
".",
"async_step_user",
"(",
"user_input",
")"
] | [
67,
4
] | [
69,
53
] | python | en | ['en', 'ja', 'en'] | False |
async_register | (
hass: HomeAssistant, register: system_health.SystemHealthRegistration
) | Register system health callbacks. | Register system health callbacks. | def async_register(
hass: HomeAssistant, register: system_health.SystemHealthRegistration
) -> None:
"""Register system health callbacks."""
register.async_register_info(system_health_info, "/hassio") | [
"def",
"async_register",
"(",
"hass",
":",
"HomeAssistant",
",",
"register",
":",
"system_health",
".",
"SystemHealthRegistration",
")",
"->",
"None",
":",
"register",
".",
"async_register_info",
"(",
"system_health_info",
",",
"\"/hassio\"",
")"
] | [
11,
0
] | [
15,
63
] | python | en | ['en', 'sv', 'en'] | True |
system_health_info | (hass: HomeAssistant) | Get info for the info page. | Get info for the info page. | async def system_health_info(hass: HomeAssistant):
"""Get info for the info page."""
info = hass.components.hassio.get_info()
host_info = hass.components.hassio.get_host_info()
supervisor_info = hass.components.hassio.get_supervisor_info()
if supervisor_info.get("healthy"):
healthy = True
else:
healthy = {
"type": "failed",
"error": "Unhealthy",
"more_info": "/hassio/system",
}
if supervisor_info.get("supported"):
supported = True
else:
supported = {
"type": "failed",
"error": "Unsupported",
"more_info": "/hassio/system",
}
information = {
"host_os": host_info.get("operating_system"),
"update_channel": info.get("channel"),
"supervisor_version": info.get("supervisor"),
"docker_version": info.get("docker"),
"disk_total": f"{host_info.get('disk_total')} GB",
"disk_used": f"{host_info.get('disk_used')} GB",
"healthy": healthy,
"supported": supported,
}
if info.get("hassos") is not None:
os_info = hass.components.hassio.get_os_info()
information["board"] = os_info.get("board")
information["supervisor_api"] = system_health.async_check_can_reach_url(
hass, SUPERVISOR_PING, OBSERVER_URL
)
information["version_api"] = system_health.async_check_can_reach_url(
hass,
f"https://version.home-assistant.io/{info.get('channel')}.json",
"/hassio/system",
)
information["installed_addons"] = ", ".join(
f"{addon['name']} ({addon['version']})"
for addon in supervisor_info.get("addons", [])
)
return information | [
"async",
"def",
"system_health_info",
"(",
"hass",
":",
"HomeAssistant",
")",
":",
"info",
"=",
"hass",
".",
"components",
".",
"hassio",
".",
"get_info",
"(",
")",
"host_info",
"=",
"hass",
".",
"components",
".",
"hassio",
".",
"get_host_info",
"(",
")",
"supervisor_info",
"=",
"hass",
".",
"components",
".",
"hassio",
".",
"get_supervisor_info",
"(",
")",
"if",
"supervisor_info",
".",
"get",
"(",
"\"healthy\"",
")",
":",
"healthy",
"=",
"True",
"else",
":",
"healthy",
"=",
"{",
"\"type\"",
":",
"\"failed\"",
",",
"\"error\"",
":",
"\"Unhealthy\"",
",",
"\"more_info\"",
":",
"\"/hassio/system\"",
",",
"}",
"if",
"supervisor_info",
".",
"get",
"(",
"\"supported\"",
")",
":",
"supported",
"=",
"True",
"else",
":",
"supported",
"=",
"{",
"\"type\"",
":",
"\"failed\"",
",",
"\"error\"",
":",
"\"Unsupported\"",
",",
"\"more_info\"",
":",
"\"/hassio/system\"",
",",
"}",
"information",
"=",
"{",
"\"host_os\"",
":",
"host_info",
".",
"get",
"(",
"\"operating_system\"",
")",
",",
"\"update_channel\"",
":",
"info",
".",
"get",
"(",
"\"channel\"",
")",
",",
"\"supervisor_version\"",
":",
"info",
".",
"get",
"(",
"\"supervisor\"",
")",
",",
"\"docker_version\"",
":",
"info",
".",
"get",
"(",
"\"docker\"",
")",
",",
"\"disk_total\"",
":",
"f\"{host_info.get('disk_total')} GB\"",
",",
"\"disk_used\"",
":",
"f\"{host_info.get('disk_used')} GB\"",
",",
"\"healthy\"",
":",
"healthy",
",",
"\"supported\"",
":",
"supported",
",",
"}",
"if",
"info",
".",
"get",
"(",
"\"hassos\"",
")",
"is",
"not",
"None",
":",
"os_info",
"=",
"hass",
".",
"components",
".",
"hassio",
".",
"get_os_info",
"(",
")",
"information",
"[",
"\"board\"",
"]",
"=",
"os_info",
".",
"get",
"(",
"\"board\"",
")",
"information",
"[",
"\"supervisor_api\"",
"]",
"=",
"system_health",
".",
"async_check_can_reach_url",
"(",
"hass",
",",
"SUPERVISOR_PING",
",",
"OBSERVER_URL",
")",
"information",
"[",
"\"version_api\"",
"]",
"=",
"system_health",
".",
"async_check_can_reach_url",
"(",
"hass",
",",
"f\"https://version.home-assistant.io/{info.get('channel')}.json\"",
",",
"\"/hassio/system\"",
",",
")",
"information",
"[",
"\"installed_addons\"",
"]",
"=",
"\", \"",
".",
"join",
"(",
"f\"{addon['name']} ({addon['version']})\"",
"for",
"addon",
"in",
"supervisor_info",
".",
"get",
"(",
"\"addons\"",
",",
"[",
"]",
")",
")",
"return",
"information"
] | [
18,
0
] | [
71,
22
] | python | en | ['en', 'en', 'en'] | True |
EventBindBinaryReader.start_datetime | (self) | datetime: Start datetime of this binary file. | datetime: Start datetime of this binary file. | def start_datetime(self) -> datetime:
"""datetime: Start datetime of this binary file."""
return self._reader.start_datetime | [
"def",
"start_datetime",
"(",
"self",
")",
"->",
"datetime",
":",
"return",
"self",
".",
"_reader",
".",
"start_datetime"
] | [
79,
4
] | [
81,
42
] | python | en | ['en', 'en', 'en'] | True |
EventBindBinaryReader.end_datetime | (self) | datetime: End datetime of this binary file. | datetime: End datetime of this binary file. | def end_datetime(self) -> datetime:
"""datetime: End datetime of this binary file."""
return self._reader.end_datetime | [
"def",
"end_datetime",
"(",
"self",
")",
"->",
"datetime",
":",
"return",
"self",
".",
"_reader",
".",
"end_datetime"
] | [
84,
4
] | [
86,
40
] | python | en | ['en', 'en', 'en'] | True |
EventBindBinaryReader.header | (self) | tuple: Header in binary file. | tuple: Header in binary file. | def header(self) -> tuple:
"""tuple: Header in binary file."""
return self._reader.header | [
"def",
"header",
"(",
"self",
")",
"->",
"tuple",
":",
"return",
"self",
".",
"_reader",
".",
"header"
] | [
89,
4
] | [
91,
34
] | python | en | ['en', 'ha', 'en'] | True |
EventBindBinaryReader.read_items | (self, tick: int) | Read items by tick and generate related events, then insert them into EventBuffer.
Args:
tick(int): Tick to get items, NOTE: the tick must specified sequentially.
| Read items by tick and generate related events, then insert them into EventBuffer. | def read_items(self, tick: int):
"""Read items by tick and generate related events, then insert them into EventBuffer.
Args:
tick(int): Tick to get items, NOTE: the tick must specified sequentially.
"""
if self._picker:
for item in self._picker.items(tick):
self._gen_event_by_item(item, tick)
return None | [
"def",
"read_items",
"(",
"self",
",",
"tick",
":",
"int",
")",
":",
"if",
"self",
".",
"_picker",
":",
"for",
"item",
"in",
"self",
".",
"_picker",
".",
"items",
"(",
"tick",
")",
":",
"self",
".",
"_gen_event_by_item",
"(",
"item",
",",
"tick",
")",
"return",
"None"
] | [
93,
4
] | [
103,
19
] | python | en | ['en', 'en', 'en'] | True |
EventBindBinaryReader.reset | (self) | Reset states of reader. | Reset states of reader. | def reset(self):
"""Reset states of reader."""
self._reader.reset()
self._picker = self._reader.items_tick_picker(start_time_offset=self._start_tick,
end_time_offset=self._end_tick, time_unit=self._time_unit) | [
"def",
"reset",
"(",
"self",
")",
":",
"self",
".",
"_reader",
".",
"reset",
"(",
")",
"self",
".",
"_picker",
"=",
"self",
".",
"_reader",
".",
"items_tick_picker",
"(",
"start_time_offset",
"=",
"self",
".",
"_start_tick",
",",
"end_time_offset",
"=",
"self",
".",
"_end_tick",
",",
"time_unit",
"=",
"self",
".",
"_time_unit",
")"
] | [
105,
4
] | [
109,
112
] | python | en | ['en', 'jv', 'en'] | True |
_set_up_units | (hass) | Set up the tests. | Set up the tests. | def _set_up_units(hass):
"""Set up the tests."""
hass.config.units = UnitSystem(
"custom", TEMP_CELSIUS, LENGTH_METERS, VOLUME_LITERS, MASS_GRAMS, PRESSURE_PA
) | [
"def",
"_set_up_units",
"(",
"hass",
")",
":",
"hass",
".",
"config",
".",
"units",
"=",
"UnitSystem",
"(",
"\"custom\"",
",",
"TEMP_CELSIUS",
",",
"LENGTH_METERS",
",",
"VOLUME_LITERS",
",",
"MASS_GRAMS",
",",
"PRESSURE_PA",
")"
] | [
28,
0
] | [
32,
5
] | python | en | ['en', 'en', 'en'] | True |
render_to_info | (hass, template_str, variables=None) | Create render info from template. | Create render info from template. | def render_to_info(hass, template_str, variables=None):
"""Create render info from template."""
tmp = template.Template(template_str, hass)
return tmp.async_render_to_info(variables) | [
"def",
"render_to_info",
"(",
"hass",
",",
"template_str",
",",
"variables",
"=",
"None",
")",
":",
"tmp",
"=",
"template",
".",
"Template",
"(",
"template_str",
",",
"hass",
")",
"return",
"tmp",
".",
"async_render_to_info",
"(",
"variables",
")"
] | [
35,
0
] | [
38,
46
] | python | en | ['en', 'en', 'en'] | True |
extract_entities | (hass, template_str, variables=None) | Extract entities from a template. | Extract entities from a template. | def extract_entities(hass, template_str, variables=None):
"""Extract entities from a template."""
info = render_to_info(hass, template_str, variables)
return info.entities | [
"def",
"extract_entities",
"(",
"hass",
",",
"template_str",
",",
"variables",
"=",
"None",
")",
":",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"template_str",
",",
"variables",
")",
"return",
"info",
".",
"entities"
] | [
41,
0
] | [
44,
24
] | python | en | ['en', 'en', 'en'] | True |
assert_result_info | (info, result, entities=None, domains=None, all_states=False) | Check result info. | Check result info. | def assert_result_info(info, result, entities=None, domains=None, all_states=False):
"""Check result info."""
assert info.result() == result
assert info.all_states == all_states
assert info.filter("invalid_entity_name.somewhere") == all_states
if entities is not None:
assert info.entities == frozenset(entities)
assert all([info.filter(entity) for entity in entities])
if not all_states:
assert not info.filter("invalid_entity_name.somewhere")
else:
assert not info.entities
if domains is not None:
assert info.domains == frozenset(domains)
assert all([info.filter(domain + ".entity") for domain in domains])
else:
assert not hasattr(info, "_domains") | [
"def",
"assert_result_info",
"(",
"info",
",",
"result",
",",
"entities",
"=",
"None",
",",
"domains",
"=",
"None",
",",
"all_states",
"=",
"False",
")",
":",
"assert",
"info",
".",
"result",
"(",
")",
"==",
"result",
"assert",
"info",
".",
"all_states",
"==",
"all_states",
"assert",
"info",
".",
"filter",
"(",
"\"invalid_entity_name.somewhere\"",
")",
"==",
"all_states",
"if",
"entities",
"is",
"not",
"None",
":",
"assert",
"info",
".",
"entities",
"==",
"frozenset",
"(",
"entities",
")",
"assert",
"all",
"(",
"[",
"info",
".",
"filter",
"(",
"entity",
")",
"for",
"entity",
"in",
"entities",
"]",
")",
"if",
"not",
"all_states",
":",
"assert",
"not",
"info",
".",
"filter",
"(",
"\"invalid_entity_name.somewhere\"",
")",
"else",
":",
"assert",
"not",
"info",
".",
"entities",
"if",
"domains",
"is",
"not",
"None",
":",
"assert",
"info",
".",
"domains",
"==",
"frozenset",
"(",
"domains",
")",
"assert",
"all",
"(",
"[",
"info",
".",
"filter",
"(",
"domain",
"+",
"\".entity\"",
")",
"for",
"domain",
"in",
"domains",
"]",
")",
"else",
":",
"assert",
"not",
"hasattr",
"(",
"info",
",",
"\"_domains\"",
")"
] | [
47,
0
] | [
63,
44
] | python | en | ['en', 'kk', 'en'] | True |
test_template_equality | () | Test template comparison and hashing. | Test template comparison and hashing. | def test_template_equality():
"""Test template comparison and hashing."""
template_one = template.Template("{{ template_one }}")
template_one_1 = template.Template("{{ template_one }}")
template_two = template.Template("{{ template_two }}")
assert template_one == template_one_1
assert template_one != template_two
assert hash(template_one) == hash(template_one_1)
assert hash(template_one) != hash(template_two)
assert str(template_one_1) == 'Template("{{ template_one }}")'
with pytest.raises(TypeError):
template.Template(["{{ template_one }}"]) | [
"def",
"test_template_equality",
"(",
")",
":",
"template_one",
"=",
"template",
".",
"Template",
"(",
"\"{{ template_one }}\"",
")",
"template_one_1",
"=",
"template",
".",
"Template",
"(",
"\"{{ template_one }}\"",
")",
"template_two",
"=",
"template",
".",
"Template",
"(",
"\"{{ template_two }}\"",
")",
"assert",
"template_one",
"==",
"template_one_1",
"assert",
"template_one",
"!=",
"template_two",
"assert",
"hash",
"(",
"template_one",
")",
"==",
"hash",
"(",
"template_one_1",
")",
"assert",
"hash",
"(",
"template_one",
")",
"!=",
"hash",
"(",
"template_two",
")",
"assert",
"str",
"(",
"template_one_1",
")",
"==",
"'Template(\"{{ template_one }}\")'",
"with",
"pytest",
".",
"raises",
"(",
"TypeError",
")",
":",
"template",
".",
"Template",
"(",
"[",
"\"{{ template_one }}\"",
"]",
")"
] | [
66,
0
] | [
80,
49
] | python | en | ['en', 'en', 'en'] | True |
test_invalid_template | (hass) | Invalid template raises error. | Invalid template raises error. | def test_invalid_template(hass):
"""Invalid template raises error."""
tmpl = template.Template("{{", hass)
with pytest.raises(TemplateError):
tmpl.ensure_valid()
with pytest.raises(TemplateError):
tmpl.async_render()
info = tmpl.async_render_to_info()
with pytest.raises(TemplateError):
assert info.result() == "impossible"
tmpl = template.Template("{{states(keyword)}}", hass)
tmpl.ensure_valid()
with pytest.raises(TemplateError):
tmpl.async_render() | [
"def",
"test_invalid_template",
"(",
"hass",
")",
":",
"tmpl",
"=",
"template",
".",
"Template",
"(",
"\"{{\"",
",",
"hass",
")",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"tmpl",
".",
"ensure_valid",
"(",
")",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"tmpl",
".",
"async_render",
"(",
")",
"info",
"=",
"tmpl",
".",
"async_render_to_info",
"(",
")",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"assert",
"info",
".",
"result",
"(",
")",
"==",
"\"impossible\"",
"tmpl",
"=",
"template",
".",
"Template",
"(",
"\"{{states(keyword)}}\"",
",",
"hass",
")",
"tmpl",
".",
"ensure_valid",
"(",
")",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"tmpl",
".",
"async_render",
"(",
")"
] | [
83,
0
] | [
102,
27
] | python | en | ['fi', 'et', 'en'] | False |
test_referring_states_by_entity_id | (hass) | Test referring states by entity id. | Test referring states by entity id. | def test_referring_states_by_entity_id(hass):
"""Test referring states by entity id."""
hass.states.async_set("test.object", "happy")
assert (
template.Template("{{ states.test.object.state }}", hass).async_render()
== "happy"
)
assert (
template.Template('{{ states["test.object"].state }}', hass).async_render()
== "happy"
)
assert (
template.Template('{{ states("test.object") }}', hass).async_render() == "happy"
) | [
"def",
"test_referring_states_by_entity_id",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ states.test.object.state }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"happy\"",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ states[\"test.object\"].state }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"happy\"",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ states(\"test.object\") }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"happy\"",
")"
] | [
105,
0
] | [
120,
5
] | python | en | ['en', 'en', 'en'] | True |
test_invalid_entity_id | (hass) | Test referring states by entity id. | Test referring states by entity id. | def test_invalid_entity_id(hass):
"""Test referring states by entity id."""
with pytest.raises(TemplateError):
template.Template('{{ states["big.fat..."] }}', hass).async_render()
with pytest.raises(TemplateError):
template.Template('{{ states.test["big.fat..."] }}', hass).async_render()
with pytest.raises(TemplateError):
template.Template('{{ states["invalid/domain"] }}', hass).async_render() | [
"def",
"test_invalid_entity_id",
"(",
"hass",
")",
":",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"template",
".",
"Template",
"(",
"'{{ states[\"big.fat...\"] }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"template",
".",
"Template",
"(",
"'{{ states.test[\"big.fat...\"] }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"template",
".",
"Template",
"(",
"'{{ states[\"invalid/domain\"] }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")"
] | [
123,
0
] | [
130,
80
] | python | en | ['en', 'en', 'en'] | True |
test_raise_exception_on_error | (hass) | Test raising an exception on error. | Test raising an exception on error. | def test_raise_exception_on_error(hass):
"""Test raising an exception on error."""
with pytest.raises(TemplateError):
template.Template("{{ invalid_syntax").ensure_valid() | [
"def",
"test_raise_exception_on_error",
"(",
"hass",
")",
":",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"template",
".",
"Template",
"(",
"\"{{ invalid_syntax\"",
")",
".",
"ensure_valid",
"(",
")"
] | [
133,
0
] | [
136,
61
] | python | en | ['en', 'de', 'en'] | True |
test_iterating_all_states | (hass) | Test iterating all states. | Test iterating all states. | def test_iterating_all_states(hass):
"""Test iterating all states."""
tmpl_str = "{% for state in states %}{{ state.state }}{% endfor %}"
info = render_to_info(hass, tmpl_str)
assert_result_info(info, "", all_states=True)
assert info.rate_limit == template.ALL_STATES_RATE_LIMIT
hass.states.async_set("test.object", "happy")
hass.states.async_set("sensor.temperature", 10)
info = render_to_info(hass, tmpl_str)
assert_result_info(info, "10happy", entities=[], all_states=True) | [
"def",
"test_iterating_all_states",
"(",
"hass",
")",
":",
"tmpl_str",
"=",
"\"{% for state in states %}{{ state.state }}{% endfor %}\"",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"tmpl_str",
")",
"assert_result_info",
"(",
"info",
",",
"\"\"",
",",
"all_states",
"=",
"True",
")",
"assert",
"info",
".",
"rate_limit",
"==",
"template",
".",
"ALL_STATES_RATE_LIMIT",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.temperature\"",
",",
"10",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"tmpl_str",
")",
"assert_result_info",
"(",
"info",
",",
"\"10happy\"",
",",
"entities",
"=",
"[",
"]",
",",
"all_states",
"=",
"True",
")"
] | [
139,
0
] | [
151,
69
] | python | en | ['en', 'en', 'en'] | True |
test_iterating_all_states_unavailable | (hass) | Test iterating all states unavailable. | Test iterating all states unavailable. | def test_iterating_all_states_unavailable(hass):
"""Test iterating all states unavailable."""
hass.states.async_set("test.object", "on")
tmpl_str = "{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count }}"
info = render_to_info(hass, tmpl_str)
assert info.all_states is True
assert info.rate_limit == template.ALL_STATES_RATE_LIMIT
hass.states.async_set("test.object", "unknown")
hass.states.async_set("sensor.temperature", 10)
info = render_to_info(hass, tmpl_str)
assert_result_info(info, 1, entities=[], all_states=True) | [
"def",
"test_iterating_all_states_unavailable",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"on\"",
")",
"tmpl_str",
"=",
"\"{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count }}\"",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"tmpl_str",
")",
"assert",
"info",
".",
"all_states",
"is",
"True",
"assert",
"info",
".",
"rate_limit",
"==",
"template",
".",
"ALL_STATES_RATE_LIMIT",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"unknown\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.temperature\"",
",",
"10",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"tmpl_str",
")",
"assert_result_info",
"(",
"info",
",",
"1",
",",
"entities",
"=",
"[",
"]",
",",
"all_states",
"=",
"True",
")"
] | [
154,
0
] | [
169,
61
] | python | en | ['en', 'en', 'it'] | True |
test_iterating_domain_states | (hass) | Test iterating domain states. | Test iterating domain states. | def test_iterating_domain_states(hass):
"""Test iterating domain states."""
tmpl_str = "{% for state in states.sensor %}{{ state.state }}{% endfor %}"
info = render_to_info(hass, tmpl_str)
assert_result_info(info, "", domains=["sensor"])
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT
hass.states.async_set("test.object", "happy")
hass.states.async_set("sensor.back_door", "open")
hass.states.async_set("sensor.temperature", 10)
info = render_to_info(hass, tmpl_str)
assert_result_info(
info,
"open10",
entities=[],
domains=["sensor"],
) | [
"def",
"test_iterating_domain_states",
"(",
"hass",
")",
":",
"tmpl_str",
"=",
"\"{% for state in states.sensor %}{{ state.state }}{% endfor %}\"",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"tmpl_str",
")",
"assert_result_info",
"(",
"info",
",",
"\"\"",
",",
"domains",
"=",
"[",
"\"sensor\"",
"]",
")",
"assert",
"info",
".",
"rate_limit",
"==",
"template",
".",
"DOMAIN_STATES_RATE_LIMIT",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.back_door\"",
",",
"\"open\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.temperature\"",
",",
"10",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"tmpl_str",
")",
"assert_result_info",
"(",
"info",
",",
"\"open10\"",
",",
"entities",
"=",
"[",
"]",
",",
"domains",
"=",
"[",
"\"sensor\"",
"]",
",",
")"
] | [
172,
0
] | [
190,
5
] | python | en | ['de', 'en', 'en'] | True |
test_float | (hass) | Test float. | Test float. | def test_float(hass):
"""Test float."""
hass.states.async_set("sensor.temperature", "12")
assert (
template.Template(
"{{ float(states.sensor.temperature.state) }}", hass
).async_render()
== 12.0
)
assert (
template.Template(
"{{ float(states.sensor.temperature.state) > 11 }}", hass
).async_render()
is True
)
assert (
template.Template("{{ float('forgiving') }}", hass).async_render()
== "forgiving"
) | [
"def",
"test_float",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.temperature\"",
",",
"\"12\"",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ float(states.sensor.temperature.state) }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"12.0",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ float(states.sensor.temperature.state) > 11 }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"is",
"True",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ float('forgiving') }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"forgiving\"",
")"
] | [
193,
0
] | [
214,
5
] | python | en | ['en', 'da', 'en'] | False |
test_rounding_value | (hass) | Test rounding value. | Test rounding value. | def test_rounding_value(hass):
"""Test rounding value."""
hass.states.async_set("sensor.temperature", 12.78)
assert (
template.Template(
"{{ states.sensor.temperature.state | round(1) }}", hass
).async_render()
== 12.8
)
assert (
template.Template(
"{{ states.sensor.temperature.state | multiply(10) | round }}", hass
).async_render()
== 128
)
assert (
template.Template(
'{{ states.sensor.temperature.state | round(1, "floor") }}', hass
).async_render()
== 12.7
)
assert (
template.Template(
'{{ states.sensor.temperature.state | round(1, "ceil") }}', hass
).async_render()
== 12.8
)
assert (
template.Template(
'{{ states.sensor.temperature.state | round(1, "half") }}', hass
).async_render()
== 13.0
) | [
"def",
"test_rounding_value",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.temperature\"",
",",
"12.78",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ states.sensor.temperature.state | round(1) }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"12.8",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ states.sensor.temperature.state | multiply(10) | round }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"128",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ states.sensor.temperature.state | round(1, \"floor\") }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"12.7",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ states.sensor.temperature.state | round(1, \"ceil\") }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"12.8",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ states.sensor.temperature.state | round(1, \"half\") }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"13.0",
")"
] | [
217,
0
] | [
254,
5
] | python | en | ['nl', 'no', 'en'] | False |
test_rounding_value_get_original_value_on_error | (hass) | Test rounding value get original value on error. | Test rounding value get original value on error. | def test_rounding_value_get_original_value_on_error(hass):
"""Test rounding value get original value on error."""
assert template.Template("{{ None | round }}", hass).async_render() is None
assert (
template.Template('{{ "no_number" | round }}', hass).async_render()
== "no_number"
) | [
"def",
"test_rounding_value_get_original_value_on_error",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"Template",
"(",
"\"{{ None | round }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"is",
"None",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ \"no_number\" | round }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"no_number\"",
")"
] | [
257,
0
] | [
264,
5
] | python | en | ['nl', 'la', 'en'] | False |
test_multiply | (hass) | Test multiply. | Test multiply. | def test_multiply(hass):
"""Test multiply."""
tests = {None: None, 10: 100, '"abcd"': "abcd"}
for inp, out in tests.items():
assert (
template.Template(
"{{ %s | multiply(10) | round }}" % inp, hass
).async_render()
== out
) | [
"def",
"test_multiply",
"(",
"hass",
")",
":",
"tests",
"=",
"{",
"None",
":",
"None",
",",
"10",
":",
"100",
",",
"'\"abcd\"'",
":",
"\"abcd\"",
"}",
"for",
"inp",
",",
"out",
"in",
"tests",
".",
"items",
"(",
")",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | multiply(10) | round }}\"",
"%",
"inp",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"out",
")"
] | [
267,
0
] | [
277,
9
] | python | en | ['en', 'no', 'en'] | False |
test_logarithm | (hass) | Test logarithm. | Test logarithm. | def test_logarithm(hass):
"""Test logarithm."""
tests = [
(4, 2, 2.0),
(1000, 10, 3.0),
(math.e, "", 1.0),
('"invalid"', "_", "invalid"),
(10, '"invalid"', 10.0),
]
for value, base, expected in tests:
assert (
template.Template(
f"{{{{ {value} | log({base}) | round(1) }}}}", hass
).async_render()
== expected
)
assert (
template.Template(
f"{{{{ log({value}, {base}) | round(1) }}}}", hass
).async_render()
== expected
) | [
"def",
"test_logarithm",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"4",
",",
"2",
",",
"2.0",
")",
",",
"(",
"1000",
",",
"10",
",",
"3.0",
")",
",",
"(",
"math",
".",
"e",
",",
"\"\"",
",",
"1.0",
")",
",",
"(",
"'\"invalid\"'",
",",
"\"_\"",
",",
"\"invalid\"",
")",
",",
"(",
"10",
",",
"'\"invalid\"'",
",",
"10.0",
")",
",",
"]",
"for",
"value",
",",
"base",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"f\"{{{{ {value} | log({base}) | round(1) }}}}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"f\"{{{{ log({value}, {base}) | round(1) }}}}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
280,
0
] | [
303,
9
] | python | en | ['en', 'fr', 'it'] | False |
test_cos | (hass) | Test cosine. | Test cosine. | def test_cos(hass):
"""Test cosine."""
tests = [
(0, 1.0),
(math.pi / 2, 0.0),
(math.pi, -1.0),
(math.pi * 1.5, -0.0),
(math.pi / 10, 0.951),
("'error'", "error"),
]
for value, expected in tests:
assert (
template.Template("{{ %s | cos | round(3) }}" % value, hass).async_render()
== expected
) | [
"def",
"test_cos",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"0",
",",
"1.0",
")",
",",
"(",
"math",
".",
"pi",
"/",
"2",
",",
"0.0",
")",
",",
"(",
"math",
".",
"pi",
",",
"-",
"1.0",
")",
",",
"(",
"math",
".",
"pi",
"*",
"1.5",
",",
"-",
"0.0",
")",
",",
"(",
"math",
".",
"pi",
"/",
"10",
",",
"0.951",
")",
",",
"(",
"\"'error'\"",
",",
"\"error\"",
")",
",",
"]",
"for",
"value",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | cos | round(3) }}\"",
"%",
"value",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
324,
0
] | [
339,
9
] | python | en | ['en', 'la', 'it'] | False |
test_tan | (hass) | Test tangent. | Test tangent. | def test_tan(hass):
"""Test tangent."""
tests = [
(0, 0.0),
(math.pi, -0.0),
(math.pi / 180 * 45, 1.0),
(math.pi / 180 * 90, "1.633123935319537e+16"),
(math.pi / 180 * 135, -1.0),
("'error'", "error"),
]
for value, expected in tests:
assert (
template.Template("{{ %s | tan | round(3) }}" % value, hass).async_render()
== expected
) | [
"def",
"test_tan",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"0",
",",
"0.0",
")",
",",
"(",
"math",
".",
"pi",
",",
"-",
"0.0",
")",
",",
"(",
"math",
".",
"pi",
"/",
"180",
"*",
"45",
",",
"1.0",
")",
",",
"(",
"math",
".",
"pi",
"/",
"180",
"*",
"90",
",",
"\"1.633123935319537e+16\"",
")",
",",
"(",
"math",
".",
"pi",
"/",
"180",
"*",
"135",
",",
"-",
"1.0",
")",
",",
"(",
"\"'error'\"",
",",
"\"error\"",
")",
",",
"]",
"for",
"value",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | tan | round(3) }}\"",
"%",
"value",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
342,
0
] | [
357,
9
] | python | ja | ['sv', 'ja', 'ur'] | False |
test_sqrt | (hass) | Test square root. | Test square root. | def test_sqrt(hass):
"""Test square root."""
tests = [
(0, 0.0),
(1, 1.0),
(2, 1.414),
(10, 3.162),
(100, 10.0),
("'error'", "error"),
]
for value, expected in tests:
assert (
template.Template("{{ %s | sqrt | round(3) }}" % value, hass).async_render()
== expected
) | [
"def",
"test_sqrt",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"0",
",",
"0.0",
")",
",",
"(",
"1",
",",
"1.0",
")",
",",
"(",
"2",
",",
"1.414",
")",
",",
"(",
"10",
",",
"3.162",
")",
",",
"(",
"100",
",",
"10.0",
")",
",",
"(",
"\"'error'\"",
",",
"\"error\"",
")",
",",
"]",
"for",
"value",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | sqrt | round(3) }}\"",
"%",
"value",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
360,
0
] | [
375,
9
] | python | en | ['en', 'en', 'en'] | True |
test_arc_sine | (hass) | Test arcus sine. | Test arcus sine. | def test_arc_sine(hass):
"""Test arcus sine."""
tests = [
(-2.0, -2.0), # value error
(-1.0, -1.571),
(-0.5, -0.524),
(0.0, 0.0),
(0.5, 0.524),
(1.0, 1.571),
(2.0, 2.0), # value error
('"error"', "error"),
]
for value, expected in tests:
assert (
template.Template("{{ %s | asin | round(3) }}" % value, hass).async_render()
== expected
) | [
"def",
"test_arc_sine",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"-",
"2.0",
",",
"-",
"2.0",
")",
",",
"# value error",
"(",
"-",
"1.0",
",",
"-",
"1.571",
")",
",",
"(",
"-",
"0.5",
",",
"-",
"0.524",
")",
",",
"(",
"0.0",
",",
"0.0",
")",
",",
"(",
"0.5",
",",
"0.524",
")",
",",
"(",
"1.0",
",",
"1.571",
")",
",",
"(",
"2.0",
",",
"2.0",
")",
",",
"# value error",
"(",
"'\"error\"'",
",",
"\"error\"",
")",
",",
"]",
"for",
"value",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | asin | round(3) }}\"",
"%",
"value",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
378,
0
] | [
395,
9
] | python | en | ['en', 'la', 'it'] | False |
test_arc_cos | (hass) | Test arcus cosine. | Test arcus cosine. | def test_arc_cos(hass):
"""Test arcus cosine."""
tests = [
(-2.0, -2.0), # value error
(-1.0, 3.142),
(-0.5, 2.094),
(0.0, 1.571),
(0.5, 1.047),
(1.0, 0.0),
(2.0, 2.0), # value error
('"error"', "error"),
]
for value, expected in tests:
assert (
template.Template("{{ %s | acos | round(3) }}" % value, hass).async_render()
== expected
) | [
"def",
"test_arc_cos",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"-",
"2.0",
",",
"-",
"2.0",
")",
",",
"# value error",
"(",
"-",
"1.0",
",",
"3.142",
")",
",",
"(",
"-",
"0.5",
",",
"2.094",
")",
",",
"(",
"0.0",
",",
"1.571",
")",
",",
"(",
"0.5",
",",
"1.047",
")",
",",
"(",
"1.0",
",",
"0.0",
")",
",",
"(",
"2.0",
",",
"2.0",
")",
",",
"# value error",
"(",
"'\"error\"'",
",",
"\"error\"",
")",
",",
"]",
"for",
"value",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | acos | round(3) }}\"",
"%",
"value",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
398,
0
] | [
415,
9
] | python | en | ['en', 'la', 'it'] | False |
test_arc_tan | (hass) | Test arcus tangent. | Test arcus tangent. | def test_arc_tan(hass):
"""Test arcus tangent."""
tests = [
(-10.0, -1.471),
(-2.0, -1.107),
(-1.0, -0.785),
(-0.5, -0.464),
(0.0, 0.0),
(0.5, 0.464),
(1.0, 0.785),
(2.0, 1.107),
(10.0, 1.471),
('"error"', "error"),
]
for value, expected in tests:
assert (
template.Template("{{ %s | atan | round(3) }}" % value, hass).async_render()
== expected
) | [
"def",
"test_arc_tan",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"-",
"10.0",
",",
"-",
"1.471",
")",
",",
"(",
"-",
"2.0",
",",
"-",
"1.107",
")",
",",
"(",
"-",
"1.0",
",",
"-",
"0.785",
")",
",",
"(",
"-",
"0.5",
",",
"-",
"0.464",
")",
",",
"(",
"0.0",
",",
"0.0",
")",
",",
"(",
"0.5",
",",
"0.464",
")",
",",
"(",
"1.0",
",",
"0.785",
")",
",",
"(",
"2.0",
",",
"1.107",
")",
",",
"(",
"10.0",
",",
"1.471",
")",
",",
"(",
"'\"error\"'",
",",
"\"error\"",
")",
",",
"]",
"for",
"value",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | atan | round(3) }}\"",
"%",
"value",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
418,
0
] | [
437,
9
] | python | it | ['sv', 'la', 'it'] | False |
test_arc_tan2 | (hass) | Test two parameter version of arcus tangent. | Test two parameter version of arcus tangent. | def test_arc_tan2(hass):
"""Test two parameter version of arcus tangent."""
tests = [
(-10.0, -10.0, -2.356),
(-10.0, 0.0, -1.571),
(-10.0, 10.0, -0.785),
(0.0, -10.0, 3.142),
(0.0, 0.0, 0.0),
(0.0, 10.0, 0.0),
(10.0, -10.0, 2.356),
(10.0, 0.0, 1.571),
(10.0, 10.0, 0.785),
(-4.0, 3.0, -0.927),
(-1.0, 2.0, -0.464),
(2.0, 1.0, 1.107),
('"duck"', '"goose"', ("duck", "goose")),
]
for y, x, expected in tests:
assert (
template.Template(
f"{{{{ ({y}, {x}) | atan2 | round(3) }}}}", hass
).async_render()
== expected
)
assert (
template.Template(
f"{{{{ atan2({y}, {x}) | round(3) }}}}", hass
).async_render()
== expected
) | [
"def",
"test_arc_tan2",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"-",
"10.0",
",",
"-",
"10.0",
",",
"-",
"2.356",
")",
",",
"(",
"-",
"10.0",
",",
"0.0",
",",
"-",
"1.571",
")",
",",
"(",
"-",
"10.0",
",",
"10.0",
",",
"-",
"0.785",
")",
",",
"(",
"0.0",
",",
"-",
"10.0",
",",
"3.142",
")",
",",
"(",
"0.0",
",",
"0.0",
",",
"0.0",
")",
",",
"(",
"0.0",
",",
"10.0",
",",
"0.0",
")",
",",
"(",
"10.0",
",",
"-",
"10.0",
",",
"2.356",
")",
",",
"(",
"10.0",
",",
"0.0",
",",
"1.571",
")",
",",
"(",
"10.0",
",",
"10.0",
",",
"0.785",
")",
",",
"(",
"-",
"4.0",
",",
"3.0",
",",
"-",
"0.927",
")",
",",
"(",
"-",
"1.0",
",",
"2.0",
",",
"-",
"0.464",
")",
",",
"(",
"2.0",
",",
"1.0",
",",
"1.107",
")",
",",
"(",
"'\"duck\"'",
",",
"'\"goose\"'",
",",
"(",
"\"duck\"",
",",
"\"goose\"",
")",
")",
",",
"]",
"for",
"y",
",",
"x",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"f\"{{{{ ({y}, {x}) | atan2 | round(3) }}}}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"f\"{{{{ atan2({y}, {x}) | round(3) }}}}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
440,
0
] | [
470,
9
] | python | en | ['en', 'en', 'en'] | True |
test_strptime | (hass) | Test the parse timestamp method. | Test the parse timestamp method. | def test_strptime(hass):
"""Test the parse timestamp method."""
tests = [
("2016-10-19 15:22:05.588122 UTC", "%Y-%m-%d %H:%M:%S.%f %Z", None),
("2016-10-19 15:22:05.588122+0100", "%Y-%m-%d %H:%M:%S.%f%z", None),
("2016-10-19 15:22:05.588122", "%Y-%m-%d %H:%M:%S.%f", None),
("2016-10-19", "%Y-%m-%d", None),
("2016", "%Y", None),
("15:22:05", "%H:%M:%S", None),
("1469119144", "%Y", 1469119144),
("invalid", "%Y", "invalid"),
]
for inp, fmt, expected in tests:
if expected is None:
expected = str(datetime.strptime(inp, fmt))
temp = f"{{{{ strptime('{inp}', '{fmt}') }}}}"
assert template.Template(temp, hass).async_render() == expected | [
"def",
"test_strptime",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"\"2016-10-19 15:22:05.588122 UTC\"",
",",
"\"%Y-%m-%d %H:%M:%S.%f %Z\"",
",",
"None",
")",
",",
"(",
"\"2016-10-19 15:22:05.588122+0100\"",
",",
"\"%Y-%m-%d %H:%M:%S.%f%z\"",
",",
"None",
")",
",",
"(",
"\"2016-10-19 15:22:05.588122\"",
",",
"\"%Y-%m-%d %H:%M:%S.%f\"",
",",
"None",
")",
",",
"(",
"\"2016-10-19\"",
",",
"\"%Y-%m-%d\"",
",",
"None",
")",
",",
"(",
"\"2016\"",
",",
"\"%Y\"",
",",
"None",
")",
",",
"(",
"\"15:22:05\"",
",",
"\"%H:%M:%S\"",
",",
"None",
")",
",",
"(",
"\"1469119144\"",
",",
"\"%Y\"",
",",
"1469119144",
")",
",",
"(",
"\"invalid\"",
",",
"\"%Y\"",
",",
"\"invalid\"",
")",
",",
"]",
"for",
"inp",
",",
"fmt",
",",
"expected",
"in",
"tests",
":",
"if",
"expected",
"is",
"None",
":",
"expected",
"=",
"str",
"(",
"datetime",
".",
"strptime",
"(",
"inp",
",",
"fmt",
")",
")",
"temp",
"=",
"f\"{{{{ strptime('{inp}', '{fmt}') }}}}\"",
"assert",
"template",
".",
"Template",
"(",
"temp",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected"
] | [
473,
0
] | [
492,
71
] | python | en | ['en', 'en', 'en'] | True |
test_timestamp_custom | (hass) | Test the timestamps to custom filter. | Test the timestamps to custom filter. | def test_timestamp_custom(hass):
"""Test the timestamps to custom filter."""
now = dt_util.utcnow()
tests = [
(None, None, None, None),
(1469119144, None, True, "2016-07-21 16:39:04"),
(1469119144, "%Y", True, 2016),
(1469119144, "invalid", True, "invalid"),
(dt_util.as_timestamp(now), None, False, now.strftime("%Y-%m-%d %H:%M:%S")),
]
for inp, fmt, local, out in tests:
if fmt:
fil = f"timestamp_custom('{fmt}')"
elif fmt and local:
fil = f"timestamp_custom('{fmt}', {local})"
else:
fil = "timestamp_custom"
assert template.Template(f"{{{{ {inp} | {fil} }}}}", hass).async_render() == out | [
"def",
"test_timestamp_custom",
"(",
"hass",
")",
":",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"tests",
"=",
"[",
"(",
"None",
",",
"None",
",",
"None",
",",
"None",
")",
",",
"(",
"1469119144",
",",
"None",
",",
"True",
",",
"\"2016-07-21 16:39:04\"",
")",
",",
"(",
"1469119144",
",",
"\"%Y\"",
",",
"True",
",",
"2016",
")",
",",
"(",
"1469119144",
",",
"\"invalid\"",
",",
"True",
",",
"\"invalid\"",
")",
",",
"(",
"dt_util",
".",
"as_timestamp",
"(",
"now",
")",
",",
"None",
",",
"False",
",",
"now",
".",
"strftime",
"(",
"\"%Y-%m-%d %H:%M:%S\"",
")",
")",
",",
"]",
"for",
"inp",
",",
"fmt",
",",
"local",
",",
"out",
"in",
"tests",
":",
"if",
"fmt",
":",
"fil",
"=",
"f\"timestamp_custom('{fmt}')\"",
"elif",
"fmt",
"and",
"local",
":",
"fil",
"=",
"f\"timestamp_custom('{fmt}', {local})\"",
"else",
":",
"fil",
"=",
"\"timestamp_custom\"",
"assert",
"template",
".",
"Template",
"(",
"f\"{{{{ {inp} | {fil} }}}}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"out"
] | [
495,
0
] | [
514,
88
] | python | en | ['en', 'en', 'en'] | True |
test_timestamp_local | (hass) | Test the timestamps to local filter. | Test the timestamps to local filter. | def test_timestamp_local(hass):
"""Test the timestamps to local filter."""
tests = {None: None, 1469119144: "2016-07-21 16:39:04"}
for inp, out in tests.items():
assert (
template.Template("{{ %s | timestamp_local }}" % inp, hass).async_render()
== out
) | [
"def",
"test_timestamp_local",
"(",
"hass",
")",
":",
"tests",
"=",
"{",
"None",
":",
"None",
",",
"1469119144",
":",
"\"2016-07-21 16:39:04\"",
"}",
"for",
"inp",
",",
"out",
"in",
"tests",
".",
"items",
"(",
")",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | timestamp_local }}\"",
"%",
"inp",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"out",
")"
] | [
517,
0
] | [
525,
9
] | python | en | ['en', 'en', 'en'] | True |
test_as_local | (hass) | Test converting time to local. | Test converting time to local. | def test_as_local(hass):
"""Test converting time to local."""
hass.states.async_set("test.object", "available")
last_updated = hass.states.get("test.object").last_updated
assert template.Template(
"{{ as_local(states.test.object.last_updated) }}", hass
).async_render() == str(dt_util.as_local(last_updated))
assert template.Template(
"{{ states.test.object.last_updated | as_local }}", hass
).async_render() == str(dt_util.as_local(last_updated)) | [
"def",
"test_as_local",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"available\"",
")",
"last_updated",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"test.object\"",
")",
".",
"last_updated",
"assert",
"template",
".",
"Template",
"(",
"\"{{ as_local(states.test.object.last_updated) }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"str",
"(",
"dt_util",
".",
"as_local",
"(",
"last_updated",
")",
")",
"assert",
"template",
".",
"Template",
"(",
"\"{{ states.test.object.last_updated | as_local }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"str",
"(",
"dt_util",
".",
"as_local",
"(",
"last_updated",
")",
")"
] | [
528,
0
] | [
538,
59
] | python | en | ['en', 'en', 'en'] | True |
test_to_json | (hass) | Test the object to JSON string filter. | Test the object to JSON string filter. | def test_to_json(hass):
"""Test the object to JSON string filter."""
# Note that we're not testing the actual json.loads and json.dumps methods,
# only the filters, so we don't need to be exhaustive with our sample JSON.
expected_result = {"Foo": "Bar"}
actual_result = template.Template(
"{{ {'Foo': 'Bar'} | to_json }}", hass
).async_render()
assert actual_result == expected_result | [
"def",
"test_to_json",
"(",
"hass",
")",
":",
"# Note that we're not testing the actual json.loads and json.dumps methods,",
"# only the filters, so we don't need to be exhaustive with our sample JSON.",
"expected_result",
"=",
"{",
"\"Foo\"",
":",
"\"Bar\"",
"}",
"actual_result",
"=",
"template",
".",
"Template",
"(",
"\"{{ {'Foo': 'Bar'} | to_json }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"assert",
"actual_result",
"==",
"expected_result"
] | [
541,
0
] | [
550,
43
] | python | en | ['en', 'en', 'en'] | True |
test_from_json | (hass) | Test the JSON string to object filter. | Test the JSON string to object filter. | def test_from_json(hass):
"""Test the JSON string to object filter."""
# Note that we're not testing the actual json.loads and json.dumps methods,
# only the filters, so we don't need to be exhaustive with our sample JSON.
expected_result = "Bar"
actual_result = template.Template(
'{{ (\'{"Foo": "Bar"}\' | from_json).Foo }}', hass
).async_render()
assert actual_result == expected_result | [
"def",
"test_from_json",
"(",
"hass",
")",
":",
"# Note that we're not testing the actual json.loads and json.dumps methods,",
"# only the filters, so we don't need to be exhaustive with our sample JSON.",
"expected_result",
"=",
"\"Bar\"",
"actual_result",
"=",
"template",
".",
"Template",
"(",
"'{{ (\\'{\"Foo\": \"Bar\"}\\' | from_json).Foo }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"assert",
"actual_result",
"==",
"expected_result"
] | [
553,
0
] | [
562,
43
] | python | en | ['en', 'en', 'en'] | True |
test_min | (hass) | Test the min filter. | Test the min filter. | def test_min(hass):
"""Test the min filter."""
assert template.Template("{{ [1, 2, 3] | min }}", hass).async_render() == 1 | [
"def",
"test_min",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"Template",
"(",
"\"{{ [1, 2, 3] | min }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"1"
] | [
565,
0
] | [
567,
79
] | python | en | ['en', 'en', 'en'] | True |
test_max | (hass) | Test the max filter. | Test the max filter. | def test_max(hass):
"""Test the max filter."""
assert template.Template("{{ [1, 2, 3] | max }}", hass).async_render() == 3 | [
"def",
"test_max",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"Template",
"(",
"\"{{ [1, 2, 3] | max }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"3"
] | [
570,
0
] | [
572,
79
] | python | en | ['en', 'en', 'en'] | True |
test_ord | (hass) | Test the ord filter. | Test the ord filter. | def test_ord(hass):
"""Test the ord filter."""
assert template.Template('{{ "d" | ord }}', hass).async_render() == 100 | [
"def",
"test_ord",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"Template",
"(",
"'{{ \"d\" | ord }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"100"
] | [
575,
0
] | [
577,
75
] | python | en | ['en', 'en', 'en'] | True |
test_base64_encode | (hass) | Test the base64_encode filter. | Test the base64_encode filter. | def test_base64_encode(hass):
"""Test the base64_encode filter."""
assert (
template.Template('{{ "homeassistant" | base64_encode }}', hass).async_render()
== "aG9tZWFzc2lzdGFudA=="
) | [
"def",
"test_base64_encode",
"(",
"hass",
")",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ \"homeassistant\" | base64_encode }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"aG9tZWFzc2lzdGFudA==\"",
")"
] | [
580,
0
] | [
585,
5
] | python | en | ['en', 'da', 'en'] | True |
test_base64_decode | (hass) | Test the base64_decode filter. | Test the base64_decode filter. | def test_base64_decode(hass):
"""Test the base64_decode filter."""
assert (
template.Template(
'{{ "aG9tZWFzc2lzdGFudA==" | base64_decode }}', hass
).async_render()
== "homeassistant"
) | [
"def",
"test_base64_decode",
"(",
"hass",
")",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ \"aG9tZWFzc2lzdGFudA==\" | base64_decode }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"homeassistant\"",
")"
] | [
588,
0
] | [
595,
5
] | python | en | ['en', 'en', 'en'] | True |
test_ordinal | (hass) | Test the ordinal filter. | Test the ordinal filter. | def test_ordinal(hass):
"""Test the ordinal filter."""
tests = [
(1, "1st"),
(2, "2nd"),
(3, "3rd"),
(4, "4th"),
(5, "5th"),
(12, "12th"),
(100, "100th"),
(101, "101st"),
]
for value, expected in tests:
assert (
template.Template("{{ %s | ordinal }}" % value, hass).async_render()
== expected
) | [
"def",
"test_ordinal",
"(",
"hass",
")",
":",
"tests",
"=",
"[",
"(",
"1",
",",
"\"1st\"",
")",
",",
"(",
"2",
",",
"\"2nd\"",
")",
",",
"(",
"3",
",",
"\"3rd\"",
")",
",",
"(",
"4",
",",
"\"4th\"",
")",
",",
"(",
"5",
",",
"\"5th\"",
")",
",",
"(",
"12",
",",
"\"12th\"",
")",
",",
"(",
"100",
",",
"\"100th\"",
")",
",",
"(",
"101",
",",
"\"101st\"",
")",
",",
"]",
"for",
"value",
",",
"expected",
"in",
"tests",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | ordinal }}\"",
"%",
"value",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"expected",
")"
] | [
598,
0
] | [
615,
9
] | python | en | ['en', 'su', 'en'] | True |
test_timestamp_utc | (hass) | Test the timestamps to local filter. | Test the timestamps to local filter. | def test_timestamp_utc(hass):
"""Test the timestamps to local filter."""
now = dt_util.utcnow()
tests = {
None: None,
1469119144: "2016-07-21 16:39:04",
dt_util.as_timestamp(now): now.strftime("%Y-%m-%d %H:%M:%S"),
}
for inp, out in tests.items():
assert (
template.Template("{{ %s | timestamp_utc }}" % inp, hass).async_render()
== out
) | [
"def",
"test_timestamp_utc",
"(",
"hass",
")",
":",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"tests",
"=",
"{",
"None",
":",
"None",
",",
"1469119144",
":",
"\"2016-07-21 16:39:04\"",
",",
"dt_util",
".",
"as_timestamp",
"(",
"now",
")",
":",
"now",
".",
"strftime",
"(",
"\"%Y-%m-%d %H:%M:%S\"",
")",
",",
"}",
"for",
"inp",
",",
"out",
"in",
"tests",
".",
"items",
"(",
")",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ %s | timestamp_utc }}\"",
"%",
"inp",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"out",
")"
] | [
618,
0
] | [
631,
9
] | python | en | ['en', 'en', 'en'] | True |
test_as_timestamp | (hass) | Test the as_timestamp function. | Test the as_timestamp function. | def test_as_timestamp(hass):
"""Test the as_timestamp function."""
assert (
template.Template('{{ as_timestamp("invalid") }}', hass).async_render() is None
)
hass.mock = None
assert (
template.Template("{{ as_timestamp(states.mock) }}", hass).async_render()
is None
)
tpl = (
'{{ as_timestamp(strptime("2024-02-03T09:10:24+0000", '
'"%Y-%m-%dT%H:%M:%S%z")) }}'
)
assert template.Template(tpl, hass).async_render() == 1706951424.0 | [
"def",
"test_as_timestamp",
"(",
"hass",
")",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ as_timestamp(\"invalid\") }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"is",
"None",
")",
"hass",
".",
"mock",
"=",
"None",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ as_timestamp(states.mock) }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"is",
"None",
")",
"tpl",
"=",
"(",
"'{{ as_timestamp(strptime(\"2024-02-03T09:10:24+0000\", '",
"'\"%Y-%m-%dT%H:%M:%S%z\")) }}'",
")",
"assert",
"template",
".",
"Template",
"(",
"tpl",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"1706951424.0"
] | [
634,
0
] | [
649,
70
] | python | en | ['en', 'en', 'en'] | True |
test_random_every_time | (test_choice, hass) | Ensure the random filter runs every time, not just once. | Ensure the random filter runs every time, not just once. | def test_random_every_time(test_choice, hass):
"""Ensure the random filter runs every time, not just once."""
tpl = template.Template("{{ [1,2] | random }}", hass)
test_choice.return_value = "foo"
assert tpl.async_render() == "foo"
test_choice.return_value = "bar"
assert tpl.async_render() == "bar" | [
"def",
"test_random_every_time",
"(",
"test_choice",
",",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ [1,2] | random }}\"",
",",
"hass",
")",
"test_choice",
".",
"return_value",
"=",
"\"foo\"",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"foo\"",
"test_choice",
".",
"return_value",
"=",
"\"bar\"",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"bar\""
] | [
653,
0
] | [
659,
38
] | python | en | ['en', 'en', 'en'] | True |
test_passing_vars_as_keywords | (hass) | Test passing variables as keywords. | Test passing variables as keywords. | def test_passing_vars_as_keywords(hass):
"""Test passing variables as keywords."""
assert template.Template("{{ hello }}", hass).async_render(hello=127) == 127 | [
"def",
"test_passing_vars_as_keywords",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"Template",
"(",
"\"{{ hello }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
"hello",
"=",
"127",
")",
"==",
"127"
] | [
662,
0
] | [
664,
80
] | python | en | ['en', 'en', 'en'] | True |
test_passing_vars_as_vars | (hass) | Test passing variables as variables. | Test passing variables as variables. | def test_passing_vars_as_vars(hass):
"""Test passing variables as variables."""
assert template.Template("{{ hello }}", hass).async_render({"hello": 127}) == 127 | [
"def",
"test_passing_vars_as_vars",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"Template",
"(",
"\"{{ hello }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
"{",
"\"hello\"",
":",
"127",
"}",
")",
"==",
"127"
] | [
667,
0
] | [
669,
85
] | python | en | ['en', 'en', 'en'] | True |
test_passing_vars_as_list | (hass) | Test passing variables as list. | Test passing variables as list. | def test_passing_vars_as_list(hass):
"""Test passing variables as list."""
assert template.render_complex(
template.Template("{{ hello }}", hass), {"hello": ["foo", "bar"]}
) == ["foo", "bar"] | [
"def",
"test_passing_vars_as_list",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"render_complex",
"(",
"template",
".",
"Template",
"(",
"\"{{ hello }}\"",
",",
"hass",
")",
",",
"{",
"\"hello\"",
":",
"[",
"\"foo\"",
",",
"\"bar\"",
"]",
"}",
")",
"==",
"[",
"\"foo\"",
",",
"\"bar\"",
"]"
] | [
672,
0
] | [
676,
23
] | python | en | ['en', 'en', 'en'] | True |
test_passing_vars_as_list_element | (hass) | Test passing variables as list. | Test passing variables as list. | def test_passing_vars_as_list_element(hass):
"""Test passing variables as list."""
assert (
template.render_complex(
template.Template("{{ hello[1] }}", hass), {"hello": ["foo", "bar"]}
)
== "bar"
) | [
"def",
"test_passing_vars_as_list_element",
"(",
"hass",
")",
":",
"assert",
"(",
"template",
".",
"render_complex",
"(",
"template",
".",
"Template",
"(",
"\"{{ hello[1] }}\"",
",",
"hass",
")",
",",
"{",
"\"hello\"",
":",
"[",
"\"foo\"",
",",
"\"bar\"",
"]",
"}",
")",
"==",
"\"bar\"",
")"
] | [
679,
0
] | [
686,
5
] | python | en | ['en', 'en', 'en'] | True |
test_passing_vars_as_dict_element | (hass) | Test passing variables as list. | Test passing variables as list. | def test_passing_vars_as_dict_element(hass):
"""Test passing variables as list."""
assert (
template.render_complex(
template.Template("{{ hello.foo }}", hass), {"hello": {"foo": "bar"}}
)
== "bar"
) | [
"def",
"test_passing_vars_as_dict_element",
"(",
"hass",
")",
":",
"assert",
"(",
"template",
".",
"render_complex",
"(",
"template",
".",
"Template",
"(",
"\"{{ hello.foo }}\"",
",",
"hass",
")",
",",
"{",
"\"hello\"",
":",
"{",
"\"foo\"",
":",
"\"bar\"",
"}",
"}",
")",
"==",
"\"bar\"",
")"
] | [
689,
0
] | [
696,
5
] | python | en | ['en', 'en', 'en'] | True |
test_passing_vars_as_dict | (hass) | Test passing variables as list. | Test passing variables as list. | def test_passing_vars_as_dict(hass):
"""Test passing variables as list."""
assert template.render_complex(
template.Template("{{ hello }}", hass), {"hello": {"foo": "bar"}}
) == {"foo": "bar"} | [
"def",
"test_passing_vars_as_dict",
"(",
"hass",
")",
":",
"assert",
"template",
".",
"render_complex",
"(",
"template",
".",
"Template",
"(",
"\"{{ hello }}\"",
",",
"hass",
")",
",",
"{",
"\"hello\"",
":",
"{",
"\"foo\"",
":",
"\"bar\"",
"}",
"}",
")",
"==",
"{",
"\"foo\"",
":",
"\"bar\"",
"}"
] | [
699,
0
] | [
703,
23
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_with_valid_json | (hass) | Render with possible JSON value with valid JSON. | Render with possible JSON value with valid JSON. | def test_render_with_possible_json_value_with_valid_json(hass):
"""Render with possible JSON value with valid JSON."""
tpl = template.Template("{{ value_json.hello }}", hass)
assert tpl.async_render_with_possible_json_value('{"hello": "world"}') == "world" | [
"def",
"test_render_with_possible_json_value_with_valid_json",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ value_json.hello }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"'{\"hello\": \"world\"}'",
")",
"==",
"\"world\""
] | [
706,
0
] | [
709,
85
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_with_invalid_json | (hass) | Render with possible JSON value with invalid JSON. | Render with possible JSON value with invalid JSON. | def test_render_with_possible_json_value_with_invalid_json(hass):
"""Render with possible JSON value with invalid JSON."""
tpl = template.Template("{{ value_json }}", hass)
assert tpl.async_render_with_possible_json_value("{ I AM NOT JSON }") == "" | [
"def",
"test_render_with_possible_json_value_with_invalid_json",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ value_json }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"\"{ I AM NOT JSON }\"",
")",
"==",
"\"\""
] | [
712,
0
] | [
715,
79
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_with_template_error_value | (hass) | Render with possible JSON value with template error value. | Render with possible JSON value with template error value. | def test_render_with_possible_json_value_with_template_error_value(hass):
"""Render with possible JSON value with template error value."""
tpl = template.Template("{{ non_existing.variable }}", hass)
assert tpl.async_render_with_possible_json_value("hello", "-") == "-" | [
"def",
"test_render_with_possible_json_value_with_template_error_value",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ non_existing.variable }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"\"hello\"",
",",
"\"-\"",
")",
"==",
"\"-\""
] | [
718,
0
] | [
721,
73
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_with_missing_json_value | (hass) | Render with possible JSON value with unknown JSON object. | Render with possible JSON value with unknown JSON object. | def test_render_with_possible_json_value_with_missing_json_value(hass):
"""Render with possible JSON value with unknown JSON object."""
tpl = template.Template("{{ value_json.goodbye }}", hass)
assert tpl.async_render_with_possible_json_value('{"hello": "world"}') == "" | [
"def",
"test_render_with_possible_json_value_with_missing_json_value",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ value_json.goodbye }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"'{\"hello\": \"world\"}'",
")",
"==",
"\"\""
] | [
724,
0
] | [
727,
80
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_valid_with_is_defined | (hass) | Render with possible JSON value with known JSON object. | Render with possible JSON value with known JSON object. | def test_render_with_possible_json_value_valid_with_is_defined(hass):
"""Render with possible JSON value with known JSON object."""
tpl = template.Template("{{ value_json.hello|is_defined }}", hass)
assert tpl.async_render_with_possible_json_value('{"hello": "world"}') == "world" | [
"def",
"test_render_with_possible_json_value_valid_with_is_defined",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ value_json.hello|is_defined }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"'{\"hello\": \"world\"}'",
")",
"==",
"\"world\""
] | [
730,
0
] | [
733,
85
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_undefined_json | (hass) | Render with possible JSON value with unknown JSON object. | Render with possible JSON value with unknown JSON object. | def test_render_with_possible_json_value_undefined_json(hass):
"""Render with possible JSON value with unknown JSON object."""
tpl = template.Template("{{ value_json.bye|is_defined }}", hass)
assert (
tpl.async_render_with_possible_json_value('{"hello": "world"}')
== '{"hello": "world"}'
) | [
"def",
"test_render_with_possible_json_value_undefined_json",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ value_json.bye|is_defined }}\"",
",",
"hass",
")",
"assert",
"(",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"'{\"hello\": \"world\"}'",
")",
"==",
"'{\"hello\": \"world\"}'",
")"
] | [
736,
0
] | [
742,
5
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_undefined_json_error_value | (hass) | Render with possible JSON value with unknown JSON object. | Render with possible JSON value with unknown JSON object. | def test_render_with_possible_json_value_undefined_json_error_value(hass):
"""Render with possible JSON value with unknown JSON object."""
tpl = template.Template("{{ value_json.bye|is_defined }}", hass)
assert tpl.async_render_with_possible_json_value('{"hello": "world"}', "") == "" | [
"def",
"test_render_with_possible_json_value_undefined_json_error_value",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ value_json.bye|is_defined }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"'{\"hello\": \"world\"}'",
",",
"\"\"",
")",
"==",
"\"\""
] | [
745,
0
] | [
748,
84
] | python | en | ['en', 'en', 'en'] | True |
test_render_with_possible_json_value_non_string_value | (hass) | Render with possible JSON value with non-string value. | Render with possible JSON value with non-string value. | def test_render_with_possible_json_value_non_string_value(hass):
"""Render with possible JSON value with non-string value."""
tpl = template.Template(
"""
{{ strptime(value~'+0000', '%Y-%m-%d %H:%M:%S%z') }}
""",
hass,
)
value = datetime(2019, 1, 18, 12, 13, 14)
expected = str(pytz.utc.localize(value))
assert tpl.async_render_with_possible_json_value(value) == expected | [
"def",
"test_render_with_possible_json_value_non_string_value",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ strptime(value~'+0000', '%Y-%m-%d %H:%M:%S%z') }}\n \"\"\"",
",",
"hass",
",",
")",
"value",
"=",
"datetime",
"(",
"2019",
",",
"1",
",",
"18",
",",
"12",
",",
"13",
",",
"14",
")",
"expected",
"=",
"str",
"(",
"pytz",
".",
"utc",
".",
"localize",
"(",
"value",
")",
")",
"assert",
"tpl",
".",
"async_render_with_possible_json_value",
"(",
"value",
")",
"==",
"expected"
] | [
751,
0
] | [
761,
71
] | python | en | ['en', 'en', 'en'] | True |
test_if_state_exists | (hass) | Test if state exists works. | Test if state exists works. | def test_if_state_exists(hass):
"""Test if state exists works."""
hass.states.async_set("test.object", "available")
tpl = template.Template(
"{% if states.test.object %}exists{% else %}not exists{% endif %}", hass
)
assert tpl.async_render() == "exists" | [
"def",
"test_if_state_exists",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"available\"",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{% if states.test.object %}exists{% else %}not exists{% endif %}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"exists\""
] | [
764,
0
] | [
770,
41
] | python | en | ['en', 'en', 'en'] | True |
test_is_state | (hass) | Test is_state method. | Test is_state method. | def test_is_state(hass):
"""Test is_state method."""
hass.states.async_set("test.object", "available")
tpl = template.Template(
"""
{% if is_state("test.object", "available") %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
tpl = template.Template(
"""
{{ is_state("test.noobject", "available") }}
""",
hass,
)
assert tpl.async_render() is False | [
"def",
"test_is_state",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"available\"",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{% if is_state(\"test.object\", \"available\") %}yes{% else %}no{% endif %}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"yes\"",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ is_state(\"test.noobject\", \"available\") }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"False"
] | [
773,
0
] | [
790,
38
] | python | en | ['en', 'en', 'en'] | True |
test_is_state_attr | (hass) | Test is_state_attr method. | Test is_state_attr method. | def test_is_state_attr(hass):
"""Test is_state_attr method."""
hass.states.async_set("test.object", "available", {"mode": "on"})
tpl = template.Template(
"""
{% if is_state_attr("test.object", "mode", "on") %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
tpl = template.Template(
"""
{{ is_state_attr("test.noobject", "mode", "on") }}
""",
hass,
)
assert tpl.async_render() is False | [
"def",
"test_is_state_attr",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"available\"",
",",
"{",
"\"mode\"",
":",
"\"on\"",
"}",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{% if is_state_attr(\"test.object\", \"mode\", \"on\") %}yes{% else %}no{% endif %}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"yes\"",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ is_state_attr(\"test.noobject\", \"mode\", \"on\") }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"False"
] | [
793,
0
] | [
810,
38
] | python | en | ['en', 'en', 'en'] | True |
test_state_attr | (hass) | Test state_attr method. | Test state_attr method. | def test_state_attr(hass):
"""Test state_attr method."""
hass.states.async_set("test.object", "available", {"mode": "on"})
tpl = template.Template(
"""
{% if state_attr("test.object", "mode") == "on" %}yes{% else %}no{% endif %}
""",
hass,
)
assert tpl.async_render() == "yes"
tpl = template.Template(
"""
{{ state_attr("test.noobject", "mode") == None }}
""",
hass,
)
assert tpl.async_render() is True | [
"def",
"test_state_attr",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"available\"",
",",
"{",
"\"mode\"",
":",
"\"on\"",
"}",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{% if state_attr(\"test.object\", \"mode\") == \"on\" %}yes{% else %}no{% endif %}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"yes\"",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ state_attr(\"test.noobject\", \"mode\") == None }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True"
] | [
813,
0
] | [
830,
37
] | python | en | ['en', 'de', 'en'] | True |
test_states_function | (hass) | Test using states as a function. | Test using states as a function. | def test_states_function(hass):
"""Test using states as a function."""
hass.states.async_set("test.object", "available")
tpl = template.Template('{{ states("test.object") }}', hass)
assert tpl.async_render() == "available"
tpl2 = template.Template('{{ states("test.object2") }}', hass)
assert tpl2.async_render() == "unknown" | [
"def",
"test_states_function",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"available\"",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ states(\"test.object\") }}'",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"available\"",
"tpl2",
"=",
"template",
".",
"Template",
"(",
"'{{ states(\"test.object2\") }}'",
",",
"hass",
")",
"assert",
"tpl2",
".",
"async_render",
"(",
")",
"==",
"\"unknown\""
] | [
833,
0
] | [
840,
43
] | python | en | ['en', 'en', 'en'] | True |
test_now | (mock_is_safe, hass) | Test now method. | Test now method. | def test_now(mock_is_safe, hass):
"""Test now method."""
now = dt_util.now()
with patch("homeassistant.util.dt.now", return_value=now):
info = template.Template("{{ now().isoformat() }}", hass).async_render_to_info()
assert now.isoformat() == info.result()
assert info.has_time is True | [
"def",
"test_now",
"(",
"mock_is_safe",
",",
"hass",
")",
":",
"now",
"=",
"dt_util",
".",
"now",
"(",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.now\"",
",",
"return_value",
"=",
"now",
")",
":",
"info",
"=",
"template",
".",
"Template",
"(",
"\"{{ now().isoformat() }}\"",
",",
"hass",
")",
".",
"async_render_to_info",
"(",
")",
"assert",
"now",
".",
"isoformat",
"(",
")",
"==",
"info",
".",
"result",
"(",
")",
"assert",
"info",
".",
"has_time",
"is",
"True"
] | [
847,
0
] | [
854,
32
] | python | en | ['en', 'sr', 'en'] | True |
test_utcnow | (mock_is_safe, hass) | Test now method. | Test now method. | def test_utcnow(mock_is_safe, hass):
"""Test now method."""
utcnow = dt_util.utcnow()
with patch("homeassistant.util.dt.utcnow", return_value=utcnow):
info = template.Template(
"{{ utcnow().isoformat() }}", hass
).async_render_to_info()
assert utcnow.isoformat() == info.result()
assert info.has_time is True | [
"def",
"test_utcnow",
"(",
"mock_is_safe",
",",
"hass",
")",
":",
"utcnow",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"utcnow",
")",
":",
"info",
"=",
"template",
".",
"Template",
"(",
"\"{{ utcnow().isoformat() }}\"",
",",
"hass",
")",
".",
"async_render_to_info",
"(",
")",
"assert",
"utcnow",
".",
"isoformat",
"(",
")",
"==",
"info",
".",
"result",
"(",
")",
"assert",
"info",
".",
"has_time",
"is",
"True"
] | [
861,
0
] | [
870,
32
] | python | en | ['en', 'sr', 'en'] | True |
test_relative_time | (mock_is_safe, hass) | Test relative_time method. | Test relative_time method. | def test_relative_time(mock_is_safe, hass):
"""Test relative_time method."""
now = datetime.strptime("2000-01-01 10:00:00 +00:00", "%Y-%m-%d %H:%M:%S %z")
with patch("homeassistant.util.dt.now", return_value=now):
assert (
"1 hour"
== template.Template(
'{{relative_time(strptime("2000-01-01 09:00:00", "%Y-%m-%d %H:%M:%S"))}}',
hass,
).async_render()
)
assert (
"2 hours"
== template.Template(
'{{relative_time(strptime("2000-01-01 09:00:00 +01:00", "%Y-%m-%d %H:%M:%S %z"))}}',
hass,
).async_render()
)
assert (
"1 hour"
== template.Template(
'{{relative_time(strptime("2000-01-01 03:00:00 -06:00", "%Y-%m-%d %H:%M:%S %z"))}}',
hass,
).async_render()
)
assert (
str(template.strptime("2000-01-01 11:00:00 +00:00", "%Y-%m-%d %H:%M:%S %z"))
== template.Template(
'{{relative_time(strptime("2000-01-01 11:00:00 +00:00", "%Y-%m-%d %H:%M:%S %z"))}}',
hass,
).async_render()
)
assert (
"string"
== template.Template(
'{{relative_time("string")}}',
hass,
).async_render()
) | [
"def",
"test_relative_time",
"(",
"mock_is_safe",
",",
"hass",
")",
":",
"now",
"=",
"datetime",
".",
"strptime",
"(",
"\"2000-01-01 10:00:00 +00:00\"",
",",
"\"%Y-%m-%d %H:%M:%S %z\"",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.now\"",
",",
"return_value",
"=",
"now",
")",
":",
"assert",
"(",
"\"1 hour\"",
"==",
"template",
".",
"Template",
"(",
"'{{relative_time(strptime(\"2000-01-01 09:00:00\", \"%Y-%m-%d %H:%M:%S\"))}}'",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"2 hours\"",
"==",
"template",
".",
"Template",
"(",
"'{{relative_time(strptime(\"2000-01-01 09:00:00 +01:00\", \"%Y-%m-%d %H:%M:%S %z\"))}}'",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"1 hour\"",
"==",
"template",
".",
"Template",
"(",
"'{{relative_time(strptime(\"2000-01-01 03:00:00 -06:00\", \"%Y-%m-%d %H:%M:%S %z\"))}}'",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"str",
"(",
"template",
".",
"strptime",
"(",
"\"2000-01-01 11:00:00 +00:00\"",
",",
"\"%Y-%m-%d %H:%M:%S %z\"",
")",
")",
"==",
"template",
".",
"Template",
"(",
"'{{relative_time(strptime(\"2000-01-01 11:00:00 +00:00\", \"%Y-%m-%d %H:%M:%S %z\"))}}'",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"string\"",
"==",
"template",
".",
"Template",
"(",
"'{{relative_time(\"string\")}}'",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")"
] | [
877,
0
] | [
915,
9
] | python | en | ['en', 'en', 'en'] | True |
test_timedelta | (mock_is_safe, hass) | Test relative_time method. | Test relative_time method. | def test_timedelta(mock_is_safe, hass):
"""Test relative_time method."""
now = datetime.strptime("2000-01-01 10:00:00 +00:00", "%Y-%m-%d %H:%M:%S %z")
with patch("homeassistant.util.dt.now", return_value=now):
assert (
"0:02:00"
== template.Template(
"{{timedelta(seconds=120)}}",
hass,
).async_render()
)
assert (
"1 day, 0:00:00"
== template.Template(
"{{timedelta(seconds=86400)}}",
hass,
).async_render()
)
assert (
"1 day, 4:00:00"
== template.Template(
"{{timedelta(days=1, hours=4)}}",
hass,
).async_render()
)
assert (
"1 hour"
== template.Template(
"{{relative_time(now() - timedelta(seconds=3600))}}",
hass,
).async_render()
)
assert (
"1 day"
== template.Template(
"{{relative_time(now() - timedelta(seconds=86400))}}",
hass,
).async_render()
)
assert (
"1 day"
== template.Template(
"{{relative_time(now() - timedelta(seconds=86401))}}",
hass,
).async_render()
)
assert (
"15 days"
== template.Template(
"{{relative_time(now() - timedelta(weeks=2, days=1))}}",
hass,
).async_render()
) | [
"def",
"test_timedelta",
"(",
"mock_is_safe",
",",
"hass",
")",
":",
"now",
"=",
"datetime",
".",
"strptime",
"(",
"\"2000-01-01 10:00:00 +00:00\"",
",",
"\"%Y-%m-%d %H:%M:%S %z\"",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.now\"",
",",
"return_value",
"=",
"now",
")",
":",
"assert",
"(",
"\"0:02:00\"",
"==",
"template",
".",
"Template",
"(",
"\"{{timedelta(seconds=120)}}\"",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"1 day, 0:00:00\"",
"==",
"template",
".",
"Template",
"(",
"\"{{timedelta(seconds=86400)}}\"",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"1 day, 4:00:00\"",
"==",
"template",
".",
"Template",
"(",
"\"{{timedelta(days=1, hours=4)}}\"",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"1 hour\"",
"==",
"template",
".",
"Template",
"(",
"\"{{relative_time(now() - timedelta(seconds=3600))}}\"",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"1 day\"",
"==",
"template",
".",
"Template",
"(",
"\"{{relative_time(now() - timedelta(seconds=86400))}}\"",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"1 day\"",
"==",
"template",
".",
"Template",
"(",
"\"{{relative_time(now() - timedelta(seconds=86401))}}\"",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")",
"assert",
"(",
"\"15 days\"",
"==",
"template",
".",
"Template",
"(",
"\"{{relative_time(now() - timedelta(weeks=2, days=1))}}\"",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
")"
] | [
922,
0
] | [
974,
9
] | python | en | ['en', 'en', 'en'] | True |
test_regex_match | (hass) | Test regex_match method. | Test regex_match method. | def test_regex_match(hass):
"""Test regex_match method."""
tpl = template.Template(
r"""
{{ '123-456-7890' | regex_match('(\\d{3})-(\\d{3})-(\\d{4})') }}
""",
hass,
)
assert tpl.async_render() is True
tpl = template.Template(
"""
{{ 'Home Assistant test' | regex_match('home', True) }}
""",
hass,
)
assert tpl.async_render() is True
tpl = template.Template(
"""
{{ 'Another Home Assistant test' | regex_match('Home') }}
""",
hass,
)
assert tpl.async_render() is False
tpl = template.Template(
"""
{{ ['Home Assistant test'] | regex_match('.*Assist') }}
""",
hass,
)
assert tpl.async_render() is True | [
"def",
"test_regex_match",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"r\"\"\"\n{{ '123-456-7890' | regex_match('(\\\\d{3})-(\\\\d{3})-(\\\\d{4})') }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 'Home Assistant test' | regex_match('home', True) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n {{ 'Another Home Assistant test' | regex_match('Home') }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"False",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ ['Home Assistant test'] | regex_match('.*Assist') }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True"
] | [
977,
0
] | [
1009,
37
] | python | de | ['de', 'de', 'en'] | True |
test_regex_search | (hass) | Test regex_search method. | Test regex_search method. | def test_regex_search(hass):
"""Test regex_search method."""
tpl = template.Template(
r"""
{{ '123-456-7890' | regex_search('(\\d{3})-(\\d{3})-(\\d{4})') }}
""",
hass,
)
assert tpl.async_render() is True
tpl = template.Template(
"""
{{ 'Home Assistant test' | regex_search('home', True) }}
""",
hass,
)
assert tpl.async_render() is True
tpl = template.Template(
"""
{{ 'Another Home Assistant test' | regex_search('Home') }}
""",
hass,
)
assert tpl.async_render() is True
tpl = template.Template(
"""
{{ ['Home Assistant test'] | regex_search('Assist') }}
""",
hass,
)
assert tpl.async_render() is True | [
"def",
"test_regex_search",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"r\"\"\"\n{{ '123-456-7890' | regex_search('(\\\\d{3})-(\\\\d{3})-(\\\\d{4})') }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 'Home Assistant test' | regex_search('home', True) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n {{ 'Another Home Assistant test' | regex_search('Home') }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ ['Home Assistant test'] | regex_search('Assist') }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"True"
] | [
1012,
0
] | [
1044,
37
] | python | cy | ['de', 'cy', 'en'] | False |
test_regex_replace | (hass) | Test regex_replace method. | Test regex_replace method. | def test_regex_replace(hass):
"""Test regex_replace method."""
tpl = template.Template(
r"""
{{ 'Hello World' | regex_replace('(Hello\\s)',) }}
""",
hass,
)
assert tpl.async_render() == "World"
tpl = template.Template(
"""
{{ ['Home hinderant test'] | regex_replace('hinder', 'Assist') }}
""",
hass,
)
assert tpl.async_render() == ["Home Assistant test"] | [
"def",
"test_regex_replace",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"r\"\"\"\n{{ 'Hello World' | regex_replace('(Hello\\\\s)',) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"World\"",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ ['Home hinderant test'] | regex_replace('hinder', 'Assist') }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"[",
"\"Home Assistant test\"",
"]"
] | [
1047,
0
] | [
1063,
56
] | python | en | ['nl', 'la', 'en'] | False |
test_regex_findall_index | (hass) | Test regex_findall_index method. | Test regex_findall_index method. | def test_regex_findall_index(hass):
"""Test regex_findall_index method."""
tpl = template.Template(
"""
{{ 'Flight from JFK to LHR' | regex_findall_index('([A-Z]{3})', 0) }}
""",
hass,
)
assert tpl.async_render() == "JFK"
tpl = template.Template(
"""
{{ 'Flight from JFK to LHR' | regex_findall_index('([A-Z]{3})', 1) }}
""",
hass,
)
assert tpl.async_render() == "LHR"
tpl = template.Template(
"""
{{ ['JFK', 'LHR'] | regex_findall_index('([A-Z]{3})', 1) }}
""",
hass,
)
assert tpl.async_render() == "LHR" | [
"def",
"test_regex_findall_index",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 'Flight from JFK to LHR' | regex_findall_index('([A-Z]{3})', 0) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"JFK\"",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 'Flight from JFK to LHR' | regex_findall_index('([A-Z]{3})', 1) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"LHR\"",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ ['JFK', 'LHR'] | regex_findall_index('([A-Z]{3})', 1) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"LHR\""
] | [
1066,
0
] | [
1090,
38
] | python | de | ['nl', 'de', 'en'] | False |
test_bitwise_and | (hass) | Test bitwise_and method. | Test bitwise_and method. | def test_bitwise_and(hass):
"""Test bitwise_and method."""
tpl = template.Template(
"""
{{ 8 | bitwise_and(8) }}
""",
hass,
)
assert tpl.async_render() == 8 & 8
tpl = template.Template(
"""
{{ 10 | bitwise_and(2) }}
""",
hass,
)
assert tpl.async_render() == 10 & 2
tpl = template.Template(
"""
{{ 8 | bitwise_and(2) }}
""",
hass,
)
assert tpl.async_render() == 8 & 2 | [
"def",
"test_bitwise_and",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 8 | bitwise_and(8) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"8",
"&",
"8",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 10 | bitwise_and(2) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"10",
"&",
"2",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 8 | bitwise_and(2) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"8",
"&",
"2"
] | [
1093,
0
] | [
1115,
38
] | python | en | ['en', 'en', 'en'] | True |
test_bitwise_or | (hass) | Test bitwise_or method. | Test bitwise_or method. | def test_bitwise_or(hass):
"""Test bitwise_or method."""
tpl = template.Template(
"""
{{ 8 | bitwise_or(8) }}
""",
hass,
)
assert tpl.async_render() == 8 | 8
tpl = template.Template(
"""
{{ 10 | bitwise_or(2) }}
""",
hass,
)
assert tpl.async_render() == 10 | 2
tpl = template.Template(
"""
{{ 8 | bitwise_or(2) }}
""",
hass,
)
assert tpl.async_render() == 8 | 2 | [
"def",
"test_bitwise_or",
"(",
"hass",
")",
":",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 8 | bitwise_or(8) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"8",
"|",
"8",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 10 | bitwise_or(2) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"10",
"|",
"2",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"\"\"\n{{ 8 | bitwise_or(2) }}\n \"\"\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"8",
"|",
"2"
] | [
1118,
0
] | [
1140,
38
] | python | en | ['en', 'en', 'en'] | True |
test_distance_function_with_1_state | (hass) | Test distance function with 1 state. | Test distance function with 1 state. | def test_distance_function_with_1_state(hass):
"""Test distance function with 1 state."""
_set_up_units(hass)
hass.states.async_set(
"test.object", "happy", {"latitude": 32.87336, "longitude": -117.22943}
)
tpl = template.Template("{{ distance(states.test.object) | round }}", hass)
assert tpl.async_render() == 187 | [
"def",
"test_distance_function_with_1_state",
"(",
"hass",
")",
":",
"_set_up_units",
"(",
"hass",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"32.87336",
",",
"\"longitude\"",
":",
"-",
"117.22943",
"}",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ distance(states.test.object) | round }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"187"
] | [
1143,
0
] | [
1150,
36
] | python | en | ['en', 'en', 'en'] | True |
test_distance_function_with_2_states | (hass) | Test distance function with 2 states. | Test distance function with 2 states. | def test_distance_function_with_2_states(hass):
"""Test distance function with 2 states."""
_set_up_units(hass)
hass.states.async_set(
"test.object", "happy", {"latitude": 32.87336, "longitude": -117.22943}
)
hass.states.async_set(
"test.object_2",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
tpl = template.Template(
"{{ distance(states.test.object, states.test.object_2) | round }}", hass
)
assert tpl.async_render() == 187 | [
"def",
"test_distance_function_with_2_states",
"(",
"hass",
")",
":",
"_set_up_units",
"(",
"hass",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"32.87336",
",",
"\"longitude\"",
":",
"-",
"117.22943",
"}",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object_2\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ distance(states.test.object, states.test.object_2) | round }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"187"
] | [
1153,
0
] | [
1167,
36
] | python | en | ['en', 'en', 'en'] | True |
test_distance_function_with_1_coord | (hass) | Test distance function with 1 coord. | Test distance function with 1 coord. | def test_distance_function_with_1_coord(hass):
"""Test distance function with 1 coord."""
_set_up_units(hass)
tpl = template.Template('{{ distance("32.87336", "-117.22943") | round }}', hass)
assert tpl.async_render() == 187 | [
"def",
"test_distance_function_with_1_coord",
"(",
"hass",
")",
":",
"_set_up_units",
"(",
"hass",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ distance(\"32.87336\", \"-117.22943\") | round }}'",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"187"
] | [
1170,
0
] | [
1174,
36
] | python | en | ['en', 'en', 'en'] | True |
test_distance_function_with_2_coords | (hass) | Test distance function with 2 coords. | Test distance function with 2 coords. | def test_distance_function_with_2_coords(hass):
"""Test distance function with 2 coords."""
_set_up_units(hass)
assert (
template.Template(
'{{ distance("32.87336", "-117.22943", %s, %s) | round }}'
% (hass.config.latitude, hass.config.longitude),
hass,
).async_render()
== 187
) | [
"def",
"test_distance_function_with_2_coords",
"(",
"hass",
")",
":",
"_set_up_units",
"(",
"hass",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ distance(\"32.87336\", \"-117.22943\", %s, %s) | round }}'",
"%",
"(",
"hass",
".",
"config",
".",
"latitude",
",",
"hass",
".",
"config",
".",
"longitude",
")",
",",
"hass",
",",
")",
".",
"async_render",
"(",
")",
"==",
"187",
")"
] | [
1177,
0
] | [
1187,
5
] | python | en | ['en', 'en', 'en'] | True |
test_distance_function_with_1_state_1_coord | (hass) | Test distance function with 1 state 1 coord. | Test distance function with 1 state 1 coord. | def test_distance_function_with_1_state_1_coord(hass):
"""Test distance function with 1 state 1 coord."""
_set_up_units(hass)
hass.states.async_set(
"test.object_2",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
tpl = template.Template(
'{{ distance("32.87336", "-117.22943", states.test.object_2) ' "| round }}",
hass,
)
assert tpl.async_render() == 187
tpl2 = template.Template(
'{{ distance(states.test.object_2, "32.87336", "-117.22943") ' "| round }}",
hass,
)
assert tpl2.async_render() == 187 | [
"def",
"test_distance_function_with_1_state_1_coord",
"(",
"hass",
")",
":",
"_set_up_units",
"(",
"hass",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object_2\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ distance(\"32.87336\", \"-117.22943\", states.test.object_2) '",
"\"| round }}\"",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"187",
"tpl2",
"=",
"template",
".",
"Template",
"(",
"'{{ distance(states.test.object_2, \"32.87336\", \"-117.22943\") '",
"\"| round }}\"",
",",
"hass",
",",
")",
"assert",
"tpl2",
".",
"async_render",
"(",
")",
"==",
"187"
] | [
1190,
0
] | [
1208,
37
] | python | en | ['en', 'en', 'en'] | True |
test_distance_function_return_none_if_invalid_state | (hass) | Test distance function return None if invalid state. | Test distance function return None if invalid state. | def test_distance_function_return_none_if_invalid_state(hass):
"""Test distance function return None if invalid state."""
hass.states.async_set("test.object_2", "happy", {"latitude": 10})
tpl = template.Template("{{ distance(states.test.object_2) | round }}", hass)
assert tpl.async_render() is None | [
"def",
"test_distance_function_return_none_if_invalid_state",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object_2\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"10",
"}",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"\"{{ distance(states.test.object_2) | round }}\"",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"None"
] | [
1211,
0
] | [
1215,
37
] | python | en | ['fr', 'en', 'en'] | True |
test_distance_function_return_none_if_invalid_coord | (hass) | Test distance function return None if invalid coord. | Test distance function return None if invalid coord. | def test_distance_function_return_none_if_invalid_coord(hass):
"""Test distance function return None if invalid coord."""
assert (
template.Template('{{ distance("123", "abc") }}', hass).async_render() is None
)
assert template.Template('{{ distance("123") }}', hass).async_render() is None
hass.states.async_set(
"test.object_2",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
tpl = template.Template('{{ distance("123", states.test_object_2) }}', hass)
assert tpl.async_render() is None | [
"def",
"test_distance_function_return_none_if_invalid_coord",
"(",
"hass",
")",
":",
"assert",
"(",
"template",
".",
"Template",
"(",
"'{{ distance(\"123\", \"abc\") }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"is",
"None",
")",
"assert",
"template",
".",
"Template",
"(",
"'{{ distance(\"123\") }}'",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"is",
"None",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object_2\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ distance(\"123\", states.test_object_2) }}'",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"is",
"None"
] | [
1218,
0
] | [
1232,
37
] | python | en | ['fr', 'en', 'en'] | True |
test_distance_function_with_2_entity_ids | (hass) | Test distance function with 2 entity ids. | Test distance function with 2 entity ids. | def test_distance_function_with_2_entity_ids(hass):
"""Test distance function with 2 entity ids."""
_set_up_units(hass)
hass.states.async_set(
"test.object", "happy", {"latitude": 32.87336, "longitude": -117.22943}
)
hass.states.async_set(
"test.object_2",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
tpl = template.Template(
'{{ distance("test.object", "test.object_2") | round }}', hass
)
assert tpl.async_render() == 187 | [
"def",
"test_distance_function_with_2_entity_ids",
"(",
"hass",
")",
":",
"_set_up_units",
"(",
"hass",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"32.87336",
",",
"\"longitude\"",
":",
"-",
"117.22943",
"}",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object_2\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ distance(\"test.object\", \"test.object_2\") | round }}'",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"187"
] | [
1235,
0
] | [
1249,
36
] | python | en | ['en', 'en', 'en'] | True |
test_distance_function_with_1_entity_1_coord | (hass) | Test distance function with 1 entity_id and 1 coord. | Test distance function with 1 entity_id and 1 coord. | def test_distance_function_with_1_entity_1_coord(hass):
"""Test distance function with 1 entity_id and 1 coord."""
_set_up_units(hass)
hass.states.async_set(
"test.object",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
tpl = template.Template(
'{{ distance("test.object", "32.87336", "-117.22943") | round }}', hass
)
assert tpl.async_render() == 187 | [
"def",
"test_distance_function_with_1_entity_1_coord",
"(",
"hass",
")",
":",
"_set_up_units",
"(",
"hass",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ distance(\"test.object\", \"32.87336\", \"-117.22943\") | round }}'",
",",
"hass",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"187"
] | [
1252,
0
] | [
1263,
36
] | python | en | ['en', 'en', 'en'] | True |
test_closest_function_home_vs_domain | (hass) | Test closest function home vs domain. | Test closest function home vs domain. | def test_closest_function_home_vs_domain(hass):
"""Test closest function home vs domain."""
hass.states.async_set(
"test_domain.object",
"happy",
{
"latitude": hass.config.latitude + 0.1,
"longitude": hass.config.longitude + 0.1,
},
)
hass.states.async_set(
"not_test_domain.but_closer",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
assert (
template.Template(
"{{ closest(states.test_domain).entity_id }}", hass
).async_render()
== "test_domain.object"
)
assert (
template.Template(
"{{ (states.test_domain | closest).entity_id }}", hass
).async_render()
== "test_domain.object"
) | [
"def",
"test_closest_function_home_vs_domain",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test_domain.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.1",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.1",
",",
"}",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"not_test_domain.but_closer\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ closest(states.test_domain).entity_id }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"test_domain.object\"",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ (states.test_domain | closest).entity_id }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"test_domain.object\"",
")"
] | [
1266,
0
] | [
1295,
5
] | python | en | ['fr', 'en', 'en'] | True |
test_closest_function_home_vs_all_states | (hass) | Test closest function home vs all states. | Test closest function home vs all states. | def test_closest_function_home_vs_all_states(hass):
"""Test closest function home vs all states."""
hass.states.async_set(
"test_domain.object",
"happy",
{
"latitude": hass.config.latitude + 0.1,
"longitude": hass.config.longitude + 0.1,
},
)
hass.states.async_set(
"test_domain_2.and_closer",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
assert (
template.Template("{{ closest(states).entity_id }}", hass).async_render()
== "test_domain_2.and_closer"
)
assert (
template.Template("{{ (states | closest).entity_id }}", hass).async_render()
== "test_domain_2.and_closer"
) | [
"def",
"test_closest_function_home_vs_all_states",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test_domain.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.1",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.1",
",",
"}",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test_domain_2.and_closer\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ closest(states).entity_id }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"test_domain_2.and_closer\"",
")",
"assert",
"(",
"template",
".",
"Template",
"(",
"\"{{ (states | closest).entity_id }}\"",
",",
"hass",
")",
".",
"async_render",
"(",
")",
"==",
"\"test_domain_2.and_closer\"",
")"
] | [
1298,
0
] | [
1323,
5
] | python | en | ['en', 'en', 'en'] | True |
test_closest_function_home_vs_group_entity_id | (hass) | Test closest function home vs group entity id. | Test closest function home vs group entity id. | async def test_closest_function_home_vs_group_entity_id(hass):
"""Test closest function home vs group entity id."""
hass.states.async_set(
"test_domain.object",
"happy",
{
"latitude": hass.config.latitude + 0.1,
"longitude": hass.config.longitude + 0.1,
},
)
hass.states.async_set(
"not_in_group.but_closer",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done()
await group.Group.async_create_group(hass, "location group", ["test_domain.object"])
info = render_to_info(hass, '{{ closest("group.location_group").entity_id }}')
assert_result_info(
info, "test_domain.object", {"group.location_group", "test_domain.object"}
)
assert info.rate_limit is None | [
"async",
"def",
"test_closest_function_home_vs_group_entity_id",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test_domain.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.1",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.1",
",",
"}",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"not_in_group.but_closer\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"\"group\"",
",",
"{",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"group",
".",
"Group",
".",
"async_create_group",
"(",
"hass",
",",
"\"location group\"",
",",
"[",
"\"test_domain.object\"",
"]",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"'{{ closest(\"group.location_group\").entity_id }}'",
")",
"assert_result_info",
"(",
"info",
",",
"\"test_domain.object\"",
",",
"{",
"\"group.location_group\"",
",",
"\"test_domain.object\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None"
] | [
1326,
0
] | [
1351,
34
] | python | en | ['en', 'en', 'en'] | True |
test_closest_function_home_vs_group_state | (hass) | Test closest function home vs group state. | Test closest function home vs group state. | async def test_closest_function_home_vs_group_state(hass):
"""Test closest function home vs group state."""
hass.states.async_set(
"test_domain.object",
"happy",
{
"latitude": hass.config.latitude + 0.1,
"longitude": hass.config.longitude + 0.1,
},
)
hass.states.async_set(
"not_in_group.but_closer",
"happy",
{"latitude": hass.config.latitude, "longitude": hass.config.longitude},
)
assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done()
await group.Group.async_create_group(hass, "location group", ["test_domain.object"])
info = render_to_info(hass, '{{ closest("group.location_group").entity_id }}')
assert_result_info(
info, "test_domain.object", {"group.location_group", "test_domain.object"}
)
assert info.rate_limit is None
info = render_to_info(hass, "{{ closest(states.group.location_group).entity_id }}")
assert_result_info(
info, "test_domain.object", {"test_domain.object", "group.location_group"}
)
assert info.rate_limit is None | [
"async",
"def",
"test_closest_function_home_vs_group_state",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test_domain.object\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.1",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.1",
",",
"}",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"not_in_group.but_closer\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"}",
",",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"\"group\"",
",",
"{",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"group",
".",
"Group",
".",
"async_create_group",
"(",
"hass",
",",
"\"location group\"",
",",
"[",
"\"test_domain.object\"",
"]",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"'{{ closest(\"group.location_group\").entity_id }}'",
")",
"assert_result_info",
"(",
"info",
",",
"\"test_domain.object\"",
",",
"{",
"\"group.location_group\"",
",",
"\"test_domain.object\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ closest(states.group.location_group).entity_id }}\"",
")",
"assert_result_info",
"(",
"info",
",",
"\"test_domain.object\"",
",",
"{",
"\"test_domain.object\"",
",",
"\"group.location_group\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None"
] | [
1354,
0
] | [
1385,
34
] | python | en | ['en', 'en', 'en'] | True |
test_expand | (hass) | Test expand function. | Test expand function. | async def test_expand(hass):
"""Test expand function."""
info = render_to_info(hass, "{{ expand('test.object') }}")
assert_result_info(info, [], ["test.object"])
assert info.rate_limit is None
info = render_to_info(hass, "{{ expand(56) }}")
assert_result_info(info, [])
assert info.rate_limit is None
hass.states.async_set("test.object", "happy")
info = render_to_info(
hass, "{{ expand('test.object') | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(info, "test.object", ["test.object"])
assert info.rate_limit is None
info = render_to_info(
hass,
"{{ expand('group.new_group') | map(attribute='entity_id') | join(', ') }}",
)
assert_result_info(info, "", ["group.new_group"])
assert info.rate_limit is None
info = render_to_info(
hass, "{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(info, "", [], ["group"])
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT
assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done()
await group.Group.async_create_group(hass, "new group", ["test.object"])
info = render_to_info(
hass,
"{{ expand('group.new_group') | map(attribute='entity_id') | join(', ') }}",
)
assert_result_info(info, "test.object", {"group.new_group", "test.object"})
assert info.rate_limit is None
info = render_to_info(
hass, "{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}"
)
assert_result_info(info, "test.object", {"test.object"}, ["group"])
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT
info = render_to_info(
hass,
"{{ expand('group.new_group', 'test.object')"
" | map(attribute='entity_id') | join(', ') }}",
)
assert_result_info(info, "test.object", {"test.object", "group.new_group"})
info = render_to_info(
hass,
"{{ ['group.new_group', 'test.object'] | expand"
" | map(attribute='entity_id') | join(', ') }}",
)
assert_result_info(info, "test.object", {"test.object", "group.new_group"})
assert info.rate_limit is None
hass.states.async_set("sensor.power_1", 0)
hass.states.async_set("sensor.power_2", 200.2)
hass.states.async_set("sensor.power_3", 400.4)
assert await async_setup_component(hass, "group", {})
await hass.async_block_till_done()
await group.Group.async_create_group(
hass, "power sensors", ["sensor.power_1", "sensor.power_2", "sensor.power_3"]
)
info = render_to_info(
hass,
"{{ states.group.power_sensors.attributes.entity_id | expand | map(attribute='state')|map('float')|sum }}",
)
assert_result_info(
info,
200.2 + 400.4,
{"group.power_sensors", "sensor.power_1", "sensor.power_2", "sensor.power_3"},
)
assert info.rate_limit is None | [
"async",
"def",
"test_expand",
"(",
"hass",
")",
":",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand('test.object') }}\"",
")",
"assert_result_info",
"(",
"info",
",",
"[",
"]",
",",
"[",
"\"test.object\"",
"]",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand(56) }}\"",
")",
"assert_result_info",
"(",
"info",
",",
"[",
"]",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test.object\"",
",",
"\"happy\"",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand('test.object') | map(attribute='entity_id') | join(', ') }}\"",
")",
"assert_result_info",
"(",
"info",
",",
"\"test.object\"",
",",
"[",
"\"test.object\"",
"]",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand('group.new_group') | map(attribute='entity_id') | join(', ') }}\"",
",",
")",
"assert_result_info",
"(",
"info",
",",
"\"\"",
",",
"[",
"\"group.new_group\"",
"]",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}\"",
")",
"assert_result_info",
"(",
"info",
",",
"\"\"",
",",
"[",
"]",
",",
"[",
"\"group\"",
"]",
")",
"assert",
"info",
".",
"rate_limit",
"==",
"template",
".",
"DOMAIN_STATES_RATE_LIMIT",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"\"group\"",
",",
"{",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"group",
".",
"Group",
".",
"async_create_group",
"(",
"hass",
",",
"\"new group\"",
",",
"[",
"\"test.object\"",
"]",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand('group.new_group') | map(attribute='entity_id') | join(', ') }}\"",
",",
")",
"assert_result_info",
"(",
"info",
",",
"\"test.object\"",
",",
"{",
"\"group.new_group\"",
",",
"\"test.object\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand(states.group) | map(attribute='entity_id') | join(', ') }}\"",
")",
"assert_result_info",
"(",
"info",
",",
"\"test.object\"",
",",
"{",
"\"test.object\"",
"}",
",",
"[",
"\"group\"",
"]",
")",
"assert",
"info",
".",
"rate_limit",
"==",
"template",
".",
"DOMAIN_STATES_RATE_LIMIT",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ expand('group.new_group', 'test.object')\"",
"\" | map(attribute='entity_id') | join(', ') }}\"",
",",
")",
"assert_result_info",
"(",
"info",
",",
"\"test.object\"",
",",
"{",
"\"test.object\"",
",",
"\"group.new_group\"",
"}",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ ['group.new_group', 'test.object'] | expand\"",
"\" | map(attribute='entity_id') | join(', ') }}\"",
",",
")",
"assert_result_info",
"(",
"info",
",",
"\"test.object\"",
",",
"{",
"\"test.object\"",
",",
"\"group.new_group\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.power_1\"",
",",
"0",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.power_2\"",
",",
"200.2",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.power_3\"",
",",
"400.4",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"\"group\"",
",",
"{",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"group",
".",
"Group",
".",
"async_create_group",
"(",
"hass",
",",
"\"power sensors\"",
",",
"[",
"\"sensor.power_1\"",
",",
"\"sensor.power_2\"",
",",
"\"sensor.power_3\"",
"]",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"{{ states.group.power_sensors.attributes.entity_id | expand | map(attribute='state')|map('float')|sum }}\"",
",",
")",
"assert_result_info",
"(",
"info",
",",
"200.2",
"+",
"400.4",
",",
"{",
"\"group.power_sensors\"",
",",
"\"sensor.power_1\"",
",",
"\"sensor.power_2\"",
",",
"\"sensor.power_3\"",
"}",
",",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None"
] | [
1388,
0
] | [
1470,
34
] | python | en | ['en', 'en', 'en'] | True |
test_closest_function_to_coord | (hass) | Test closest function to coord. | Test closest function to coord. | def test_closest_function_to_coord(hass):
"""Test closest function to coord."""
hass.states.async_set(
"test_domain.closest_home",
"happy",
{
"latitude": hass.config.latitude + 0.1,
"longitude": hass.config.longitude + 0.1,
},
)
hass.states.async_set(
"test_domain.closest_zone",
"happy",
{
"latitude": hass.config.latitude + 0.2,
"longitude": hass.config.longitude + 0.2,
},
)
hass.states.async_set(
"zone.far_away",
"zoning",
{
"latitude": hass.config.latitude + 0.3,
"longitude": hass.config.longitude + 0.3,
},
)
tpl = template.Template(
'{{ closest("%s", %s, states.test_domain).entity_id }}'
% (hass.config.latitude + 0.3, hass.config.longitude + 0.3),
hass,
)
assert tpl.async_render() == "test_domain.closest_zone"
tpl = template.Template(
'{{ (states.test_domain | closest("%s", %s)).entity_id }}'
% (hass.config.latitude + 0.3, hass.config.longitude + 0.3),
hass,
)
assert tpl.async_render() == "test_domain.closest_zone" | [
"def",
"test_closest_function_to_coord",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test_domain.closest_home\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.1",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.1",
",",
"}",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"test_domain.closest_zone\"",
",",
"\"happy\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.2",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.2",
",",
"}",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"zone.far_away\"",
",",
"\"zoning\"",
",",
"{",
"\"latitude\"",
":",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.3",
",",
"\"longitude\"",
":",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.3",
",",
"}",
",",
")",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ closest(\"%s\", %s, states.test_domain).entity_id }}'",
"%",
"(",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.3",
",",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.3",
")",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"test_domain.closest_zone\"",
"tpl",
"=",
"template",
".",
"Template",
"(",
"'{{ (states.test_domain | closest(\"%s\", %s)).entity_id }}'",
"%",
"(",
"hass",
".",
"config",
".",
"latitude",
"+",
"0.3",
",",
"hass",
".",
"config",
".",
"longitude",
"+",
"0.3",
")",
",",
"hass",
",",
")",
"assert",
"tpl",
".",
"async_render",
"(",
")",
"==",
"\"test_domain.closest_zone\""
] | [
1473,
0
] | [
1516,
59
] | python | en | ['en', 'en', 'en'] | True |
test_async_render_to_info_with_branching | (hass) | Test async_render_to_info function by domain. | Test async_render_to_info function by domain. | def test_async_render_to_info_with_branching(hass):
"""Test async_render_to_info function by domain."""
hass.states.async_set("light.a", "off")
hass.states.async_set("light.b", "on")
hass.states.async_set("light.c", "off")
info = render_to_info(
hass,
"""
{% if states.light.a == "on" %}
{{ states.light.b.state }}
{% else %}
{{ states.light.c.state }}
{% endif %}
""",
)
assert_result_info(info, "off", {"light.a", "light.c"})
assert info.rate_limit is None
info = render_to_info(
hass,
"""
{% if states.light.a.state == "off" %}
{% set domain = "light" %}
{{ states[domain].b.state }}
{% endif %}
""",
)
assert_result_info(info, "on", {"light.a", "light.b"})
assert info.rate_limit is None | [
"def",
"test_async_render_to_info_with_branching",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.a\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.b\"",
",",
"\"on\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.c\"",
",",
"\"off\"",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"\"\"\n{% if states.light.a == \"on\" %}\n {{ states.light.b.state }}\n{% else %}\n {{ states.light.c.state }}\n{% endif %}\n\"\"\"",
",",
")",
"assert_result_info",
"(",
"info",
",",
"\"off\"",
",",
"{",
"\"light.a\"",
",",
"\"light.c\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"\"\"\n {% if states.light.a.state == \"off\" %}\n {% set domain = \"light\" %}\n {{ states[domain].b.state }}\n {% endif %}\n\"\"\"",
",",
")",
"assert_result_info",
"(",
"info",
",",
"\"on\"",
",",
"{",
"\"light.a\"",
",",
"\"light.b\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"is",
"None"
] | [
1519,
0
] | [
1548,
34
] | python | en | ['en', 'en', 'en'] | True |
test_async_render_to_info_with_complex_branching | (hass) | Test async_render_to_info function by domain. | Test async_render_to_info function by domain. | def test_async_render_to_info_with_complex_branching(hass):
"""Test async_render_to_info function by domain."""
hass.states.async_set("light.a", "off")
hass.states.async_set("light.b", "on")
hass.states.async_set("light.c", "off")
hass.states.async_set("vacuum.a", "off")
hass.states.async_set("device_tracker.a", "off")
hass.states.async_set("device_tracker.b", "off")
hass.states.async_set("lock.a", "off")
hass.states.async_set("sensor.a", "off")
hass.states.async_set("binary_sensor.a", "off")
info = render_to_info(
hass,
"""
{% set domain = "vacuum" %}
{% if states.light.a == "on" %}
{{ states.light.b.state }}
{% elif states.light.a == "on" %}
{{ states.device_tracker }}
{% elif states.light.a == "on" %}
{{ states[domain] | list }}
{% elif states('light.b') == "on" %}
{{ states[otherdomain] | map(attribute='entity_id') | list }}
{% elif states.light.a == "on" %}
{{ states["nonexist"] | list }}
{% else %}
else
{% endif %}
""",
{"otherdomain": "sensor"},
)
assert_result_info(info, ["sensor.a"], {"light.a", "light.b"}, {"sensor"})
assert info.rate_limit == template.DOMAIN_STATES_RATE_LIMIT | [
"def",
"test_async_render_to_info_with_complex_branching",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.a\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.b\"",
",",
"\"on\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.c\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"vacuum.a\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"device_tracker.a\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"device_tracker.b\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"lock.a\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.a\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"binary_sensor.a\"",
",",
"\"off\"",
")",
"info",
"=",
"render_to_info",
"(",
"hass",
",",
"\"\"\"\n{% set domain = \"vacuum\" %}\n{% if states.light.a == \"on\" %}\n {{ states.light.b.state }}\n{% elif states.light.a == \"on\" %}\n {{ states.device_tracker }}\n{% elif states.light.a == \"on\" %}\n {{ states[domain] | list }}\n{% elif states('light.b') == \"on\" %}\n {{ states[otherdomain] | map(attribute='entity_id') | list }}\n{% elif states.light.a == \"on\" %}\n {{ states[\"nonexist\"] | list }}\n{% else %}\n else\n{% endif %}\n\"\"\"",
",",
"{",
"\"otherdomain\"",
":",
"\"sensor\"",
"}",
",",
")",
"assert_result_info",
"(",
"info",
",",
"[",
"\"sensor.a\"",
"]",
",",
"{",
"\"light.a\"",
",",
"\"light.b\"",
"}",
",",
"{",
"\"sensor\"",
"}",
")",
"assert",
"info",
".",
"rate_limit",
"==",
"template",
".",
"DOMAIN_STATES_RATE_LIMIT"
] | [
1551,
0
] | [
1585,
63
] | python | en | ['en', 'en', 'en'] | True |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.