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
sequence | start_point
sequence | end_point
sequence | language
stringclasses 1
value | docstring_language
stringlengths 2
7
| docstring_language_predictions
stringlengths 18
23
| is_langid_reliable
stringclasses 2
values |
---|---|---|---|---|---|---|---|---|---|---|---|
test_no_color_channels_value | (mock_openzwave) | Test value changed for rgb lights. | Test value changed for rgb lights. | def test_no_color_channels_value(mock_openzwave):
"""Test value changed for rgb lights."""
node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR])
value = MockValue(data=0, node=node)
color = MockValue(data="#0000000000", node=node)
values = MockLightValues(primary=value, color=color)
device = light.get_device(node=node, values=values, node_config={})
assert device.hs_color is None | [
"def",
"test_no_color_channels_value",
"(",
"mock_openzwave",
")",
":",
"node",
"=",
"MockNode",
"(",
"command_classes",
"=",
"[",
"const",
".",
"COMMAND_CLASS_SWITCH_COLOR",
"]",
")",
"value",
"=",
"MockValue",
"(",
"data",
"=",
"0",
",",
"node",
"=",
"node",
")",
"color",
"=",
"MockValue",
"(",
"data",
"=",
"\"#0000000000\"",
",",
"node",
"=",
"node",
")",
"values",
"=",
"MockLightValues",
"(",
"primary",
"=",
"value",
",",
"color",
"=",
"color",
")",
"device",
"=",
"light",
".",
"get_device",
"(",
"node",
"=",
"node",
",",
"values",
"=",
"values",
",",
"node_config",
"=",
"{",
"}",
")",
"assert",
"device",
".",
"hs_color",
"is",
"None"
] | [
369,
0
] | [
377,
34
] | python | en | ['en', 'en', 'en'] | True |
test_rgb_value_changed | (mock_openzwave) | Test value changed for rgb lights. | Test value changed for rgb lights. | def test_rgb_value_changed(mock_openzwave):
"""Test value changed for rgb lights."""
node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR])
value = MockValue(data=0, node=node)
color = MockValue(data="#0000000000", node=node)
# Supports RGB only
color_channels = MockValue(data=0x1C, node=node)
values = MockLightValues(primary=value, color=color, color_channels=color_channels)
device = light.get_device(node=node, values=values, node_config={})
assert device.hs_color == (0, 0)
color.data = "#ffbf800000"
value_changed(color)
assert device.hs_color == (29.764, 49.804) | [
"def",
"test_rgb_value_changed",
"(",
"mock_openzwave",
")",
":",
"node",
"=",
"MockNode",
"(",
"command_classes",
"=",
"[",
"const",
".",
"COMMAND_CLASS_SWITCH_COLOR",
"]",
")",
"value",
"=",
"MockValue",
"(",
"data",
"=",
"0",
",",
"node",
"=",
"node",
")",
"color",
"=",
"MockValue",
"(",
"data",
"=",
"\"#0000000000\"",
",",
"node",
"=",
"node",
")",
"# Supports RGB only",
"color_channels",
"=",
"MockValue",
"(",
"data",
"=",
"0x1C",
",",
"node",
"=",
"node",
")",
"values",
"=",
"MockLightValues",
"(",
"primary",
"=",
"value",
",",
"color",
"=",
"color",
",",
"color_channels",
"=",
"color_channels",
")",
"device",
"=",
"light",
".",
"get_device",
"(",
"node",
"=",
"node",
",",
"values",
"=",
"values",
",",
"node_config",
"=",
"{",
"}",
")",
"assert",
"device",
".",
"hs_color",
"==",
"(",
"0",
",",
"0",
")",
"color",
".",
"data",
"=",
"\"#ffbf800000\"",
"value_changed",
"(",
"color",
")",
"assert",
"device",
".",
"hs_color",
"==",
"(",
"29.764",
",",
"49.804",
")"
] | [
380,
0
] | [
395,
46
] | python | en | ['en', 'en', 'en'] | True |
test_rgbww_value_changed | (mock_openzwave) | Test value changed for rgb lights. | Test value changed for rgb lights. | def test_rgbww_value_changed(mock_openzwave):
"""Test value changed for rgb lights."""
node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR])
value = MockValue(data=0, node=node)
color = MockValue(data="#0000000000", node=node)
# Supports RGB, Warm White
color_channels = MockValue(data=0x1D, node=node)
values = MockLightValues(primary=value, color=color, color_channels=color_channels)
device = light.get_device(node=node, values=values, node_config={})
assert device.hs_color == (0, 0)
assert device.white_value == 0
color.data = "#c86400c800"
value_changed(color)
assert device.hs_color == (30, 100)
assert device.white_value == 200 | [
"def",
"test_rgbww_value_changed",
"(",
"mock_openzwave",
")",
":",
"node",
"=",
"MockNode",
"(",
"command_classes",
"=",
"[",
"const",
".",
"COMMAND_CLASS_SWITCH_COLOR",
"]",
")",
"value",
"=",
"MockValue",
"(",
"data",
"=",
"0",
",",
"node",
"=",
"node",
")",
"color",
"=",
"MockValue",
"(",
"data",
"=",
"\"#0000000000\"",
",",
"node",
"=",
"node",
")",
"# Supports RGB, Warm White",
"color_channels",
"=",
"MockValue",
"(",
"data",
"=",
"0x1D",
",",
"node",
"=",
"node",
")",
"values",
"=",
"MockLightValues",
"(",
"primary",
"=",
"value",
",",
"color",
"=",
"color",
",",
"color_channels",
"=",
"color_channels",
")",
"device",
"=",
"light",
".",
"get_device",
"(",
"node",
"=",
"node",
",",
"values",
"=",
"values",
",",
"node_config",
"=",
"{",
"}",
")",
"assert",
"device",
".",
"hs_color",
"==",
"(",
"0",
",",
"0",
")",
"assert",
"device",
".",
"white_value",
"==",
"0",
"color",
".",
"data",
"=",
"\"#c86400c800\"",
"value_changed",
"(",
"color",
")",
"assert",
"device",
".",
"hs_color",
"==",
"(",
"30",
",",
"100",
")",
"assert",
"device",
".",
"white_value",
"==",
"200"
] | [
398,
0
] | [
415,
36
] | python | en | ['en', 'en', 'en'] | True |
test_rgbcw_value_changed | (mock_openzwave) | Test value changed for rgb lights. | Test value changed for rgb lights. | def test_rgbcw_value_changed(mock_openzwave):
"""Test value changed for rgb lights."""
node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR])
value = MockValue(data=0, node=node)
color = MockValue(data="#0000000000", node=node)
# Supports RGB, Cold White
color_channels = MockValue(data=0x1E, node=node)
values = MockLightValues(primary=value, color=color, color_channels=color_channels)
device = light.get_device(node=node, values=values, node_config={})
assert device.hs_color == (0, 0)
assert device.white_value == 0
color.data = "#c86400c800"
value_changed(color)
assert device.hs_color == (30, 100)
assert device.white_value == 200 | [
"def",
"test_rgbcw_value_changed",
"(",
"mock_openzwave",
")",
":",
"node",
"=",
"MockNode",
"(",
"command_classes",
"=",
"[",
"const",
".",
"COMMAND_CLASS_SWITCH_COLOR",
"]",
")",
"value",
"=",
"MockValue",
"(",
"data",
"=",
"0",
",",
"node",
"=",
"node",
")",
"color",
"=",
"MockValue",
"(",
"data",
"=",
"\"#0000000000\"",
",",
"node",
"=",
"node",
")",
"# Supports RGB, Cold White",
"color_channels",
"=",
"MockValue",
"(",
"data",
"=",
"0x1E",
",",
"node",
"=",
"node",
")",
"values",
"=",
"MockLightValues",
"(",
"primary",
"=",
"value",
",",
"color",
"=",
"color",
",",
"color_channels",
"=",
"color_channels",
")",
"device",
"=",
"light",
".",
"get_device",
"(",
"node",
"=",
"node",
",",
"values",
"=",
"values",
",",
"node_config",
"=",
"{",
"}",
")",
"assert",
"device",
".",
"hs_color",
"==",
"(",
"0",
",",
"0",
")",
"assert",
"device",
".",
"white_value",
"==",
"0",
"color",
".",
"data",
"=",
"\"#c86400c800\"",
"value_changed",
"(",
"color",
")",
"assert",
"device",
".",
"hs_color",
"==",
"(",
"30",
",",
"100",
")",
"assert",
"device",
".",
"white_value",
"==",
"200"
] | [
418,
0
] | [
435,
36
] | python | en | ['en', 'en', 'en'] | True |
test_ct_value_changed | (mock_openzwave) | Test value changed for zw098 lights. | Test value changed for zw098 lights. | def test_ct_value_changed(mock_openzwave):
"""Test value changed for zw098 lights."""
node = MockNode(
manufacturer_id="0086",
product_id="0062",
command_classes=[const.COMMAND_CLASS_SWITCH_COLOR],
)
value = MockValue(data=0, node=node)
color = MockValue(data="#0000000000", node=node)
# Supports RGB, Cold White
color_channels = MockValue(data=0x1F, node=node)
values = MockLightValues(primary=value, color=color, color_channels=color_channels)
device = light.get_device(node=node, values=values, node_config={})
assert device.color_temp == light.TEMP_MID_HASS
color.data = "#000000ff00"
value_changed(color)
assert device.color_temp == light.TEMP_WARM_HASS
color.data = "#00000000ff"
value_changed(color)
assert device.color_temp == light.TEMP_COLD_HASS | [
"def",
"test_ct_value_changed",
"(",
"mock_openzwave",
")",
":",
"node",
"=",
"MockNode",
"(",
"manufacturer_id",
"=",
"\"0086\"",
",",
"product_id",
"=",
"\"0062\"",
",",
"command_classes",
"=",
"[",
"const",
".",
"COMMAND_CLASS_SWITCH_COLOR",
"]",
",",
")",
"value",
"=",
"MockValue",
"(",
"data",
"=",
"0",
",",
"node",
"=",
"node",
")",
"color",
"=",
"MockValue",
"(",
"data",
"=",
"\"#0000000000\"",
",",
"node",
"=",
"node",
")",
"# Supports RGB, Cold White",
"color_channels",
"=",
"MockValue",
"(",
"data",
"=",
"0x1F",
",",
"node",
"=",
"node",
")",
"values",
"=",
"MockLightValues",
"(",
"primary",
"=",
"value",
",",
"color",
"=",
"color",
",",
"color_channels",
"=",
"color_channels",
")",
"device",
"=",
"light",
".",
"get_device",
"(",
"node",
"=",
"node",
",",
"values",
"=",
"values",
",",
"node_config",
"=",
"{",
"}",
")",
"assert",
"device",
".",
"color_temp",
"==",
"light",
".",
"TEMP_MID_HASS",
"color",
".",
"data",
"=",
"\"#000000ff00\"",
"value_changed",
"(",
"color",
")",
"assert",
"device",
".",
"color_temp",
"==",
"light",
".",
"TEMP_WARM_HASS",
"color",
".",
"data",
"=",
"\"#00000000ff\"",
"value_changed",
"(",
"color",
")",
"assert",
"device",
".",
"color_temp",
"==",
"light",
".",
"TEMP_COLD_HASS"
] | [
438,
0
] | [
462,
52
] | python | en | ['en', 'en', 'en'] | True |
MockLightValues.__init__ | (self, **kwargs) | Initialize the mock zwave values. | Initialize the mock zwave values. | def __init__(self, **kwargs):
"""Initialize the mock zwave values."""
self.dimming_duration = None
self.color = None
self.color_channels = None
super().__init__(**kwargs) | [
"def",
"__init__",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"self",
".",
"dimming_duration",
"=",
"None",
"self",
".",
"color",
"=",
"None",
"self",
".",
"color_channels",
"=",
"None",
"super",
"(",
")",
".",
"__init__",
"(",
"*",
"*",
"kwargs",
")"
] | [
23,
4
] | [
28,
34
] | python | en | ['en', 'en', 'en'] | True |
test_available_template_with_entities | (hass) | Test availability templates with values from other entities. | Test availability templates with values from other entities. | async def test_available_template_with_entities(hass):
"""Test availability templates with values from other entities."""
await setup.async_setup_component(
hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"availability_template": "{{ is_state('availability_boolean.state', 'on') }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
# When template returns true..
hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_ON)
await hass.async_block_till_done()
# Device State should not be unavailable
assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE
# When Availability template returns false
hass.states.async_set(_STATE_AVAILABILITY_BOOLEAN, STATE_OFF)
await hass.async_block_till_done()
# device state should be unavailable
assert hass.states.get("light.test_template_light").state == STATE_UNAVAILABLE | [
"async",
"def",
"test_available_template_with_entities",
"(",
"hass",
")",
":",
"await",
"setup",
".",
"async_setup_component",
"(",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"availability_template\"",
":",
"\"{{ is_state('availability_boolean.state', 'on') }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"hass",
".",
"async_start",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# When template returns true..",
"hass",
".",
"states",
".",
"async_set",
"(",
"_STATE_AVAILABILITY_BOOLEAN",
",",
"STATE_ON",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# Device State should not be unavailable",
"assert",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
".",
"state",
"!=",
"STATE_UNAVAILABLE",
"# When Availability template returns false",
"hass",
".",
"states",
".",
"async_set",
"(",
"_STATE_AVAILABILITY_BOOLEAN",
",",
"STATE_OFF",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# device state should be unavailable",
"assert",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
".",
"state",
"==",
"STATE_UNAVAILABLE"
] | [
1078,
0
] | [
1125,
82
] | python | en | ['en', 'en', 'en'] | True |
test_invalid_availability_template_keeps_component_available | (hass, caplog) | Test that an invalid availability keeps the device available. | Test that an invalid availability keeps the device available. | async def test_invalid_availability_template_keeps_component_available(hass, caplog):
"""Test that an invalid availability keeps the device available."""
await setup.async_setup_component(
hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"availability_template": "{{ x - 12 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
assert hass.states.get("light.test_template_light").state != STATE_UNAVAILABLE
assert ("UndefinedError: 'x' is undefined") in caplog.text | [
"async",
"def",
"test_invalid_availability_template_keeps_component_available",
"(",
"hass",
",",
"caplog",
")",
":",
"await",
"setup",
".",
"async_setup_component",
"(",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"availability_template\"",
":",
"\"{{ x - 12 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"hass",
".",
"async_start",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
".",
"state",
"!=",
"STATE_UNAVAILABLE",
"assert",
"(",
"\"UndefinedError: 'x' is undefined\"",
")",
"in",
"caplog",
".",
"text"
] | [
1128,
0
] | [
1165,
62
] | python | en | ['en', 'en', 'en'] | True |
test_unique_id | (hass) | Test unique_id option only creates one light per id. | Test unique_id option only creates one light per id. | async def test_unique_id(hass):
"""Test unique_id option only creates one light per id."""
await setup.async_setup_component(
hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light_01": {
"unique_id": "not-so-unique-anymore",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
},
"test_template_light_02": {
"unique_id": "not-so-unique-anymore",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
},
},
},
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1 | [
"async",
"def",
"test_unique_id",
"(",
"hass",
")",
":",
"await",
"setup",
".",
"async_setup_component",
"(",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light_01\"",
":",
"{",
"\"unique_id\"",
":",
"\"not-so-unique-anymore\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"}",
",",
"\"test_template_light_02\"",
":",
"{",
"\"unique_id\"",
":",
"\"not-so-unique-anymore\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"}",
",",
"}",
",",
"}",
",",
"}",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"hass",
".",
"async_start",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"hass",
".",
"states",
".",
"async_all",
"(",
")",
")",
"==",
"1"
] | [
1168,
0
] | [
1208,
44
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.setup_method | (self, method) | Set up things to be run when tests are started. | Set up things to be run when tests are started. | def setup_method(self, method):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.calls = []
@callback
def record_call(service):
"""Track function calls."""
self.calls.append(service)
self.hass.services.register("test", "automation", record_call) | [
"def",
"setup_method",
"(",
"self",
",",
"method",
")",
":",
"self",
".",
"hass",
"=",
"get_test_home_assistant",
"(",
")",
"self",
".",
"calls",
"=",
"[",
"]",
"@",
"callback",
"def",
"record_call",
"(",
"service",
")",
":",
"\"\"\"Track function calls.\"\"\"",
"self",
".",
"calls",
".",
"append",
"(",
"service",
")",
"self",
".",
"hass",
".",
"services",
".",
"register",
"(",
"\"test\"",
",",
"\"automation\"",
",",
"record_call",
")"
] | [
31,
4
] | [
41,
70
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.teardown_method | (self, method) | Stop everything that was started. | Stop everything that was started. | def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop() | [
"def",
"teardown_method",
"(",
"self",
",",
"method",
")",
":",
"self",
".",
"hass",
".",
"stop",
"(",
")"
] | [
43,
4
] | [
45,
24
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_template_state_invalid | (self) | Test template state with render error. | Test template state with render error. | def test_template_state_invalid(self):
"""Test template state with render error."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{states.test['big.fat...']}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_OFF | [
"def",
"test_template_state_invalid",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{states.test['big.fat...']}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_OFF"
] | [
47,
4
] | [
85,
39
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_template_state_text | (self) | Test the state text of a template. | Test the state text of a template. | def test_template_state_text(self):
"""Test the state text of a template."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{ states.light.test_state.state }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.set("light.test_state", STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_ON
state = self.hass.states.set("light.test_state", STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_OFF | [
"def",
"test_template_state_text",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{ states.light.test_state.state }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"set",
"(",
"\"light.test_state\"",
",",
"STATE_ON",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_ON",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"set",
"(",
"\"light.test_state\"",
",",
"STATE_OFF",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_OFF"
] | [
87,
4
] | [
134,
39
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_template_state_boolean | (self, expected_state, template) | Test the setting of the state with boolean on. | Test the setting of the state with boolean on. | def test_template_state_boolean(self, expected_state, template):
"""Test the setting of the state with boolean on."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": template,
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == expected_state | [
"def",
"test_template_state_boolean",
"(",
"self",
",",
"expected_state",
",",
"template",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"template",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"expected_state"
] | [
140,
4
] | [
178,
44
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_template_syntax_error | (self) | Test templating syntax error. | Test templating syntax error. | def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{%- if false -%}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == [] | [
"def",
"test_template_syntax_error",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"0",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{%- if false -%}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"self",
".",
"hass",
".",
"states",
".",
"all",
"(",
")",
"==",
"[",
"]"
] | [
180,
4
] | [
217,
43
] | python | ca | ['ca', 'de', 'en'] | False |
TestTemplateLight.test_invalid_name_does_not_create | (self) | Test invalid name. | Test invalid name. | def test_invalid_name_does_not_create(self):
"""Test invalid name."""
with assert_setup_component(0, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"bad name here": {
"value_template": "{{ 1== 1}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == [] | [
"def",
"test_invalid_name_does_not_create",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"0",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"bad name here\"",
":",
"{",
"\"value_template\"",
":",
"\"{{ 1== 1}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"self",
".",
"hass",
".",
"states",
".",
"all",
"(",
")",
"==",
"[",
"]"
] | [
219,
4
] | [
256,
43
] | python | en | ['en', 'et', 'en'] | True |
TestTemplateLight.test_invalid_light_does_not_create | (self) | Test invalid light. | Test invalid light. | def test_invalid_light_does_not_create(self):
"""Test invalid light."""
with assert_setup_component(0, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"switches": {"test_template_light": "Invalid"},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == [] | [
"def",
"test_invalid_light_does_not_create",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"0",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"switches\"",
":",
"{",
"\"test_template_light\"",
":",
"\"Invalid\"",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"self",
".",
"hass",
".",
"states",
".",
"all",
"(",
")",
"==",
"[",
"]"
] | [
258,
4
] | [
276,
43
] | python | en | ['en', 'et', 'en'] | True |
TestTemplateLight.test_no_lights_does_not_create | (self) | Test if there are no lights no creation. | Test if there are no lights no creation. | def test_no_lights_does_not_create(self):
"""Test if there are no lights no creation."""
with assert_setup_component(0, "light"):
assert setup.setup_component(
self.hass, "light", {"light": {"platform": "template"}}
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
assert self.hass.states.all() == [] | [
"def",
"test_no_lights_does_not_create",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"0",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
"}",
"}",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"self",
".",
"hass",
".",
"states",
".",
"all",
"(",
")",
"==",
"[",
"]"
] | [
278,
4
] | [
289,
43
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_missing_key | (self, missing_key, count) | Test missing template. | Test missing template. | def test_missing_key(self, missing_key, count):
"""Test missing template."""
light = {
"light": {
"platform": "template",
"lights": {
"light_one": {
"value_template": "{{ 1== 1}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
}
del light["light"]["lights"]["light_one"][missing_key]
with assert_setup_component(count, "light"):
assert setup.setup_component(self.hass, "light", light)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
if count:
assert self.hass.states.all() != []
else:
assert self.hass.states.all() == [] | [
"def",
"test_missing_key",
"(",
"self",
",",
"missing_key",
",",
"count",
")",
":",
"light",
"=",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"light_one\"",
":",
"{",
"\"value_template\"",
":",
"\"{{ 1== 1}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
"del",
"light",
"[",
"\"light\"",
"]",
"[",
"\"lights\"",
"]",
"[",
"\"light_one\"",
"]",
"[",
"missing_key",
"]",
"with",
"assert_setup_component",
"(",
"count",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"light",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"if",
"count",
":",
"assert",
"self",
".",
"hass",
".",
"states",
".",
"all",
"(",
")",
"!=",
"[",
"]",
"else",
":",
"assert",
"self",
".",
"hass",
".",
"states",
".",
"all",
"(",
")",
"==",
"[",
"]"
] | [
294,
4
] | [
332,
47
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_on_action | (self) | Test on action. | Test on action. | def test_on_action(self):
"""Test on action."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{states.light.test_state.state}}",
"turn_on": {"service": "test.automation"},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
self.hass.states.set("light.test_state", STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_OFF
common.turn_on(self.hass, "light.test_template_light")
self.hass.block_till_done()
assert len(self.calls) == 1 | [
"def",
"test_on_action",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{states.light.test_state.state}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"states",
".",
"set",
"(",
"\"light.test_state\"",
",",
"STATE_OFF",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_OFF",
"common",
".",
"turn_on",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"1"
] | [
334,
4
] | [
376,
35
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_on_action_optimistic | (self) | Test on action with optimistic state. | Test on action with optimistic state. | def test_on_action_optimistic(self):
"""Test on action with optimistic state."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"turn_on": {"service": "test.automation"},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
self.hass.states.set("light.test_state", STATE_OFF)
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_OFF
common.turn_on(self.hass, "light.test_template_light")
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert len(self.calls) == 1
assert state.state == STATE_ON | [
"def",
"test_on_action_optimistic",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"states",
".",
"set",
"(",
"\"light.test_state\"",
",",
"STATE_OFF",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_OFF",
"common",
".",
"turn_on",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"1",
"assert",
"state",
".",
"state",
"==",
"STATE_ON"
] | [
378,
4
] | [
421,
38
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_off_action | (self) | Test off action. | Test off action. | def test_off_action(self):
"""Test off action."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{states.light.test_state.state}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {"service": "test.automation"},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
self.hass.states.set("light.test_state", STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_ON
common.turn_off(self.hass, "light.test_template_light")
self.hass.block_till_done()
assert len(self.calls) == 1 | [
"def",
"test_off_action",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{states.light.test_state.state}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"states",
".",
"set",
"(",
"\"light.test_state\"",
",",
"STATE_ON",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_ON",
"common",
".",
"turn_off",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"1"
] | [
423,
4
] | [
465,
35
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_off_action_optimistic | (self) | Test off action with optimistic state. | Test off action with optimistic state. | def test_off_action_optimistic(self):
"""Test off action with optimistic state."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {"service": "test.automation"},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_OFF
common.turn_off(self.hass, "light.test_template_light")
self.hass.block_till_done()
assert len(self.calls) == 1
state = self.hass.states.get("light.test_template_light")
assert state.state == STATE_OFF | [
"def",
"test_off_action_optimistic",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_OFF",
"common",
".",
"turn_off",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"1",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"state",
"==",
"STATE_OFF"
] | [
467,
4
] | [
507,
39
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_white_value_action_no_template | (self) | Test setting white value with optimistic template. | Test setting white value with optimistic template. | def test_white_value_action_no_template(self):
"""Test setting white value with optimistic template."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{1 == 1}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_white_value": {
"service": "test.automation",
"data_template": {
"entity_id": "test.test_state",
"white_value": "{{white_value}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes.get("white_value") is None
common.turn_on(
self.hass, "light.test_template_light", **{ATTR_WHITE_VALUE: 124}
)
self.hass.block_till_done()
assert len(self.calls) == 1
assert self.calls[0].data["white_value"] == 124
state = self.hass.states.get("light.test_template_light")
assert state is not None
assert state.attributes.get("white_value") == 124 | [
"def",
"test_white_value_action_no_template",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{1 == 1}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_white_value\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"test.test_state\"",
",",
"\"white_value\"",
":",
"\"{{white_value}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"white_value\"",
")",
"is",
"None",
"common",
".",
"turn_on",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
",",
"*",
"*",
"{",
"ATTR_WHITE_VALUE",
":",
"124",
"}",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"1",
"assert",
"self",
".",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"white_value\"",
"]",
"==",
"124",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"white_value\"",
")",
"==",
"124"
] | [
509,
4
] | [
556,
57
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_white_value_template | (self, expected_white_value, template) | Test the template for the white value. | Test the template for the white value. | def test_white_value_template(self, expected_white_value, template):
"""Test the template for the white value."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_white_value": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"white_value": "{{white_value}}",
},
},
"white_value_template": template,
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state is not None
assert state.attributes.get("white_value") == expected_white_value | [
"def",
"test_white_value_template",
"(",
"self",
",",
"expected_white_value",
",",
"template",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{ 1 == 1 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_white_value\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"white_value\"",
":",
"\"{{white_value}}\"",
",",
"}",
",",
"}",
",",
"\"white_value_template\"",
":",
"template",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"white_value\"",
")",
"==",
"expected_white_value"
] | [
568,
4
] | [
608,
74
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_level_action_no_template | (self) | Test setting brightness with optimistic template. | Test setting brightness with optimistic template. | def test_level_action_no_template(self):
"""Test setting brightness with optimistic template."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{1 == 1}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "test.automation",
"data_template": {
"entity_id": "test.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes.get("brightness") is None
common.turn_on(self.hass, "light.test_template_light", **{ATTR_BRIGHTNESS: 124})
self.hass.block_till_done()
assert len(self.calls) == 1
assert self.calls[0].data["brightness"] == 124
state = self.hass.states.get("light.test_template_light")
_LOGGER.info(str(state.attributes))
assert state is not None
assert state.attributes.get("brightness") == 124 | [
"def",
"test_level_action_no_template",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{1 == 1}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"test.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"brightness\"",
")",
"is",
"None",
"common",
".",
"turn_on",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
",",
"*",
"*",
"{",
"ATTR_BRIGHTNESS",
":",
"124",
"}",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"1",
"assert",
"self",
".",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"brightness\"",
"]",
"==",
"124",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"_LOGGER",
".",
"info",
"(",
"str",
"(",
"state",
".",
"attributes",
")",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"brightness\"",
")",
"==",
"124"
] | [
610,
4
] | [
656,
56
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_level_template | (self, expected_level, template) | Test the template for the level. | Test the template for the level. | def test_level_template(self, expected_level, template):
"""Test the template for the level."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
"level_template": template,
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state is not None
assert state.attributes.get("brightness") == expected_level | [
"def",
"test_level_template",
"(",
"self",
",",
"expected_level",
",",
"template",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{ 1 == 1 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"\"level_template\"",
":",
"template",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"brightness\"",
")",
"==",
"expected_level"
] | [
668,
4
] | [
708,
67
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_temperature_template | (self, expected_temp, template) | Test the template for the temperature. | Test the template for the temperature. | def test_temperature_template(self, expected_temp, template):
"""Test the template for the temperature."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_temperature": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"color_temp": "{{color_temp}}",
},
},
"temperature_template": template,
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state is not None
assert state.attributes.get("color_temp") == expected_temp | [
"def",
"test_temperature_template",
"(",
"self",
",",
"expected_temp",
",",
"template",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{ 1 == 1 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_temperature\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"color_temp\"",
":",
"\"{{color_temp}}\"",
",",
"}",
",",
"}",
",",
"\"temperature_template\"",
":",
"template",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"color_temp\"",
")",
"==",
"expected_temp"
] | [
721,
4
] | [
761,
66
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_temperature_action_no_template | (self) | Test setting temperature with optimistic template. | Test setting temperature with optimistic template. | def test_temperature_action_no_template(self):
"""Test setting temperature with optimistic template."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{1 == 1}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_temperature": {
"service": "test.automation",
"data_template": {
"entity_id": "test.test_state",
"color_temp": "{{color_temp}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes.get("color_template") is None
common.turn_on(self.hass, "light.test_template_light", **{ATTR_COLOR_TEMP: 345})
self.hass.block_till_done()
assert len(self.calls) == 1
assert self.calls[0].data["color_temp"] == 345
state = self.hass.states.get("light.test_template_light")
_LOGGER.info(str(state.attributes))
assert state is not None
assert state.attributes.get("color_temp") == 345 | [
"def",
"test_temperature_action_no_template",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{1 == 1}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_temperature\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"test.test_state\"",
",",
"\"color_temp\"",
":",
"\"{{color_temp}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"color_template\"",
")",
"is",
"None",
"common",
".",
"turn_on",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
",",
"*",
"*",
"{",
"ATTR_COLOR_TEMP",
":",
"345",
"}",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"1",
"assert",
"self",
".",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"color_temp\"",
"]",
"==",
"345",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"_LOGGER",
".",
"info",
"(",
"str",
"(",
"state",
".",
"attributes",
")",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"color_temp\"",
")",
"==",
"345"
] | [
763,
4
] | [
809,
56
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_friendly_name | (self) | Test the accessibility of the friendly_name attribute. | Test the accessibility of the friendly_name attribute. | def test_friendly_name(self):
"""Test the accessibility of the friendly_name attribute."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"friendly_name": "Template light",
"value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state is not None
assert state.attributes.get("friendly_name") == "Template light" | [
"def",
"test_friendly_name",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"friendly_name\"",
":",
"\"Template light\"",
",",
"\"value_template\"",
":",
"\"{{ 1 == 1 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"friendly_name\"",
")",
"==",
"\"Template light\""
] | [
811,
4
] | [
852,
72
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_icon_template | (self) | Test icon template. | Test icon template. | def test_icon_template(self):
"""Test icon template."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"friendly_name": "Template light",
"value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
"icon_template": "{% if states.light.test_state.state %}"
"mdi:check"
"{% endif %}",
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes.get("icon") == ""
state = self.hass.states.set("light.test_state", STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes["icon"] == "mdi:check" | [
"def",
"test_icon_template",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"friendly_name\"",
":",
"\"Template light\"",
",",
"\"value_template\"",
":",
"\"{{ 1 == 1 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"\"icon_template\"",
":",
"\"{% if states.light.test_state.state %}\"",
"\"mdi:check\"",
"\"{% endif %}\"",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"icon\"",
")",
"==",
"\"\"",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"set",
"(",
"\"light.test_state\"",
",",
"STATE_ON",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
"[",
"\"icon\"",
"]",
"==",
"\"mdi:check\""
] | [
854,
4
] | [
903,
54
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_entity_picture_template | (self) | Test entity_picture template. | Test entity_picture template. | def test_entity_picture_template(self):
"""Test entity_picture template."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"friendly_name": "Template light",
"value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_level": {
"service": "light.turn_on",
"data_template": {
"entity_id": "light.test_state",
"brightness": "{{brightness}}",
},
},
"entity_picture_template": "{% if states.light.test_state.state %}"
"/local/light.png"
"{% endif %}",
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes.get("entity_picture") == ""
state = self.hass.states.set("light.test_state", STATE_ON)
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes["entity_picture"] == "/local/light.png" | [
"def",
"test_entity_picture_template",
"(",
"self",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"friendly_name\"",
":",
"\"Template light\"",
",",
"\"value_template\"",
":",
"\"{{ 1 == 1 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_level\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"\"brightness\"",
":",
"\"{{brightness}}\"",
",",
"}",
",",
"}",
",",
"\"entity_picture_template\"",
":",
"\"{% if states.light.test_state.state %}\"",
"\"/local/light.png\"",
"\"{% endif %}\"",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"entity_picture\"",
")",
"==",
"\"\"",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"set",
"(",
"\"light.test_state\"",
",",
"STATE_ON",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
"[",
"\"entity_picture\"",
"]",
"==",
"\"/local/light.png\""
] | [
905,
4
] | [
954,
71
] | python | en | ['fr', 'en', 'en'] | True |
TestTemplateLight.test_color_action_no_template | (self) | Test setting color with optimistic template. | Test setting color with optimistic template. | def test_color_action_no_template(self):
"""Test setting color with optimistic template."""
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{1 == 1}}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_color": [
{
"service": "test.automation",
"data_template": {
"entity_id": "test.test_state",
"h": "{{h}}",
"s": "{{s}}",
},
},
{
"service": "test.automation",
"data_template": {
"entity_id": "test.test_state",
"s": "{{s}}",
"h": "{{h}}",
},
},
],
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state.attributes.get("hs_color") is None
common.turn_on(
self.hass, "light.test_template_light", **{ATTR_HS_COLOR: (40, 50)}
)
self.hass.block_till_done()
assert len(self.calls) == 2
assert self.calls[0].data["h"] == 40
assert self.calls[0].data["s"] == 50
assert self.calls[1].data["h"] == 40
assert self.calls[1].data["s"] == 50
state = self.hass.states.get("light.test_template_light")
_LOGGER.info(str(state.attributes))
assert state is not None
assert self.calls[0].data["h"] == 40
assert self.calls[0].data["s"] == 50
assert self.calls[1].data["h"] == 40
assert self.calls[1].data["s"] == 50 | [
"def",
"test_color_action_no_template",
"(",
"self",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{1 == 1}}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_color\"",
":",
"[",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"test.test_state\"",
",",
"\"h\"",
":",
"\"{{h}}\"",
",",
"\"s\"",
":",
"\"{{s}}\"",
",",
"}",
",",
"}",
",",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"test.test_state\"",
",",
"\"s\"",
":",
"\"{{s}}\"",
",",
"\"h\"",
":",
"\"{{h}}\"",
",",
"}",
",",
"}",
",",
"]",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"hs_color\"",
")",
"is",
"None",
"common",
".",
"turn_on",
"(",
"self",
".",
"hass",
",",
"\"light.test_template_light\"",
",",
"*",
"*",
"{",
"ATTR_HS_COLOR",
":",
"(",
"40",
",",
"50",
")",
"}",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"assert",
"len",
"(",
"self",
".",
"calls",
")",
"==",
"2",
"assert",
"self",
".",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"h\"",
"]",
"==",
"40",
"assert",
"self",
".",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"s\"",
"]",
"==",
"50",
"assert",
"self",
".",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"h\"",
"]",
"==",
"40",
"assert",
"self",
".",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"s\"",
"]",
"==",
"50",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"_LOGGER",
".",
"info",
"(",
"str",
"(",
"state",
".",
"attributes",
")",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"self",
".",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"h\"",
"]",
"==",
"40",
"assert",
"self",
".",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"s\"",
"]",
"==",
"50",
"assert",
"self",
".",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"h\"",
"]",
"==",
"40",
"assert",
"self",
".",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"s\"",
"]",
"==",
"50"
] | [
956,
4
] | [
1021,
44
] | python | en | ['en', 'en', 'en'] | True |
TestTemplateLight.test_color_template | (self, expected_hs, template) | Test the template for the color. | Test the template for the color. | def test_color_template(self, expected_hs, template):
"""Test the template for the color."""
with assert_setup_component(1, "light"):
assert setup.setup_component(
self.hass,
"light",
{
"light": {
"platform": "template",
"lights": {
"test_template_light": {
"value_template": "{{ 1 == 1 }}",
"turn_on": {
"service": "light.turn_on",
"entity_id": "light.test_state",
},
"turn_off": {
"service": "light.turn_off",
"entity_id": "light.test_state",
},
"set_color": [
{
"service": "input_number.set_value",
"data_template": {
"entity_id": "input_number.h",
"color_temp": "{{h}}",
},
}
],
"color_template": template,
}
},
}
},
)
self.hass.block_till_done()
self.hass.start()
self.hass.block_till_done()
state = self.hass.states.get("light.test_template_light")
assert state is not None
assert state.attributes.get("hs_color") == expected_hs | [
"def",
"test_color_template",
"(",
"self",
",",
"expected_hs",
",",
"template",
")",
":",
"with",
"assert_setup_component",
"(",
"1",
",",
"\"light\"",
")",
":",
"assert",
"setup",
".",
"setup_component",
"(",
"self",
".",
"hass",
",",
"\"light\"",
",",
"{",
"\"light\"",
":",
"{",
"\"platform\"",
":",
"\"template\"",
",",
"\"lights\"",
":",
"{",
"\"test_template_light\"",
":",
"{",
"\"value_template\"",
":",
"\"{{ 1 == 1 }}\"",
",",
"\"turn_on\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_on\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"turn_off\"",
":",
"{",
"\"service\"",
":",
"\"light.turn_off\"",
",",
"\"entity_id\"",
":",
"\"light.test_state\"",
",",
"}",
",",
"\"set_color\"",
":",
"[",
"{",
"\"service\"",
":",
"\"input_number.set_value\"",
",",
"\"data_template\"",
":",
"{",
"\"entity_id\"",
":",
"\"input_number.h\"",
",",
"\"color_temp\"",
":",
"\"{{h}}\"",
",",
"}",
",",
"}",
"]",
",",
"\"color_template\"",
":",
"template",
",",
"}",
"}",
",",
"}",
"}",
",",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"self",
".",
"hass",
".",
"start",
"(",
")",
"self",
".",
"hass",
".",
"block_till_done",
"(",
")",
"state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"\"light.test_template_light\"",
")",
"assert",
"state",
"is",
"not",
"None",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"\"hs_color\"",
")",
"==",
"expected_hs"
] | [
1035,
4
] | [
1075,
62
] | python | en | ['en', 'en', 'en'] | True |
teardown | () | Stop everything that was started. | Stop everything that was started. | def teardown():
"""Stop everything that was started."""
dt_util.set_default_time_zone(DEFAULT_TIME_ZONE) | [
"def",
"teardown",
"(",
")",
":",
"dt_util",
".",
"set_default_time_zone",
"(",
"DEFAULT_TIME_ZONE",
")"
] | [
47,
0
] | [
49,
52
] | python | en | ['en', 'en', 'en'] | True |
test_track_point_in_time | (hass) | Test track point in time. | Test track point in time. | async def test_track_point_in_time(hass):
"""Test track point in time."""
before_birthday = datetime(1985, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
birthday_paulus = datetime(1986, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
after_birthday = datetime(1987, 7, 9, 12, 0, 0, tzinfo=dt_util.UTC)
runs = []
async_track_point_in_utc_time(
hass, callback(lambda x: runs.append(x)), birthday_paulus
)
async_fire_time_changed(hass, before_birthday)
await hass.async_block_till_done()
assert len(runs) == 0
async_fire_time_changed(hass, birthday_paulus)
await hass.async_block_till_done()
assert len(runs) == 1
# A point in time tracker will only fire once, this should do nothing
async_fire_time_changed(hass, birthday_paulus)
await hass.async_block_till_done()
assert len(runs) == 1
async_track_point_in_utc_time(
hass, callback(lambda x: runs.append(x)), birthday_paulus
)
async_fire_time_changed(hass, after_birthday)
await hass.async_block_till_done()
assert len(runs) == 2
unsub = async_track_point_in_time(
hass, callback(lambda x: runs.append(x)), birthday_paulus
)
unsub()
async_fire_time_changed(hass, after_birthday)
await hass.async_block_till_done()
assert len(runs) == 2 | [
"async",
"def",
"test_track_point_in_time",
"(",
"hass",
")",
":",
"before_birthday",
"=",
"datetime",
"(",
"1985",
",",
"7",
",",
"9",
",",
"12",
",",
"0",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"birthday_paulus",
"=",
"datetime",
"(",
"1986",
",",
"7",
",",
"9",
",",
"12",
",",
"0",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"after_birthday",
"=",
"datetime",
"(",
"1987",
",",
"7",
",",
"9",
",",
"12",
",",
"0",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"runs",
"=",
"[",
"]",
"async_track_point_in_utc_time",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"runs",
".",
"append",
"(",
"x",
")",
")",
",",
"birthday_paulus",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"before_birthday",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"birthday_paulus",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"# A point in time tracker will only fire once, this should do nothing",
"async_fire_time_changed",
"(",
"hass",
",",
"birthday_paulus",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"async_track_point_in_utc_time",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"runs",
".",
"append",
"(",
"x",
")",
")",
",",
"birthday_paulus",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"after_birthday",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"2",
"unsub",
"=",
"async_track_point_in_time",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"runs",
".",
"append",
"(",
"x",
")",
")",
",",
"birthday_paulus",
")",
"unsub",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"after_birthday",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"2"
] | [
52,
0
] | [
92,
25
] | python | en | ['en', 'en', 'en'] | True |
test_track_point_in_time_drift_rearm | (hass) | Test tasks with the time rolling backwards. | Test tasks with the time rolling backwards. | async def test_track_point_in_time_drift_rearm(hass):
"""Test tasks with the time rolling backwards."""
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC
)
async_track_point_in_utc_time(
hass,
callback(lambda x: specific_runs.append(x)),
time_that_will_not_match_right_away,
)
async_fire_time_changed(
hass,
datetime(now.year + 1, 5, 24, 21, 59, 00, tzinfo=dt_util.UTC),
fire_all=True,
)
await hass.async_block_till_done()
assert len(specific_runs) == 0
async_fire_time_changed(
hass,
datetime(now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC),
)
await hass.async_block_till_done()
assert len(specific_runs) == 1 | [
"async",
"def",
"test_track_point_in_time_drift_rearm",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"21",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"async_track_point_in_utc_time",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"time_that_will_not_match_right_away",
",",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"21",
",",
"59",
",",
"00",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
",",
"fire_all",
"=",
"True",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"21",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1"
] | [
95,
0
] | [
124,
34
] | python | en | ['en', 'en', 'en'] | True |
test_track_state_change_from_to_state_match | (hass) | Test track_state_change with from and to state matchers. | Test track_state_change with from and to state matchers. | async def test_track_state_change_from_to_state_match(hass):
"""Test track_state_change with from and to state matchers."""
from_and_to_state_runs = []
only_from_runs = []
only_to_runs = []
match_all_runs = []
no_to_from_specified_runs = []
def from_and_to_state_callback(entity_id, old_state, new_state):
from_and_to_state_runs.append(1)
def only_from_state_callback(entity_id, old_state, new_state):
only_from_runs.append(1)
def only_to_state_callback(entity_id, old_state, new_state):
only_to_runs.append(1)
def match_all_callback(entity_id, old_state, new_state):
match_all_runs.append(1)
def no_to_from_specified_callback(entity_id, old_state, new_state):
no_to_from_specified_runs.append(1)
async_track_state_change(
hass, "light.Bowl", from_and_to_state_callback, "on", "off"
)
async_track_state_change(hass, "light.Bowl", only_from_state_callback, "on", None)
async_track_state_change(
hass, "light.Bowl", only_to_state_callback, None, ["off", "standby"]
)
async_track_state_change(
hass, "light.Bowl", match_all_callback, MATCH_ALL, MATCH_ALL
)
async_track_state_change(hass, "light.Bowl", no_to_from_specified_callback)
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(from_and_to_state_runs) == 0
assert len(only_from_runs) == 0
assert len(only_to_runs) == 0
assert len(match_all_runs) == 1
assert len(no_to_from_specified_runs) == 1
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(from_and_to_state_runs) == 1
assert len(only_from_runs) == 1
assert len(only_to_runs) == 1
assert len(match_all_runs) == 2
assert len(no_to_from_specified_runs) == 2
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(from_and_to_state_runs) == 1
assert len(only_from_runs) == 1
assert len(only_to_runs) == 1
assert len(match_all_runs) == 3
assert len(no_to_from_specified_runs) == 3
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(from_and_to_state_runs) == 1
assert len(only_from_runs) == 1
assert len(only_to_runs) == 1
assert len(match_all_runs) == 3
assert len(no_to_from_specified_runs) == 3
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(from_and_to_state_runs) == 2
assert len(only_from_runs) == 2
assert len(only_to_runs) == 2
assert len(match_all_runs) == 4
assert len(no_to_from_specified_runs) == 4
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(from_and_to_state_runs) == 2
assert len(only_from_runs) == 2
assert len(only_to_runs) == 2
assert len(match_all_runs) == 4
assert len(no_to_from_specified_runs) == 4 | [
"async",
"def",
"test_track_state_change_from_to_state_match",
"(",
"hass",
")",
":",
"from_and_to_state_runs",
"=",
"[",
"]",
"only_from_runs",
"=",
"[",
"]",
"only_to_runs",
"=",
"[",
"]",
"match_all_runs",
"=",
"[",
"]",
"no_to_from_specified_runs",
"=",
"[",
"]",
"def",
"from_and_to_state_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"from_and_to_state_runs",
".",
"append",
"(",
"1",
")",
"def",
"only_from_state_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"only_from_runs",
".",
"append",
"(",
"1",
")",
"def",
"only_to_state_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"only_to_runs",
".",
"append",
"(",
"1",
")",
"def",
"match_all_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"match_all_runs",
".",
"append",
"(",
"1",
")",
"def",
"no_to_from_specified_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"no_to_from_specified_runs",
".",
"append",
"(",
"1",
")",
"async_track_state_change",
"(",
"hass",
",",
"\"light.Bowl\"",
",",
"from_and_to_state_callback",
",",
"\"on\"",
",",
"\"off\"",
")",
"async_track_state_change",
"(",
"hass",
",",
"\"light.Bowl\"",
",",
"only_from_state_callback",
",",
"\"on\"",
",",
"None",
")",
"async_track_state_change",
"(",
"hass",
",",
"\"light.Bowl\"",
",",
"only_to_state_callback",
",",
"None",
",",
"[",
"\"off\"",
",",
"\"standby\"",
"]",
")",
"async_track_state_change",
"(",
"hass",
",",
"\"light.Bowl\"",
",",
"match_all_callback",
",",
"MATCH_ALL",
",",
"MATCH_ALL",
")",
"async_track_state_change",
"(",
"hass",
",",
"\"light.Bowl\"",
",",
"no_to_from_specified_callback",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"from_and_to_state_runs",
")",
"==",
"0",
"assert",
"len",
"(",
"only_from_runs",
")",
"==",
"0",
"assert",
"len",
"(",
"only_to_runs",
")",
"==",
"0",
"assert",
"len",
"(",
"match_all_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"no_to_from_specified_runs",
")",
"==",
"1",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"from_and_to_state_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"only_from_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"only_to_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"match_all_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"no_to_from_specified_runs",
")",
"==",
"2",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"from_and_to_state_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"only_from_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"only_to_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"match_all_runs",
")",
"==",
"3",
"assert",
"len",
"(",
"no_to_from_specified_runs",
")",
"==",
"3",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"from_and_to_state_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"only_from_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"only_to_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"match_all_runs",
")",
"==",
"3",
"assert",
"len",
"(",
"no_to_from_specified_runs",
")",
"==",
"3",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"from_and_to_state_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"only_from_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"only_to_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"match_all_runs",
")",
"==",
"4",
"assert",
"len",
"(",
"no_to_from_specified_runs",
")",
"==",
"4",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"from_and_to_state_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"only_from_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"only_to_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"match_all_runs",
")",
"==",
"4",
"assert",
"len",
"(",
"no_to_from_specified_runs",
")",
"==",
"4"
] | [
127,
0
] | [
208,
46
] | python | en | ['en', 'en', 'en'] | True |
test_track_state_change | (hass) | Test track_state_change. | Test track_state_change. | async def test_track_state_change(hass):
"""Test track_state_change."""
# 2 lists to track how often our callbacks get called
specific_runs = []
wildcard_runs = []
wildercard_runs = []
def specific_run_callback(entity_id, old_state, new_state):
specific_runs.append(1)
# This is the rare use case
async_track_state_change(hass, "light.Bowl", specific_run_callback, "on", "off")
@ha.callback
def wildcard_run_callback(entity_id, old_state, new_state):
wildcard_runs.append((old_state, new_state))
# This is the most common use case
async_track_state_change(hass, "light.Bowl", wildcard_run_callback)
async def wildercard_run_callback(entity_id, old_state, new_state):
wildercard_runs.append((old_state, new_state))
async_track_state_change(hass, MATCH_ALL, wildercard_run_callback)
# Adding state to state machine
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 0
assert len(wildcard_runs) == 1
assert len(wildercard_runs) == 1
assert wildcard_runs[-1][0] is None
assert wildcard_runs[-1][1] is not None
# Set same state should not trigger a state change/listener
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 0
assert len(wildcard_runs) == 1
assert len(wildercard_runs) == 1
# State change off -> on
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 2
assert len(wildercard_runs) == 2
# State change off -> off
hass.states.async_set("light.Bowl", "off", {"some_attr": 1})
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 3
assert len(wildercard_runs) == 3
# State change off -> on
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 4
assert len(wildercard_runs) == 4
hass.states.async_remove("light.bowl")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 5
assert len(wildercard_runs) == 5
assert wildcard_runs[-1][0] is not None
assert wildcard_runs[-1][1] is None
assert wildercard_runs[-1][0] is not None
assert wildercard_runs[-1][1] is None
# Set state for different entity id
hass.states.async_set("switch.kitchen", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 5
assert len(wildercard_runs) == 6 | [
"async",
"def",
"test_track_state_change",
"(",
"hass",
")",
":",
"# 2 lists to track how often our callbacks get called",
"specific_runs",
"=",
"[",
"]",
"wildcard_runs",
"=",
"[",
"]",
"wildercard_runs",
"=",
"[",
"]",
"def",
"specific_run_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"specific_runs",
".",
"append",
"(",
"1",
")",
"# This is the rare use case",
"async_track_state_change",
"(",
"hass",
",",
"\"light.Bowl\"",
",",
"specific_run_callback",
",",
"\"on\"",
",",
"\"off\"",
")",
"@",
"ha",
".",
"callback",
"def",
"wildcard_run_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"wildcard_runs",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"# This is the most common use case",
"async_track_state_change",
"(",
"hass",
",",
"\"light.Bowl\"",
",",
"wildcard_run_callback",
")",
"async",
"def",
"wildercard_run_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"wildercard_runs",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"async_track_state_change",
"(",
"hass",
",",
"MATCH_ALL",
",",
"wildercard_run_callback",
")",
"# Adding state to state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"1",
"assert",
"wildcard_runs",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"None",
"assert",
"wildcard_runs",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"not",
"None",
"# Set same state should not trigger a state change/listener",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"1",
"# State change off -> on",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"2",
"# State change off -> off",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
",",
"{",
"\"some_attr\"",
":",
"1",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"3",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"3",
"# State change off -> on",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"4",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"4",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"5",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"5",
"assert",
"wildcard_runs",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"assert",
"wildcard_runs",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"assert",
"wildercard_runs",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"assert",
"wildercard_runs",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"# Set state for different entity id",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.kitchen\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"5",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"6"
] | [
211,
0
] | [
288,
36
] | python | en | ['en', 'en', 'en'] | False |
test_async_track_state_change_filtered | (hass) | Test async_track_state_change_filtered. | Test async_track_state_change_filtered. | async def test_async_track_state_change_filtered(hass):
"""Test async_track_state_change_filtered."""
single_entity_id_tracker = []
multiple_entity_id_tracker = []
@ha.callback
def single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
single_entity_id_tracker.append((old_state, new_state))
@ha.callback
def multiple_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
multiple_entity_id_tracker.append((old_state, new_state))
@ha.callback
def callback_that_throws(event):
raise ValueError
track_single = async_track_state_change_filtered(
hass, TrackStates(False, {"light.bowl"}, None), single_run_callback
)
assert track_single.listeners == {
"all": False,
"domains": None,
"entities": {"light.bowl"},
}
track_multi = async_track_state_change_filtered(
hass, TrackStates(False, {"light.bowl"}, {"switch"}), multiple_run_callback
)
assert track_multi.listeners == {
"all": False,
"domains": {"switch"},
"entities": {"light.bowl"},
}
track_throws = async_track_state_change_filtered(
hass, TrackStates(False, {"light.bowl"}, {"switch"}), callback_that_throws
)
assert track_throws.listeners == {
"all": False,
"domains": {"switch"},
"entities": {"light.bowl"},
}
# Adding state to state machine
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert single_entity_id_tracker[-1][0] is None
assert single_entity_id_tracker[-1][1] is not None
assert len(multiple_entity_id_tracker) == 1
assert multiple_entity_id_tracker[-1][0] is None
assert multiple_entity_id_tracker[-1][1] is not None
# Set same state should not trigger a state change/listener
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 1
# State change off -> on
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 2
assert len(multiple_entity_id_tracker) == 2
# State change off -> off
hass.states.async_set("light.Bowl", "off", {"some_attr": 1})
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 3
assert len(multiple_entity_id_tracker) == 3
# State change off -> on
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 4
assert len(multiple_entity_id_tracker) == 4
hass.states.async_remove("light.bowl")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 5
assert single_entity_id_tracker[-1][0] is not None
assert single_entity_id_tracker[-1][1] is None
assert len(multiple_entity_id_tracker) == 5
assert multiple_entity_id_tracker[-1][0] is not None
assert multiple_entity_id_tracker[-1][1] is None
# Set state for different entity id
hass.states.async_set("switch.kitchen", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 5
assert len(multiple_entity_id_tracker) == 6
track_single.async_remove()
# Ensure unsubing the listener works
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 5
assert len(multiple_entity_id_tracker) == 7
assert track_multi.listeners == {
"all": False,
"domains": {"switch"},
"entities": {"light.bowl"},
}
track_multi.async_update_listeners(TrackStates(False, {"light.bowl"}, None))
assert track_multi.listeners == {
"all": False,
"domains": None,
"entities": {"light.bowl"},
}
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(multiple_entity_id_tracker) == 8
hass.states.async_set("switch.kitchen", "off")
await hass.async_block_till_done()
assert len(multiple_entity_id_tracker) == 8
track_multi.async_update_listeners(TrackStates(True, None, None))
hass.states.async_set("switch.kitchen", "off")
await hass.async_block_till_done()
assert len(multiple_entity_id_tracker) == 8
hass.states.async_set("switch.any", "off")
await hass.async_block_till_done()
assert len(multiple_entity_id_tracker) == 9
track_multi.async_remove()
track_throws.async_remove() | [
"async",
"def",
"test_async_track_state_change_filtered",
"(",
"hass",
")",
":",
"single_entity_id_tracker",
"=",
"[",
"]",
"multiple_entity_id_tracker",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"single_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"multiple_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"multiple_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"callback_that_throws",
"(",
"event",
")",
":",
"raise",
"ValueError",
"track_single",
"=",
"async_track_state_change_filtered",
"(",
"hass",
",",
"TrackStates",
"(",
"False",
",",
"{",
"\"light.bowl\"",
"}",
",",
"None",
")",
",",
"single_run_callback",
")",
"assert",
"track_single",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"None",
",",
"\"entities\"",
":",
"{",
"\"light.bowl\"",
"}",
",",
"}",
"track_multi",
"=",
"async_track_state_change_filtered",
"(",
"hass",
",",
"TrackStates",
"(",
"False",
",",
"{",
"\"light.bowl\"",
"}",
",",
"{",
"\"switch\"",
"}",
")",
",",
"multiple_run_callback",
")",
"assert",
"track_multi",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"switch\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"light.bowl\"",
"}",
",",
"}",
"track_throws",
"=",
"async_track_state_change_filtered",
"(",
"hass",
",",
"TrackStates",
"(",
"False",
",",
"{",
"\"light.bowl\"",
"}",
",",
"{",
"\"switch\"",
"}",
")",
",",
"callback_that_throws",
")",
"assert",
"track_throws",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"switch\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"light.bowl\"",
"}",
",",
"}",
"# Adding state to state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"None",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"not",
"None",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"None",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"not",
"None",
"# Set same state should not trigger a state change/listener",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"# State change off -> on",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"2",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"2",
"# State change off -> off",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
",",
"{",
"\"some_attr\"",
":",
"1",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"3",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"3",
"# State change off -> on",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"4",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"4",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"5",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"5",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"# Set state for different entity id",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.kitchen\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"5",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"6",
"track_single",
".",
"async_remove",
"(",
")",
"# Ensure unsubing the listener works",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"5",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"7",
"assert",
"track_multi",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"switch\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"light.bowl\"",
"}",
",",
"}",
"track_multi",
".",
"async_update_listeners",
"(",
"TrackStates",
"(",
"False",
",",
"{",
"\"light.bowl\"",
"}",
",",
"None",
")",
")",
"assert",
"track_multi",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"None",
",",
"\"entities\"",
":",
"{",
"\"light.bowl\"",
"}",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"8",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.kitchen\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"8",
"track_multi",
".",
"async_update_listeners",
"(",
"TrackStates",
"(",
"True",
",",
"None",
",",
"None",
")",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.kitchen\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"8",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.any\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"9",
"track_multi",
".",
"async_remove",
"(",
")",
"track_throws",
".",
"async_remove",
"(",
")"
] | [
291,
0
] | [
424,
31
] | python | en | ['en', 'en', 'en'] | False |
test_async_track_state_change_event | (hass) | Test async_track_state_change_event. | Test async_track_state_change_event. | async def test_async_track_state_change_event(hass):
"""Test async_track_state_change_event."""
single_entity_id_tracker = []
multiple_entity_id_tracker = []
@ha.callback
def single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
single_entity_id_tracker.append((old_state, new_state))
@ha.callback
def multiple_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
multiple_entity_id_tracker.append((old_state, new_state))
@ha.callback
def callback_that_throws(event):
raise ValueError
unsub_single = async_track_state_change_event(
hass, ["light.Bowl"], single_run_callback
)
unsub_multi = async_track_state_change_event(
hass, ["light.Bowl", "switch.kitchen"], multiple_run_callback
)
unsub_throws = async_track_state_change_event(
hass, ["light.Bowl", "switch.kitchen"], callback_that_throws
)
# Adding state to state machine
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert single_entity_id_tracker[-1][0] is None
assert single_entity_id_tracker[-1][1] is not None
assert len(multiple_entity_id_tracker) == 1
assert multiple_entity_id_tracker[-1][0] is None
assert multiple_entity_id_tracker[-1][1] is not None
# Set same state should not trigger a state change/listener
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 1
# State change off -> on
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 2
assert len(multiple_entity_id_tracker) == 2
# State change off -> off
hass.states.async_set("light.Bowl", "off", {"some_attr": 1})
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 3
assert len(multiple_entity_id_tracker) == 3
# State change off -> on
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 4
assert len(multiple_entity_id_tracker) == 4
hass.states.async_remove("light.bowl")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 5
assert single_entity_id_tracker[-1][0] is not None
assert single_entity_id_tracker[-1][1] is None
assert len(multiple_entity_id_tracker) == 5
assert multiple_entity_id_tracker[-1][0] is not None
assert multiple_entity_id_tracker[-1][1] is None
# Set state for different entity id
hass.states.async_set("switch.kitchen", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 5
assert len(multiple_entity_id_tracker) == 6
unsub_single()
# Ensure unsubing the listener works
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 5
assert len(multiple_entity_id_tracker) == 7
unsub_multi()
unsub_throws() | [
"async",
"def",
"test_async_track_state_change_event",
"(",
"hass",
")",
":",
"single_entity_id_tracker",
"=",
"[",
"]",
"multiple_entity_id_tracker",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"single_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"multiple_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"multiple_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"callback_that_throws",
"(",
"event",
")",
":",
"raise",
"ValueError",
"unsub_single",
"=",
"async_track_state_change_event",
"(",
"hass",
",",
"[",
"\"light.Bowl\"",
"]",
",",
"single_run_callback",
")",
"unsub_multi",
"=",
"async_track_state_change_event",
"(",
"hass",
",",
"[",
"\"light.Bowl\"",
",",
"\"switch.kitchen\"",
"]",
",",
"multiple_run_callback",
")",
"unsub_throws",
"=",
"async_track_state_change_event",
"(",
"hass",
",",
"[",
"\"light.Bowl\"",
",",
"\"switch.kitchen\"",
"]",
",",
"callback_that_throws",
")",
"# Adding state to state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"None",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"not",
"None",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"None",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"not",
"None",
"# Set same state should not trigger a state change/listener",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"# State change off -> on",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"2",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"2",
"# State change off -> off",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
",",
"{",
"\"some_attr\"",
":",
"1",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"3",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"3",
"# State change off -> on",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"4",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"4",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"5",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"5",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"# Set state for different entity id",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.kitchen\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"5",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"6",
"unsub_single",
"(",
")",
"# Ensure unsubing the listener works",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"5",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"7",
"unsub_multi",
"(",
")",
"unsub_throws",
"(",
")"
] | [
427,
0
] | [
517,
18
] | python | en | ['en', 'en', 'en'] | False |
test_async_track_state_change_event_with_empty_list | (hass) | Test async_track_state_change_event passing an empty list of entities. | Test async_track_state_change_event passing an empty list of entities. | async def test_async_track_state_change_event_with_empty_list(hass):
"""Test async_track_state_change_event passing an empty list of entities."""
unsub_single = async_track_state_change_event(
hass, [], ha.callback(lambda event: None)
)
unsub_single2 = async_track_state_change_event(
hass, [], ha.callback(lambda event: None)
)
unsub_single2()
unsub_single() | [
"async",
"def",
"test_async_track_state_change_event_with_empty_list",
"(",
"hass",
")",
":",
"unsub_single",
"=",
"async_track_state_change_event",
"(",
"hass",
",",
"[",
"]",
",",
"ha",
".",
"callback",
"(",
"lambda",
"event",
":",
"None",
")",
")",
"unsub_single2",
"=",
"async_track_state_change_event",
"(",
"hass",
",",
"[",
"]",
",",
"ha",
".",
"callback",
"(",
"lambda",
"event",
":",
"None",
")",
")",
"unsub_single2",
"(",
")",
"unsub_single",
"(",
")"
] | [
520,
0
] | [
530,
18
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_state_added_domain | (hass) | Test async_track_state_added_domain. | Test async_track_state_added_domain. | async def test_async_track_state_added_domain(hass):
"""Test async_track_state_added_domain."""
single_entity_id_tracker = []
multiple_entity_id_tracker = []
@ha.callback
def single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
single_entity_id_tracker.append((old_state, new_state))
@ha.callback
def multiple_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
multiple_entity_id_tracker.append((old_state, new_state))
@ha.callback
def callback_that_throws(event):
raise ValueError
unsub_single = async_track_state_added_domain(hass, "light", single_run_callback)
unsub_multi = async_track_state_added_domain(
hass, ["light", "switch"], multiple_run_callback
)
unsub_throws = async_track_state_added_domain(
hass, ["light", "switch"], callback_that_throws
)
# Adding state to state machine
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert single_entity_id_tracker[-1][0] is None
assert single_entity_id_tracker[-1][1] is not None
assert len(multiple_entity_id_tracker) == 1
assert multiple_entity_id_tracker[-1][0] is None
assert multiple_entity_id_tracker[-1][1] is not None
# Set same state should not trigger a state change/listener
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 1
# State change off -> on - nothing added so no trigger
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 1
# State change off -> off - nothing added so no trigger
hass.states.async_set("light.Bowl", "off", {"some_attr": 1})
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 1
# Removing state does not trigger
hass.states.async_remove("light.bowl")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 1
# Set state for different entity id
hass.states.async_set("switch.kitchen", "on")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 2
unsub_single()
# Ensure unsubing the listener works
hass.states.async_set("light.new", "off")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(multiple_entity_id_tracker) == 3
unsub_multi()
unsub_throws() | [
"async",
"def",
"test_async_track_state_added_domain",
"(",
"hass",
")",
":",
"single_entity_id_tracker",
"=",
"[",
"]",
"multiple_entity_id_tracker",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"single_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"multiple_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"multiple_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"callback_that_throws",
"(",
"event",
")",
":",
"raise",
"ValueError",
"unsub_single",
"=",
"async_track_state_added_domain",
"(",
"hass",
",",
"\"light\"",
",",
"single_run_callback",
")",
"unsub_multi",
"=",
"async_track_state_added_domain",
"(",
"hass",
",",
"[",
"\"light\"",
",",
"\"switch\"",
"]",
",",
"multiple_run_callback",
")",
"unsub_throws",
"=",
"async_track_state_added_domain",
"(",
"hass",
",",
"[",
"\"light\"",
",",
"\"switch\"",
"]",
",",
"callback_that_throws",
")",
"# Adding state to state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"None",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"not",
"None",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"None",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"not",
"None",
"# Set same state should not trigger a state change/listener",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"# State change off -> on - nothing added so no trigger",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"# State change off -> off - nothing added so no trigger",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
",",
"{",
"\"some_attr\"",
":",
"1",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"# Removing state does not trigger",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"# Set state for different entity id",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.kitchen\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"2",
"unsub_single",
"(",
")",
"# Ensure unsubing the listener works",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.new\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"3",
"unsub_multi",
"(",
")",
"unsub_throws",
"(",
")"
] | [
533,
0
] | [
612,
18
] | python | en | ['en', 'en', 'en'] | False |
test_async_track_state_added_domain_with_empty_list | (hass) | Test async_track_state_added_domain passing an empty list of domains. | Test async_track_state_added_domain passing an empty list of domains. | async def test_async_track_state_added_domain_with_empty_list(hass):
"""Test async_track_state_added_domain passing an empty list of domains."""
unsub_single = async_track_state_added_domain(
hass, [], ha.callback(lambda event: None)
)
unsub_single2 = async_track_state_added_domain(
hass, [], ha.callback(lambda event: None)
)
unsub_single2()
unsub_single() | [
"async",
"def",
"test_async_track_state_added_domain_with_empty_list",
"(",
"hass",
")",
":",
"unsub_single",
"=",
"async_track_state_added_domain",
"(",
"hass",
",",
"[",
"]",
",",
"ha",
".",
"callback",
"(",
"lambda",
"event",
":",
"None",
")",
")",
"unsub_single2",
"=",
"async_track_state_added_domain",
"(",
"hass",
",",
"[",
"]",
",",
"ha",
".",
"callback",
"(",
"lambda",
"event",
":",
"None",
")",
")",
"unsub_single2",
"(",
")",
"unsub_single",
"(",
")"
] | [
615,
0
] | [
625,
18
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_state_removed_domain_with_empty_list | (hass) | Test async_track_state_removed_domain passing an empty list of domains. | Test async_track_state_removed_domain passing an empty list of domains. | async def test_async_track_state_removed_domain_with_empty_list(hass):
"""Test async_track_state_removed_domain passing an empty list of domains."""
unsub_single = async_track_state_removed_domain(
hass, [], ha.callback(lambda event: None)
)
unsub_single2 = async_track_state_removed_domain(
hass, [], ha.callback(lambda event: None)
)
unsub_single2()
unsub_single() | [
"async",
"def",
"test_async_track_state_removed_domain_with_empty_list",
"(",
"hass",
")",
":",
"unsub_single",
"=",
"async_track_state_removed_domain",
"(",
"hass",
",",
"[",
"]",
",",
"ha",
".",
"callback",
"(",
"lambda",
"event",
":",
"None",
")",
")",
"unsub_single2",
"=",
"async_track_state_removed_domain",
"(",
"hass",
",",
"[",
"]",
",",
"ha",
".",
"callback",
"(",
"lambda",
"event",
":",
"None",
")",
")",
"unsub_single2",
"(",
")",
"unsub_single",
"(",
")"
] | [
628,
0
] | [
638,
18
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_state_removed_domain | (hass) | Test async_track_state_removed_domain. | Test async_track_state_removed_domain. | async def test_async_track_state_removed_domain(hass):
"""Test async_track_state_removed_domain."""
single_entity_id_tracker = []
multiple_entity_id_tracker = []
@ha.callback
def single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
single_entity_id_tracker.append((old_state, new_state))
@ha.callback
def multiple_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
multiple_entity_id_tracker.append((old_state, new_state))
@ha.callback
def callback_that_throws(event):
raise ValueError
unsub_single = async_track_state_removed_domain(hass, "light", single_run_callback)
unsub_multi = async_track_state_removed_domain(
hass, ["light", "switch"], multiple_run_callback
)
unsub_throws = async_track_state_removed_domain(
hass, ["light", "switch"], callback_that_throws
)
# Adding state to state machine
hass.states.async_set("light.Bowl", "on")
hass.states.async_remove("light.Bowl")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert single_entity_id_tracker[-1][1] is None
assert single_entity_id_tracker[-1][0] is not None
assert len(multiple_entity_id_tracker) == 1
assert multiple_entity_id_tracker[-1][1] is None
assert multiple_entity_id_tracker[-1][0] is not None
# Added and than removed (light)
hass.states.async_set("light.Bowl", "on")
hass.states.async_remove("light.Bowl")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 2
assert len(multiple_entity_id_tracker) == 2
# Added and than removed (light)
hass.states.async_set("light.Bowl", "off")
hass.states.async_remove("light.Bowl")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 3
assert len(multiple_entity_id_tracker) == 3
# Added and than removed (light)
hass.states.async_set("light.Bowl", "off", {"some_attr": 1})
hass.states.async_remove("light.Bowl")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 4
assert len(multiple_entity_id_tracker) == 4
# Added and than removed (switch)
hass.states.async_set("switch.kitchen", "on")
hass.states.async_remove("switch.kitchen")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 4
assert len(multiple_entity_id_tracker) == 5
unsub_single()
# Ensure unsubing the listener works
hass.states.async_set("light.new", "off")
hass.states.async_remove("light.new")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 4
assert len(multiple_entity_id_tracker) == 6
unsub_multi()
unsub_throws() | [
"async",
"def",
"test_async_track_state_removed_domain",
"(",
"hass",
")",
":",
"single_entity_id_tracker",
"=",
"[",
"]",
"multiple_entity_id_tracker",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"single_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"multiple_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"multiple_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"callback_that_throws",
"(",
"event",
")",
":",
"raise",
"ValueError",
"unsub_single",
"=",
"async_track_state_removed_domain",
"(",
"hass",
",",
"\"light\"",
",",
"single_run_callback",
")",
"unsub_multi",
"=",
"async_track_state_removed_domain",
"(",
"hass",
",",
"[",
"\"light\"",
",",
"\"switch\"",
"]",
",",
"multiple_run_callback",
")",
"unsub_throws",
"=",
"async_track_state_removed_domain",
"(",
"hass",
",",
"[",
"\"light\"",
",",
"\"switch\"",
"]",
",",
"callback_that_throws",
")",
"# Adding state to state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.Bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"assert",
"single_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"1",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
"is",
"None",
"assert",
"multiple_entity_id_tracker",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"is",
"not",
"None",
"# Added and than removed (light)",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.Bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"2",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"2",
"# Added and than removed (light)",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.Bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"3",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"3",
"# Added and than removed (light)",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
",",
"{",
"\"some_attr\"",
":",
"1",
"}",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.Bowl\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"4",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"4",
"# Added and than removed (switch)",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.kitchen\"",
",",
"\"on\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"switch.kitchen\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"4",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"5",
"unsub_single",
"(",
")",
"# Ensure unsubing the listener works",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.new\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.new\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"4",
"assert",
"len",
"(",
"multiple_entity_id_tracker",
")",
"==",
"6",
"unsub_multi",
"(",
")",
"unsub_throws",
"(",
")"
] | [
641,
0
] | [
720,
18
] | python | en | ['en', 'en', 'en'] | False |
test_async_track_state_removed_domain_match_all | (hass) | Test async_track_state_removed_domain with a match_all. | Test async_track_state_removed_domain with a match_all. | async def test_async_track_state_removed_domain_match_all(hass):
"""Test async_track_state_removed_domain with a match_all."""
single_entity_id_tracker = []
match_all_entity_id_tracker = []
@ha.callback
def single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
single_entity_id_tracker.append((old_state, new_state))
@ha.callback
def match_all_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
match_all_entity_id_tracker.append((old_state, new_state))
unsub_single = async_track_state_removed_domain(hass, "light", single_run_callback)
unsub_match_all = async_track_state_removed_domain(
hass, MATCH_ALL, match_all_run_callback
)
hass.states.async_set("light.new", "off")
hass.states.async_remove("light.new")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(match_all_entity_id_tracker) == 1
hass.states.async_set("switch.new", "off")
hass.states.async_remove("switch.new")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(match_all_entity_id_tracker) == 2
unsub_match_all()
unsub_single()
hass.states.async_set("switch.new", "off")
hass.states.async_remove("switch.new")
await hass.async_block_till_done()
assert len(single_entity_id_tracker) == 1
assert len(match_all_entity_id_tracker) == 2 | [
"async",
"def",
"test_async_track_state_removed_domain_match_all",
"(",
"hass",
")",
":",
"single_entity_id_tracker",
"=",
"[",
"]",
"match_all_entity_id_tracker",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"single_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"match_all_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"match_all_entity_id_tracker",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"unsub_single",
"=",
"async_track_state_removed_domain",
"(",
"hass",
",",
"\"light\"",
",",
"single_run_callback",
")",
"unsub_match_all",
"=",
"async_track_state_removed_domain",
"(",
"hass",
",",
"MATCH_ALL",
",",
"match_all_run_callback",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.new\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"light.new\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"match_all_entity_id_tracker",
")",
"==",
"1",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.new\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"switch.new\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"match_all_entity_id_tracker",
")",
"==",
"2",
"unsub_match_all",
"(",
")",
"unsub_single",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.new\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"switch.new\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"single_entity_id_tracker",
")",
"==",
"1",
"assert",
"len",
"(",
"match_all_entity_id_tracker",
")",
"==",
"2"
] | [
723,
0
] | [
764,
48
] | python | en | ['en', 'en', 'en'] | True |
test_track_template | (hass) | Test tracking template. | Test tracking template. | async def test_track_template(hass):
"""Test tracking template."""
specific_runs = []
wildcard_runs = []
wildercard_runs = []
template_condition = Template("{{states.switch.test.state == 'on'}}", hass)
template_condition_var = Template(
"{{states.switch.test.state == 'on' and test == 5}}", hass
)
hass.states.async_set("switch.test", "off")
def specific_run_callback(entity_id, old_state, new_state):
specific_runs.append(1)
async_track_template(hass, template_condition, specific_run_callback)
@ha.callback
def wildcard_run_callback(entity_id, old_state, new_state):
wildcard_runs.append((old_state, new_state))
async_track_template(hass, template_condition, wildcard_run_callback)
async def wildercard_run_callback(entity_id, old_state, new_state):
wildercard_runs.append((old_state, new_state))
async_track_template(
hass, template_condition_var, wildercard_run_callback, {"test": 5}
)
hass.states.async_set("switch.test", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 1
assert len(wildercard_runs) == 1
hass.states.async_set("switch.test", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 1
assert len(wildercard_runs) == 1
hass.states.async_set("switch.test", "off")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 1
assert len(wildercard_runs) == 1
hass.states.async_set("switch.test", "off")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 1
assert len(wildercard_runs) == 1
hass.states.async_set("switch.test", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 2
assert len(wildcard_runs) == 2
assert len(wildercard_runs) == 2
template_iterate = Template("{{ (states.switch | length) > 0 }}", hass)
iterate_calls = []
@ha.callback
def iterate_callback(entity_id, old_state, new_state):
iterate_calls.append((entity_id, old_state, new_state))
async_track_template(hass, template_iterate, iterate_callback)
await hass.async_block_till_done()
hass.states.async_set("switch.new", "on")
await hass.async_block_till_done()
assert len(iterate_calls) == 1
assert iterate_calls[0][0] == "switch.new"
assert iterate_calls[0][1] is None
assert iterate_calls[0][2].state == "on" | [
"async",
"def",
"test_track_template",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"wildcard_runs",
"=",
"[",
"]",
"wildercard_runs",
"=",
"[",
"]",
"template_condition",
"=",
"Template",
"(",
"\"{{states.switch.test.state == 'on'}}\"",
",",
"hass",
")",
"template_condition_var",
"=",
"Template",
"(",
"\"{{states.switch.test.state == 'on' and test == 5}}\"",
",",
"hass",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"def",
"specific_run_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"specific_runs",
".",
"append",
"(",
"1",
")",
"async_track_template",
"(",
"hass",
",",
"template_condition",
",",
"specific_run_callback",
")",
"@",
"ha",
".",
"callback",
"def",
"wildcard_run_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"wildcard_runs",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"async_track_template",
"(",
"hass",
",",
"template_condition",
",",
"wildcard_run_callback",
")",
"async",
"def",
"wildercard_run_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"wildercard_runs",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"async_track_template",
"(",
"hass",
",",
"template_condition_var",
",",
"wildercard_run_callback",
",",
"{",
"\"test\"",
":",
"5",
"}",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"1",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"1",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"1",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"1",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"2",
"template_iterate",
"=",
"Template",
"(",
"\"{{ (states.switch | length) > 0 }}\"",
",",
"hass",
")",
"iterate_calls",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"iterate_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"iterate_calls",
".",
"append",
"(",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
")",
"async_track_template",
"(",
"hass",
",",
"template_iterate",
",",
"iterate_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.new\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"iterate_calls",
")",
"==",
"1",
"assert",
"iterate_calls",
"[",
"0",
"]",
"[",
"0",
"]",
"==",
"\"switch.new\"",
"assert",
"iterate_calls",
"[",
"0",
"]",
"[",
"1",
"]",
"is",
"None",
"assert",
"iterate_calls",
"[",
"0",
"]",
"[",
"2",
"]",
".",
"state",
"==",
"\"on\""
] | [
767,
0
] | [
849,
44
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_error | (hass, caplog) | Test tracking template with error. | Test tracking template with error. | async def test_track_template_error(hass, caplog):
"""Test tracking template with error."""
template_error = Template("{{ (states.switch | lunch) > 0 }}", hass)
error_calls = []
@ha.callback
def error_callback(entity_id, old_state, new_state):
error_calls.append((entity_id, old_state, new_state))
async_track_template(hass, template_error, error_callback)
await hass.async_block_till_done()
hass.states.async_set("switch.new", "on")
await hass.async_block_till_done()
assert not error_calls
assert "lunch" in caplog.text
assert "TemplateAssertionError" in caplog.text
caplog.clear()
with patch.object(Template, "async_render") as render:
render.return_value = "ok"
hass.states.async_set("switch.not_exist", "off")
await hass.async_block_till_done()
assert "no filter named 'lunch'" not in caplog.text
assert "TemplateAssertionError" not in caplog.text | [
"async",
"def",
"test_track_template_error",
"(",
"hass",
",",
"caplog",
")",
":",
"template_error",
"=",
"Template",
"(",
"\"{{ (states.switch | lunch) > 0 }}\"",
",",
"hass",
")",
"error_calls",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"error_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"error_calls",
".",
"append",
"(",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
")",
"async_track_template",
"(",
"hass",
",",
"template_error",
",",
"error_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.new\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"not",
"error_calls",
"assert",
"\"lunch\"",
"in",
"caplog",
".",
"text",
"assert",
"\"TemplateAssertionError\"",
"in",
"caplog",
".",
"text",
"caplog",
".",
"clear",
"(",
")",
"with",
"patch",
".",
"object",
"(",
"Template",
",",
"\"async_render\"",
")",
"as",
"render",
":",
"render",
".",
"return_value",
"=",
"\"ok\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.not_exist\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"\"no filter named 'lunch'\"",
"not",
"in",
"caplog",
".",
"text",
"assert",
"\"TemplateAssertionError\"",
"not",
"in",
"caplog",
".",
"text"
] | [
852,
0
] | [
880,
54
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_error_can_recover | (hass, caplog) | Test tracking template with error. | Test tracking template with error. | async def test_track_template_error_can_recover(hass, caplog):
"""Test tracking template with error."""
hass.states.async_set("switch.data_system", "cow", {"opmode": 0})
template_error = Template(
"{{ states.sensor.data_system.attributes['opmode'] == '0' }}", hass
)
error_calls = []
@ha.callback
def error_callback(entity_id, old_state, new_state):
error_calls.append((entity_id, old_state, new_state))
async_track_template(hass, template_error, error_callback)
await hass.async_block_till_done()
assert not error_calls
hass.states.async_remove("switch.data_system")
assert "UndefinedError" in caplog.text
hass.states.async_set("switch.data_system", "cow", {"opmode": 0})
caplog.clear()
assert "UndefinedError" not in caplog.text | [
"async",
"def",
"test_track_template_error_can_recover",
"(",
"hass",
",",
"caplog",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.data_system\"",
",",
"\"cow\"",
",",
"{",
"\"opmode\"",
":",
"0",
"}",
")",
"template_error",
"=",
"Template",
"(",
"\"{{ states.sensor.data_system.attributes['opmode'] == '0' }}\"",
",",
"hass",
")",
"error_calls",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"error_callback",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
":",
"error_calls",
".",
"append",
"(",
"(",
"entity_id",
",",
"old_state",
",",
"new_state",
")",
")",
"async_track_template",
"(",
"hass",
",",
"template_error",
",",
"error_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"not",
"error_calls",
"hass",
".",
"states",
".",
"async_remove",
"(",
"\"switch.data_system\"",
")",
"assert",
"\"UndefinedError\"",
"in",
"caplog",
".",
"text",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.data_system\"",
",",
"\"cow\"",
",",
"{",
"\"opmode\"",
":",
"0",
"}",
")",
"caplog",
".",
"clear",
"(",
")",
"assert",
"\"UndefinedError\"",
"not",
"in",
"caplog",
".",
"text"
] | [
883,
0
] | [
907,
46
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_result | (hass) | Test tracking template. | Test tracking template. | async def test_track_template_result(hass):
"""Test tracking template."""
specific_runs = []
wildcard_runs = []
wildercard_runs = []
template_condition = Template("{{states.sensor.test.state}}", hass)
template_condition_var = Template(
"{{(states.sensor.test.state|int) + test }}", hass
)
def specific_run_callback(event, updates):
track_result = updates.pop()
specific_runs.append(int(track_result.result))
async_track_template_result(
hass, [TrackTemplate(template_condition, None)], specific_run_callback
)
@ha.callback
def wildcard_run_callback(event, updates):
track_result = updates.pop()
wildcard_runs.append(
(int(track_result.last_result or 0), int(track_result.result))
)
async_track_template_result(
hass, [TrackTemplate(template_condition, None)], wildcard_run_callback
)
async def wildercard_run_callback(event, updates):
track_result = updates.pop()
wildercard_runs.append(
(int(track_result.last_result or 0), int(track_result.result))
)
async_track_template_result(
hass,
[TrackTemplate(template_condition_var, {"test": 5})],
wildercard_run_callback,
)
await hass.async_block_till_done()
hass.states.async_set("sensor.test", 5)
await hass.async_block_till_done()
assert specific_runs == [5]
assert wildcard_runs == [(0, 5)]
assert wildercard_runs == [(0, 10)]
hass.states.async_set("sensor.test", 30)
await hass.async_block_till_done()
assert specific_runs == [5, 30]
assert wildcard_runs == [(0, 5), (5, 30)]
assert wildercard_runs == [(0, 10), (10, 35)]
hass.states.async_set("sensor.test", 30)
await hass.async_block_till_done()
assert len(specific_runs) == 2
assert len(wildcard_runs) == 2
assert len(wildercard_runs) == 2
hass.states.async_set("sensor.test", 5)
await hass.async_block_till_done()
assert len(specific_runs) == 3
assert len(wildcard_runs) == 3
assert len(wildercard_runs) == 3
hass.states.async_set("sensor.test", 5)
await hass.async_block_till_done()
assert len(specific_runs) == 3
assert len(wildcard_runs) == 3
assert len(wildercard_runs) == 3
hass.states.async_set("sensor.test", 20)
await hass.async_block_till_done()
assert len(specific_runs) == 4
assert len(wildcard_runs) == 4
assert len(wildercard_runs) == 4 | [
"async",
"def",
"test_track_template_result",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"wildcard_runs",
"=",
"[",
"]",
"wildercard_runs",
"=",
"[",
"]",
"template_condition",
"=",
"Template",
"(",
"\"{{states.sensor.test.state}}\"",
",",
"hass",
")",
"template_condition_var",
"=",
"Template",
"(",
"\"{{(states.sensor.test.state|int) + test }}\"",
",",
"hass",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"track_result",
"=",
"updates",
".",
"pop",
"(",
")",
"specific_runs",
".",
"append",
"(",
"int",
"(",
"track_result",
".",
"result",
")",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_condition",
",",
"None",
")",
"]",
",",
"specific_run_callback",
")",
"@",
"ha",
".",
"callback",
"def",
"wildcard_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"track_result",
"=",
"updates",
".",
"pop",
"(",
")",
"wildcard_runs",
".",
"append",
"(",
"(",
"int",
"(",
"track_result",
".",
"last_result",
"or",
"0",
")",
",",
"int",
"(",
"track_result",
".",
"result",
")",
")",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_condition",
",",
"None",
")",
"]",
",",
"wildcard_run_callback",
")",
"async",
"def",
"wildercard_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"track_result",
"=",
"updates",
".",
"pop",
"(",
")",
"wildercard_runs",
".",
"append",
"(",
"(",
"int",
"(",
"track_result",
".",
"last_result",
"or",
"0",
")",
",",
"int",
"(",
"track_result",
".",
"result",
")",
")",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_condition_var",
",",
"{",
"\"test\"",
":",
"5",
"}",
")",
"]",
",",
"wildercard_run_callback",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"5",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"specific_runs",
"==",
"[",
"5",
"]",
"assert",
"wildcard_runs",
"==",
"[",
"(",
"0",
",",
"5",
")",
"]",
"assert",
"wildercard_runs",
"==",
"[",
"(",
"0",
",",
"10",
")",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"30",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"specific_runs",
"==",
"[",
"5",
",",
"30",
"]",
"assert",
"wildcard_runs",
"==",
"[",
"(",
"0",
",",
"5",
")",
",",
"(",
"5",
",",
"30",
")",
"]",
"assert",
"wildercard_runs",
"==",
"[",
"(",
"0",
",",
"10",
")",
",",
"(",
"10",
",",
"35",
")",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"30",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"2",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"5",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"3",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"3",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"3",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"5",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"3",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"3",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"3",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"20",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"4",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"4",
"assert",
"len",
"(",
"wildercard_runs",
")",
"==",
"4"
] | [
910,
0
] | [
993,
36
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_result_complex | (hass) | Test tracking template. | Test tracking template. | async def test_track_template_result_complex(hass):
"""Test tracking template."""
specific_runs = []
template_complex_str = """
{% if states("sensor.domain") == "light" %}
{{ states.light | map(attribute='entity_id') | list }}
{% elif states("sensor.domain") == "lock" %}
{{ states.lock | map(attribute='entity_id') | list }}
{% elif states("sensor.domain") == "single_binary_sensor" %}
{{ states("binary_sensor.single") }}
{% else %}
{{ states | map(attribute='entity_id') | list }}
{% endif %}
"""
template_complex = Template(template_complex_str, hass)
def specific_run_callback(event, updates):
specific_runs.append(updates.pop().result)
hass.states.async_set("light.one", "on")
hass.states.async_set("lock.one", "locked")
info = async_track_template_result(
hass,
[TrackTemplate(template_complex, None, timedelta(seconds=0))],
specific_run_callback,
)
await hass.async_block_till_done()
assert info.listeners == {
"all": True,
"domains": set(),
"entities": set(),
"time": False,
}
hass.states.async_set("sensor.domain", "light")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert specific_runs[0] == ["light.one"]
assert info.listeners == {
"all": False,
"domains": {"light"},
"entities": {"sensor.domain"},
"time": False,
}
hass.states.async_set("sensor.domain", "lock")
await hass.async_block_till_done()
assert len(specific_runs) == 2
assert specific_runs[1] == ["lock.one"]
assert info.listeners == {
"all": False,
"domains": {"lock"},
"entities": {"sensor.domain"},
"time": False,
}
hass.states.async_set("sensor.domain", "all")
await hass.async_block_till_done()
assert len(specific_runs) == 3
assert "light.one" in specific_runs[2]
assert "lock.one" in specific_runs[2]
assert "sensor.domain" in specific_runs[2]
assert info.listeners == {
"all": True,
"domains": set(),
"entities": set(),
"time": False,
}
hass.states.async_set("sensor.domain", "light")
await hass.async_block_till_done()
assert len(specific_runs) == 4
assert specific_runs[3] == ["light.one"]
assert info.listeners == {
"all": False,
"domains": {"light"},
"entities": {"sensor.domain"},
"time": False,
}
hass.states.async_set("light.two", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 5
assert "light.one" in specific_runs[4]
assert "light.two" in specific_runs[4]
assert "sensor.domain" not in specific_runs[4]
assert info.listeners == {
"all": False,
"domains": {"light"},
"entities": {"sensor.domain"},
"time": False,
}
hass.states.async_set("light.three", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 6
assert "light.one" in specific_runs[5]
assert "light.two" in specific_runs[5]
assert "light.three" in specific_runs[5]
assert "sensor.domain" not in specific_runs[5]
assert info.listeners == {
"all": False,
"domains": {"light"},
"entities": {"sensor.domain"},
"time": False,
}
hass.states.async_set("sensor.domain", "lock")
await hass.async_block_till_done()
assert len(specific_runs) == 7
assert specific_runs[6] == ["lock.one"]
assert info.listeners == {
"all": False,
"domains": {"lock"},
"entities": {"sensor.domain"},
"time": False,
}
hass.states.async_set("sensor.domain", "single_binary_sensor")
await hass.async_block_till_done()
assert len(specific_runs) == 8
assert specific_runs[7] == "unknown"
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.single", "sensor.domain"},
"time": False,
}
hass.states.async_set("binary_sensor.single", "binary_sensor_on")
await hass.async_block_till_done()
assert len(specific_runs) == 9
assert specific_runs[8] == "binary_sensor_on"
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.single", "sensor.domain"},
"time": False,
}
hass.states.async_set("sensor.domain", "lock")
await hass.async_block_till_done()
assert len(specific_runs) == 10
assert specific_runs[9] == ["lock.one"]
assert info.listeners == {
"all": False,
"domains": {"lock"},
"entities": {"sensor.domain"},
"time": False,
} | [
"async",
"def",
"test_track_template_result_complex",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"template_complex_str",
"=",
"\"\"\"\n{% if states(\"sensor.domain\") == \"light\" %}\n {{ states.light | map(attribute='entity_id') | list }}\n{% elif states(\"sensor.domain\") == \"lock\" %}\n {{ states.lock | map(attribute='entity_id') | list }}\n{% elif states(\"sensor.domain\") == \"single_binary_sensor\" %}\n {{ states(\"binary_sensor.single\") }}\n{% else %}\n {{ states | map(attribute='entity_id') | list }}\n{% endif %}\n\n\"\"\"",
"template_complex",
"=",
"Template",
"(",
"template_complex_str",
",",
"hass",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"specific_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.one\"",
",",
"\"on\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"lock.one\"",
",",
"\"locked\"",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_complex",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"0",
")",
")",
"]",
",",
"specific_run_callback",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"True",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.domain\"",
",",
"\"light\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"specific_runs",
"[",
"0",
"]",
"==",
"[",
"\"light.one\"",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"light\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.domain\"",
",",
"\"lock\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"assert",
"specific_runs",
"[",
"1",
"]",
"==",
"[",
"\"lock.one\"",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"lock\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.domain\"",
",",
"\"all\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"3",
"assert",
"\"light.one\"",
"in",
"specific_runs",
"[",
"2",
"]",
"assert",
"\"lock.one\"",
"in",
"specific_runs",
"[",
"2",
"]",
"assert",
"\"sensor.domain\"",
"in",
"specific_runs",
"[",
"2",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"True",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.domain\"",
",",
"\"light\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"4",
"assert",
"specific_runs",
"[",
"3",
"]",
"==",
"[",
"\"light.one\"",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"light\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.two\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"5",
"assert",
"\"light.one\"",
"in",
"specific_runs",
"[",
"4",
"]",
"assert",
"\"light.two\"",
"in",
"specific_runs",
"[",
"4",
"]",
"assert",
"\"sensor.domain\"",
"not",
"in",
"specific_runs",
"[",
"4",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"light\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.three\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"6",
"assert",
"\"light.one\"",
"in",
"specific_runs",
"[",
"5",
"]",
"assert",
"\"light.two\"",
"in",
"specific_runs",
"[",
"5",
"]",
"assert",
"\"light.three\"",
"in",
"specific_runs",
"[",
"5",
"]",
"assert",
"\"sensor.domain\"",
"not",
"in",
"specific_runs",
"[",
"5",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"light\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.domain\"",
",",
"\"lock\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"7",
"assert",
"specific_runs",
"[",
"6",
"]",
"==",
"[",
"\"lock.one\"",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"lock\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.domain\"",
",",
"\"single_binary_sensor\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"8",
"assert",
"specific_runs",
"[",
"7",
"]",
"==",
"\"unknown\"",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"binary_sensor.single\"",
",",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"binary_sensor.single\"",
",",
"\"binary_sensor_on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"9",
"assert",
"specific_runs",
"[",
"8",
"]",
"==",
"\"binary_sensor_on\"",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"binary_sensor.single\"",
",",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.domain\"",
",",
"\"lock\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"10",
"assert",
"specific_runs",
"[",
"9",
"]",
"==",
"[",
"\"lock.one\"",
"]",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"lock\"",
"}",
",",
"\"entities\"",
":",
"{",
"\"sensor.domain\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}"
] | [
996,
0
] | [
1149,
5
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_result_with_wildcard | (hass) | Test tracking template with a wildcard. | Test tracking template with a wildcard. | async def test_track_template_result_with_wildcard(hass):
"""Test tracking template with a wildcard."""
specific_runs = []
template_complex_str = r"""
{% for state in states %}
{% if state.entity_id | regex_match('.*\.office_') %}
{{ state.entity_id }}={{ state.state }}
{% endif %}
{% endfor %}
"""
template_complex = Template(template_complex_str, hass)
def specific_run_callback(event, updates):
specific_runs.append(updates.pop().result)
hass.states.async_set("cover.office_drapes", "closed")
hass.states.async_set("cover.office_window", "closed")
hass.states.async_set("cover.office_skylight", "open")
info = async_track_template_result(
hass, [TrackTemplate(template_complex, None)], specific_run_callback
)
await hass.async_block_till_done()
hass.states.async_set("cover.office_window", "open")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert info.listeners == {
"all": True,
"domains": set(),
"entities": set(),
"time": False,
}
assert "cover.office_drapes=closed" in specific_runs[0]
assert "cover.office_window=open" in specific_runs[0]
assert "cover.office_skylight=open" in specific_runs[0] | [
"async",
"def",
"test_track_template_result_with_wildcard",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"template_complex_str",
"=",
"r\"\"\"\n\n{% for state in states %}\n {% if state.entity_id | regex_match('.*\\.office_') %}\n {{ state.entity_id }}={{ state.state }}\n {% endif %}\n{% endfor %}\n\n\"\"\"",
"template_complex",
"=",
"Template",
"(",
"template_complex_str",
",",
"hass",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"specific_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.office_drapes\"",
",",
"\"closed\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.office_window\"",
",",
"\"closed\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.office_skylight\"",
",",
"\"open\"",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_complex",
",",
"None",
")",
"]",
",",
"specific_run_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.office_window\"",
",",
"\"open\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"True",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"assert",
"\"cover.office_drapes=closed\"",
"in",
"specific_runs",
"[",
"0",
"]",
"assert",
"\"cover.office_window=open\"",
"in",
"specific_runs",
"[",
"0",
"]",
"assert",
"\"cover.office_skylight=open\"",
"in",
"specific_runs",
"[",
"0",
"]"
] | [
1152,
0
] | [
1190,
59
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_result_with_group | (hass) | Test tracking template with a group. | Test tracking template with a group. | async def test_track_template_result_with_group(hass):
"""Test tracking template with a group."""
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)
hass.states.async_set("sensor.power_4", 800.8)
assert await async_setup_component(
hass,
"group",
{"group": {"power_sensors": "sensor.power_1,sensor.power_2,sensor.power_3"}},
)
await hass.async_block_till_done()
assert hass.states.get("group.power_sensors")
assert hass.states.get("group.power_sensors").state
specific_runs = []
template_complex_str = r"""
{{ states.group.power_sensors.attributes.entity_id | expand | map(attribute='state')|map('float')|sum }}
"""
template_complex = Template(template_complex_str, hass)
def specific_run_callback(event, updates):
specific_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_complex, None)], specific_run_callback
)
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {
"group.power_sensors",
"sensor.power_1",
"sensor.power_2",
"sensor.power_3",
},
"time": False,
}
hass.states.async_set("sensor.power_1", 100.1)
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert specific_runs[0] == 100.1 + 200.2 + 400.4
hass.states.async_set("sensor.power_3", 0)
await hass.async_block_till_done()
assert len(specific_runs) == 2
assert specific_runs[1] == 100.1 + 200.2 + 0
with patch(
"homeassistant.config.load_yaml_config_file",
return_value={
"group": {
"power_sensors": "sensor.power_1,sensor.power_2,sensor.power_3,sensor.power_4",
}
},
):
await hass.services.async_call("group", "reload")
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert specific_runs[-1] == 100.1 + 200.2 + 0 + 800.8 | [
"async",
"def",
"test_track_template_result_with_group",
"(",
"hass",
")",
":",
"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",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.power_4\"",
",",
"800.8",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"\"group\"",
",",
"{",
"\"group\"",
":",
"{",
"\"power_sensors\"",
":",
"\"sensor.power_1,sensor.power_2,sensor.power_3\"",
"}",
"}",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"hass",
".",
"states",
".",
"get",
"(",
"\"group.power_sensors\"",
")",
"assert",
"hass",
".",
"states",
".",
"get",
"(",
"\"group.power_sensors\"",
")",
".",
"state",
"specific_runs",
"=",
"[",
"]",
"template_complex_str",
"=",
"r\"\"\"\n\n{{ states.group.power_sensors.attributes.entity_id | expand | map(attribute='state')|map('float')|sum }}\n\n\"\"\"",
"template_complex",
"=",
"Template",
"(",
"template_complex_str",
",",
"hass",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"specific_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_complex",
",",
"None",
")",
"]",
",",
"specific_run_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"group.power_sensors\"",
",",
"\"sensor.power_1\"",
",",
"\"sensor.power_2\"",
",",
"\"sensor.power_3\"",
",",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.power_1\"",
",",
"100.1",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"specific_runs",
"[",
"0",
"]",
"==",
"100.1",
"+",
"200.2",
"+",
"400.4",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.power_3\"",
",",
"0",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"assert",
"specific_runs",
"[",
"1",
"]",
"==",
"100.1",
"+",
"200.2",
"+",
"0",
"with",
"patch",
"(",
"\"homeassistant.config.load_yaml_config_file\"",
",",
"return_value",
"=",
"{",
"\"group\"",
":",
"{",
"\"power_sensors\"",
":",
"\"sensor.power_1,sensor.power_2,sensor.power_3,sensor.power_4\"",
",",
"}",
"}",
",",
")",
":",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"\"group\"",
",",
"\"reload\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"specific_runs",
"[",
"-",
"1",
"]",
"==",
"100.1",
"+",
"200.2",
"+",
"0",
"+",
"800.8"
] | [
1193,
0
] | [
1263,
57
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_result_and_conditional | (hass) | Test tracking template with an and conditional. | Test tracking template with an and conditional. | async def test_track_template_result_and_conditional(hass):
"""Test tracking template with an and conditional."""
specific_runs = []
hass.states.async_set("light.a", "off")
hass.states.async_set("light.b", "off")
template_str = '{% if states.light.a.state == "on" and states.light.b.state == "on" %}on{% else %}off{% endif %}'
template = Template(template_str, hass)
def specific_run_callback(event, updates):
specific_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template, None)], specific_run_callback
)
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"light.a"},
"time": False,
}
hass.states.async_set("light.b", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 0
hass.states.async_set("light.a", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert specific_runs[0] == "on"
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"light.a", "light.b"},
"time": False,
}
hass.states.async_set("light.b", "off")
await hass.async_block_till_done()
assert len(specific_runs) == 2
assert specific_runs[1] == "off"
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"light.a", "light.b"},
"time": False,
}
hass.states.async_set("light.a", "off")
await hass.async_block_till_done()
assert len(specific_runs) == 2
hass.states.async_set("light.b", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 2
hass.states.async_set("light.a", "on")
await hass.async_block_till_done()
assert len(specific_runs) == 3
assert specific_runs[2] == "on" | [
"async",
"def",
"test_track_template_result_and_conditional",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.a\"",
",",
"\"off\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.b\"",
",",
"\"off\"",
")",
"template_str",
"=",
"'{% if states.light.a.state == \"on\" and states.light.b.state == \"on\" %}on{% else %}off{% endif %}'",
"template",
"=",
"Template",
"(",
"template_str",
",",
"hass",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"specific_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template",
",",
"None",
")",
"]",
",",
"specific_run_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"light.a\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.b\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.a\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"specific_runs",
"[",
"0",
"]",
"==",
"\"on\"",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"light.a\"",
",",
"\"light.b\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.b\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"assert",
"specific_runs",
"[",
"1",
"]",
"==",
"\"off\"",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"light.a\"",
",",
"\"light.b\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.a\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.b\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.a\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"3",
"assert",
"specific_runs",
"[",
"2",
"]",
"==",
"\"on\""
] | [
1266,
0
] | [
1326,
35
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_result_iterator | (hass) | Test tracking template. | Test tracking template. | async def test_track_template_result_iterator(hass):
"""Test tracking template."""
iterator_runs = []
@ha.callback
def iterator_callback(event, updates):
iterator_runs.append(updates.pop().result)
async_track_template_result(
hass,
[
TrackTemplate(
Template(
"""
{% for state in states.sensor %}
{% if state.state == 'on' %}
{{ state.entity_id }},
{% endif %}
{% endfor %}
""",
hass,
),
None,
timedelta(seconds=0),
)
],
iterator_callback,
)
await hass.async_block_till_done()
hass.states.async_set("sensor.test", 5)
await hass.async_block_till_done()
assert iterator_runs == [""]
filter_runs = []
@ha.callback
def filter_callback(event, updates):
filter_runs.append(updates.pop().result)
info = async_track_template_result(
hass,
[
TrackTemplate(
Template(
"""{{ states.sensor|selectattr("state","equalto","on")
|join(",", attribute="entity_id") }}""",
hass,
),
None,
timedelta(seconds=0),
)
],
filter_callback,
)
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": {"sensor"},
"entities": set(),
"time": False,
}
hass.states.async_set("sensor.test", 6)
await hass.async_block_till_done()
assert filter_runs == [""]
assert iterator_runs == [""]
hass.states.async_set("sensor.new", "on")
await hass.async_block_till_done()
assert iterator_runs == ["", "sensor.new,"]
assert filter_runs == ["", "sensor.new"] | [
"async",
"def",
"test_track_template_result_iterator",
"(",
"hass",
")",
":",
"iterator_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"iterator_callback",
"(",
"event",
",",
"updates",
")",
":",
"iterator_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"Template",
"(",
"\"\"\"\n {% for state in states.sensor %}\n {% if state.state == 'on' %}\n {{ state.entity_id }},\n {% endif %}\n {% endfor %}\n \"\"\"",
",",
"hass",
",",
")",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"0",
")",
",",
")",
"]",
",",
"iterator_callback",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"5",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"iterator_runs",
"==",
"[",
"\"\"",
"]",
"filter_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"filter_callback",
"(",
"event",
",",
"updates",
")",
":",
"filter_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"Template",
"(",
"\"\"\"{{ states.sensor|selectattr(\"state\",\"equalto\",\"on\")\n |join(\",\", attribute=\"entity_id\") }}\"\"\"",
",",
"hass",
",",
")",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"0",
")",
",",
")",
"]",
",",
"filter_callback",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"{",
"\"sensor\"",
"}",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.test\"",
",",
"6",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"filter_runs",
"==",
"[",
"\"\"",
"]",
"assert",
"iterator_runs",
"==",
"[",
"\"\"",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.new\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"iterator_runs",
"==",
"[",
"\"\"",
",",
"\"sensor.new,\"",
"]",
"assert",
"filter_runs",
"==",
"[",
"\"\"",
",",
"\"sensor.new\"",
"]"
] | [
1329,
0
] | [
1402,
44
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_result_errors | (hass, caplog) | Test tracking template with errors in the template. | Test tracking template with errors in the template. | async def test_track_template_result_errors(hass, caplog):
"""Test tracking template with errors in the template."""
template_syntax_error = Template("{{states.switch", hass)
template_not_exist = Template("{{states.switch.not_exist.state }}", hass)
syntax_error_runs = []
not_exist_runs = []
@ha.callback
def syntax_error_listener(event, updates):
track_result = updates.pop()
syntax_error_runs.append(
(
event,
track_result.template,
track_result.last_result,
track_result.result,
)
)
async_track_template_result(
hass, [TrackTemplate(template_syntax_error, None)], syntax_error_listener
)
await hass.async_block_till_done()
assert len(syntax_error_runs) == 0
assert "TemplateSyntaxError" in caplog.text
@ha.callback
def not_exist_runs_error_listener(event, updates):
template_track = updates.pop()
not_exist_runs.append(
(
event,
template_track.template,
template_track.last_result,
template_track.result,
)
)
async_track_template_result(
hass,
[TrackTemplate(template_not_exist, None)],
not_exist_runs_error_listener,
)
await hass.async_block_till_done()
assert len(syntax_error_runs) == 0
assert len(not_exist_runs) == 0
hass.states.async_set("switch.not_exist", "off")
await hass.async_block_till_done()
assert len(not_exist_runs) == 1
assert not_exist_runs[0][0].data.get("entity_id") == "switch.not_exist"
assert not_exist_runs[0][1] == template_not_exist
assert not_exist_runs[0][2] is None
assert not_exist_runs[0][3] == "off"
hass.states.async_set("switch.not_exist", "on")
await hass.async_block_till_done()
assert len(syntax_error_runs) == 1
assert len(not_exist_runs) == 2
assert not_exist_runs[1][0].data.get("entity_id") == "switch.not_exist"
assert not_exist_runs[1][1] == template_not_exist
assert not_exist_runs[1][2] == "off"
assert not_exist_runs[1][3] == "on"
with patch.object(Template, "async_render") as render:
render.side_effect = TemplateError(jinja2.TemplateError())
hass.states.async_set("switch.not_exist", "off")
await hass.async_block_till_done()
assert len(not_exist_runs) == 3
assert not_exist_runs[2][0].data.get("entity_id") == "switch.not_exist"
assert not_exist_runs[2][1] == template_not_exist
assert not_exist_runs[2][2] == "on"
assert isinstance(not_exist_runs[2][3], TemplateError) | [
"async",
"def",
"test_track_template_result_errors",
"(",
"hass",
",",
"caplog",
")",
":",
"template_syntax_error",
"=",
"Template",
"(",
"\"{{states.switch\"",
",",
"hass",
")",
"template_not_exist",
"=",
"Template",
"(",
"\"{{states.switch.not_exist.state }}\"",
",",
"hass",
")",
"syntax_error_runs",
"=",
"[",
"]",
"not_exist_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"syntax_error_listener",
"(",
"event",
",",
"updates",
")",
":",
"track_result",
"=",
"updates",
".",
"pop",
"(",
")",
"syntax_error_runs",
".",
"append",
"(",
"(",
"event",
",",
"track_result",
".",
"template",
",",
"track_result",
".",
"last_result",
",",
"track_result",
".",
"result",
",",
")",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_syntax_error",
",",
"None",
")",
"]",
",",
"syntax_error_listener",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"syntax_error_runs",
")",
"==",
"0",
"assert",
"\"TemplateSyntaxError\"",
"in",
"caplog",
".",
"text",
"@",
"ha",
".",
"callback",
"def",
"not_exist_runs_error_listener",
"(",
"event",
",",
"updates",
")",
":",
"template_track",
"=",
"updates",
".",
"pop",
"(",
")",
"not_exist_runs",
".",
"append",
"(",
"(",
"event",
",",
"template_track",
".",
"template",
",",
"template_track",
".",
"last_result",
",",
"template_track",
".",
"result",
",",
")",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_not_exist",
",",
"None",
")",
"]",
",",
"not_exist_runs_error_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"syntax_error_runs",
")",
"==",
"0",
"assert",
"len",
"(",
"not_exist_runs",
")",
"==",
"0",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.not_exist\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"not_exist_runs",
")",
"==",
"1",
"assert",
"not_exist_runs",
"[",
"0",
"]",
"[",
"0",
"]",
".",
"data",
".",
"get",
"(",
"\"entity_id\"",
")",
"==",
"\"switch.not_exist\"",
"assert",
"not_exist_runs",
"[",
"0",
"]",
"[",
"1",
"]",
"==",
"template_not_exist",
"assert",
"not_exist_runs",
"[",
"0",
"]",
"[",
"2",
"]",
"is",
"None",
"assert",
"not_exist_runs",
"[",
"0",
"]",
"[",
"3",
"]",
"==",
"\"off\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.not_exist\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"syntax_error_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"not_exist_runs",
")",
"==",
"2",
"assert",
"not_exist_runs",
"[",
"1",
"]",
"[",
"0",
"]",
".",
"data",
".",
"get",
"(",
"\"entity_id\"",
")",
"==",
"\"switch.not_exist\"",
"assert",
"not_exist_runs",
"[",
"1",
"]",
"[",
"1",
"]",
"==",
"template_not_exist",
"assert",
"not_exist_runs",
"[",
"1",
"]",
"[",
"2",
"]",
"==",
"\"off\"",
"assert",
"not_exist_runs",
"[",
"1",
"]",
"[",
"3",
"]",
"==",
"\"on\"",
"with",
"patch",
".",
"object",
"(",
"Template",
",",
"\"async_render\"",
")",
"as",
"render",
":",
"render",
".",
"side_effect",
"=",
"TemplateError",
"(",
"jinja2",
".",
"TemplateError",
"(",
")",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.not_exist\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"not_exist_runs",
")",
"==",
"3",
"assert",
"not_exist_runs",
"[",
"2",
"]",
"[",
"0",
"]",
".",
"data",
".",
"get",
"(",
"\"entity_id\"",
")",
"==",
"\"switch.not_exist\"",
"assert",
"not_exist_runs",
"[",
"2",
"]",
"[",
"1",
"]",
"==",
"template_not_exist",
"assert",
"not_exist_runs",
"[",
"2",
"]",
"[",
"2",
"]",
"==",
"\"on\"",
"assert",
"isinstance",
"(",
"not_exist_runs",
"[",
"2",
"]",
"[",
"3",
"]",
",",
"TemplateError",
")"
] | [
1405,
0
] | [
1485,
62
] | python | en | ['en', 'en', 'en'] | True |
test_static_string | (hass) | Test a static string. | Test a static string. | async def test_static_string(hass):
"""Test a static string."""
template_refresh = Template("{{ 'static' }}", hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_refresh, None)], refresh_listener
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == ["static"] | [
"async",
"def",
"test_static_string",
"(",
"hass",
")",
":",
"template_refresh",
"=",
"Template",
"(",
"\"{{ 'static' }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
")",
"]",
",",
"refresh_listener",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"static\"",
"]"
] | [
1488,
0
] | [
1505,
37
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_rate_limit | (hass) | Test template rate limit. | Test template rate limit. | async def test_track_template_rate_limit(hass):
"""Test template rate limit."""
template_refresh = Template("{{ states | count }}", hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass,
[TrackTemplate(template_refresh, None, timedelta(seconds=0.1))],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == [0]
hass.states.async_set("sensor.one", "any")
await hass.async_block_till_done()
assert refresh_runs == [0]
info.async_refresh()
assert refresh_runs == [0, 1]
hass.states.async_set("sensor.two", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1]
next_time = dt_util.utcnow() + timedelta(seconds=0.125)
with patch(
"homeassistant.helpers.ratelimit.dt_util.utcnow", return_value=next_time
):
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
assert refresh_runs == [0, 1, 2]
hass.states.async_set("sensor.three", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1, 2]
hass.states.async_set("sensor.four", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1, 2]
next_time = dt_util.utcnow() + timedelta(seconds=0.125 * 2)
with patch(
"homeassistant.helpers.ratelimit.dt_util.utcnow", return_value=next_time
):
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
assert refresh_runs == [0, 1, 2, 4]
hass.states.async_set("sensor.five", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1, 2, 4] | [
"async",
"def",
"test_track_template_rate_limit",
"(",
"hass",
")",
":",
"template_refresh",
"=",
"Template",
"(",
"\"{{ states | count }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"0.1",
")",
")",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
"]",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.two\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
"]",
"next_time",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.125",
")",
"with",
"patch",
"(",
"\"homeassistant.helpers.ratelimit.dt_util.utcnow\"",
",",
"return_value",
"=",
"next_time",
")",
":",
"async_fire_time_changed",
"(",
"hass",
",",
"next_time",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.three\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.four\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"next_time",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.125",
"*",
"2",
")",
"with",
"patch",
"(",
"\"homeassistant.helpers.ratelimit.dt_util.utcnow\"",
",",
"return_value",
"=",
"next_time",
")",
":",
"async_fire_time_changed",
"(",
"hass",
",",
"next_time",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
",",
"4",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.five\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
",",
"4",
"]"
] | [
1508,
0
] | [
1558,
39
] | python | en | ['it', 'en', 'en'] | True |
test_track_template_rate_limit_suppress_listener | (hass) | Test template rate limit will suppress the listener during the rate limit. | Test template rate limit will suppress the listener during the rate limit. | async def test_track_template_rate_limit_suppress_listener(hass):
"""Test template rate limit will suppress the listener during the rate limit."""
template_refresh = Template("{{ states | count }}", hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass,
[TrackTemplate(template_refresh, None, timedelta(seconds=0.1))],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
assert info.listeners == {
"all": True,
"domains": set(),
"entities": set(),
"time": False,
}
await hass.async_block_till_done()
assert refresh_runs == [0]
hass.states.async_set("sensor.one", "any")
await hass.async_block_till_done()
assert refresh_runs == [0]
info.async_refresh()
assert refresh_runs == [0, 1]
hass.states.async_set("sensor.two", "any")
await hass.async_block_till_done()
# Should be suppressed during the rate limit
assert info.listeners == {
"all": False,
"domains": set(),
"entities": set(),
"time": False,
}
assert refresh_runs == [0, 1]
next_time = dt_util.utcnow() + timedelta(seconds=0.125)
with patch(
"homeassistant.helpers.ratelimit.dt_util.utcnow", return_value=next_time
):
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
# Rate limit released and the all listener returns
assert info.listeners == {
"all": True,
"domains": set(),
"entities": set(),
"time": False,
}
assert refresh_runs == [0, 1, 2]
hass.states.async_set("sensor.three", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1, 2]
hass.states.async_set("sensor.four", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1, 2]
# Rate limit hit and the all listener is shut off
assert info.listeners == {
"all": False,
"domains": set(),
"entities": set(),
"time": False,
}
next_time = dt_util.utcnow() + timedelta(seconds=0.125 * 2)
with patch(
"homeassistant.helpers.ratelimit.dt_util.utcnow", return_value=next_time
):
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
# Rate limit released and the all listener returns
assert info.listeners == {
"all": True,
"domains": set(),
"entities": set(),
"time": False,
}
assert refresh_runs == [0, 1, 2, 4]
hass.states.async_set("sensor.five", "any")
await hass.async_block_till_done()
# Rate limit hit and the all listener is shut off
assert info.listeners == {
"all": False,
"domains": set(),
"entities": set(),
"time": False,
}
assert refresh_runs == [0, 1, 2, 4] | [
"async",
"def",
"test_track_template_rate_limit_suppress_listener",
"(",
"hass",
")",
":",
"template_refresh",
"=",
"Template",
"(",
"\"{{ states | count }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"0.1",
")",
")",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"True",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
"]",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.two\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# Should be suppressed during the rate limit",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
"]",
"next_time",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.125",
")",
"with",
"patch",
"(",
"\"homeassistant.helpers.ratelimit.dt_util.utcnow\"",
",",
"return_value",
"=",
"next_time",
")",
":",
"async_fire_time_changed",
"(",
"hass",
",",
"next_time",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# Rate limit released and the all listener returns",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"True",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.three\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.four\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"# Rate limit hit and the all listener is shut off",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"next_time",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.125",
"*",
"2",
")",
"with",
"patch",
"(",
"\"homeassistant.helpers.ratelimit.dt_util.utcnow\"",
",",
"return_value",
"=",
"next_time",
")",
":",
"async_fire_time_changed",
"(",
"hass",
",",
"next_time",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# Rate limit released and the all listener returns",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"True",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
",",
"4",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.five\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# Rate limit hit and the all listener is shut off",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"False",
",",
"}",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
",",
"2",
",",
"4",
"]"
] | [
1561,
0
] | [
1653,
39
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_rate_limit_five | (hass) | Test template rate limit of 5 seconds. | Test template rate limit of 5 seconds. | async def test_track_template_rate_limit_five(hass):
"""Test template rate limit of 5 seconds."""
template_refresh = Template("{{ states | count }}", hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass,
[TrackTemplate(template_refresh, None, timedelta(seconds=5))],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == [0]
hass.states.async_set("sensor.one", "any")
await hass.async_block_till_done()
assert refresh_runs == [0]
info.async_refresh()
assert refresh_runs == [0, 1]
hass.states.async_set("sensor.two", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1]
hass.states.async_set("sensor.three", "any")
await hass.async_block_till_done()
assert refresh_runs == [0, 1] | [
"async",
"def",
"test_track_template_rate_limit_five",
"(",
"hass",
")",
":",
"template_refresh",
"=",
"Template",
"(",
"\"{{ states | count }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"5",
")",
")",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
"]",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.two\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.three\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"0",
",",
"1",
"]"
] | [
1656,
0
] | [
1686,
33
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_has_default_rate_limit | (hass) | Test template has a rate limit by default. | Test template has a rate limit by default. | async def test_track_template_has_default_rate_limit(hass):
"""Test template has a rate limit by default."""
hass.states.async_set("sensor.zero", "any")
template_refresh = Template("{{ states | list | count }}", hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass,
[TrackTemplate(template_refresh, None)],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == [1]
hass.states.async_set("sensor.one", "any")
await hass.async_block_till_done()
assert refresh_runs == [1]
info.async_refresh()
assert refresh_runs == [1, 2]
hass.states.async_set("sensor.two", "any")
await hass.async_block_till_done()
assert refresh_runs == [1, 2]
hass.states.async_set("sensor.three", "any")
await hass.async_block_till_done()
assert refresh_runs == [1, 2] | [
"async",
"def",
"test_track_template_has_default_rate_limit",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.zero\"",
",",
"\"any\"",
")",
"template_refresh",
"=",
"Template",
"(",
"\"{{ states | list | count }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
")",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
"]",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.two\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.three\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
",",
"2",
"]"
] | [
1689,
0
] | [
1720,
33
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_unavailable_sates_has_default_rate_limit | (hass) | Test template watching for unavailable states has a rate limit by default. | Test template watching for unavailable states has a rate limit by default. | async def test_track_template_unavailable_sates_has_default_rate_limit(hass):
"""Test template watching for unavailable states has a rate limit by default."""
hass.states.async_set("sensor.zero", "unknown")
template_refresh = Template(
"{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count }}",
hass,
)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass,
[TrackTemplate(template_refresh, None)],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == [1]
hass.states.async_set("sensor.one", "unknown")
await hass.async_block_till_done()
assert refresh_runs == [1]
info.async_refresh()
assert refresh_runs == [1, 2]
hass.states.async_set("sensor.two", "any")
await hass.async_block_till_done()
assert refresh_runs == [1, 2]
hass.states.async_set("sensor.three", "unknown")
await hass.async_block_till_done()
assert refresh_runs == [1, 2]
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == [1, 2, 3]
info.async_remove() | [
"async",
"def",
"test_track_template_unavailable_sates_has_default_rate_limit",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.zero\"",
",",
"\"unknown\"",
")",
"template_refresh",
"=",
"Template",
"(",
"\"{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count }}\"",
",",
"hass",
",",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
")",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"unknown\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
"]",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.two\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
",",
"2",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.three\"",
",",
"\"unknown\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
",",
"2",
"]",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"1",
",",
"2",
",",
"3",
"]",
"info",
".",
"async_remove",
"(",
")"
] | [
1723,
0
] | [
1761,
23
] | python | en | ['en', 'en', 'en'] | True |
test_specifically_referenced_entity_is_not_rate_limited | (hass) | Test template rate limit of 5 seconds. | Test template rate limit of 5 seconds. | async def test_specifically_referenced_entity_is_not_rate_limited(hass):
"""Test template rate limit of 5 seconds."""
hass.states.async_set("sensor.one", "none")
template_refresh = Template('{{ states | count }}_{{ states("sensor.one") }}', hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass,
[TrackTemplate(template_refresh, None, timedelta(seconds=5))],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == ["1_none"]
hass.states.async_set("sensor.one", "any")
await hass.async_block_till_done()
assert refresh_runs == ["1_none", "1_any"]
info.async_refresh()
assert refresh_runs == ["1_none", "1_any"]
hass.states.async_set("sensor.two", "any")
await hass.async_block_till_done()
assert refresh_runs == ["1_none", "1_any"]
hass.states.async_set("sensor.three", "any")
await hass.async_block_till_done()
assert refresh_runs == ["1_none", "1_any"]
hass.states.async_set("sensor.one", "none")
await hass.async_block_till_done()
assert refresh_runs == ["1_none", "1_any", "3_none"]
info.async_remove() | [
"async",
"def",
"test_specifically_referenced_entity_is_not_rate_limited",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"none\"",
")",
"template_refresh",
"=",
"Template",
"(",
"'{{ states | count }}_{{ states(\"sensor.one\") }}'",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"5",
")",
")",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"1_none\"",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"1_none\"",
",",
"\"1_any\"",
"]",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"1_none\"",
",",
"\"1_any\"",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.two\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"1_none\"",
",",
"\"1_any\"",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.three\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"1_none\"",
",",
"\"1_any\"",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"none\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"1_none\"",
",",
"\"1_any\"",
",",
"\"3_none\"",
"]",
"info",
".",
"async_remove",
"(",
")"
] | [
1764,
0
] | [
1800,
23
] | python | en | ['en', 'en', 'en'] | True |
test_track_two_templates_with_different_rate_limits | (hass) | Test two templates with different rate limits. | Test two templates with different rate limits. | async def test_track_two_templates_with_different_rate_limits(hass):
"""Test two templates with different rate limits."""
template_one = Template("{{ (states | count) + 0 }}", hass)
template_five = Template("{{ states | count }}", hass)
refresh_runs = {
template_one: [],
template_five: [],
}
@ha.callback
def refresh_listener(event, updates):
for update in updates:
refresh_runs[update.template].append(update.result)
info = async_track_template_result(
hass,
[
TrackTemplate(template_one, None, timedelta(seconds=0.1)),
TrackTemplate(template_five, None, timedelta(seconds=5)),
],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs[template_one] == [0]
assert refresh_runs[template_five] == [0]
hass.states.async_set("sensor.one", "any")
await hass.async_block_till_done()
assert refresh_runs[template_one] == [0]
assert refresh_runs[template_five] == [0]
info.async_refresh()
assert refresh_runs[template_one] == [0, 1]
assert refresh_runs[template_five] == [0, 1]
hass.states.async_set("sensor.two", "any")
await hass.async_block_till_done()
assert refresh_runs[template_one] == [0, 1]
assert refresh_runs[template_five] == [0, 1]
next_time = dt_util.utcnow() + timedelta(seconds=0.125 * 1)
with patch(
"homeassistant.helpers.ratelimit.dt_util.utcnow", return_value=next_time
):
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
await hass.async_block_till_done()
assert refresh_runs[template_one] == [0, 1, 2]
assert refresh_runs[template_five] == [0, 1]
hass.states.async_set("sensor.three", "any")
await hass.async_block_till_done()
assert refresh_runs[template_one] == [0, 1, 2]
assert refresh_runs[template_five] == [0, 1]
hass.states.async_set("sensor.four", "any")
await hass.async_block_till_done()
assert refresh_runs[template_one] == [0, 1, 2]
assert refresh_runs[template_five] == [0, 1]
hass.states.async_set("sensor.five", "any")
await hass.async_block_till_done()
assert refresh_runs[template_one] == [0, 1, 2]
assert refresh_runs[template_five] == [0, 1]
info.async_remove() | [
"async",
"def",
"test_track_two_templates_with_different_rate_limits",
"(",
"hass",
")",
":",
"template_one",
"=",
"Template",
"(",
"\"{{ (states | count) + 0 }}\"",
",",
"hass",
")",
"template_five",
"=",
"Template",
"(",
"\"{{ states | count }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"{",
"template_one",
":",
"[",
"]",
",",
"template_five",
":",
"[",
"]",
",",
"}",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"for",
"update",
"in",
"updates",
":",
"refresh_runs",
"[",
"update",
".",
"template",
"]",
".",
"append",
"(",
"update",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_one",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"0.1",
")",
")",
",",
"TrackTemplate",
"(",
"template_five",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"5",
")",
")",
",",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.one\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
"]",
"info",
".",
"async_refresh",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.two\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"next_time",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.125",
"*",
"1",
")",
"with",
"patch",
"(",
"\"homeassistant.helpers.ratelimit.dt_util.utcnow\"",
",",
"return_value",
"=",
"next_time",
")",
":",
"async_fire_time_changed",
"(",
"hass",
",",
"next_time",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.three\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.four\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"sensor.five\"",
",",
"\"any\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"[",
"template_one",
"]",
"==",
"[",
"0",
",",
"1",
",",
"2",
"]",
"assert",
"refresh_runs",
"[",
"template_five",
"]",
"==",
"[",
"0",
",",
"1",
"]",
"info",
".",
"async_remove",
"(",
")"
] | [
1803,
0
] | [
1865,
23
] | python | en | ['en', 'en', 'en'] | True |
test_string | (hass) | Test a string. | Test a string. | async def test_string(hass):
"""Test a string."""
template_refresh = Template("no_template", hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_refresh, None)], refresh_listener
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == ["no_template"] | [
"async",
"def",
"test_string",
"(",
"hass",
")",
":",
"template_refresh",
"=",
"Template",
"(",
"\"no_template\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
")",
"]",
",",
"refresh_listener",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"no_template\"",
"]"
] | [
1868,
0
] | [
1885,
42
] | python | en | ['en', 'cy', 'en'] | True |
test_track_template_result_refresh_cancel | (hass) | Test cancelling and refreshing result. | Test cancelling and refreshing result. | async def test_track_template_result_refresh_cancel(hass):
"""Test cancelling and refreshing result."""
template_refresh = Template("{{states.switch.test.state == 'on' and now() }}", hass)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_refresh, None)], refresh_listener
)
await hass.async_block_till_done()
hass.states.async_set("switch.test", "off")
await hass.async_block_till_done()
assert refresh_runs == [False]
assert len(refresh_runs) == 1
info.async_refresh()
hass.states.async_set("switch.test", "on")
await hass.async_block_till_done()
assert len(refresh_runs) == 2
assert refresh_runs[0] != refresh_runs[1]
info.async_remove()
hass.states.async_set("switch.test", "off")
await hass.async_block_till_done()
assert len(refresh_runs) == 2
template_refresh = Template("{{ value }}", hass)
refresh_runs = []
info = async_track_template_result(
hass,
[TrackTemplate(template_refresh, {"value": "duck"})],
refresh_listener,
)
await hass.async_block_till_done()
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == ["duck"]
info.async_refresh()
await hass.async_block_till_done()
assert refresh_runs == ["duck"] | [
"async",
"def",
"test_track_template_result_refresh_cancel",
"(",
"hass",
")",
":",
"template_refresh",
"=",
"Template",
"(",
"\"{{states.switch.test.state == 'on' and now() }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"None",
")",
"]",
",",
"refresh_listener",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"False",
"]",
"assert",
"len",
"(",
"refresh_runs",
")",
"==",
"1",
"info",
".",
"async_refresh",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"refresh_runs",
")",
"==",
"2",
"assert",
"refresh_runs",
"[",
"0",
"]",
"!=",
"refresh_runs",
"[",
"1",
"]",
"info",
".",
"async_remove",
"(",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"refresh_runs",
")",
"==",
"2",
"template_refresh",
"=",
"Template",
"(",
"\"{{ value }}\"",
",",
"hass",
")",
"refresh_runs",
"=",
"[",
"]",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_refresh",
",",
"{",
"\"value\"",
":",
"\"duck\"",
"}",
")",
"]",
",",
"refresh_listener",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"duck\"",
"]",
"info",
".",
"async_refresh",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"\"duck\"",
"]"
] | [
1888,
0
] | [
1939,
35
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_template_result_multiple_templates | (hass) | Test tracking multiple templates. | Test tracking multiple templates. | async def test_async_track_template_result_multiple_templates(hass):
"""Test tracking multiple templates."""
template_1 = Template("{{ states.switch.test.state == 'on' }}")
template_2 = Template("{{ states.switch.test.state == 'on' }}")
template_3 = Template("{{ states.switch.test.state == 'off' }}")
template_4 = Template(
"{{ states.binary_sensor | map(attribute='entity_id') | list }}"
)
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates)
async_track_template_result(
hass,
[
TrackTemplate(template_1, None),
TrackTemplate(template_2, None),
TrackTemplate(template_3, None),
TrackTemplate(template_4, None),
],
refresh_listener,
)
hass.states.async_set("switch.test", "on")
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(template_1, None, True),
TrackTemplateResult(template_2, None, True),
TrackTemplateResult(template_3, None, False),
]
]
refresh_runs = []
hass.states.async_set("switch.test", "off")
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(template_1, True, False),
TrackTemplateResult(template_2, True, False),
TrackTemplateResult(template_3, False, True),
]
]
refresh_runs = []
hass.states.async_set("binary_sensor.test", "off")
await hass.async_block_till_done()
assert refresh_runs == [
[TrackTemplateResult(template_4, None, ["binary_sensor.test"])]
] | [
"async",
"def",
"test_async_track_template_result_multiple_templates",
"(",
"hass",
")",
":",
"template_1",
"=",
"Template",
"(",
"\"{{ states.switch.test.state == 'on' }}\"",
")",
"template_2",
"=",
"Template",
"(",
"\"{{ states.switch.test.state == 'on' }}\"",
")",
"template_3",
"=",
"Template",
"(",
"\"{{ states.switch.test.state == 'off' }}\"",
")",
"template_4",
"=",
"Template",
"(",
"\"{{ states.binary_sensor | map(attribute='entity_id') | list }}\"",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_1",
",",
"None",
")",
",",
"TrackTemplate",
"(",
"template_2",
",",
"None",
")",
",",
"TrackTemplate",
"(",
"template_3",
",",
"None",
")",
",",
"TrackTemplate",
"(",
"template_4",
",",
"None",
")",
",",
"]",
",",
"refresh_listener",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_1",
",",
"None",
",",
"True",
")",
",",
"TrackTemplateResult",
"(",
"template_2",
",",
"None",
",",
"True",
")",
",",
"TrackTemplateResult",
"(",
"template_3",
",",
"None",
",",
"False",
")",
",",
"]",
"]",
"refresh_runs",
"=",
"[",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_1",
",",
"True",
",",
"False",
")",
",",
"TrackTemplateResult",
"(",
"template_2",
",",
"True",
",",
"False",
")",
",",
"TrackTemplateResult",
"(",
"template_3",
",",
"False",
",",
"True",
")",
",",
"]",
"]",
"refresh_runs",
"=",
"[",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"binary_sensor.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_4",
",",
"None",
",",
"[",
"\"binary_sensor.test\"",
"]",
")",
"]",
"]"
] | [
1942,
0
] | [
1998,
5
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_template_result_multiple_templates_mixing_domain | (hass) | Test tracking multiple templates when tracking entities and an entire domain. | Test tracking multiple templates when tracking entities and an entire domain. | async def test_async_track_template_result_multiple_templates_mixing_domain(hass):
"""Test tracking multiple templates when tracking entities and an entire domain."""
template_1 = Template("{{ states.switch.test.state == 'on' }}")
template_2 = Template("{{ states.switch.test.state == 'on' }}")
template_3 = Template("{{ states.switch.test.state == 'off' }}")
template_4 = Template("{{ states.switch | map(attribute='entity_id') | list }}")
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates)
async_track_template_result(
hass,
[
TrackTemplate(template_1, None),
TrackTemplate(template_2, None),
TrackTemplate(template_3, None),
TrackTemplate(template_4, None, timedelta(seconds=0)),
],
refresh_listener,
)
hass.states.async_set("switch.test", "on")
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(template_1, None, True),
TrackTemplateResult(template_2, None, True),
TrackTemplateResult(template_3, None, False),
TrackTemplateResult(template_4, None, ["switch.test"]),
]
]
refresh_runs = []
hass.states.async_set("switch.test", "off")
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(template_1, True, False),
TrackTemplateResult(template_2, True, False),
TrackTemplateResult(template_3, False, True),
]
]
refresh_runs = []
hass.states.async_set("binary_sensor.test", "off")
await hass.async_block_till_done()
assert refresh_runs == []
refresh_runs = []
hass.states.async_set("switch.new", "off")
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(
template_4, ["switch.test"], ["switch.new", "switch.test"]
)
]
] | [
"async",
"def",
"test_async_track_template_result_multiple_templates_mixing_domain",
"(",
"hass",
")",
":",
"template_1",
"=",
"Template",
"(",
"\"{{ states.switch.test.state == 'on' }}\"",
")",
"template_2",
"=",
"Template",
"(",
"\"{{ states.switch.test.state == 'on' }}\"",
")",
"template_3",
"=",
"Template",
"(",
"\"{{ states.switch.test.state == 'off' }}\"",
")",
"template_4",
"=",
"Template",
"(",
"\"{{ states.switch | map(attribute='entity_id') | list }}\"",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
")",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_1",
",",
"None",
")",
",",
"TrackTemplate",
"(",
"template_2",
",",
"None",
")",
",",
"TrackTemplate",
"(",
"template_3",
",",
"None",
")",
",",
"TrackTemplate",
"(",
"template_4",
",",
"None",
",",
"timedelta",
"(",
"seconds",
"=",
"0",
")",
")",
",",
"]",
",",
"refresh_listener",
",",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_1",
",",
"None",
",",
"True",
")",
",",
"TrackTemplateResult",
"(",
"template_2",
",",
"None",
",",
"True",
")",
",",
"TrackTemplateResult",
"(",
"template_3",
",",
"None",
",",
"False",
")",
",",
"TrackTemplateResult",
"(",
"template_4",
",",
"None",
",",
"[",
"\"switch.test\"",
"]",
")",
",",
"]",
"]",
"refresh_runs",
"=",
"[",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_1",
",",
"True",
",",
"False",
")",
",",
"TrackTemplateResult",
"(",
"template_2",
",",
"True",
",",
"False",
")",
",",
"TrackTemplateResult",
"(",
"template_3",
",",
"False",
",",
"True",
")",
",",
"]",
"]",
"refresh_runs",
"=",
"[",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"binary_sensor.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"]",
"refresh_runs",
"=",
"[",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.new\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_4",
",",
"[",
"\"switch.test\"",
"]",
",",
"[",
"\"switch.new\"",
",",
"\"switch.test\"",
"]",
")",
"]",
"]"
] | [
2001,
0
] | [
2066,
5
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_template_result_raise_on_template_error | (hass) | Test that we raise as soon as we encounter a failed template. | Test that we raise as soon as we encounter a failed template. | async def test_async_track_template_result_raise_on_template_error(hass):
"""Test that we raise as soon as we encounter a failed template."""
with pytest.raises(TemplateError):
async_track_template_result(
hass,
[
TrackTemplate(
Template(
"{{ states.switch | function_that_does_not_exist | list }}"
),
None,
),
],
ha.callback(lambda event, updates: None),
raise_on_template_error=True,
) | [
"async",
"def",
"test_async_track_template_result_raise_on_template_error",
"(",
"hass",
")",
":",
"with",
"pytest",
".",
"raises",
"(",
"TemplateError",
")",
":",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"Template",
"(",
"\"{{ states.switch | function_that_does_not_exist | list }}\"",
")",
",",
"None",
",",
")",
",",
"]",
",",
"ha",
".",
"callback",
"(",
"lambda",
"event",
",",
"updates",
":",
"None",
")",
",",
"raise_on_template_error",
"=",
"True",
",",
")"
] | [
2069,
0
] | [
2085,
9
] | python | en | ['en', 'gd', 'en'] | True |
test_track_template_with_time | (hass) | Test tracking template with time. | Test tracking template with time. | async def test_track_template_with_time(hass):
"""Test tracking template with time."""
hass.states.async_set("switch.test", "on")
specific_runs = []
template_complex = Template("{{ states.switch.test.state and now() }}", hass)
def specific_run_callback(event, updates):
specific_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_complex, None)], specific_run_callback
)
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"switch.test"},
"time": True,
}
await hass.async_block_till_done()
now = dt_util.utcnow()
async_fire_time_changed(hass, now + timedelta(seconds=61))
async_fire_time_changed(hass, now + timedelta(seconds=61 * 2))
await hass.async_block_till_done()
assert specific_runs[-1] != specific_runs[0]
info.async_remove() | [
"async",
"def",
"test_track_template_with_time",
"(",
"hass",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"specific_runs",
"=",
"[",
"]",
"template_complex",
"=",
"Template",
"(",
"\"{{ states.switch.test.state and now() }}\"",
",",
"hass",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"specific_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_complex",
",",
"None",
")",
"]",
",",
"specific_run_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"switch.test\"",
"}",
",",
"\"time\"",
":",
"True",
",",
"}",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"now",
"+",
"timedelta",
"(",
"seconds",
"=",
"61",
")",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"now",
"+",
"timedelta",
"(",
"seconds",
"=",
"61",
"*",
"2",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"specific_runs",
"[",
"-",
"1",
"]",
"!=",
"specific_runs",
"[",
"0",
"]",
"info",
".",
"async_remove",
"(",
")"
] | [
2088,
0
] | [
2116,
23
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_with_time_default | (hass) | Test tracking template with time. | Test tracking template with time. | async def test_track_template_with_time_default(hass):
"""Test tracking template with time."""
specific_runs = []
template_complex = Template("{{ now() }}", hass)
def specific_run_callback(event, updates):
specific_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_complex, None)], specific_run_callback
)
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": set(),
"time": True,
}
await hass.async_block_till_done()
now = dt_util.utcnow()
async_fire_time_changed(hass, now + timedelta(seconds=2))
async_fire_time_changed(hass, now + timedelta(seconds=4))
await hass.async_block_till_done()
assert len(specific_runs) < 2
async_fire_time_changed(hass, now + timedelta(minutes=2))
await hass.async_block_till_done()
async_fire_time_changed(hass, now + timedelta(minutes=4))
await hass.async_block_till_done()
assert len(specific_runs) >= 2
assert specific_runs[-1] != specific_runs[0]
info.async_remove() | [
"async",
"def",
"test_track_template_with_time_default",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"template_complex",
"=",
"Template",
"(",
"\"{{ now() }}\"",
",",
"hass",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"specific_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_complex",
",",
"None",
")",
"]",
",",
"specific_run_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"set",
"(",
")",
",",
"\"time\"",
":",
"True",
",",
"}",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"now",
"+",
"timedelta",
"(",
"seconds",
"=",
"2",
")",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"now",
"+",
"timedelta",
"(",
"seconds",
"=",
"4",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"<",
"2",
"async_fire_time_changed",
"(",
"hass",
",",
"now",
"+",
"timedelta",
"(",
"minutes",
"=",
"2",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"now",
"+",
"timedelta",
"(",
"minutes",
"=",
"4",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
">=",
"2",
"assert",
"specific_runs",
"[",
"-",
"1",
"]",
"!=",
"specific_runs",
"[",
"0",
"]",
"info",
".",
"async_remove",
"(",
")"
] | [
2119,
0
] | [
2152,
23
] | python | en | ['en', 'en', 'en'] | True |
test_track_template_with_time_that_leaves_scope | (hass) | Test tracking template with time. | Test tracking template with time. | async def test_track_template_with_time_that_leaves_scope(hass):
"""Test tracking template with time."""
now = dt_util.utcnow()
test_time = datetime(now.year + 1, 5, 24, 11, 59, 1, 500000, tzinfo=dt_util.UTC)
with patch("homeassistant.util.dt.utcnow", return_value=test_time):
hass.states.async_set("binary_sensor.washing_machine", "on")
specific_runs = []
template_complex = Template(
"""
{% if states.binary_sensor.washing_machine.state == "on" %}
{{ now() }}
{% else %}
{{ states.binary_sensor.washing_machine.last_updated }}
{% endif %}
""",
hass,
)
def specific_run_callback(event, updates):
specific_runs.append(updates.pop().result)
info = async_track_template_result(
hass, [TrackTemplate(template_complex, None)], specific_run_callback
)
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": True,
}
hass.states.async_set("binary_sensor.washing_machine", "off")
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": False,
}
hass.states.async_set("binary_sensor.washing_machine", "on")
await hass.async_block_till_done()
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"binary_sensor.washing_machine"},
"time": True,
}
# Verify we do not update before the minute rolls over
callback_count_before_time_change = len(specific_runs)
async_fire_time_changed(hass, test_time)
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change
async_fire_time_changed(hass, test_time + timedelta(seconds=58))
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change
# Verify we do update on the next change of minute
async_fire_time_changed(hass, test_time + timedelta(seconds=59))
await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change + 1
info.async_remove() | [
"async",
"def",
"test_track_template_with_time_that_leaves_scope",
"(",
"hass",
")",
":",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"test_time",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"11",
",",
"59",
",",
"1",
",",
"500000",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"test_time",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"binary_sensor.washing_machine\"",
",",
"\"on\"",
")",
"specific_runs",
"=",
"[",
"]",
"template_complex",
"=",
"Template",
"(",
"\"\"\"\n {% if states.binary_sensor.washing_machine.state == \"on\" %}\n {{ now() }}\n {% else %}\n {{ states.binary_sensor.washing_machine.last_updated }}\n {% endif %}\n \"\"\"",
",",
"hass",
",",
")",
"def",
"specific_run_callback",
"(",
"event",
",",
"updates",
")",
":",
"specific_runs",
".",
"append",
"(",
"updates",
".",
"pop",
"(",
")",
".",
"result",
")",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_complex",
",",
"None",
")",
"]",
",",
"specific_run_callback",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"binary_sensor.washing_machine\"",
"}",
",",
"\"time\"",
":",
"True",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"binary_sensor.washing_machine\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"binary_sensor.washing_machine\"",
"}",
",",
"\"time\"",
":",
"False",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"binary_sensor.washing_machine\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"binary_sensor.washing_machine\"",
"}",
",",
"\"time\"",
":",
"True",
",",
"}",
"# Verify we do not update before the minute rolls over",
"callback_count_before_time_change",
"=",
"len",
"(",
"specific_runs",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"test_time",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"callback_count_before_time_change",
"async_fire_time_changed",
"(",
"hass",
",",
"test_time",
"+",
"timedelta",
"(",
"seconds",
"=",
"58",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"callback_count_before_time_change",
"# Verify we do update on the next change of minute",
"async_fire_time_changed",
"(",
"hass",
",",
"test_time",
"+",
"timedelta",
"(",
"seconds",
"=",
"59",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"callback_count_before_time_change",
"+",
"1",
"info",
".",
"async_remove",
"(",
")"
] | [
2155,
0
] | [
2225,
23
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_template_result_multiple_templates_mixing_listeners | (hass) | Test tracking multiple templates with mixing listener types. | Test tracking multiple templates with mixing listener types. | async def test_async_track_template_result_multiple_templates_mixing_listeners(hass):
"""Test tracking multiple templates with mixing listener types."""
template_1 = Template("{{ states.switch.test.state == 'on' }}")
template_2 = Template("{{ now() and True }}")
refresh_runs = []
@ha.callback
def refresh_listener(event, updates):
refresh_runs.append(updates)
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 11, 59, 55, tzinfo=dt_util.UTC
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
info = async_track_template_result(
hass,
[
TrackTemplate(template_1, None),
TrackTemplate(template_2, None),
],
refresh_listener,
)
assert info.listeners == {
"all": False,
"domains": set(),
"entities": {"switch.test"},
"time": True,
}
hass.states.async_set("switch.test", "on")
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(template_1, None, True),
]
]
refresh_runs = []
hass.states.async_set("switch.test", "off")
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(template_1, True, False),
]
]
refresh_runs = []
next_time = time_that_will_not_match_right_away + timedelta(hours=25)
with patch("homeassistant.util.dt.utcnow", return_value=next_time):
async_fire_time_changed(hass, next_time)
await hass.async_block_till_done()
assert refresh_runs == [
[
TrackTemplateResult(template_2, None, True),
]
] | [
"async",
"def",
"test_async_track_template_result_multiple_templates_mixing_listeners",
"(",
"hass",
")",
":",
"template_1",
"=",
"Template",
"(",
"\"{{ states.switch.test.state == 'on' }}\"",
")",
"template_2",
"=",
"Template",
"(",
"\"{{ now() and True }}\"",
")",
"refresh_runs",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"refresh_listener",
"(",
"event",
",",
"updates",
")",
":",
"refresh_runs",
".",
"append",
"(",
"updates",
")",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"11",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"info",
"=",
"async_track_template_result",
"(",
"hass",
",",
"[",
"TrackTemplate",
"(",
"template_1",
",",
"None",
")",
",",
"TrackTemplate",
"(",
"template_2",
",",
"None",
")",
",",
"]",
",",
"refresh_listener",
",",
")",
"assert",
"info",
".",
"listeners",
"==",
"{",
"\"all\"",
":",
"False",
",",
"\"domains\"",
":",
"set",
"(",
")",
",",
"\"entities\"",
":",
"{",
"\"switch.test\"",
"}",
",",
"\"time\"",
":",
"True",
",",
"}",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_1",
",",
"None",
",",
"True",
")",
",",
"]",
"]",
"refresh_runs",
"=",
"[",
"]",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"switch.test\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_1",
",",
"True",
",",
"False",
")",
",",
"]",
"]",
"refresh_runs",
"=",
"[",
"]",
"next_time",
"=",
"time_that_will_not_match_right_away",
"+",
"timedelta",
"(",
"hours",
"=",
"25",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"next_time",
")",
":",
"async_fire_time_changed",
"(",
"hass",
",",
"next_time",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"refresh_runs",
"==",
"[",
"[",
"TrackTemplateResult",
"(",
"template_2",
",",
"None",
",",
"True",
")",
",",
"]",
"]"
] | [
2228,
0
] | [
2293,
5
] | python | en | ['en', 'en', 'en'] | True |
test_track_same_state_simple_no_trigger | (hass) | Test track_same_change with no trigger. | Test track_same_change with no trigger. | async def test_track_same_state_simple_no_trigger(hass):
"""Test track_same_change with no trigger."""
callback_runs = []
period = timedelta(minutes=1)
@ha.callback
def callback_run_callback():
callback_runs.append(1)
async_track_same_state(
hass,
period,
callback_run_callback,
callback(lambda _, _2, to_s: to_s.state == "on"),
entity_ids="light.Bowl",
)
# Adding state to state machine
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
assert len(callback_runs) == 0
# Change state on state machine
hass.states.async_set("light.Bowl", "off")
await hass.async_block_till_done()
assert len(callback_runs) == 0
# change time to track and see if they trigger
future = dt_util.utcnow() + period
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
assert len(callback_runs) == 0 | [
"async",
"def",
"test_track_same_state_simple_no_trigger",
"(",
"hass",
")",
":",
"callback_runs",
"=",
"[",
"]",
"period",
"=",
"timedelta",
"(",
"minutes",
"=",
"1",
")",
"@",
"ha",
".",
"callback",
"def",
"callback_run_callback",
"(",
")",
":",
"callback_runs",
".",
"append",
"(",
"1",
")",
"async_track_same_state",
"(",
"hass",
",",
"period",
",",
"callback_run_callback",
",",
"callback",
"(",
"lambda",
"_",
",",
"_2",
",",
"to_s",
":",
"to_s",
".",
"state",
"==",
"\"on\"",
")",
",",
"entity_ids",
"=",
"\"light.Bowl\"",
",",
")",
"# Adding state to state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"callback_runs",
")",
"==",
"0",
"# Change state on state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"callback_runs",
")",
"==",
"0",
"# change time to track and see if they trigger",
"future",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"period",
"async_fire_time_changed",
"(",
"hass",
",",
"future",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"callback_runs",
")",
"==",
"0"
] | [
2296,
0
] | [
2327,
34
] | python | en | ['en', 'en', 'en'] | True |
test_track_same_state_simple_trigger_check_funct | (hass) | Test track_same_change with trigger and check funct. | Test track_same_change with trigger and check funct. | async def test_track_same_state_simple_trigger_check_funct(hass):
"""Test track_same_change with trigger and check funct."""
callback_runs = []
check_func = []
period = timedelta(minutes=1)
@ha.callback
def callback_run_callback():
callback_runs.append(1)
@ha.callback
def async_check_func(entity, from_s, to_s):
check_func.append((entity, from_s, to_s))
return True
async_track_same_state(
hass,
period,
callback_run_callback,
entity_ids="light.Bowl",
async_check_same_func=async_check_func,
)
# Adding state to state machine
hass.states.async_set("light.Bowl", "on")
await hass.async_block_till_done()
await hass.async_block_till_done()
assert len(callback_runs) == 0
assert check_func[-1][2].state == "on"
assert check_func[-1][0] == "light.bowl"
# change time to track and see if they trigger
future = dt_util.utcnow() + period
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
assert len(callback_runs) == 1 | [
"async",
"def",
"test_track_same_state_simple_trigger_check_funct",
"(",
"hass",
")",
":",
"callback_runs",
"=",
"[",
"]",
"check_func",
"=",
"[",
"]",
"period",
"=",
"timedelta",
"(",
"minutes",
"=",
"1",
")",
"@",
"ha",
".",
"callback",
"def",
"callback_run_callback",
"(",
")",
":",
"callback_runs",
".",
"append",
"(",
"1",
")",
"@",
"ha",
".",
"callback",
"def",
"async_check_func",
"(",
"entity",
",",
"from_s",
",",
"to_s",
")",
":",
"check_func",
".",
"append",
"(",
"(",
"entity",
",",
"from_s",
",",
"to_s",
")",
")",
"return",
"True",
"async_track_same_state",
"(",
"hass",
",",
"period",
",",
"callback_run_callback",
",",
"entity_ids",
"=",
"\"light.Bowl\"",
",",
"async_check_same_func",
"=",
"async_check_func",
",",
")",
"# Adding state to state machine",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.Bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"callback_runs",
")",
"==",
"0",
"assert",
"check_func",
"[",
"-",
"1",
"]",
"[",
"2",
"]",
".",
"state",
"==",
"\"on\"",
"assert",
"check_func",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
"==",
"\"light.bowl\"",
"# change time to track and see if they trigger",
"future",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"period",
"async_fire_time_changed",
"(",
"hass",
",",
"future",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"callback_runs",
")",
"==",
"1"
] | [
2330,
0
] | [
2365,
34
] | python | en | ['en', 'en', 'en'] | True |
test_track_time_interval | (hass) | Test tracking time interval. | Test tracking time interval. | async def test_track_time_interval(hass):
"""Test tracking time interval."""
specific_runs = []
utc_now = dt_util.utcnow()
unsub = async_track_time_interval(
hass, callback(lambda x: specific_runs.append(x)), timedelta(seconds=10)
)
async_fire_time_changed(hass, utc_now + timedelta(seconds=5))
await hass.async_block_till_done()
assert len(specific_runs) == 0
async_fire_time_changed(hass, utc_now + timedelta(seconds=13))
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(hass, utc_now + timedelta(minutes=20))
await hass.async_block_till_done()
assert len(specific_runs) == 2
unsub()
async_fire_time_changed(hass, utc_now + timedelta(seconds=30))
await hass.async_block_till_done()
assert len(specific_runs) == 2 | [
"async",
"def",
"test_track_time_interval",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"utc_now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"unsub",
"=",
"async_track_time_interval",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"timedelta",
"(",
"seconds",
"=",
"10",
")",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"utc_now",
"+",
"timedelta",
"(",
"seconds",
"=",
"5",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"utc_now",
"+",
"timedelta",
"(",
"seconds",
"=",
"13",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"utc_now",
"+",
"timedelta",
"(",
"minutes",
"=",
"20",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"unsub",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"utc_now",
"+",
"timedelta",
"(",
"seconds",
"=",
"30",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2"
] | [
2368,
0
] | [
2393,
34
] | python | en | ['vi', 'en', 'en'] | True |
test_track_sunrise | (hass, legacy_patchable_time) | Test track the sunrise. | Test track the sunrise. | async def test_track_sunrise(hass, legacy_patchable_time):
"""Test track the sunrise."""
latitude = 32.87336
longitude = 117.22743
# Setup sun component
hass.config.latitude = latitude
hass.config.longitude = longitude
assert await async_setup_component(
hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}
)
# Get next sunrise/sunset
astral = Astral()
utc_now = datetime(2014, 5, 24, 12, 0, 0, tzinfo=dt_util.UTC)
utc_today = utc_now.date()
mod = -1
while True:
next_rising = astral.sunrise_utc(
utc_today + timedelta(days=mod), latitude, longitude
)
if next_rising > utc_now:
break
mod += 1
# Track sunrise
runs = []
with patch("homeassistant.util.dt.utcnow", return_value=utc_now):
unsub = async_track_sunrise(hass, callback(lambda: runs.append(1)))
offset_runs = []
offset = timedelta(minutes=30)
with patch("homeassistant.util.dt.utcnow", return_value=utc_now):
unsub2 = async_track_sunrise(
hass, callback(lambda: offset_runs.append(1)), offset
)
# run tests
async_fire_time_changed(hass, next_rising - offset)
await hass.async_block_till_done()
assert len(runs) == 0
assert len(offset_runs) == 0
async_fire_time_changed(hass, next_rising)
await hass.async_block_till_done()
assert len(runs) == 1
assert len(offset_runs) == 0
async_fire_time_changed(hass, next_rising + offset)
await hass.async_block_till_done()
assert len(runs) == 1
assert len(offset_runs) == 1
unsub()
unsub2()
async_fire_time_changed(hass, next_rising + offset)
await hass.async_block_till_done()
assert len(runs) == 1
assert len(offset_runs) == 1 | [
"async",
"def",
"test_track_sunrise",
"(",
"hass",
",",
"legacy_patchable_time",
")",
":",
"latitude",
"=",
"32.87336",
"longitude",
"=",
"117.22743",
"# Setup sun component",
"hass",
".",
"config",
".",
"latitude",
"=",
"latitude",
"hass",
".",
"config",
".",
"longitude",
"=",
"longitude",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"sun",
".",
"DOMAIN",
",",
"{",
"sun",
".",
"DOMAIN",
":",
"{",
"sun",
".",
"CONF_ELEVATION",
":",
"0",
"}",
"}",
")",
"# Get next sunrise/sunset",
"astral",
"=",
"Astral",
"(",
")",
"utc_now",
"=",
"datetime",
"(",
"2014",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"utc_today",
"=",
"utc_now",
".",
"date",
"(",
")",
"mod",
"=",
"-",
"1",
"while",
"True",
":",
"next_rising",
"=",
"astral",
".",
"sunrise_utc",
"(",
"utc_today",
"+",
"timedelta",
"(",
"days",
"=",
"mod",
")",
",",
"latitude",
",",
"longitude",
")",
"if",
"next_rising",
">",
"utc_now",
":",
"break",
"mod",
"+=",
"1",
"# Track sunrise",
"runs",
"=",
"[",
"]",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"utc_now",
")",
":",
"unsub",
"=",
"async_track_sunrise",
"(",
"hass",
",",
"callback",
"(",
"lambda",
":",
"runs",
".",
"append",
"(",
"1",
")",
")",
")",
"offset_runs",
"=",
"[",
"]",
"offset",
"=",
"timedelta",
"(",
"minutes",
"=",
"30",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"utc_now",
")",
":",
"unsub2",
"=",
"async_track_sunrise",
"(",
"hass",
",",
"callback",
"(",
"lambda",
":",
"offset_runs",
".",
"append",
"(",
"1",
")",
")",
",",
"offset",
")",
"# run tests",
"async_fire_time_changed",
"(",
"hass",
",",
"next_rising",
"-",
"offset",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"0",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"next_rising",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"next_rising",
"+",
"offset",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"1",
"unsub",
"(",
")",
"unsub2",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"next_rising",
"+",
"offset",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"1"
] | [
2396,
0
] | [
2456,
32
] | python | en | ['en', 'zu', 'en'] | True |
test_track_sunrise_update_location | (hass, legacy_patchable_time) | Test track the sunrise. | Test track the sunrise. | async def test_track_sunrise_update_location(hass, legacy_patchable_time):
"""Test track the sunrise."""
# Setup sun component
hass.config.latitude = 32.87336
hass.config.longitude = 117.22743
assert await async_setup_component(
hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}
)
# Get next sunrise
astral = Astral()
utc_now = datetime(2014, 5, 24, 12, 0, 0, tzinfo=dt_util.UTC)
utc_today = utc_now.date()
mod = -1
while True:
next_rising = astral.sunrise_utc(
utc_today + timedelta(days=mod), hass.config.latitude, hass.config.longitude
)
if next_rising > utc_now:
break
mod += 1
# Track sunrise
runs = []
with patch("homeassistant.util.dt.utcnow", return_value=utc_now):
async_track_sunrise(hass, callback(lambda: runs.append(1)))
# Mimic sunrise
async_fire_time_changed(hass, next_rising)
await hass.async_block_till_done()
assert len(runs) == 1
# Move!
with patch("homeassistant.util.dt.utcnow", return_value=utc_now):
await hass.config.async_update(latitude=40.755931, longitude=-73.984606)
await hass.async_block_till_done()
# Mimic sunrise
async_fire_time_changed(hass, next_rising)
await hass.async_block_till_done()
# Did not increase
assert len(runs) == 1
# Get next sunrise
mod = -1
while True:
next_rising = astral.sunrise_utc(
utc_today + timedelta(days=mod), hass.config.latitude, hass.config.longitude
)
if next_rising > utc_now:
break
mod += 1
# Mimic sunrise at new location
async_fire_time_changed(hass, next_rising)
await hass.async_block_till_done()
assert len(runs) == 2 | [
"async",
"def",
"test_track_sunrise_update_location",
"(",
"hass",
",",
"legacy_patchable_time",
")",
":",
"# Setup sun component",
"hass",
".",
"config",
".",
"latitude",
"=",
"32.87336",
"hass",
".",
"config",
".",
"longitude",
"=",
"117.22743",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"sun",
".",
"DOMAIN",
",",
"{",
"sun",
".",
"DOMAIN",
":",
"{",
"sun",
".",
"CONF_ELEVATION",
":",
"0",
"}",
"}",
")",
"# Get next sunrise",
"astral",
"=",
"Astral",
"(",
")",
"utc_now",
"=",
"datetime",
"(",
"2014",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"utc_today",
"=",
"utc_now",
".",
"date",
"(",
")",
"mod",
"=",
"-",
"1",
"while",
"True",
":",
"next_rising",
"=",
"astral",
".",
"sunrise_utc",
"(",
"utc_today",
"+",
"timedelta",
"(",
"days",
"=",
"mod",
")",
",",
"hass",
".",
"config",
".",
"latitude",
",",
"hass",
".",
"config",
".",
"longitude",
")",
"if",
"next_rising",
">",
"utc_now",
":",
"break",
"mod",
"+=",
"1",
"# Track sunrise",
"runs",
"=",
"[",
"]",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"utc_now",
")",
":",
"async_track_sunrise",
"(",
"hass",
",",
"callback",
"(",
"lambda",
":",
"runs",
".",
"append",
"(",
"1",
")",
")",
")",
"# Mimic sunrise",
"async_fire_time_changed",
"(",
"hass",
",",
"next_rising",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"# Move!",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"utc_now",
")",
":",
"await",
"hass",
".",
"config",
".",
"async_update",
"(",
"latitude",
"=",
"40.755931",
",",
"longitude",
"=",
"-",
"73.984606",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# Mimic sunrise",
"async_fire_time_changed",
"(",
"hass",
",",
"next_rising",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"# Did not increase",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"# Get next sunrise",
"mod",
"=",
"-",
"1",
"while",
"True",
":",
"next_rising",
"=",
"astral",
".",
"sunrise_utc",
"(",
"utc_today",
"+",
"timedelta",
"(",
"days",
"=",
"mod",
")",
",",
"hass",
".",
"config",
".",
"latitude",
",",
"hass",
".",
"config",
".",
"longitude",
")",
"if",
"next_rising",
">",
"utc_now",
":",
"break",
"mod",
"+=",
"1",
"# Mimic sunrise at new location",
"async_fire_time_changed",
"(",
"hass",
",",
"next_rising",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"2"
] | [
2459,
0
] | [
2516,
25
] | python | en | ['en', 'zu', 'en'] | True |
test_track_sunset | (hass, legacy_patchable_time) | Test track the sunset. | Test track the sunset. | async def test_track_sunset(hass, legacy_patchable_time):
"""Test track the sunset."""
latitude = 32.87336
longitude = 117.22743
# Setup sun component
hass.config.latitude = latitude
hass.config.longitude = longitude
assert await async_setup_component(
hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}
)
# Get next sunrise/sunset
astral = Astral()
utc_now = datetime(2014, 5, 24, 12, 0, 0, tzinfo=dt_util.UTC)
utc_today = utc_now.date()
mod = -1
while True:
next_setting = astral.sunset_utc(
utc_today + timedelta(days=mod), latitude, longitude
)
if next_setting > utc_now:
break
mod += 1
# Track sunset
runs = []
with patch("homeassistant.util.dt.utcnow", return_value=utc_now):
unsub = async_track_sunset(hass, callback(lambda: runs.append(1)))
offset_runs = []
offset = timedelta(minutes=30)
with patch("homeassistant.util.dt.utcnow", return_value=utc_now):
unsub2 = async_track_sunset(
hass, callback(lambda: offset_runs.append(1)), offset
)
# Run tests
async_fire_time_changed(hass, next_setting - offset)
await hass.async_block_till_done()
assert len(runs) == 0
assert len(offset_runs) == 0
async_fire_time_changed(hass, next_setting)
await hass.async_block_till_done()
assert len(runs) == 1
assert len(offset_runs) == 0
async_fire_time_changed(hass, next_setting + offset)
await hass.async_block_till_done()
assert len(runs) == 1
assert len(offset_runs) == 1
unsub()
unsub2()
async_fire_time_changed(hass, next_setting + offset)
await hass.async_block_till_done()
assert len(runs) == 1
assert len(offset_runs) == 1 | [
"async",
"def",
"test_track_sunset",
"(",
"hass",
",",
"legacy_patchable_time",
")",
":",
"latitude",
"=",
"32.87336",
"longitude",
"=",
"117.22743",
"# Setup sun component",
"hass",
".",
"config",
".",
"latitude",
"=",
"latitude",
"hass",
".",
"config",
".",
"longitude",
"=",
"longitude",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"sun",
".",
"DOMAIN",
",",
"{",
"sun",
".",
"DOMAIN",
":",
"{",
"sun",
".",
"CONF_ELEVATION",
":",
"0",
"}",
"}",
")",
"# Get next sunrise/sunset",
"astral",
"=",
"Astral",
"(",
")",
"utc_now",
"=",
"datetime",
"(",
"2014",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"utc_today",
"=",
"utc_now",
".",
"date",
"(",
")",
"mod",
"=",
"-",
"1",
"while",
"True",
":",
"next_setting",
"=",
"astral",
".",
"sunset_utc",
"(",
"utc_today",
"+",
"timedelta",
"(",
"days",
"=",
"mod",
")",
",",
"latitude",
",",
"longitude",
")",
"if",
"next_setting",
">",
"utc_now",
":",
"break",
"mod",
"+=",
"1",
"# Track sunset",
"runs",
"=",
"[",
"]",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"utc_now",
")",
":",
"unsub",
"=",
"async_track_sunset",
"(",
"hass",
",",
"callback",
"(",
"lambda",
":",
"runs",
".",
"append",
"(",
"1",
")",
")",
")",
"offset_runs",
"=",
"[",
"]",
"offset",
"=",
"timedelta",
"(",
"minutes",
"=",
"30",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"utc_now",
")",
":",
"unsub2",
"=",
"async_track_sunset",
"(",
"hass",
",",
"callback",
"(",
"lambda",
":",
"offset_runs",
".",
"append",
"(",
"1",
")",
")",
",",
"offset",
")",
"# Run tests",
"async_fire_time_changed",
"(",
"hass",
",",
"next_setting",
"-",
"offset",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"0",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"next_setting",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"next_setting",
"+",
"offset",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"1",
"unsub",
"(",
")",
"unsub2",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"next_setting",
"+",
"offset",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"runs",
")",
"==",
"1",
"assert",
"len",
"(",
"offset_runs",
")",
"==",
"1"
] | [
2519,
0
] | [
2579,
32
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_time_change | (hass) | Test tracking time change. | Test tracking time change. | async def test_async_track_time_change(hass):
"""Test tracking time change."""
wildcard_runs = []
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 11, 59, 55, tzinfo=dt_util.UTC
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_time_change(
hass, callback(lambda x: wildcard_runs.append(x))
)
unsub_utc = async_track_utc_time_change(
hass, callback(lambda x: specific_runs.append(x)), second=[0, 30]
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 0, 15, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
assert len(wildcard_runs) == 2
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
assert len(wildcard_runs) == 3
unsub()
unsub_utc()
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 0, 30, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
assert len(wildcard_runs) == 3 | [
"async",
"def",
"test_async_track_time_change",
"(",
"hass",
")",
":",
"wildcard_runs",
"=",
"[",
"]",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"11",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"unsub",
"=",
"async_track_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"wildcard_runs",
".",
"append",
"(",
"x",
")",
")",
")",
"unsub_utc",
"=",
"async_track_utc_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"second",
"=",
"[",
"0",
",",
"30",
"]",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"15",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"2",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"30",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"3",
"unsub",
"(",
")",
"unsub_utc",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"30",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"assert",
"len",
"(",
"wildcard_runs",
")",
"==",
"3"
] | [
2582,
0
] | [
2632,
34
] | python | en | ['vi', 'en', 'en'] | True |
test_periodic_task_minute | (hass) | Test periodic tasks per minute. | Test periodic tasks per minute. | async def test_periodic_task_minute(hass):
"""Test periodic tasks per minute."""
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 11, 59, 55, tzinfo=dt_util.UTC
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass, callback(lambda x: specific_runs.append(x)), minute="/5", second=0
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 3, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
unsub()
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 12, 5, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2 | [
"async",
"def",
"test_periodic_task_minute",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"11",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"unsub",
"=",
"async_track_utc_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"minute",
"=",
"\"/5\"",
",",
"second",
"=",
"0",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"3",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"5",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"unsub",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"12",
",",
"5",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2"
] | [
2635,
0
] | [
2676,
34
] | python | ca | ['ca', 'la', 'it'] | False |
test_periodic_task_hour | (hass) | Test periodic tasks per hour. | Test periodic tasks per hour. | async def test_periodic_task_hour(hass):
"""Test periodic tasks per hour."""
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 23, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 1, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 3
unsub()
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 2, 0, 0, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 3 | [
"async",
"def",
"test_periodic_task_hour",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"21",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"unsub",
"=",
"async_track_utc_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"hour",
"=",
"\"/2\"",
",",
"minute",
"=",
"0",
",",
"second",
"=",
"0",
",",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"22",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"23",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"25",
",",
"0",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"25",
",",
"1",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"25",
",",
"2",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"3",
"unsub",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"25",
",",
"2",
",",
"0",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"3"
] | [
2679,
0
] | [
2736,
34
] | python | en | ['en', 'la', 'it'] | False |
test_periodic_task_wrong_input | (hass) | Test periodic tasks with wrong input. | Test periodic tasks with wrong input. | async def test_periodic_task_wrong_input(hass):
"""Test periodic tasks with wrong input."""
specific_runs = []
now = dt_util.utcnow()
with pytest.raises(ValueError):
async_track_utc_time_change(
hass, callback(lambda x: specific_runs.append(x)), hour="/two"
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 2, 0, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 0 | [
"async",
"def",
"test_periodic_task_wrong_input",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"with",
"pytest",
".",
"raises",
"(",
"ValueError",
")",
":",
"async_track_utc_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"hour",
"=",
"\"/two\"",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"2",
",",
"0",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0"
] | [
2739,
0
] | [
2754,
34
] | python | en | ['en', 'en', 'en'] | True |
test_periodic_task_clock_rollback | (hass) | Test periodic tasks with the time rolling backwards. | Test periodic tasks with the time rolling backwards. | async def test_periodic_task_clock_rollback(hass):
"""Test periodic tasks with the time rolling backwards."""
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 23, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass,
datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC),
fire_all=True,
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass,
datetime(now.year + 1, 5, 24, 0, 0, 0, 999999, tzinfo=dt_util.UTC),
fire_all=True,
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
unsub()
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 2, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2 | [
"async",
"def",
"test_periodic_task_clock_rollback",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"21",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"unsub",
"=",
"async_track_utc_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"hour",
"=",
"\"/2\"",
",",
"minute",
"=",
"0",
",",
"second",
"=",
"0",
",",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"22",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"23",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"22",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
",",
"fire_all",
"=",
"True",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"0",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
",",
"fire_all",
"=",
"True",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"25",
",",
"2",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"unsub",
"(",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"25",
",",
"2",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2"
] | [
2757,
0
] | [
2818,
34
] | python | en | ['en', 'en', 'en'] | True |
test_periodic_task_duplicate_time | (hass) | Test periodic tasks not triggering on duplicate time. | Test periodic tasks not triggering on duplicate time. | async def test_periodic_task_duplicate_time(hass):
"""Test periodic tasks not triggering on duplicate time."""
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = datetime(
now.year + 1, 5, 24, 21, 59, 55, tzinfo=dt_util.UTC
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_utc_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour="/2",
minute=0,
second=0,
)
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 24, 22, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass, datetime(now.year + 1, 5, 25, 0, 0, 0, 999999, tzinfo=dt_util.UTC)
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
unsub() | [
"async",
"def",
"test_periodic_task_duplicate_time",
"(",
"hass",
")",
":",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"21",
",",
"59",
",",
"55",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"unsub",
"=",
"async_track_utc_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"hour",
"=",
"\"/2\"",
",",
"minute",
"=",
"0",
",",
"second",
"=",
"0",
",",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"22",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"24",
",",
"22",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"5",
",",
"25",
",",
"0",
",",
"0",
",",
"0",
",",
"999999",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"unsub",
"(",
")"
] | [
2821,
0
] | [
2860,
11
] | python | en | ['en', 'en', 'en'] | True |
test_periodic_task_entering_dst | (hass) | Test periodic task behavior when entering dst. | Test periodic task behavior when entering dst. | async def test_periodic_task_entering_dst(hass):
"""Test periodic task behavior when entering dst."""
timezone = dt_util.get_time_zone("Europe/Vienna")
dt_util.set_default_time_zone(timezone)
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = timezone.localize(
datetime(now.year + 1, 3, 25, 2, 31, 0)
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour=2,
minute=30,
second=0,
)
async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 25, 1, 50, 0, 999999))
)
await hass.async_block_till_done()
assert len(specific_runs) == 0
async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 25, 3, 50, 0, 999999))
)
await hass.async_block_till_done()
assert len(specific_runs) == 0
async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 26, 1, 50, 0, 999999))
)
await hass.async_block_till_done()
assert len(specific_runs) == 0
async_fire_time_changed(
hass, timezone.localize(datetime(now.year + 1, 3, 26, 2, 50, 0, 999999))
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
unsub() | [
"async",
"def",
"test_periodic_task_entering_dst",
"(",
"hass",
")",
":",
"timezone",
"=",
"dt_util",
".",
"get_time_zone",
"(",
"\"Europe/Vienna\"",
")",
"dt_util",
".",
"set_default_time_zone",
"(",
"timezone",
")",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"3",
",",
"25",
",",
"2",
",",
"31",
",",
"0",
")",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"unsub",
"=",
"async_track_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"hour",
"=",
"2",
",",
"minute",
"=",
"30",
",",
"second",
"=",
"0",
",",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"3",
",",
"25",
",",
"1",
",",
"50",
",",
"0",
",",
"999999",
")",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"3",
",",
"25",
",",
"3",
",",
"50",
",",
"0",
",",
"999999",
")",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"3",
",",
"26",
",",
"1",
",",
"50",
",",
"0",
",",
"999999",
")",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"3",
",",
"26",
",",
"2",
",",
"50",
",",
"0",
",",
"999999",
")",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"unsub",
"(",
")"
] | [
2863,
0
] | [
2909,
11
] | python | en | ['en', 'en', 'en'] | True |
test_periodic_task_leaving_dst | (hass) | Test periodic task behavior when leaving dst. | Test periodic task behavior when leaving dst. | async def test_periodic_task_leaving_dst(hass):
"""Test periodic task behavior when leaving dst."""
timezone = dt_util.get_time_zone("Europe/Vienna")
dt_util.set_default_time_zone(timezone)
specific_runs = []
now = dt_util.utcnow()
time_that_will_not_match_right_away = timezone.localize(
datetime(now.year + 1, 10, 28, 2, 28, 0), is_dst=True
)
with patch(
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
):
unsub = async_track_time_change(
hass,
callback(lambda x: specific_runs.append(x)),
hour=2,
minute=30,
second=0,
)
async_fire_time_changed(
hass,
timezone.localize(
datetime(now.year + 1, 10, 28, 2, 5, 0, 999999), is_dst=False
),
)
await hass.async_block_till_done()
assert len(specific_runs) == 0
async_fire_time_changed(
hass,
timezone.localize(
datetime(now.year + 1, 10, 28, 2, 55, 0, 999999), is_dst=False
),
)
await hass.async_block_till_done()
assert len(specific_runs) == 1
async_fire_time_changed(
hass,
timezone.localize(
datetime(now.year + 2, 10, 28, 2, 45, 0, 999999), is_dst=True
),
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
async_fire_time_changed(
hass,
timezone.localize(
datetime(now.year + 2, 10, 28, 2, 55, 0, 999999), is_dst=True
),
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
async_fire_time_changed(
hass,
timezone.localize(
datetime(now.year + 2, 10, 28, 2, 55, 0, 999999), is_dst=True
),
)
await hass.async_block_till_done()
assert len(specific_runs) == 2
unsub() | [
"async",
"def",
"test_periodic_task_leaving_dst",
"(",
"hass",
")",
":",
"timezone",
"=",
"dt_util",
".",
"get_time_zone",
"(",
"\"Europe/Vienna\"",
")",
"dt_util",
".",
"set_default_time_zone",
"(",
"timezone",
")",
"specific_runs",
"=",
"[",
"]",
"now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"time_that_will_not_match_right_away",
"=",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"10",
",",
"28",
",",
"2",
",",
"28",
",",
"0",
")",
",",
"is_dst",
"=",
"True",
")",
"with",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"time_that_will_not_match_right_away",
")",
":",
"unsub",
"=",
"async_track_time_change",
"(",
"hass",
",",
"callback",
"(",
"lambda",
"x",
":",
"specific_runs",
".",
"append",
"(",
"x",
")",
")",
",",
"hour",
"=",
"2",
",",
"minute",
"=",
"30",
",",
"second",
"=",
"0",
",",
")",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"10",
",",
"28",
",",
"2",
",",
"5",
",",
"0",
",",
"999999",
")",
",",
"is_dst",
"=",
"False",
")",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"1",
",",
"10",
",",
"28",
",",
"2",
",",
"55",
",",
"0",
",",
"999999",
")",
",",
"is_dst",
"=",
"False",
")",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"1",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"2",
",",
"10",
",",
"28",
",",
"2",
",",
"45",
",",
"0",
",",
"999999",
")",
",",
"is_dst",
"=",
"True",
")",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"2",
",",
"10",
",",
"28",
",",
"2",
",",
"55",
",",
"0",
",",
"999999",
")",
",",
"is_dst",
"=",
"True",
")",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"async_fire_time_changed",
"(",
"hass",
",",
"timezone",
".",
"localize",
"(",
"datetime",
"(",
"now",
".",
"year",
"+",
"2",
",",
"10",
",",
"28",
",",
"2",
",",
"55",
",",
"0",
",",
"999999",
")",
",",
"is_dst",
"=",
"True",
")",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"specific_runs",
")",
"==",
"2",
"unsub",
"(",
")"
] | [
2912,
0
] | [
2980,
11
] | python | en | ['en', 'en', 'en'] | True |
test_call_later | (hass) | Test calling an action later. | Test calling an action later. | async def test_call_later(hass):
"""Test calling an action later."""
def action():
pass
now = datetime(2017, 12, 19, 15, 40, 0, tzinfo=dt_util.UTC)
with patch(
"homeassistant.helpers.event.async_track_point_in_utc_time"
) as mock, patch("homeassistant.util.dt.utcnow", return_value=now):
async_call_later(hass, 3, action)
assert len(mock.mock_calls) == 1
p_hass, p_action, p_point = mock.mock_calls[0][1]
assert p_hass is hass
assert p_action is action
assert p_point == now + timedelta(seconds=3) | [
"async",
"def",
"test_call_later",
"(",
"hass",
")",
":",
"def",
"action",
"(",
")",
":",
"pass",
"now",
"=",
"datetime",
"(",
"2017",
",",
"12",
",",
"19",
",",
"15",
",",
"40",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.helpers.event.async_track_point_in_utc_time\"",
")",
"as",
"mock",
",",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"now",
")",
":",
"async_call_later",
"(",
"hass",
",",
"3",
",",
"action",
")",
"assert",
"len",
"(",
"mock",
".",
"mock_calls",
")",
"==",
"1",
"p_hass",
",",
"p_action",
",",
"p_point",
"=",
"mock",
".",
"mock_calls",
"[",
"0",
"]",
"[",
"1",
"]",
"assert",
"p_hass",
"is",
"hass",
"assert",
"p_action",
"is",
"action",
"assert",
"p_point",
"==",
"now",
"+",
"timedelta",
"(",
"seconds",
"=",
"3",
")"
] | [
2983,
0
] | [
3000,
48
] | python | en | ['en', 'en', 'en'] | True |
test_async_call_later | (hass) | Test calling an action later. | Test calling an action later. | async def test_async_call_later(hass):
"""Test calling an action later."""
def action():
pass
now = datetime(2017, 12, 19, 15, 40, 0, tzinfo=dt_util.UTC)
with patch(
"homeassistant.helpers.event.async_track_point_in_utc_time"
) as mock, patch("homeassistant.util.dt.utcnow", return_value=now):
remove = async_call_later(hass, 3, action)
assert len(mock.mock_calls) == 1
p_hass, p_action, p_point = mock.mock_calls[0][1]
assert p_hass is hass
assert p_action is action
assert p_point == now + timedelta(seconds=3)
assert remove is mock() | [
"async",
"def",
"test_async_call_later",
"(",
"hass",
")",
":",
"def",
"action",
"(",
")",
":",
"pass",
"now",
"=",
"datetime",
"(",
"2017",
",",
"12",
",",
"19",
",",
"15",
",",
"40",
",",
"0",
",",
"tzinfo",
"=",
"dt_util",
".",
"UTC",
")",
"with",
"patch",
"(",
"\"homeassistant.helpers.event.async_track_point_in_utc_time\"",
")",
"as",
"mock",
",",
"patch",
"(",
"\"homeassistant.util.dt.utcnow\"",
",",
"return_value",
"=",
"now",
")",
":",
"remove",
"=",
"async_call_later",
"(",
"hass",
",",
"3",
",",
"action",
")",
"assert",
"len",
"(",
"mock",
".",
"mock_calls",
")",
"==",
"1",
"p_hass",
",",
"p_action",
",",
"p_point",
"=",
"mock",
".",
"mock_calls",
"[",
"0",
"]",
"[",
"1",
"]",
"assert",
"p_hass",
"is",
"hass",
"assert",
"p_action",
"is",
"action",
"assert",
"p_point",
"==",
"now",
"+",
"timedelta",
"(",
"seconds",
"=",
"3",
")",
"assert",
"remove",
"is",
"mock",
"(",
")"
] | [
3003,
0
] | [
3021,
27
] | python | en | ['en', 'en', 'en'] | True |
test_track_state_change_event_chain_multple_entity | (hass) | Test that adding a new state tracker inside a tracker does not fire right away. | Test that adding a new state tracker inside a tracker does not fire right away. | async def test_track_state_change_event_chain_multple_entity(hass):
"""Test that adding a new state tracker inside a tracker does not fire right away."""
tracker_called = []
chained_tracker_called = []
chained_tracker_unsub = []
tracker_unsub = []
@ha.callback
def chained_single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
chained_tracker_called.append((old_state, new_state))
@ha.callback
def single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
tracker_called.append((old_state, new_state))
chained_tracker_unsub.append(
async_track_state_change_event(
hass, ["light.bowl", "light.top"], chained_single_run_callback
)
)
tracker_unsub.append(
async_track_state_change_event(
hass, ["light.bowl", "light.top"], single_run_callback
)
)
hass.states.async_set("light.bowl", "on")
hass.states.async_set("light.top", "on")
await hass.async_block_till_done()
assert len(tracker_called) == 2
assert len(chained_tracker_called) == 1
assert len(tracker_unsub) == 1
assert len(chained_tracker_unsub) == 2
hass.states.async_set("light.bowl", "off")
await hass.async_block_till_done()
assert len(tracker_called) == 3
assert len(chained_tracker_called) == 3
assert len(tracker_unsub) == 1
assert len(chained_tracker_unsub) == 3 | [
"async",
"def",
"test_track_state_change_event_chain_multple_entity",
"(",
"hass",
")",
":",
"tracker_called",
"=",
"[",
"]",
"chained_tracker_called",
"=",
"[",
"]",
"chained_tracker_unsub",
"=",
"[",
"]",
"tracker_unsub",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"chained_single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"chained_tracker_called",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"tracker_called",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"chained_tracker_unsub",
".",
"append",
"(",
"async_track_state_change_event",
"(",
"hass",
",",
"[",
"\"light.bowl\"",
",",
"\"light.top\"",
"]",
",",
"chained_single_run_callback",
")",
")",
"tracker_unsub",
".",
"append",
"(",
"async_track_state_change_event",
"(",
"hass",
",",
"[",
"\"light.bowl\"",
",",
"\"light.top\"",
"]",
",",
"single_run_callback",
")",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.bowl\"",
",",
"\"on\"",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.top\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"tracker_called",
")",
"==",
"2",
"assert",
"len",
"(",
"chained_tracker_called",
")",
"==",
"1",
"assert",
"len",
"(",
"tracker_unsub",
")",
"==",
"1",
"assert",
"len",
"(",
"chained_tracker_unsub",
")",
"==",
"2",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"tracker_called",
")",
"==",
"3",
"assert",
"len",
"(",
"chained_tracker_called",
")",
"==",
"3",
"assert",
"len",
"(",
"tracker_unsub",
")",
"==",
"1",
"assert",
"len",
"(",
"chained_tracker_unsub",
")",
"==",
"3"
] | [
3024,
0
] | [
3073,
42
] | python | en | ['en', 'en', 'en'] | True |
test_track_state_change_event_chain_single_entity | (hass) | Test that adding a new state tracker inside a tracker does not fire right away. | Test that adding a new state tracker inside a tracker does not fire right away. | async def test_track_state_change_event_chain_single_entity(hass):
"""Test that adding a new state tracker inside a tracker does not fire right away."""
tracker_called = []
chained_tracker_called = []
chained_tracker_unsub = []
tracker_unsub = []
@ha.callback
def chained_single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
chained_tracker_called.append((old_state, new_state))
@ha.callback
def single_run_callback(event):
old_state = event.data.get("old_state")
new_state = event.data.get("new_state")
tracker_called.append((old_state, new_state))
chained_tracker_unsub.append(
async_track_state_change_event(
hass, "light.bowl", chained_single_run_callback
)
)
tracker_unsub.append(
async_track_state_change_event(hass, "light.bowl", single_run_callback)
)
hass.states.async_set("light.bowl", "on")
await hass.async_block_till_done()
assert len(tracker_called) == 1
assert len(chained_tracker_called) == 0
assert len(tracker_unsub) == 1
assert len(chained_tracker_unsub) == 1
hass.states.async_set("light.bowl", "off")
await hass.async_block_till_done()
assert len(tracker_called) == 2
assert len(chained_tracker_called) == 1
assert len(tracker_unsub) == 1
assert len(chained_tracker_unsub) == 2 | [
"async",
"def",
"test_track_state_change_event_chain_single_entity",
"(",
"hass",
")",
":",
"tracker_called",
"=",
"[",
"]",
"chained_tracker_called",
"=",
"[",
"]",
"chained_tracker_unsub",
"=",
"[",
"]",
"tracker_unsub",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"chained_single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"chained_tracker_called",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"@",
"ha",
".",
"callback",
"def",
"single_run_callback",
"(",
"event",
")",
":",
"old_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"old_state\"",
")",
"new_state",
"=",
"event",
".",
"data",
".",
"get",
"(",
"\"new_state\"",
")",
"tracker_called",
".",
"append",
"(",
"(",
"old_state",
",",
"new_state",
")",
")",
"chained_tracker_unsub",
".",
"append",
"(",
"async_track_state_change_event",
"(",
"hass",
",",
"\"light.bowl\"",
",",
"chained_single_run_callback",
")",
")",
"tracker_unsub",
".",
"append",
"(",
"async_track_state_change_event",
"(",
"hass",
",",
"\"light.bowl\"",
",",
"single_run_callback",
")",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.bowl\"",
",",
"\"on\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"tracker_called",
")",
"==",
"1",
"assert",
"len",
"(",
"chained_tracker_called",
")",
"==",
"0",
"assert",
"len",
"(",
"tracker_unsub",
")",
"==",
"1",
"assert",
"len",
"(",
"chained_tracker_unsub",
")",
"==",
"1",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"light.bowl\"",
",",
"\"off\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"tracker_called",
")",
"==",
"2",
"assert",
"len",
"(",
"chained_tracker_called",
")",
"==",
"1",
"assert",
"len",
"(",
"tracker_unsub",
")",
"==",
"1",
"assert",
"len",
"(",
"chained_tracker_unsub",
")",
"==",
"2"
] | [
3076,
0
] | [
3122,
42
] | python | en | ['en', 'en', 'en'] | True |
test_track_point_in_utc_time_cancel | (hass) | Test cancel of async track point in time. | Test cancel of async track point in time. | async def test_track_point_in_utc_time_cancel(hass):
"""Test cancel of async track point in time."""
times = []
@ha.callback
def run_callback(utc_time):
nonlocal times
times.append(utc_time)
def _setup_listeners():
"""Ensure we test the non-async version."""
utc_now = dt_util.utcnow()
with pytest.raises(TypeError):
track_point_in_utc_time("nothass", run_callback, utc_now)
unsub1 = hass.helpers.event.track_point_in_utc_time(
run_callback, utc_now + timedelta(seconds=0.1)
)
hass.helpers.event.track_point_in_utc_time(
run_callback, utc_now + timedelta(seconds=0.1)
)
unsub1()
await hass.async_add_executor_job(_setup_listeners)
await asyncio.sleep(0.2)
assert len(times) == 1
assert times[0].tzinfo == dt_util.UTC | [
"async",
"def",
"test_track_point_in_utc_time_cancel",
"(",
"hass",
")",
":",
"times",
"=",
"[",
"]",
"@",
"ha",
".",
"callback",
"def",
"run_callback",
"(",
"utc_time",
")",
":",
"nonlocal",
"times",
"times",
".",
"append",
"(",
"utc_time",
")",
"def",
"_setup_listeners",
"(",
")",
":",
"\"\"\"Ensure we test the non-async version.\"\"\"",
"utc_now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"with",
"pytest",
".",
"raises",
"(",
"TypeError",
")",
":",
"track_point_in_utc_time",
"(",
"\"nothass\"",
",",
"run_callback",
",",
"utc_now",
")",
"unsub1",
"=",
"hass",
".",
"helpers",
".",
"event",
".",
"track_point_in_utc_time",
"(",
"run_callback",
",",
"utc_now",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.1",
")",
")",
"hass",
".",
"helpers",
".",
"event",
".",
"track_point_in_utc_time",
"(",
"run_callback",
",",
"utc_now",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.1",
")",
")",
"unsub1",
"(",
")",
"await",
"hass",
".",
"async_add_executor_job",
"(",
"_setup_listeners",
")",
"await",
"asyncio",
".",
"sleep",
"(",
"0.2",
")",
"assert",
"len",
"(",
"times",
")",
"==",
"1",
"assert",
"times",
"[",
"0",
"]",
".",
"tzinfo",
"==",
"dt_util",
".",
"UTC"
] | [
3125,
0
] | [
3156,
41
] | python | en | ['en', 'en', 'en'] | True |
test_async_track_point_in_time_cancel | (hass) | Test cancel of async track point in time. | Test cancel of async track point in time. | async def test_async_track_point_in_time_cancel(hass):
"""Test cancel of async track point in time."""
times = []
hst_tz = dt_util.get_time_zone("US/Hawaii")
dt_util.set_default_time_zone(hst_tz)
@ha.callback
def run_callback(local_time):
nonlocal times
times.append(local_time)
utc_now = dt_util.utcnow()
hst_now = utc_now.astimezone(hst_tz)
unsub1 = hass.helpers.event.async_track_point_in_time(
run_callback, hst_now + timedelta(seconds=0.1)
)
hass.helpers.event.async_track_point_in_time(
run_callback, hst_now + timedelta(seconds=0.1)
)
unsub1()
await asyncio.sleep(0.2)
assert len(times) == 1
assert times[0].tzinfo.zone == "US/Hawaii" | [
"async",
"def",
"test_async_track_point_in_time_cancel",
"(",
"hass",
")",
":",
"times",
"=",
"[",
"]",
"hst_tz",
"=",
"dt_util",
".",
"get_time_zone",
"(",
"\"US/Hawaii\"",
")",
"dt_util",
".",
"set_default_time_zone",
"(",
"hst_tz",
")",
"@",
"ha",
".",
"callback",
"def",
"run_callback",
"(",
"local_time",
")",
":",
"nonlocal",
"times",
"times",
".",
"append",
"(",
"local_time",
")",
"utc_now",
"=",
"dt_util",
".",
"utcnow",
"(",
")",
"hst_now",
"=",
"utc_now",
".",
"astimezone",
"(",
"hst_tz",
")",
"unsub1",
"=",
"hass",
".",
"helpers",
".",
"event",
".",
"async_track_point_in_time",
"(",
"run_callback",
",",
"hst_now",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.1",
")",
")",
"hass",
".",
"helpers",
".",
"event",
".",
"async_track_point_in_time",
"(",
"run_callback",
",",
"hst_now",
"+",
"timedelta",
"(",
"seconds",
"=",
"0.1",
")",
")",
"unsub1",
"(",
")",
"await",
"asyncio",
".",
"sleep",
"(",
"0.2",
")",
"assert",
"len",
"(",
"times",
")",
"==",
"1",
"assert",
"times",
"[",
"0",
"]",
".",
"tzinfo",
".",
"zone",
"==",
"\"US/Hawaii\""
] | [
3159,
0
] | [
3186,
46
] | python | en | ['en', 'en', 'en'] | True |
AlexaConfig.__init__ | (self, hass, config, prefs: CloudPreferences, cloud) | Initialize the Alexa config. | Initialize the Alexa config. | def __init__(self, hass, config, prefs: CloudPreferences, cloud):
"""Initialize the Alexa config."""
super().__init__(hass)
self._config = config
self._prefs = prefs
self._cloud = cloud
self._token = None
self._token_valid = None
self._cur_entity_prefs = prefs.alexa_entity_configs
self._cur_default_expose = prefs.alexa_default_expose
self._alexa_sync_unsub = None
self._endpoint = None
prefs.async_listen_updates(self._async_prefs_updated)
hass.bus.async_listen(
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED,
self._handle_entity_registry_updated,
) | [
"def",
"__init__",
"(",
"self",
",",
"hass",
",",
"config",
",",
"prefs",
":",
"CloudPreferences",
",",
"cloud",
")",
":",
"super",
"(",
")",
".",
"__init__",
"(",
"hass",
")",
"self",
".",
"_config",
"=",
"config",
"self",
".",
"_prefs",
"=",
"prefs",
"self",
".",
"_cloud",
"=",
"cloud",
"self",
".",
"_token",
"=",
"None",
"self",
".",
"_token_valid",
"=",
"None",
"self",
".",
"_cur_entity_prefs",
"=",
"prefs",
".",
"alexa_entity_configs",
"self",
".",
"_cur_default_expose",
"=",
"prefs",
".",
"alexa_default_expose",
"self",
".",
"_alexa_sync_unsub",
"=",
"None",
"self",
".",
"_endpoint",
"=",
"None",
"prefs",
".",
"async_listen_updates",
"(",
"self",
".",
"_async_prefs_updated",
")",
"hass",
".",
"bus",
".",
"async_listen",
"(",
"entity_registry",
".",
"EVENT_ENTITY_REGISTRY_UPDATED",
",",
"self",
".",
"_handle_entity_registry_updated",
",",
")"
] | [
34,
4
] | [
51,
9
] | python | en | ['en', 'en', 'en'] | True |
AlexaConfig.enabled | (self) | Return if Alexa is enabled. | Return if Alexa is enabled. | def enabled(self):
"""Return if Alexa is enabled."""
return self._prefs.alexa_enabled | [
"def",
"enabled",
"(",
"self",
")",
":",
"return",
"self",
".",
"_prefs",
".",
"alexa_enabled"
] | [
54,
4
] | [
56,
40
] | python | en | ['en', 'af', 'en'] | True |
AlexaConfig.supports_auth | (self) | Return if config supports auth. | Return if config supports auth. | def supports_auth(self):
"""Return if config supports auth."""
return True | [
"def",
"supports_auth",
"(",
"self",
")",
":",
"return",
"True"
] | [
59,
4
] | [
61,
19
] | python | en | ['en', 'en', 'en'] | True |
AlexaConfig.should_report_state | (self) | Return if states should be proactively reported. | Return if states should be proactively reported. | def should_report_state(self):
"""Return if states should be proactively reported."""
return self._prefs.alexa_report_state | [
"def",
"should_report_state",
"(",
"self",
")",
":",
"return",
"self",
".",
"_prefs",
".",
"alexa_report_state"
] | [
64,
4
] | [
66,
45
] | python | en | ['en', 'en', 'en'] | True |
AlexaConfig.endpoint | (self) | Endpoint for report state. | Endpoint for report state. | def endpoint(self):
"""Endpoint for report state."""
if self._endpoint is None:
raise ValueError("No endpoint available. Fetch access token first")
return self._endpoint | [
"def",
"endpoint",
"(",
"self",
")",
":",
"if",
"self",
".",
"_endpoint",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"No endpoint available. Fetch access token first\"",
")",
"return",
"self",
".",
"_endpoint"
] | [
69,
4
] | [
74,
29
] | python | en | ['en', 'en', 'en'] | True |
AlexaConfig.locale | (self) | Return config locale. | Return config locale. | def locale(self):
"""Return config locale."""
# Not clear how to determine locale atm.
return "en-US" | [
"def",
"locale",
"(",
"self",
")",
":",
"# Not clear how to determine locale atm.",
"return",
"\"en-US\""
] | [
77,
4
] | [
80,
22
] | python | de | ['de', 'la', 'en'] | False |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.