Search is not available for this dataset
identifier
stringlengths 1
155
| parameters
stringlengths 2
6.09k
| docstring
stringlengths 11
63.4k
| docstring_summary
stringlengths 0
63.4k
| function
stringlengths 29
99.8k
| function_tokens
list | start_point
list | end_point
list | language
stringclasses 1
value | docstring_language
stringlengths 2
7
| docstring_language_predictions
stringlengths 18
23
| is_langid_reliable
stringclasses 2
values |
---|---|---|---|---|---|---|---|---|---|---|---|
MyStromLight.hs_color | (self) | Return the color of the light. | Return the color of the light. | def hs_color(self):
"""Return the color of the light."""
return self._color_h, self._color_s | [
"def",
"hs_color",
"(",
"self",
")",
":",
"return",
"self",
".",
"_color_h",
",",
"self",
".",
"_color_s"
] | [
96,
4
] | [
98,
43
] | python | en | ['en', 'en', 'en'] | True |
MyStromLight.available | (self) | Return True if entity is available. | Return True if entity is available. | def available(self) -> bool:
"""Return True if entity is available."""
return self._available | [
"def",
"available",
"(",
"self",
")",
"->",
"bool",
":",
"return",
"self",
".",
"_available"
] | [
101,
4
] | [
103,
30
] | python | en | ['en', 'en', 'en'] | True |
MyStromLight.effect_list | (self) | Return the list of supported effects. | Return the list of supported effects. | def effect_list(self):
"""Return the list of supported effects."""
return MYSTROM_EFFECT_LIST | [
"def",
"effect_list",
"(",
"self",
")",
":",
"return",
"MYSTROM_EFFECT_LIST"
] | [
106,
4
] | [
108,
34
] | python | en | ['en', 'en', 'en'] | True |
MyStromLight.is_on | (self) | Return true if light is on. | Return true if light is on. | def is_on(self):
"""Return true if light is on."""
return self._state | [
"def",
"is_on",
"(",
"self",
")",
":",
"return",
"self",
".",
"_state"
] | [
111,
4
] | [
113,
26
] | python | en | ['en', 'et', 'en'] | True |
MyStromLight.async_turn_on | (self, **kwargs) | Turn on the light. | Turn on the light. | async def async_turn_on(self, **kwargs):
"""Turn on the light."""
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
effect = kwargs.get(ATTR_EFFECT)
if ATTR_HS_COLOR in kwargs:
color_h, color_s = kwargs[ATTR_HS_COLOR]
elif ATTR_BRIGHTNESS in kwargs:
# Brightness update, keep color
color_h, color_s = self._color_h, self._color_s
else:
color_h, color_s = 0, 0 # Back to white
try:
if not self.is_on:
await self._bulb.set_on()
if brightness is not None:
await self._bulb.set_color_hsv(
int(color_h), int(color_s), round(brightness * 100 / 255)
)
if effect == EFFECT_SUNRISE:
await self._bulb.set_sunrise(30)
if effect == EFFECT_RAINBOW:
await self._bulb.set_rainbow(30)
except MyStromConnectionError:
_LOGGER.warning("No route to myStrom bulb") | [
"async",
"def",
"async_turn_on",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"brightness",
"=",
"kwargs",
".",
"get",
"(",
"ATTR_BRIGHTNESS",
",",
"255",
")",
"effect",
"=",
"kwargs",
".",
"get",
"(",
"ATTR_EFFECT",
")",
"if",
"ATTR_HS_COLOR",
"in",
"kwargs",
":",
"color_h",
",",
"color_s",
"=",
"kwargs",
"[",
"ATTR_HS_COLOR",
"]",
"elif",
"ATTR_BRIGHTNESS",
"in",
"kwargs",
":",
"# Brightness update, keep color",
"color_h",
",",
"color_s",
"=",
"self",
".",
"_color_h",
",",
"self",
".",
"_color_s",
"else",
":",
"color_h",
",",
"color_s",
"=",
"0",
",",
"0",
"# Back to white",
"try",
":",
"if",
"not",
"self",
".",
"is_on",
":",
"await",
"self",
".",
"_bulb",
".",
"set_on",
"(",
")",
"if",
"brightness",
"is",
"not",
"None",
":",
"await",
"self",
".",
"_bulb",
".",
"set_color_hsv",
"(",
"int",
"(",
"color_h",
")",
",",
"int",
"(",
"color_s",
")",
",",
"round",
"(",
"brightness",
"*",
"100",
"/",
"255",
")",
")",
"if",
"effect",
"==",
"EFFECT_SUNRISE",
":",
"await",
"self",
".",
"_bulb",
".",
"set_sunrise",
"(",
"30",
")",
"if",
"effect",
"==",
"EFFECT_RAINBOW",
":",
"await",
"self",
".",
"_bulb",
".",
"set_rainbow",
"(",
"30",
")",
"except",
"MyStromConnectionError",
":",
"_LOGGER",
".",
"warning",
"(",
"\"No route to myStrom bulb\"",
")"
] | [
115,
4
] | [
140,
55
] | python | en | ['en', 'et', 'en'] | True |
MyStromLight.async_turn_off | (self, **kwargs) | Turn off the bulb. | Turn off the bulb. | async def async_turn_off(self, **kwargs):
"""Turn off the bulb."""
try:
await self._bulb.set_off()
except MyStromConnectionError:
_LOGGER.warning("myStrom bulb not online") | [
"async",
"def",
"async_turn_off",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"try",
":",
"await",
"self",
".",
"_bulb",
".",
"set_off",
"(",
")",
"except",
"MyStromConnectionError",
":",
"_LOGGER",
".",
"warning",
"(",
"\"myStrom bulb not online\"",
")"
] | [
142,
4
] | [
147,
54
] | python | en | ['en', 'ig', 'en'] | True |
MyStromLight.async_update | (self) | Fetch new state data for this light. | Fetch new state data for this light. | async def async_update(self):
"""Fetch new state data for this light."""
try:
await self._bulb.get_state()
self._state = self._bulb.state
colors = self._bulb.color
try:
color_h, color_s, color_v = colors.split(";")
except ValueError:
color_s, color_v = colors.split(";")
color_h = 0
self._color_h = int(color_h)
self._color_s = int(color_s)
self._brightness = int(color_v) * 255 / 100
self._available = True
except MyStromConnectionError:
_LOGGER.warning("No route to myStrom bulb")
self._available = False | [
"async",
"def",
"async_update",
"(",
"self",
")",
":",
"try",
":",
"await",
"self",
".",
"_bulb",
".",
"get_state",
"(",
")",
"self",
".",
"_state",
"=",
"self",
".",
"_bulb",
".",
"state",
"colors",
"=",
"self",
".",
"_bulb",
".",
"color",
"try",
":",
"color_h",
",",
"color_s",
",",
"color_v",
"=",
"colors",
".",
"split",
"(",
"\";\"",
")",
"except",
"ValueError",
":",
"color_s",
",",
"color_v",
"=",
"colors",
".",
"split",
"(",
"\";\"",
")",
"color_h",
"=",
"0",
"self",
".",
"_color_h",
"=",
"int",
"(",
"color_h",
")",
"self",
".",
"_color_s",
"=",
"int",
"(",
"color_s",
")",
"self",
".",
"_brightness",
"=",
"int",
"(",
"color_v",
")",
"*",
"255",
"/",
"100",
"self",
".",
"_available",
"=",
"True",
"except",
"MyStromConnectionError",
":",
"_LOGGER",
".",
"warning",
"(",
"\"No route to myStrom bulb\"",
")",
"self",
".",
"_available",
"=",
"False"
] | [
149,
4
] | [
169,
35
] | python | en | ['en', 'en', 'en'] | True |
test_temperature_not_a_number | (hass) | Test that temperature is a number. | Test that temperature is a number. | def test_temperature_not_a_number(hass):
"""Test that temperature is a number."""
temp = "Temperature"
with pytest.raises(Exception) as exception:
display_temp(hass, temp, TEMP_CELSIUS, PRECISION_HALVES)
assert f"Temperature is not a number: {temp}" in str(exception.value) | [
"def",
"test_temperature_not_a_number",
"(",
"hass",
")",
":",
"temp",
"=",
"\"Temperature\"",
"with",
"pytest",
".",
"raises",
"(",
"Exception",
")",
"as",
"exception",
":",
"display_temp",
"(",
"hass",
",",
"temp",
",",
"TEMP_CELSIUS",
",",
"PRECISION_HALVES",
")",
"assert",
"f\"Temperature is not a number: {temp}\"",
"in",
"str",
"(",
"exception",
".",
"value",
")"
] | [
15,
0
] | [
21,
73
] | python | en | ['en', 'en', 'en'] | True |
test_celsius_halves | (hass) | Test temperature to celsius rounding to halves. | Test temperature to celsius rounding to halves. | def test_celsius_halves(hass):
"""Test temperature to celsius rounding to halves."""
assert display_temp(hass, TEMP, TEMP_CELSIUS, PRECISION_HALVES) == 24.5 | [
"def",
"test_celsius_halves",
"(",
"hass",
")",
":",
"assert",
"display_temp",
"(",
"hass",
",",
"TEMP",
",",
"TEMP_CELSIUS",
",",
"PRECISION_HALVES",
")",
"==",
"24.5"
] | [
24,
0
] | [
26,
75
] | python | en | ['en', 'en', 'en'] | True |
test_celsius_tenths | (hass) | Test temperature to celsius rounding to tenths. | Test temperature to celsius rounding to tenths. | def test_celsius_tenths(hass):
"""Test temperature to celsius rounding to tenths."""
assert display_temp(hass, TEMP, TEMP_CELSIUS, PRECISION_TENTHS) == 24.6 | [
"def",
"test_celsius_tenths",
"(",
"hass",
")",
":",
"assert",
"display_temp",
"(",
"hass",
",",
"TEMP",
",",
"TEMP_CELSIUS",
",",
"PRECISION_TENTHS",
")",
"==",
"24.6"
] | [
29,
0
] | [
31,
75
] | python | en | ['en', 'en', 'en'] | True |
test_fahrenheit_wholes | (hass) | Test temperature to fahrenheit rounding to wholes. | Test temperature to fahrenheit rounding to wholes. | def test_fahrenheit_wholes(hass):
"""Test temperature to fahrenheit rounding to wholes."""
assert display_temp(hass, TEMP, TEMP_FAHRENHEIT, PRECISION_WHOLE) == -4 | [
"def",
"test_fahrenheit_wholes",
"(",
"hass",
")",
":",
"assert",
"display_temp",
"(",
"hass",
",",
"TEMP",
",",
"TEMP_FAHRENHEIT",
",",
"PRECISION_WHOLE",
")",
"==",
"-",
"4"
] | [
34,
0
] | [
36,
75
] | python | en | ['en', 'el-Latn', 'en'] | True |
async_setup | (hass, base_config) | Set up of Tesla component. | Set up of Tesla component. | async def async_setup(hass, base_config):
"""Set up of Tesla component."""
def _update_entry(email, data=None, options=None):
data = data or {}
options = options or {
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
CONF_WAKE_ON_START: DEFAULT_WAKE_ON_START,
}
for entry in hass.config_entries.async_entries(DOMAIN):
if email != entry.title:
continue
hass.config_entries.async_update_entry(entry, data=data, options=options)
config = base_config.get(DOMAIN)
if not config:
return True
email = config[CONF_USERNAME]
password = config[CONF_PASSWORD]
scan_interval = config[CONF_SCAN_INTERVAL]
if email in configured_instances(hass):
try:
info = await validate_input(hass, config)
except (CannotConnect, InvalidAuth):
return False
_update_entry(
email,
data={
CONF_ACCESS_TOKEN: info[CONF_ACCESS_TOKEN],
CONF_TOKEN: info[CONF_TOKEN],
},
options={CONF_SCAN_INTERVAL: scan_interval},
)
else:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={CONF_USERNAME: email, CONF_PASSWORD: password},
)
)
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][email] = {CONF_SCAN_INTERVAL: scan_interval}
return True | [
"async",
"def",
"async_setup",
"(",
"hass",
",",
"base_config",
")",
":",
"def",
"_update_entry",
"(",
"email",
",",
"data",
"=",
"None",
",",
"options",
"=",
"None",
")",
":",
"data",
"=",
"data",
"or",
"{",
"}",
"options",
"=",
"options",
"or",
"{",
"CONF_SCAN_INTERVAL",
":",
"DEFAULT_SCAN_INTERVAL",
",",
"CONF_WAKE_ON_START",
":",
"DEFAULT_WAKE_ON_START",
",",
"}",
"for",
"entry",
"in",
"hass",
".",
"config_entries",
".",
"async_entries",
"(",
"DOMAIN",
")",
":",
"if",
"email",
"!=",
"entry",
".",
"title",
":",
"continue",
"hass",
".",
"config_entries",
".",
"async_update_entry",
"(",
"entry",
",",
"data",
"=",
"data",
",",
"options",
"=",
"options",
")",
"config",
"=",
"base_config",
".",
"get",
"(",
"DOMAIN",
")",
"if",
"not",
"config",
":",
"return",
"True",
"email",
"=",
"config",
"[",
"CONF_USERNAME",
"]",
"password",
"=",
"config",
"[",
"CONF_PASSWORD",
"]",
"scan_interval",
"=",
"config",
"[",
"CONF_SCAN_INTERVAL",
"]",
"if",
"email",
"in",
"configured_instances",
"(",
"hass",
")",
":",
"try",
":",
"info",
"=",
"await",
"validate_input",
"(",
"hass",
",",
"config",
")",
"except",
"(",
"CannotConnect",
",",
"InvalidAuth",
")",
":",
"return",
"False",
"_update_entry",
"(",
"email",
",",
"data",
"=",
"{",
"CONF_ACCESS_TOKEN",
":",
"info",
"[",
"CONF_ACCESS_TOKEN",
"]",
",",
"CONF_TOKEN",
":",
"info",
"[",
"CONF_TOKEN",
"]",
",",
"}",
",",
"options",
"=",
"{",
"CONF_SCAN_INTERVAL",
":",
"scan_interval",
"}",
",",
")",
"else",
":",
"hass",
".",
"async_create_task",
"(",
"hass",
".",
"config_entries",
".",
"flow",
".",
"async_init",
"(",
"DOMAIN",
",",
"context",
"=",
"{",
"\"source\"",
":",
"SOURCE_IMPORT",
"}",
",",
"data",
"=",
"{",
"CONF_USERNAME",
":",
"email",
",",
"CONF_PASSWORD",
":",
"password",
"}",
",",
")",
")",
"hass",
".",
"data",
".",
"setdefault",
"(",
"DOMAIN",
",",
"{",
"}",
")",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"[",
"email",
"]",
"=",
"{",
"CONF_SCAN_INTERVAL",
":",
"scan_interval",
"}",
"return",
"True"
] | [
77,
0
] | [
120,
15
] | python | en | ['en', 'da', 'en'] | True |
async_setup_entry | (hass, config_entry) | Set up Tesla as config entry. | Set up Tesla as config entry. | async def async_setup_entry(hass, config_entry):
"""Set up Tesla as config entry."""
hass.data.setdefault(DOMAIN, {})
config = config_entry.data
websession = aiohttp_client.async_get_clientsession(hass)
email = config_entry.title
if email in hass.data[DOMAIN] and CONF_SCAN_INTERVAL in hass.data[DOMAIN][email]:
scan_interval = hass.data[DOMAIN][email][CONF_SCAN_INTERVAL]
hass.config_entries.async_update_entry(
config_entry, options={CONF_SCAN_INTERVAL: scan_interval}
)
hass.data[DOMAIN].pop(email)
try:
controller = TeslaAPI(
websession,
refresh_token=config[CONF_TOKEN],
access_token=config[CONF_ACCESS_TOKEN],
update_interval=config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
)
(refresh_token, access_token) = await controller.connect(
wake_if_asleep=config_entry.options.get(
CONF_WAKE_ON_START, DEFAULT_WAKE_ON_START
)
)
except TeslaException as ex:
_LOGGER.error("Unable to communicate with Tesla API: %s", ex.message)
return False
_async_save_tokens(hass, config_entry, access_token, refresh_token)
coordinator = TeslaDataUpdateCoordinator(
hass, config_entry=config_entry, controller=controller
)
# Fetch initial data so we have data when entities subscribe
entry_data = hass.data[DOMAIN][config_entry.entry_id] = {
"coordinator": coordinator,
"devices": defaultdict(list),
DATA_LISTENER: [config_entry.add_update_listener(update_listener)],
}
_LOGGER.debug("Connected to the Tesla API")
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise ConfigEntryNotReady
all_devices = controller.get_homeassistant_components()
if not all_devices:
return False
for device in all_devices:
entry_data["devices"][device.hass_type].append(device)
for component in TESLA_COMPONENTS:
_LOGGER.debug("Loading %s", component)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, component)
)
return True | [
"async",
"def",
"async_setup_entry",
"(",
"hass",
",",
"config_entry",
")",
":",
"hass",
".",
"data",
".",
"setdefault",
"(",
"DOMAIN",
",",
"{",
"}",
")",
"config",
"=",
"config_entry",
".",
"data",
"websession",
"=",
"aiohttp_client",
".",
"async_get_clientsession",
"(",
"hass",
")",
"email",
"=",
"config_entry",
".",
"title",
"if",
"email",
"in",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"and",
"CONF_SCAN_INTERVAL",
"in",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"[",
"email",
"]",
":",
"scan_interval",
"=",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"[",
"email",
"]",
"[",
"CONF_SCAN_INTERVAL",
"]",
"hass",
".",
"config_entries",
".",
"async_update_entry",
"(",
"config_entry",
",",
"options",
"=",
"{",
"CONF_SCAN_INTERVAL",
":",
"scan_interval",
"}",
")",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
".",
"pop",
"(",
"email",
")",
"try",
":",
"controller",
"=",
"TeslaAPI",
"(",
"websession",
",",
"refresh_token",
"=",
"config",
"[",
"CONF_TOKEN",
"]",
",",
"access_token",
"=",
"config",
"[",
"CONF_ACCESS_TOKEN",
"]",
",",
"update_interval",
"=",
"config_entry",
".",
"options",
".",
"get",
"(",
"CONF_SCAN_INTERVAL",
",",
"DEFAULT_SCAN_INTERVAL",
")",
",",
")",
"(",
"refresh_token",
",",
"access_token",
")",
"=",
"await",
"controller",
".",
"connect",
"(",
"wake_if_asleep",
"=",
"config_entry",
".",
"options",
".",
"get",
"(",
"CONF_WAKE_ON_START",
",",
"DEFAULT_WAKE_ON_START",
")",
")",
"except",
"TeslaException",
"as",
"ex",
":",
"_LOGGER",
".",
"error",
"(",
"\"Unable to communicate with Tesla API: %s\"",
",",
"ex",
".",
"message",
")",
"return",
"False",
"_async_save_tokens",
"(",
"hass",
",",
"config_entry",
",",
"access_token",
",",
"refresh_token",
")",
"coordinator",
"=",
"TeslaDataUpdateCoordinator",
"(",
"hass",
",",
"config_entry",
"=",
"config_entry",
",",
"controller",
"=",
"controller",
")",
"# Fetch initial data so we have data when entities subscribe",
"entry_data",
"=",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"[",
"config_entry",
".",
"entry_id",
"]",
"=",
"{",
"\"coordinator\"",
":",
"coordinator",
",",
"\"devices\"",
":",
"defaultdict",
"(",
"list",
")",
",",
"DATA_LISTENER",
":",
"[",
"config_entry",
".",
"add_update_listener",
"(",
"update_listener",
")",
"]",
",",
"}",
"_LOGGER",
".",
"debug",
"(",
"\"Connected to the Tesla API\"",
")",
"await",
"coordinator",
".",
"async_refresh",
"(",
")",
"if",
"not",
"coordinator",
".",
"last_update_success",
":",
"raise",
"ConfigEntryNotReady",
"all_devices",
"=",
"controller",
".",
"get_homeassistant_components",
"(",
")",
"if",
"not",
"all_devices",
":",
"return",
"False",
"for",
"device",
"in",
"all_devices",
":",
"entry_data",
"[",
"\"devices\"",
"]",
"[",
"device",
".",
"hass_type",
"]",
".",
"append",
"(",
"device",
")",
"for",
"component",
"in",
"TESLA_COMPONENTS",
":",
"_LOGGER",
".",
"debug",
"(",
"\"Loading %s\"",
",",
"component",
")",
"hass",
".",
"async_create_task",
"(",
"hass",
".",
"config_entries",
".",
"async_forward_entry_setup",
"(",
"config_entry",
",",
"component",
")",
")",
"return",
"True"
] | [
123,
0
] | [
181,
15
] | python | en | ['en', 'en', 'en'] | True |
async_unload_entry | (hass, config_entry) | Unload a config entry. | Unload a config entry. | async def async_unload_entry(hass, config_entry) -> bool:
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, component)
for component in TESLA_COMPONENTS
]
)
)
for listener in hass.data[DOMAIN][config_entry.entry_id][DATA_LISTENER]:
listener()
username = config_entry.title
if unload_ok:
hass.data[DOMAIN].pop(config_entry.entry_id)
_LOGGER.debug("Unloaded entry for %s", username)
return True
return False | [
"async",
"def",
"async_unload_entry",
"(",
"hass",
",",
"config_entry",
")",
"->",
"bool",
":",
"unload_ok",
"=",
"all",
"(",
"await",
"asyncio",
".",
"gather",
"(",
"*",
"[",
"hass",
".",
"config_entries",
".",
"async_forward_entry_unload",
"(",
"config_entry",
",",
"component",
")",
"for",
"component",
"in",
"TESLA_COMPONENTS",
"]",
")",
")",
"for",
"listener",
"in",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"[",
"config_entry",
".",
"entry_id",
"]",
"[",
"DATA_LISTENER",
"]",
":",
"listener",
"(",
")",
"username",
"=",
"config_entry",
".",
"title",
"if",
"unload_ok",
":",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
".",
"pop",
"(",
"config_entry",
".",
"entry_id",
")",
"_LOGGER",
".",
"debug",
"(",
"\"Unloaded entry for %s\"",
",",
"username",
")",
"return",
"True",
"return",
"False"
] | [
184,
0
] | [
201,
16
] | python | en | ['en', 'es', 'en'] | True |
update_listener | (hass, config_entry) | Update when config_entry options update. | Update when config_entry options update. | async def update_listener(hass, config_entry):
"""Update when config_entry options update."""
controller = hass.data[DOMAIN][config_entry.entry_id]["coordinator"].controller
old_update_interval = controller.update_interval
controller.update_interval = config_entry.options.get(CONF_SCAN_INTERVAL)
if old_update_interval != controller.update_interval:
_LOGGER.debug(
"Changing scan_interval from %s to %s",
old_update_interval,
controller.update_interval,
) | [
"async",
"def",
"update_listener",
"(",
"hass",
",",
"config_entry",
")",
":",
"controller",
"=",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"[",
"config_entry",
".",
"entry_id",
"]",
"[",
"\"coordinator\"",
"]",
".",
"controller",
"old_update_interval",
"=",
"controller",
".",
"update_interval",
"controller",
".",
"update_interval",
"=",
"config_entry",
".",
"options",
".",
"get",
"(",
"CONF_SCAN_INTERVAL",
")",
"if",
"old_update_interval",
"!=",
"controller",
".",
"update_interval",
":",
"_LOGGER",
".",
"debug",
"(",
"\"Changing scan_interval from %s to %s\"",
",",
"old_update_interval",
",",
"controller",
".",
"update_interval",
",",
")"
] | [
204,
0
] | [
214,
9
] | python | en | ['en', 'en', 'en'] | True |
TeslaDataUpdateCoordinator.__init__ | (self, hass, *, config_entry, controller) | Initialize global Tesla data updater. | Initialize global Tesla data updater. | def __init__(self, hass, *, config_entry, controller):
"""Initialize global Tesla data updater."""
self.controller = controller
self.config_entry = config_entry
update_interval = timedelta(seconds=MIN_SCAN_INTERVAL)
super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=update_interval,
) | [
"def",
"__init__",
"(",
"self",
",",
"hass",
",",
"*",
",",
"config_entry",
",",
"controller",
")",
":",
"self",
".",
"controller",
"=",
"controller",
"self",
".",
"config_entry",
"=",
"config_entry",
"update_interval",
"=",
"timedelta",
"(",
"seconds",
"=",
"MIN_SCAN_INTERVAL",
")",
"super",
"(",
")",
".",
"__init__",
"(",
"hass",
",",
"_LOGGER",
",",
"name",
"=",
"DOMAIN",
",",
"update_interval",
"=",
"update_interval",
",",
")"
] | [
220,
4
] | [
232,
9
] | python | en | ['en', 'en', 'it'] | True |
TeslaDataUpdateCoordinator._async_update_data | (self) | Fetch data from API endpoint. | Fetch data from API endpoint. | async def _async_update_data(self):
"""Fetch data from API endpoint."""
if self.controller.is_token_refreshed():
(refresh_token, access_token) = self.controller.get_tokens()
_async_save_tokens(
self.hass, self.config_entry, access_token, refresh_token
)
_LOGGER.debug("Saving new tokens in config_entry")
try:
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(30):
return await self.controller.update()
except TeslaException as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err | [
"async",
"def",
"_async_update_data",
"(",
"self",
")",
":",
"if",
"self",
".",
"controller",
".",
"is_token_refreshed",
"(",
")",
":",
"(",
"refresh_token",
",",
"access_token",
")",
"=",
"self",
".",
"controller",
".",
"get_tokens",
"(",
")",
"_async_save_tokens",
"(",
"self",
".",
"hass",
",",
"self",
".",
"config_entry",
",",
"access_token",
",",
"refresh_token",
")",
"_LOGGER",
".",
"debug",
"(",
"\"Saving new tokens in config_entry\"",
")",
"try",
":",
"# Note: asyncio.TimeoutError and aiohttp.ClientError are already",
"# handled by the data update coordinator.",
"async",
"with",
"async_timeout",
".",
"timeout",
"(",
"30",
")",
":",
"return",
"await",
"self",
".",
"controller",
".",
"update",
"(",
")",
"except",
"TeslaException",
"as",
"err",
":",
"raise",
"UpdateFailed",
"(",
"f\"Error communicating with API: {err}\"",
")",
"from",
"err"
] | [
234,
4
] | [
249,
79
] | python | en | ['en', 'en', 'en'] | True |
TeslaDevice.__init__ | (self, tesla_device, coordinator) | Initialise the Tesla device. | Initialise the Tesla device. | def __init__(self, tesla_device, coordinator):
"""Initialise the Tesla device."""
super().__init__(coordinator)
self.tesla_device = tesla_device
self._name = self.tesla_device.name
self._unique_id = slugify(self.tesla_device.uniq_name)
self._attributes = self.tesla_device.attrs.copy() | [
"def",
"__init__",
"(",
"self",
",",
"tesla_device",
",",
"coordinator",
")",
":",
"super",
"(",
")",
".",
"__init__",
"(",
"coordinator",
")",
"self",
".",
"tesla_device",
"=",
"tesla_device",
"self",
".",
"_name",
"=",
"self",
".",
"tesla_device",
".",
"name",
"self",
".",
"_unique_id",
"=",
"slugify",
"(",
"self",
".",
"tesla_device",
".",
"uniq_name",
")",
"self",
".",
"_attributes",
"=",
"self",
".",
"tesla_device",
".",
"attrs",
".",
"copy",
"(",
")"
] | [
255,
4
] | [
261,
57
] | python | en | ['en', 'zu', 'en'] | True |
TeslaDevice.name | (self) | Return the name of the device. | Return the name of the device. | def name(self):
"""Return the name of the device."""
return self._name | [
"def",
"name",
"(",
"self",
")",
":",
"return",
"self",
".",
"_name"
] | [
264,
4
] | [
266,
25
] | python | en | ['en', 'en', 'en'] | True |
TeslaDevice.unique_id | (self) | Return a unique ID. | Return a unique ID. | def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id | [
"def",
"unique_id",
"(",
"self",
")",
"->",
"str",
":",
"return",
"self",
".",
"_unique_id"
] | [
269,
4
] | [
271,
30
] | python | ca | ['fr', 'ca', 'en'] | False |
TeslaDevice.icon | (self) | Return the icon of the sensor. | Return the icon of the sensor. | def icon(self):
"""Return the icon of the sensor."""
if self.device_class:
return None
return ICONS.get(self.tesla_device.type) | [
"def",
"icon",
"(",
"self",
")",
":",
"if",
"self",
".",
"device_class",
":",
"return",
"None",
"return",
"ICONS",
".",
"get",
"(",
"self",
".",
"tesla_device",
".",
"type",
")"
] | [
274,
4
] | [
279,
48
] | python | en | ['en', 'en', 'en'] | True |
TeslaDevice.device_state_attributes | (self) | Return the state attributes of the device. | Return the state attributes of the device. | def device_state_attributes(self):
"""Return the state attributes of the device."""
attr = self._attributes
if self.tesla_device.has_battery():
attr[ATTR_BATTERY_LEVEL] = self.tesla_device.battery_level()
attr[ATTR_BATTERY_CHARGING] = self.tesla_device.battery_charging()
return attr | [
"def",
"device_state_attributes",
"(",
"self",
")",
":",
"attr",
"=",
"self",
".",
"_attributes",
"if",
"self",
".",
"tesla_device",
".",
"has_battery",
"(",
")",
":",
"attr",
"[",
"ATTR_BATTERY_LEVEL",
"]",
"=",
"self",
".",
"tesla_device",
".",
"battery_level",
"(",
")",
"attr",
"[",
"ATTR_BATTERY_CHARGING",
"]",
"=",
"self",
".",
"tesla_device",
".",
"battery_charging",
"(",
")",
"return",
"attr"
] | [
282,
4
] | [
288,
19
] | python | en | ['en', 'en', 'en'] | True |
TeslaDevice.device_info | (self) | Return the device_info of the device. | Return the device_info of the device. | def device_info(self):
"""Return the device_info of the device."""
return {
"identifiers": {(DOMAIN, self.tesla_device.id())},
"name": self.tesla_device.car_name(),
"manufacturer": "Tesla",
"model": self.tesla_device.car_type,
"sw_version": self.tesla_device.car_version,
} | [
"def",
"device_info",
"(",
"self",
")",
":",
"return",
"{",
"\"identifiers\"",
":",
"{",
"(",
"DOMAIN",
",",
"self",
".",
"tesla_device",
".",
"id",
"(",
")",
")",
"}",
",",
"\"name\"",
":",
"self",
".",
"tesla_device",
".",
"car_name",
"(",
")",
",",
"\"manufacturer\"",
":",
"\"Tesla\"",
",",
"\"model\"",
":",
"self",
".",
"tesla_device",
".",
"car_type",
",",
"\"sw_version\"",
":",
"self",
".",
"tesla_device",
".",
"car_version",
",",
"}"
] | [
291,
4
] | [
299,
9
] | python | en | ['en', 'en', 'en'] | True |
TeslaDevice.async_added_to_hass | (self) | Register state update callback. | Register state update callback. | async def async_added_to_hass(self):
"""Register state update callback."""
self.async_on_remove(self.coordinator.async_add_listener(self.refresh)) | [
"async",
"def",
"async_added_to_hass",
"(",
"self",
")",
":",
"self",
".",
"async_on_remove",
"(",
"self",
".",
"coordinator",
".",
"async_add_listener",
"(",
"self",
".",
"refresh",
")",
")"
] | [
301,
4
] | [
303,
79
] | python | en | ['en', 'co', 'en'] | True |
TeslaDevice.refresh | (self) | Refresh the state of the device.
This assumes the coordinator has updated the controller.
| Refresh the state of the device. | def refresh(self) -> None:
"""Refresh the state of the device.
This assumes the coordinator has updated the controller.
"""
self.tesla_device.refresh()
self._attributes = self.tesla_device.attrs.copy()
self.async_write_ha_state() | [
"def",
"refresh",
"(",
"self",
")",
"->",
"None",
":",
"self",
".",
"tesla_device",
".",
"refresh",
"(",
")",
"self",
".",
"_attributes",
"=",
"self",
".",
"tesla_device",
".",
"attrs",
".",
"copy",
"(",
")",
"self",
".",
"async_write_ha_state",
"(",
")"
] | [
306,
4
] | [
313,
35
] | python | en | ['en', 'en', 'en'] | True |
test_capped_setup | (hass, aioclient_mock) | Test the default setup. | Test the default setup. | async def test_capped_setup(hass, aioclient_mock):
"""Test the default setup."""
config = {
"platform": "startca",
"api_key": "NOTAKEY",
"total_bandwidth": 400,
"monitored_variables": [
"usage",
"usage_gb",
"limit",
"used_download",
"used_upload",
"used_total",
"grace_download",
"grace_upload",
"grace_total",
"total_download",
"total_upload",
"used_remaining",
],
}
result = (
'<?xml version="1.0" encoding="ISO-8859-15"?>'
"<usage>"
"<version>1.1</version>"
"<total> <!-- total actual usage -->"
"<download>304946829777</download>"
"<upload>6480700153</upload>"
"</total>"
"<used> <!-- part of usage that counts against quota -->"
"<download>304946829777</download>"
"<upload>6480700153</upload>"
"</used>"
"<grace> <!-- part of usage that is free -->"
"<download>304946829777</download>"
"<upload>6480700153</upload>"
"</grace>"
"</usage>"
)
aioclient_mock.get(
"https://www.start.ca/support/usage/api?key=NOTAKEY", text=result
)
await async_setup_component(hass, "sensor", {"sensor": config})
await hass.async_block_till_done()
state = hass.states.get("sensor.start_ca_usage_ratio")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
assert state.state == "76.24"
state = hass.states.get("sensor.start_ca_usage")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "304.95"
state = hass.states.get("sensor.start_ca_data_limit")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "400"
state = hass.states.get("sensor.start_ca_used_download")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "304.95"
state = hass.states.get("sensor.start_ca_used_upload")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "6.48"
state = hass.states.get("sensor.start_ca_used_total")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "311.43"
state = hass.states.get("sensor.start_ca_grace_download")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "304.95"
state = hass.states.get("sensor.start_ca_grace_upload")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "6.48"
state = hass.states.get("sensor.start_ca_grace_total")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "311.43"
state = hass.states.get("sensor.start_ca_total_download")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "304.95"
state = hass.states.get("sensor.start_ca_total_upload")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "6.48"
state = hass.states.get("sensor.start_ca_remaining")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "95.05" | [
"async",
"def",
"test_capped_setup",
"(",
"hass",
",",
"aioclient_mock",
")",
":",
"config",
"=",
"{",
"\"platform\"",
":",
"\"startca\"",
",",
"\"api_key\"",
":",
"\"NOTAKEY\"",
",",
"\"total_bandwidth\"",
":",
"400",
",",
"\"monitored_variables\"",
":",
"[",
"\"usage\"",
",",
"\"usage_gb\"",
",",
"\"limit\"",
",",
"\"used_download\"",
",",
"\"used_upload\"",
",",
"\"used_total\"",
",",
"\"grace_download\"",
",",
"\"grace_upload\"",
",",
"\"grace_total\"",
",",
"\"total_download\"",
",",
"\"total_upload\"",
",",
"\"used_remaining\"",
",",
"]",
",",
"}",
"result",
"=",
"(",
"'<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>'",
"\"<usage>\"",
"\"<version>1.1</version>\"",
"\"<total> <!-- total actual usage -->\"",
"\"<download>304946829777</download>\"",
"\"<upload>6480700153</upload>\"",
"\"</total>\"",
"\"<used> <!-- part of usage that counts against quota -->\"",
"\"<download>304946829777</download>\"",
"\"<upload>6480700153</upload>\"",
"\"</used>\"",
"\"<grace> <!-- part of usage that is free -->\"",
"\"<download>304946829777</download>\"",
"\"<upload>6480700153</upload>\"",
"\"</grace>\"",
"\"</usage>\"",
")",
"aioclient_mock",
".",
"get",
"(",
"\"https://www.start.ca/support/usage/api?key=NOTAKEY\"",
",",
"text",
"=",
"result",
")",
"await",
"async_setup_component",
"(",
"hass",
",",
"\"sensor\"",
",",
"{",
"\"sensor\"",
":",
"config",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_usage_ratio\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"PERCENTAGE",
"assert",
"state",
".",
"state",
"==",
"\"76.24\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_usage\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"304.95\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_data_limit\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"400\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_used_download\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"304.95\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_used_upload\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"6.48\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_used_total\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"311.43\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_grace_download\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"304.95\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_grace_upload\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"6.48\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_grace_total\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"311.43\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_total_download\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"304.95\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_total_upload\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"6.48\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_remaining\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"95.05\""
] | [
12,
0
] | [
105,
33
] | python | en | ['en', 'da', 'en'] | True |
test_unlimited_setup | (hass, aioclient_mock) | Test the default setup. | Test the default setup. | async def test_unlimited_setup(hass, aioclient_mock):
"""Test the default setup."""
config = {
"platform": "startca",
"api_key": "NOTAKEY",
"total_bandwidth": 0,
"monitored_variables": [
"usage",
"usage_gb",
"limit",
"used_download",
"used_upload",
"used_total",
"grace_download",
"grace_upload",
"grace_total",
"total_download",
"total_upload",
"used_remaining",
],
}
result = (
'<?xml version="1.0" encoding="ISO-8859-15"?>'
"<usage>"
"<version>1.1</version>"
"<total> <!-- total actual usage -->"
"<download>304946829777</download>"
"<upload>6480700153</upload>"
"</total>"
"<used> <!-- part of usage that counts against quota -->"
"<download>0</download>"
"<upload>0</upload>"
"</used>"
"<grace> <!-- part of usage that is free -->"
"<download>304946829777</download>"
"<upload>6480700153</upload>"
"</grace>"
"</usage>"
)
aioclient_mock.get(
"https://www.start.ca/support/usage/api?key=NOTAKEY", text=result
)
await async_setup_component(hass, "sensor", {"sensor": config})
await hass.async_block_till_done()
state = hass.states.get("sensor.start_ca_usage_ratio")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == PERCENTAGE
assert state.state == "0"
state = hass.states.get("sensor.start_ca_usage")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "0.0"
state = hass.states.get("sensor.start_ca_data_limit")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "inf"
state = hass.states.get("sensor.start_ca_used_download")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "0.0"
state = hass.states.get("sensor.start_ca_used_upload")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "0.0"
state = hass.states.get("sensor.start_ca_used_total")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "0.0"
state = hass.states.get("sensor.start_ca_grace_download")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "304.95"
state = hass.states.get("sensor.start_ca_grace_upload")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "6.48"
state = hass.states.get("sensor.start_ca_grace_total")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "311.43"
state = hass.states.get("sensor.start_ca_total_download")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "304.95"
state = hass.states.get("sensor.start_ca_total_upload")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "6.48"
state = hass.states.get("sensor.start_ca_remaining")
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == DATA_GIGABYTES
assert state.state == "inf" | [
"async",
"def",
"test_unlimited_setup",
"(",
"hass",
",",
"aioclient_mock",
")",
":",
"config",
"=",
"{",
"\"platform\"",
":",
"\"startca\"",
",",
"\"api_key\"",
":",
"\"NOTAKEY\"",
",",
"\"total_bandwidth\"",
":",
"0",
",",
"\"monitored_variables\"",
":",
"[",
"\"usage\"",
",",
"\"usage_gb\"",
",",
"\"limit\"",
",",
"\"used_download\"",
",",
"\"used_upload\"",
",",
"\"used_total\"",
",",
"\"grace_download\"",
",",
"\"grace_upload\"",
",",
"\"grace_total\"",
",",
"\"total_download\"",
",",
"\"total_upload\"",
",",
"\"used_remaining\"",
",",
"]",
",",
"}",
"result",
"=",
"(",
"'<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>'",
"\"<usage>\"",
"\"<version>1.1</version>\"",
"\"<total> <!-- total actual usage -->\"",
"\"<download>304946829777</download>\"",
"\"<upload>6480700153</upload>\"",
"\"</total>\"",
"\"<used> <!-- part of usage that counts against quota -->\"",
"\"<download>0</download>\"",
"\"<upload>0</upload>\"",
"\"</used>\"",
"\"<grace> <!-- part of usage that is free -->\"",
"\"<download>304946829777</download>\"",
"\"<upload>6480700153</upload>\"",
"\"</grace>\"",
"\"</usage>\"",
")",
"aioclient_mock",
".",
"get",
"(",
"\"https://www.start.ca/support/usage/api?key=NOTAKEY\"",
",",
"text",
"=",
"result",
")",
"await",
"async_setup_component",
"(",
"hass",
",",
"\"sensor\"",
",",
"{",
"\"sensor\"",
":",
"config",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_usage_ratio\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"PERCENTAGE",
"assert",
"state",
".",
"state",
"==",
"\"0\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_usage\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"0.0\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_data_limit\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"inf\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_used_download\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"0.0\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_used_upload\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"0.0\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_used_total\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"0.0\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_grace_download\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"304.95\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_grace_upload\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"6.48\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_grace_total\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"311.43\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_total_download\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"304.95\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_total_upload\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"6.48\"",
"state",
"=",
"hass",
".",
"states",
".",
"get",
"(",
"\"sensor.start_ca_remaining\"",
")",
"assert",
"state",
".",
"attributes",
".",
"get",
"(",
"ATTR_UNIT_OF_MEASUREMENT",
")",
"==",
"DATA_GIGABYTES",
"assert",
"state",
".",
"state",
"==",
"\"inf\""
] | [
108,
0
] | [
201,
31
] | python | en | ['en', 'da', 'en'] | True |
test_bad_return_code | (hass, aioclient_mock) | Test handling a return code that isn't HTTP OK. | Test handling a return code that isn't HTTP OK. | async def test_bad_return_code(hass, aioclient_mock):
"""Test handling a return code that isn't HTTP OK."""
aioclient_mock.get(
"https://www.start.ca/support/usage/api?key=NOTAKEY", status=HTTP_NOT_FOUND
)
scd = StartcaData(hass.loop, async_get_clientsession(hass), "NOTAKEY", 400)
result = await scd.async_update()
assert result is False | [
"async",
"def",
"test_bad_return_code",
"(",
"hass",
",",
"aioclient_mock",
")",
":",
"aioclient_mock",
".",
"get",
"(",
"\"https://www.start.ca/support/usage/api?key=NOTAKEY\"",
",",
"status",
"=",
"HTTP_NOT_FOUND",
")",
"scd",
"=",
"StartcaData",
"(",
"hass",
".",
"loop",
",",
"async_get_clientsession",
"(",
"hass",
")",
",",
"\"NOTAKEY\"",
",",
"400",
")",
"result",
"=",
"await",
"scd",
".",
"async_update",
"(",
")",
"assert",
"result",
"is",
"False"
] | [
204,
0
] | [
213,
26
] | python | en | ['en', 'lb', 'en'] | True |
test_bad_json_decode | (hass, aioclient_mock) | Test decoding invalid json result. | Test decoding invalid json result. | async def test_bad_json_decode(hass, aioclient_mock):
"""Test decoding invalid json result."""
aioclient_mock.get(
"https://www.start.ca/support/usage/api?key=NOTAKEY", text="this is not xml"
)
scd = StartcaData(hass.loop, async_get_clientsession(hass), "NOTAKEY", 400)
result = await scd.async_update()
assert result is False | [
"async",
"def",
"test_bad_json_decode",
"(",
"hass",
",",
"aioclient_mock",
")",
":",
"aioclient_mock",
".",
"get",
"(",
"\"https://www.start.ca/support/usage/api?key=NOTAKEY\"",
",",
"text",
"=",
"\"this is not xml\"",
")",
"scd",
"=",
"StartcaData",
"(",
"hass",
".",
"loop",
",",
"async_get_clientsession",
"(",
"hass",
")",
",",
"\"NOTAKEY\"",
",",
"400",
")",
"result",
"=",
"await",
"scd",
".",
"async_update",
"(",
")",
"assert",
"result",
"is",
"False"
] | [
216,
0
] | [
225,
26
] | python | da | ['fr', 'da', 'en'] | False |
_async_create_entities | (hass, config) | Create the Template switches. | Create the Template switches. | async def _async_create_entities(hass, config):
"""Create the Template switches."""
switches = []
for device, device_config in config[CONF_SWITCHES].items():
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
state_template = device_config.get(CONF_VALUE_TEMPLATE)
icon_template = device_config.get(CONF_ICON_TEMPLATE)
entity_picture_template = device_config.get(CONF_ENTITY_PICTURE_TEMPLATE)
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
on_action = device_config[ON_ACTION]
off_action = device_config[OFF_ACTION]
unique_id = device_config.get(CONF_UNIQUE_ID)
switches.append(
SwitchTemplate(
hass,
device,
friendly_name,
state_template,
icon_template,
entity_picture_template,
availability_template,
on_action,
off_action,
unique_id,
)
)
return switches | [
"async",
"def",
"_async_create_entities",
"(",
"hass",
",",
"config",
")",
":",
"switches",
"=",
"[",
"]",
"for",
"device",
",",
"device_config",
"in",
"config",
"[",
"CONF_SWITCHES",
"]",
".",
"items",
"(",
")",
":",
"friendly_name",
"=",
"device_config",
".",
"get",
"(",
"ATTR_FRIENDLY_NAME",
",",
"device",
")",
"state_template",
"=",
"device_config",
".",
"get",
"(",
"CONF_VALUE_TEMPLATE",
")",
"icon_template",
"=",
"device_config",
".",
"get",
"(",
"CONF_ICON_TEMPLATE",
")",
"entity_picture_template",
"=",
"device_config",
".",
"get",
"(",
"CONF_ENTITY_PICTURE_TEMPLATE",
")",
"availability_template",
"=",
"device_config",
".",
"get",
"(",
"CONF_AVAILABILITY_TEMPLATE",
")",
"on_action",
"=",
"device_config",
"[",
"ON_ACTION",
"]",
"off_action",
"=",
"device_config",
"[",
"OFF_ACTION",
"]",
"unique_id",
"=",
"device_config",
".",
"get",
"(",
"CONF_UNIQUE_ID",
")",
"switches",
".",
"append",
"(",
"SwitchTemplate",
"(",
"hass",
",",
"device",
",",
"friendly_name",
",",
"state_template",
",",
"icon_template",
",",
"entity_picture_template",
",",
"availability_template",
",",
"on_action",
",",
"off_action",
",",
"unique_id",
",",
")",
")",
"return",
"switches"
] | [
58,
0
] | [
87,
19
] | python | en | ['en', 'en', 'en'] | True |
async_setup_platform | (hass, config, async_add_entities, discovery_info=None) | Set up the template switches. | Set up the template switches. | async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the template switches."""
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
async_add_entities(await _async_create_entities(hass, config)) | [
"async",
"def",
"async_setup_platform",
"(",
"hass",
",",
"config",
",",
"async_add_entities",
",",
"discovery_info",
"=",
"None",
")",
":",
"await",
"async_setup_reload_service",
"(",
"hass",
",",
"DOMAIN",
",",
"PLATFORMS",
")",
"async_add_entities",
"(",
"await",
"_async_create_entities",
"(",
"hass",
",",
"config",
")",
")"
] | [
90,
0
] | [
94,
66
] | python | en | ['en', 'en', 'en'] | True |
SwitchTemplate.__init__ | (
self,
hass,
device_id,
friendly_name,
state_template,
icon_template,
entity_picture_template,
availability_template,
on_action,
off_action,
unique_id,
) | Initialize the Template switch. | Initialize the Template switch. | def __init__(
self,
hass,
device_id,
friendly_name,
state_template,
icon_template,
entity_picture_template,
availability_template,
on_action,
off_action,
unique_id,
):
"""Initialize the Template switch."""
super().__init__(
availability_template=availability_template,
icon_template=icon_template,
entity_picture_template=entity_picture_template,
)
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, device_id, hass=hass
)
self._name = friendly_name
self._template = state_template
domain = __name__.split(".")[-2]
self._on_script = Script(hass, on_action, friendly_name, domain)
self._off_script = Script(hass, off_action, friendly_name, domain)
self._state = False
self._unique_id = unique_id | [
"def",
"__init__",
"(",
"self",
",",
"hass",
",",
"device_id",
",",
"friendly_name",
",",
"state_template",
",",
"icon_template",
",",
"entity_picture_template",
",",
"availability_template",
",",
"on_action",
",",
"off_action",
",",
"unique_id",
",",
")",
":",
"super",
"(",
")",
".",
"__init__",
"(",
"availability_template",
"=",
"availability_template",
",",
"icon_template",
"=",
"icon_template",
",",
"entity_picture_template",
"=",
"entity_picture_template",
",",
")",
"self",
".",
"entity_id",
"=",
"async_generate_entity_id",
"(",
"ENTITY_ID_FORMAT",
",",
"device_id",
",",
"hass",
"=",
"hass",
")",
"self",
".",
"_name",
"=",
"friendly_name",
"self",
".",
"_template",
"=",
"state_template",
"domain",
"=",
"__name__",
".",
"split",
"(",
"\".\"",
")",
"[",
"-",
"2",
"]",
"self",
".",
"_on_script",
"=",
"Script",
"(",
"hass",
",",
"on_action",
",",
"friendly_name",
",",
"domain",
")",
"self",
".",
"_off_script",
"=",
"Script",
"(",
"hass",
",",
"off_action",
",",
"friendly_name",
",",
"domain",
")",
"self",
".",
"_state",
"=",
"False",
"self",
".",
"_unique_id",
"=",
"unique_id"
] | [
100,
4
] | [
128,
35
] | python | en | ['en', 'en', 'en'] | True |
SwitchTemplate.async_added_to_hass | (self) | Register callbacks. | Register callbacks. | async def async_added_to_hass(self):
"""Register callbacks."""
if self._template is None:
# restore state after startup
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state:
self._state = state.state == STATE_ON
# no need to listen for events
else:
self.add_template_attribute(
"_state", self._template, None, self._update_state
)
await super().async_added_to_hass() | [
"async",
"def",
"async_added_to_hass",
"(",
"self",
")",
":",
"if",
"self",
".",
"_template",
"is",
"None",
":",
"# restore state after startup",
"await",
"super",
"(",
")",
".",
"async_added_to_hass",
"(",
")",
"state",
"=",
"await",
"self",
".",
"async_get_last_state",
"(",
")",
"if",
"state",
":",
"self",
".",
"_state",
"=",
"state",
".",
"state",
"==",
"STATE_ON",
"# no need to listen for events",
"else",
":",
"self",
".",
"add_template_attribute",
"(",
"\"_state\"",
",",
"self",
".",
"_template",
",",
"None",
",",
"self",
".",
"_update_state",
")",
"await",
"super",
"(",
")",
".",
"async_added_to_hass",
"(",
")"
] | [
147,
4
] | [
164,
43
] | python | en | ['en', 'no', 'en'] | False |
SwitchTemplate.name | (self) | Return the name of the switch. | Return the name of the switch. | def name(self):
"""Return the name of the switch."""
return self._name | [
"def",
"name",
"(",
"self",
")",
":",
"return",
"self",
".",
"_name"
] | [
167,
4
] | [
169,
25
] | python | en | ['en', 'en', 'en'] | True |
SwitchTemplate.unique_id | (self) | Return the unique id of this switch. | Return the unique id of this switch. | def unique_id(self):
"""Return the unique id of this switch."""
return self._unique_id | [
"def",
"unique_id",
"(",
"self",
")",
":",
"return",
"self",
".",
"_unique_id"
] | [
172,
4
] | [
174,
30
] | python | en | ['en', 'en', 'en'] | True |
SwitchTemplate.is_on | (self) | Return true if device is on. | Return true if device is on. | def is_on(self):
"""Return true if device is on."""
return self._state | [
"def",
"is_on",
"(",
"self",
")",
":",
"return",
"self",
".",
"_state"
] | [
177,
4
] | [
179,
26
] | python | en | ['en', 'fy', 'en'] | True |
SwitchTemplate.should_poll | (self) | Return the polling state. | Return the polling state. | def should_poll(self):
"""Return the polling state."""
return False | [
"def",
"should_poll",
"(",
"self",
")",
":",
"return",
"False"
] | [
182,
4
] | [
184,
20
] | python | en | ['en', 'en', 'en'] | True |
SwitchTemplate.async_turn_on | (self, **kwargs) | Fire the on action. | Fire the on action. | async def async_turn_on(self, **kwargs):
"""Fire the on action."""
await self._on_script.async_run(context=self._context)
if self._template is None:
self._state = True
self.async_write_ha_state() | [
"async",
"def",
"async_turn_on",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"await",
"self",
".",
"_on_script",
".",
"async_run",
"(",
"context",
"=",
"self",
".",
"_context",
")",
"if",
"self",
".",
"_template",
"is",
"None",
":",
"self",
".",
"_state",
"=",
"True",
"self",
".",
"async_write_ha_state",
"(",
")"
] | [
186,
4
] | [
191,
39
] | python | en | ['en', 'en', 'en'] | True |
SwitchTemplate.async_turn_off | (self, **kwargs) | Fire the off action. | Fire the off action. | async def async_turn_off(self, **kwargs):
"""Fire the off action."""
await self._off_script.async_run(context=self._context)
if self._template is None:
self._state = False
self.async_write_ha_state() | [
"async",
"def",
"async_turn_off",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"await",
"self",
".",
"_off_script",
".",
"async_run",
"(",
"context",
"=",
"self",
".",
"_context",
")",
"if",
"self",
".",
"_template",
"is",
"None",
":",
"self",
".",
"_state",
"=",
"False",
"self",
".",
"async_write_ha_state",
"(",
")"
] | [
193,
4
] | [
198,
39
] | python | en | ['en', 'en', 'en'] | True |
SwitchTemplate.assumed_state | (self) | State is assumed, if no template given. | State is assumed, if no template given. | def assumed_state(self):
"""State is assumed, if no template given."""
return self._template is None | [
"def",
"assumed_state",
"(",
"self",
")",
":",
"return",
"self",
".",
"_template",
"is",
"None"
] | [
201,
4
] | [
203,
37
] | python | en | ['en', 'en', 'en'] | True |
async_setup_platform | (hass, config, async_add_entities, discovery_info=None) | Set up the Zodiac sensor platform. | Set up the Zodiac sensor platform. | async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Zodiac sensor platform."""
if discovery_info is None:
return
async_add_entities([ZodiacSensor()], True) | [
"async",
"def",
"async_setup_platform",
"(",
"hass",
",",
"config",
",",
"async_add_entities",
",",
"discovery_info",
"=",
"None",
")",
":",
"if",
"discovery_info",
"is",
"None",
":",
"return",
"async_add_entities",
"(",
"[",
"ZodiacSensor",
"(",
")",
"]",
",",
"True",
")"
] | [
156,
0
] | [
161,
46
] | python | en | ['en', 'pt', 'en'] | True |
ZodiacSensor.__init__ | (self) | Initialize the zodiac sensor. | Initialize the zodiac sensor. | def __init__(self):
"""Initialize the zodiac sensor."""
self._attrs = None
self._state = None | [
"def",
"__init__",
"(",
"self",
")",
":",
"self",
".",
"_attrs",
"=",
"None",
"self",
".",
"_state",
"=",
"None"
] | [
167,
4
] | [
170,
26
] | python | en | ['en', 'pl', 'en'] | True |
ZodiacSensor.unique_id | (self) | Return a unique ID. | Return a unique ID. | def unique_id(self):
"""Return a unique ID."""
return DOMAIN | [
"def",
"unique_id",
"(",
"self",
")",
":",
"return",
"DOMAIN"
] | [
173,
4
] | [
175,
21
] | python | ca | ['fr', 'ca', 'en'] | False |
ZodiacSensor.name | (self) | Return the name of the entity. | Return the name of the entity. | def name(self):
"""Return the name of the entity."""
return "Zodiac" | [
"def",
"name",
"(",
"self",
")",
":",
"return",
"\"Zodiac\""
] | [
178,
4
] | [
180,
23
] | python | en | ['en', 'en', 'en'] | True |
ZodiacSensor.device_class | (self) | Return the device class of the entity. | Return the device class of the entity. | def device_class(self):
"""Return the device class of the entity."""
return "zodiac__sign" | [
"def",
"device_class",
"(",
"self",
")",
":",
"return",
"\"zodiac__sign\""
] | [
183,
4
] | [
185,
29
] | python | en | ['en', 'en', 'en'] | True |
ZodiacSensor.state | (self) | Return the state of the device. | Return the state of the device. | def state(self):
"""Return the state of the device."""
return self._state | [
"def",
"state",
"(",
"self",
")",
":",
"return",
"self",
".",
"_state"
] | [
188,
4
] | [
190,
26
] | python | en | ['en', 'en', 'en'] | True |
ZodiacSensor.icon | (self) | Icon to use in the frontend, if any. | Icon to use in the frontend, if any. | def icon(self):
"""Icon to use in the frontend, if any."""
return ZODIAC_ICONS.get(self._state) | [
"def",
"icon",
"(",
"self",
")",
":",
"return",
"ZODIAC_ICONS",
".",
"get",
"(",
"self",
".",
"_state",
")"
] | [
193,
4
] | [
195,
44
] | python | en | ['en', 'en', 'en'] | True |
ZodiacSensor.device_state_attributes | (self) | Return the state attributes. | Return the state attributes. | def device_state_attributes(self):
"""Return the state attributes."""
return self._attrs | [
"def",
"device_state_attributes",
"(",
"self",
")",
":",
"return",
"self",
".",
"_attrs"
] | [
198,
4
] | [
200,
26
] | python | en | ['en', 'en', 'en'] | True |
ZodiacSensor.async_update | (self) | Get the time and updates the state. | Get the time and updates the state. | async def async_update(self):
"""Get the time and updates the state."""
today = as_local(utcnow()).date()
month = int(today.month)
day = int(today.day)
for sign in ZODIAC_BY_DATE:
if (month == sign[0][1] and day >= sign[0][0]) or (
month == sign[1][1] and day <= sign[1][0]
):
self._state = sign[2]
self._attrs = sign[3]
break | [
"async",
"def",
"async_update",
"(",
"self",
")",
":",
"today",
"=",
"as_local",
"(",
"utcnow",
"(",
")",
")",
".",
"date",
"(",
")",
"month",
"=",
"int",
"(",
"today",
".",
"month",
")",
"day",
"=",
"int",
"(",
"today",
".",
"day",
")",
"for",
"sign",
"in",
"ZODIAC_BY_DATE",
":",
"if",
"(",
"month",
"==",
"sign",
"[",
"0",
"]",
"[",
"1",
"]",
"and",
"day",
">=",
"sign",
"[",
"0",
"]",
"[",
"0",
"]",
")",
"or",
"(",
"month",
"==",
"sign",
"[",
"1",
"]",
"[",
"1",
"]",
"and",
"day",
"<=",
"sign",
"[",
"1",
"]",
"[",
"0",
"]",
")",
":",
"self",
".",
"_state",
"=",
"sign",
"[",
"2",
"]",
"self",
".",
"_attrs",
"=",
"sign",
"[",
"3",
"]",
"break"
] | [
202,
4
] | [
215,
21
] | python | en | ['en', 'en', 'en'] | True |
test_sonos_playback | (hass, mock_plex_server) | Test playing media on a Sonos speaker. | Test playing media on a Sonos speaker. | async def test_sonos_playback(hass, mock_plex_server):
"""Test playing media on a Sonos speaker."""
server_id = mock_plex_server.machineIdentifier
loaded_server = hass.data[DOMAIN][SERVERS][server_id]
# Test Sonos integration lookup failure
with patch.object(
hass.components.sonos, "get_coordinator_name", side_effect=HomeAssistantError
):
assert await hass.services.async_call(
DOMAIN,
SERVICE_PLAY_ON_SONOS,
{
ATTR_ENTITY_ID: "media_player.sonos_kitchen",
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_MEDIA_CONTENT_ID: '{"library_name": "Music", "artist_name": "Artist", "album_name": "Album"}',
},
True,
)
# Test success with plex_key
with patch.object(
hass.components.sonos,
"get_coordinator_name",
return_value="media_player.sonos_kitchen",
), patch("plexapi.playqueue.PlayQueue.create"):
assert await hass.services.async_call(
DOMAIN,
SERVICE_PLAY_ON_SONOS,
{
ATTR_ENTITY_ID: "media_player.sonos_kitchen",
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_MEDIA_CONTENT_ID: "2",
},
True,
)
# Test success with dict
with patch.object(
hass.components.sonos,
"get_coordinator_name",
return_value="media_player.sonos_kitchen",
), patch("plexapi.playqueue.PlayQueue.create"):
assert await hass.services.async_call(
DOMAIN,
SERVICE_PLAY_ON_SONOS,
{
ATTR_ENTITY_ID: "media_player.sonos_kitchen",
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_MEDIA_CONTENT_ID: '{"library_name": "Music", "artist_name": "Artist", "album_name": "Album"}',
},
True,
)
# Test media lookup failure
with patch.object(
hass.components.sonos,
"get_coordinator_name",
return_value="media_player.sonos_kitchen",
), patch.object(mock_plex_server, "fetchItem", side_effect=NotFound):
assert await hass.services.async_call(
DOMAIN,
SERVICE_PLAY_ON_SONOS,
{
ATTR_ENTITY_ID: "media_player.sonos_kitchen",
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_MEDIA_CONTENT_ID: "999",
},
True,
)
# Test invalid Plex server requested
with patch.object(
hass.components.sonos,
"get_coordinator_name",
return_value="media_player.sonos_kitchen",
):
assert await hass.services.async_call(
DOMAIN,
SERVICE_PLAY_ON_SONOS,
{
ATTR_ENTITY_ID: "media_player.sonos_kitchen",
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_MEDIA_CONTENT_ID: '{"plex_server": "unknown_plex_server", "library_name": "Music", "artist_name": "Artist", "album_name": "Album"}',
},
True,
)
# Test no speakers available
with patch.object(
loaded_server.account, "sonos_speaker", return_value=None
), patch.object(
hass.components.sonos,
"get_coordinator_name",
return_value="media_player.sonos_kitchen",
), patch(
"plexapi.playqueue.PlayQueue.create"
):
assert await hass.services.async_call(
DOMAIN,
SERVICE_PLAY_ON_SONOS,
{
ATTR_ENTITY_ID: "media_player.sonos_kitchen",
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_MEDIA_CONTENT_ID: '{"library_name": "Music", "artist_name": "Artist", "album_name": "Album"}',
},
True,
) | [
"async",
"def",
"test_sonos_playback",
"(",
"hass",
",",
"mock_plex_server",
")",
":",
"server_id",
"=",
"mock_plex_server",
".",
"machineIdentifier",
"loaded_server",
"=",
"hass",
".",
"data",
"[",
"DOMAIN",
"]",
"[",
"SERVERS",
"]",
"[",
"server_id",
"]",
"# Test Sonos integration lookup failure",
"with",
"patch",
".",
"object",
"(",
"hass",
".",
"components",
".",
"sonos",
",",
"\"get_coordinator_name\"",
",",
"side_effect",
"=",
"HomeAssistantError",
")",
":",
"assert",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"DOMAIN",
",",
"SERVICE_PLAY_ON_SONOS",
",",
"{",
"ATTR_ENTITY_ID",
":",
"\"media_player.sonos_kitchen\"",
",",
"ATTR_MEDIA_CONTENT_TYPE",
":",
"MEDIA_TYPE_MUSIC",
",",
"ATTR_MEDIA_CONTENT_ID",
":",
"'{\"library_name\": \"Music\", \"artist_name\": \"Artist\", \"album_name\": \"Album\"}'",
",",
"}",
",",
"True",
",",
")",
"# Test success with plex_key",
"with",
"patch",
".",
"object",
"(",
"hass",
".",
"components",
".",
"sonos",
",",
"\"get_coordinator_name\"",
",",
"return_value",
"=",
"\"media_player.sonos_kitchen\"",
",",
")",
",",
"patch",
"(",
"\"plexapi.playqueue.PlayQueue.create\"",
")",
":",
"assert",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"DOMAIN",
",",
"SERVICE_PLAY_ON_SONOS",
",",
"{",
"ATTR_ENTITY_ID",
":",
"\"media_player.sonos_kitchen\"",
",",
"ATTR_MEDIA_CONTENT_TYPE",
":",
"MEDIA_TYPE_MUSIC",
",",
"ATTR_MEDIA_CONTENT_ID",
":",
"\"2\"",
",",
"}",
",",
"True",
",",
")",
"# Test success with dict",
"with",
"patch",
".",
"object",
"(",
"hass",
".",
"components",
".",
"sonos",
",",
"\"get_coordinator_name\"",
",",
"return_value",
"=",
"\"media_player.sonos_kitchen\"",
",",
")",
",",
"patch",
"(",
"\"plexapi.playqueue.PlayQueue.create\"",
")",
":",
"assert",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"DOMAIN",
",",
"SERVICE_PLAY_ON_SONOS",
",",
"{",
"ATTR_ENTITY_ID",
":",
"\"media_player.sonos_kitchen\"",
",",
"ATTR_MEDIA_CONTENT_TYPE",
":",
"MEDIA_TYPE_MUSIC",
",",
"ATTR_MEDIA_CONTENT_ID",
":",
"'{\"library_name\": \"Music\", \"artist_name\": \"Artist\", \"album_name\": \"Album\"}'",
",",
"}",
",",
"True",
",",
")",
"# Test media lookup failure",
"with",
"patch",
".",
"object",
"(",
"hass",
".",
"components",
".",
"sonos",
",",
"\"get_coordinator_name\"",
",",
"return_value",
"=",
"\"media_player.sonos_kitchen\"",
",",
")",
",",
"patch",
".",
"object",
"(",
"mock_plex_server",
",",
"\"fetchItem\"",
",",
"side_effect",
"=",
"NotFound",
")",
":",
"assert",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"DOMAIN",
",",
"SERVICE_PLAY_ON_SONOS",
",",
"{",
"ATTR_ENTITY_ID",
":",
"\"media_player.sonos_kitchen\"",
",",
"ATTR_MEDIA_CONTENT_TYPE",
":",
"MEDIA_TYPE_MUSIC",
",",
"ATTR_MEDIA_CONTENT_ID",
":",
"\"999\"",
",",
"}",
",",
"True",
",",
")",
"# Test invalid Plex server requested",
"with",
"patch",
".",
"object",
"(",
"hass",
".",
"components",
".",
"sonos",
",",
"\"get_coordinator_name\"",
",",
"return_value",
"=",
"\"media_player.sonos_kitchen\"",
",",
")",
":",
"assert",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"DOMAIN",
",",
"SERVICE_PLAY_ON_SONOS",
",",
"{",
"ATTR_ENTITY_ID",
":",
"\"media_player.sonos_kitchen\"",
",",
"ATTR_MEDIA_CONTENT_TYPE",
":",
"MEDIA_TYPE_MUSIC",
",",
"ATTR_MEDIA_CONTENT_ID",
":",
"'{\"plex_server\": \"unknown_plex_server\", \"library_name\": \"Music\", \"artist_name\": \"Artist\", \"album_name\": \"Album\"}'",
",",
"}",
",",
"True",
",",
")",
"# Test no speakers available",
"with",
"patch",
".",
"object",
"(",
"loaded_server",
".",
"account",
",",
"\"sonos_speaker\"",
",",
"return_value",
"=",
"None",
")",
",",
"patch",
".",
"object",
"(",
"hass",
".",
"components",
".",
"sonos",
",",
"\"get_coordinator_name\"",
",",
"return_value",
"=",
"\"media_player.sonos_kitchen\"",
",",
")",
",",
"patch",
"(",
"\"plexapi.playqueue.PlayQueue.create\"",
")",
":",
"assert",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"DOMAIN",
",",
"SERVICE_PLAY_ON_SONOS",
",",
"{",
"ATTR_ENTITY_ID",
":",
"\"media_player.sonos_kitchen\"",
",",
"ATTR_MEDIA_CONTENT_TYPE",
":",
"MEDIA_TYPE_MUSIC",
",",
"ATTR_MEDIA_CONTENT_ID",
":",
"'{\"library_name\": \"Music\", \"artist_name\": \"Artist\", \"album_name\": \"Album\"}'",
",",
"}",
",",
"True",
",",
")"
] | [
23,
0
] | [
130,
9
] | python | en | ['en', 'en', 'en'] | True |
test_playback_multiple_servers | (hass, mock_websocket, setup_plex_server) | Test playing media when multiple servers available. | Test playing media when multiple servers available. | async def test_playback_multiple_servers(hass, mock_websocket, setup_plex_server):
"""Test playing media when multiple servers available."""
secondary_entry = MockConfigEntry(
domain=DOMAIN,
data=SECONDARY_DATA,
options=DEFAULT_OPTIONS,
unique_id=SECONDARY_DATA["server_id"],
)
await setup_plex_server()
await setup_plex_server(config_entry=secondary_entry)
with patch.object(
hass.components.sonos,
"get_coordinator_name",
return_value="media_player.sonos_kitchen",
), patch("plexapi.playqueue.PlayQueue.create"):
assert await hass.services.async_call(
DOMAIN,
SERVICE_PLAY_ON_SONOS,
{
ATTR_ENTITY_ID: "media_player.sonos_kitchen",
ATTR_MEDIA_CONTENT_TYPE: MEDIA_TYPE_MUSIC,
ATTR_MEDIA_CONTENT_ID: f'{{"plex_server": "{SECONDARY_DATA[CONF_SERVER]}", "library_name": "Music", "artist_name": "Artist", "album_name": "Album"}}',
},
True,
) | [
"async",
"def",
"test_playback_multiple_servers",
"(",
"hass",
",",
"mock_websocket",
",",
"setup_plex_server",
")",
":",
"secondary_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"DOMAIN",
",",
"data",
"=",
"SECONDARY_DATA",
",",
"options",
"=",
"DEFAULT_OPTIONS",
",",
"unique_id",
"=",
"SECONDARY_DATA",
"[",
"\"server_id\"",
"]",
",",
")",
"await",
"setup_plex_server",
"(",
")",
"await",
"setup_plex_server",
"(",
"config_entry",
"=",
"secondary_entry",
")",
"with",
"patch",
".",
"object",
"(",
"hass",
".",
"components",
".",
"sonos",
",",
"\"get_coordinator_name\"",
",",
"return_value",
"=",
"\"media_player.sonos_kitchen\"",
",",
")",
",",
"patch",
"(",
"\"plexapi.playqueue.PlayQueue.create\"",
")",
":",
"assert",
"await",
"hass",
".",
"services",
".",
"async_call",
"(",
"DOMAIN",
",",
"SERVICE_PLAY_ON_SONOS",
",",
"{",
"ATTR_ENTITY_ID",
":",
"\"media_player.sonos_kitchen\"",
",",
"ATTR_MEDIA_CONTENT_TYPE",
":",
"MEDIA_TYPE_MUSIC",
",",
"ATTR_MEDIA_CONTENT_ID",
":",
"f'{{\"plex_server\": \"{SECONDARY_DATA[CONF_SERVER]}\", \"library_name\": \"Music\", \"artist_name\": \"Artist\", \"album_name\": \"Album\"}}'",
",",
"}",
",",
"True",
",",
")"
] | [
133,
0
] | [
159,
9
] | python | en | ['en', 'en', 'en'] | True |
init_integration | (
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
skip_setup: bool = False,
) | Set up the BSBLan integration in Home Assistant. | Set up the BSBLan integration in Home Assistant. | async def init_integration(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
skip_setup: bool = False,
) -> MockConfigEntry:
"""Set up the BSBLan integration in Home Assistant."""
aioclient_mock.post(
"http://example.local:80/1234/JQ?Parameter=6224,6225,6226",
params={"Parameter": "6224,6225,6226"},
text=load_fixture("bsblan/info.json"),
headers={"Content-Type": CONTENT_TYPE_JSON},
)
entry = MockConfigEntry(
domain=DOMAIN,
unique_id="RVS21.831F/127",
data={
CONF_HOST: "example.local",
CONF_PASSKEY: "1234",
CONF_PORT: 80,
CONF_DEVICE_IDENT: "RVS21.831F/127",
},
)
entry.add_to_hass(hass)
if not skip_setup:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry | [
"async",
"def",
"init_integration",
"(",
"hass",
":",
"HomeAssistant",
",",
"aioclient_mock",
":",
"AiohttpClientMocker",
",",
"skip_setup",
":",
"bool",
"=",
"False",
",",
")",
"->",
"MockConfigEntry",
":",
"aioclient_mock",
".",
"post",
"(",
"\"http://example.local:80/1234/JQ?Parameter=6224,6225,6226\"",
",",
"params",
"=",
"{",
"\"Parameter\"",
":",
"\"6224,6225,6226\"",
"}",
",",
"text",
"=",
"load_fixture",
"(",
"\"bsblan/info.json\"",
")",
",",
"headers",
"=",
"{",
"\"Content-Type\"",
":",
"CONTENT_TYPE_JSON",
"}",
",",
")",
"entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"DOMAIN",
",",
"unique_id",
"=",
"\"RVS21.831F/127\"",
",",
"data",
"=",
"{",
"CONF_HOST",
":",
"\"example.local\"",
",",
"CONF_PASSKEY",
":",
"\"1234\"",
",",
"CONF_PORT",
":",
"80",
",",
"CONF_DEVICE_IDENT",
":",
"\"RVS21.831F/127\"",
",",
"}",
",",
")",
"entry",
".",
"add_to_hass",
"(",
"hass",
")",
"if",
"not",
"skip_setup",
":",
"await",
"hass",
".",
"config_entries",
".",
"async_setup",
"(",
"entry",
".",
"entry_id",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"return",
"entry"
] | [
14,
0
] | [
45,
16
] | python | en | ['en', 'en', 'en'] | True |
device_reg | (hass) | Return an empty, loaded, registry. | Return an empty, loaded, registry. | def device_reg(hass):
"""Return an empty, loaded, registry."""
return mock_device_registry(hass) | [
"def",
"device_reg",
"(",
"hass",
")",
":",
"return",
"mock_device_registry",
"(",
"hass",
")"
] | [
27,
0
] | [
29,
37
] | python | en | ['en', 'fy', 'en'] | True |
entity_reg | (hass) | Return an empty, loaded, registry. | Return an empty, loaded, registry. | def entity_reg(hass):
"""Return an empty, loaded, registry."""
return mock_registry(hass) | [
"def",
"entity_reg",
"(",
"hass",
")",
":",
"return",
"mock_registry",
"(",
"hass",
")"
] | [
33,
0
] | [
35,
30
] | python | en | ['en', 'fy', 'en'] | True |
calls | (hass) | Track calls to a mock service. | Track calls to a mock service. | def calls(hass):
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation") | [
"def",
"calls",
"(",
"hass",
")",
":",
"return",
"async_mock_service",
"(",
"hass",
",",
"\"test\"",
",",
"\"automation\"",
")"
] | [
39,
0
] | [
41,
57
] | python | en | ['en', 'en', 'en'] | True |
test_get_conditions | (hass, device_reg, entity_reg) | Test we get the expected conditions from a cover. | Test we get the expected conditions from a cover. | async def test_get_conditions(hass, device_reg, entity_reg):
"""Test we get the expected conditions from a cover."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[0]
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
expected_conditions = [
{
"condition": "device",
"domain": DOMAIN,
"type": "is_open",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_closed",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_opening",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_closing",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
]
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
assert_lists_same(conditions, expected_conditions) | [
"async",
"def",
"test_get_conditions",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"0",
"]",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"ent",
".",
"unique_id",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"expected_conditions",
"=",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_open\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_closed\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_opening\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_closing\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"]",
"conditions",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"condition\"",
",",
"device_entry",
".",
"id",
")",
"assert_lists_same",
"(",
"conditions",
",",
"expected_conditions",
")"
] | [
44,
0
] | [
92,
54
] | python | en | ['en', 'en', 'en'] | True |
test_get_conditions_set_pos | (hass, device_reg, entity_reg) | Test we get the expected conditions from a cover. | Test we get the expected conditions from a cover. | async def test_get_conditions_set_pos(hass, device_reg, entity_reg):
"""Test we get the expected conditions from a cover."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[1]
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
expected_conditions = [
{
"condition": "device",
"domain": DOMAIN,
"type": "is_open",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_closed",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_opening",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_closing",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_position",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
]
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
assert_lists_same(conditions, expected_conditions) | [
"async",
"def",
"test_get_conditions_set_pos",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"1",
"]",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"ent",
".",
"unique_id",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"expected_conditions",
"=",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_open\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_closed\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_opening\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_closing\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_position\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"]",
"conditions",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"condition\"",
",",
"device_entry",
".",
"id",
")",
"assert_lists_same",
"(",
"conditions",
",",
"expected_conditions",
")"
] | [
95,
0
] | [
150,
54
] | python | en | ['en', 'en', 'en'] | True |
test_get_conditions_set_tilt_pos | (hass, device_reg, entity_reg) | Test we get the expected conditions from a cover. | Test we get the expected conditions from a cover. | async def test_get_conditions_set_tilt_pos(hass, device_reg, entity_reg):
"""Test we get the expected conditions from a cover."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[2]
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
expected_conditions = [
{
"condition": "device",
"domain": DOMAIN,
"type": "is_open",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_closed",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_opening",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_closing",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
{
"condition": "device",
"domain": DOMAIN,
"type": "is_tilt_position",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_{ent.unique_id}",
},
]
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
assert_lists_same(conditions, expected_conditions) | [
"async",
"def",
"test_get_conditions_set_tilt_pos",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"2",
"]",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"ent",
".",
"unique_id",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"expected_conditions",
"=",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_open\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_closed\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_opening\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_closing\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"is_tilt_position\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_{ent.unique_id}\"",
",",
"}",
",",
"]",
"conditions",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"condition\"",
",",
"device_entry",
".",
"id",
")",
"assert_lists_same",
"(",
"conditions",
",",
"expected_conditions",
")"
] | [
153,
0
] | [
208,
54
] | python | en | ['en', 'en', 'en'] | True |
test_get_condition_capabilities | (hass, device_reg, entity_reg) | Test we get the expected capabilities from a cover condition. | Test we get the expected capabilities from a cover condition. | async def test_get_condition_capabilities(hass, device_reg, entity_reg):
"""Test we get the expected capabilities from a cover condition."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[0]
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
assert len(conditions) == 4
for condition in conditions:
capabilities = await async_get_device_automation_capabilities(
hass, "condition", condition
)
assert capabilities == {"extra_fields": []} | [
"async",
"def",
"test_get_condition_capabilities",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"0",
"]",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"ent",
".",
"unique_id",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"conditions",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"condition\"",
",",
"device_entry",
".",
"id",
")",
"assert",
"len",
"(",
"conditions",
")",
"==",
"4",
"for",
"condition",
"in",
"conditions",
":",
"capabilities",
"=",
"await",
"async_get_device_automation_capabilities",
"(",
"hass",
",",
"\"condition\"",
",",
"condition",
")",
"assert",
"capabilities",
"==",
"{",
"\"extra_fields\"",
":",
"[",
"]",
"}"
] | [
211,
0
] | [
235,
51
] | python | en | ['en', 'en', 'en'] | True |
test_get_condition_capabilities_set_pos | (hass, device_reg, entity_reg) | Test we get the expected capabilities from a cover condition. | Test we get the expected capabilities from a cover condition. | async def test_get_condition_capabilities_set_pos(hass, device_reg, entity_reg):
"""Test we get the expected capabilities from a cover condition."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[1]
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
expected_capabilities = {
"extra_fields": [
{
"name": "above",
"optional": True,
"type": "integer",
"default": 0,
"valueMax": 100,
"valueMin": 0,
},
{
"name": "below",
"optional": True,
"type": "integer",
"default": 100,
"valueMax": 100,
"valueMin": 0,
},
]
}
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
assert len(conditions) == 5
for condition in conditions:
capabilities = await async_get_device_automation_capabilities(
hass, "condition", condition
)
if condition["type"] == "is_position":
assert capabilities == expected_capabilities
else:
assert capabilities == {"extra_fields": []} | [
"async",
"def",
"test_get_condition_capabilities_set_pos",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"1",
"]",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"ent",
".",
"unique_id",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"expected_capabilities",
"=",
"{",
"\"extra_fields\"",
":",
"[",
"{",
"\"name\"",
":",
"\"above\"",
",",
"\"optional\"",
":",
"True",
",",
"\"type\"",
":",
"\"integer\"",
",",
"\"default\"",
":",
"0",
",",
"\"valueMax\"",
":",
"100",
",",
"\"valueMin\"",
":",
"0",
",",
"}",
",",
"{",
"\"name\"",
":",
"\"below\"",
",",
"\"optional\"",
":",
"True",
",",
"\"type\"",
":",
"\"integer\"",
",",
"\"default\"",
":",
"100",
",",
"\"valueMax\"",
":",
"100",
",",
"\"valueMin\"",
":",
"0",
",",
"}",
",",
"]",
"}",
"conditions",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"condition\"",
",",
"device_entry",
".",
"id",
")",
"assert",
"len",
"(",
"conditions",
")",
"==",
"5",
"for",
"condition",
"in",
"conditions",
":",
"capabilities",
"=",
"await",
"async_get_device_automation_capabilities",
"(",
"hass",
",",
"\"condition\"",
",",
"condition",
")",
"if",
"condition",
"[",
"\"type\"",
"]",
"==",
"\"is_position\"",
":",
"assert",
"capabilities",
"==",
"expected_capabilities",
"else",
":",
"assert",
"capabilities",
"==",
"{",
"\"extra_fields\"",
":",
"[",
"]",
"}"
] | [
238,
0
] | [
285,
55
] | python | en | ['en', 'en', 'en'] | True |
test_get_condition_capabilities_set_tilt_pos | (hass, device_reg, entity_reg) | Test we get the expected capabilities from a cover condition. | Test we get the expected capabilities from a cover condition. | async def test_get_condition_capabilities_set_tilt_pos(hass, device_reg, entity_reg):
"""Test we get the expected capabilities from a cover condition."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[2]
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(
DOMAIN, "test", ent.unique_id, device_id=device_entry.id
)
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
expected_capabilities = {
"extra_fields": [
{
"name": "above",
"optional": True,
"type": "integer",
"default": 0,
"valueMax": 100,
"valueMin": 0,
},
{
"name": "below",
"optional": True,
"type": "integer",
"default": 100,
"valueMax": 100,
"valueMin": 0,
},
]
}
conditions = await async_get_device_automations(hass, "condition", device_entry.id)
assert len(conditions) == 5
for condition in conditions:
capabilities = await async_get_device_automation_capabilities(
hass, "condition", condition
)
if condition["type"] == "is_tilt_position":
assert capabilities == expected_capabilities
else:
assert capabilities == {"extra_fields": []} | [
"async",
"def",
"test_get_condition_capabilities_set_tilt_pos",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"2",
"]",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"ent",
".",
"unique_id",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"expected_capabilities",
"=",
"{",
"\"extra_fields\"",
":",
"[",
"{",
"\"name\"",
":",
"\"above\"",
",",
"\"optional\"",
":",
"True",
",",
"\"type\"",
":",
"\"integer\"",
",",
"\"default\"",
":",
"0",
",",
"\"valueMax\"",
":",
"100",
",",
"\"valueMin\"",
":",
"0",
",",
"}",
",",
"{",
"\"name\"",
":",
"\"below\"",
",",
"\"optional\"",
":",
"True",
",",
"\"type\"",
":",
"\"integer\"",
",",
"\"default\"",
":",
"100",
",",
"\"valueMax\"",
":",
"100",
",",
"\"valueMin\"",
":",
"0",
",",
"}",
",",
"]",
"}",
"conditions",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"condition\"",
",",
"device_entry",
".",
"id",
")",
"assert",
"len",
"(",
"conditions",
")",
"==",
"5",
"for",
"condition",
"in",
"conditions",
":",
"capabilities",
"=",
"await",
"async_get_device_automation_capabilities",
"(",
"hass",
",",
"\"condition\"",
",",
"condition",
")",
"if",
"condition",
"[",
"\"type\"",
"]",
"==",
"\"is_tilt_position\"",
":",
"assert",
"capabilities",
"==",
"expected_capabilities",
"else",
":",
"assert",
"capabilities",
"==",
"{",
"\"extra_fields\"",
":",
"[",
"]",
"}"
] | [
288,
0
] | [
335,
55
] | python | en | ['en', 'en', 'en'] | True |
test_if_state | (hass, calls) | Test for turn_on and turn_off conditions. | Test for turn_on and turn_off conditions. | async def test_if_state(hass, calls):
"""Test for turn_on and turn_off conditions."""
hass.states.async_set("cover.entity", STATE_OPEN)
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event1"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": "cover.entity",
"type": "is_open",
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_open - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event2"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": "cover.entity",
"type": "is_closed",
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_closed - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event3"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": "cover.entity",
"type": "is_opening",
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_opening - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event4"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": "cover.entity",
"type": "is_closing",
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_closing - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
]
},
)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "is_open - event - test_event1"
hass.states.async_set("cover.entity", STATE_CLOSED)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "is_closed - event - test_event2"
hass.states.async_set("cover.entity", STATE_OPENING)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert len(calls) == 3
assert calls[2].data["some"] == "is_opening - event - test_event3"
hass.states.async_set("cover.entity", STATE_CLOSING)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event4")
await hass.async_block_till_done()
assert len(calls) == 4
assert calls[3].data["some"] == "is_closing - event - test_event4" | [
"async",
"def",
"test_if_state",
"(",
"hass",
",",
"calls",
")",
":",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.entity\"",
",",
"STATE_OPEN",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"automation",
".",
"DOMAIN",
",",
"{",
"automation",
".",
"DOMAIN",
":",
"[",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event1\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"\"cover.entity\"",
",",
"\"type\"",
":",
"\"is_open\"",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_open - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event2\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"\"cover.entity\"",
",",
"\"type\"",
":",
"\"is_closed\"",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_closed - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event3\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"\"cover.entity\"",
",",
"\"type\"",
":",
"\"is_opening\"",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_opening - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event4\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"\"cover.entity\"",
",",
"\"type\"",
":",
"\"is_closing\"",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_closing - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"]",
"}",
",",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"1",
"assert",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_open - event - test_event1\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.entity\"",
",",
"STATE_CLOSED",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"2",
"assert",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_closed - event - test_event2\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.entity\"",
",",
"STATE_OPENING",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event3\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"3",
"assert",
"calls",
"[",
"2",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_opening - event - test_event3\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"\"cover.entity\"",
",",
"STATE_CLOSING",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event4\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"4",
"assert",
"calls",
"[",
"3",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_closing - event - test_event4\""
] | [
338,
0
] | [
447,
70
] | python | en | ['en', 'en', 'en'] | True |
test_if_position | (hass, calls) | Test for position conditions. | Test for position conditions. | async def test_if_position(hass, calls):
"""Test for position conditions."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[1]
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done()
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event1"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent.entity_id,
"type": "is_position",
"above": 45,
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_pos_gt_45 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event2"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent.entity_id,
"type": "is_position",
"below": 90,
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_pos_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event3"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent.entity_id,
"type": "is_position",
"above": 45,
"below": 90,
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_pos_gt_45_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
]
},
)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert len(calls) == 3
assert calls[0].data["some"] == "is_pos_gt_45 - event - test_event1"
assert calls[1].data["some"] == "is_pos_lt_90 - event - test_event2"
assert calls[2].data["some"] == "is_pos_gt_45_lt_90 - event - test_event3"
hass.states.async_set(
ent.entity_id, STATE_CLOSED, attributes={"current_position": 45}
)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert len(calls) == 4
assert calls[3].data["some"] == "is_pos_lt_90 - event - test_event2"
hass.states.async_set(
ent.entity_id, STATE_CLOSED, attributes={"current_position": 90}
)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert len(calls) == 5
assert calls[4].data["some"] == "is_pos_gt_45 - event - test_event1" | [
"async",
"def",
"test_if_position",
"(",
"hass",
",",
"calls",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"1",
"]",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"automation",
".",
"DOMAIN",
",",
"{",
"automation",
".",
"DOMAIN",
":",
"[",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event1\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent",
".",
"entity_id",
",",
"\"type\"",
":",
"\"is_position\"",
",",
"\"above\"",
":",
"45",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_pos_gt_45 - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event2\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent",
".",
"entity_id",
",",
"\"type\"",
":",
"\"is_position\"",
",",
"\"below\"",
":",
"90",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_pos_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event3\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent",
".",
"entity_id",
",",
"\"type\"",
":",
"\"is_position\"",
",",
"\"above\"",
":",
"45",
",",
"\"below\"",
":",
"90",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_pos_gt_45_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"]",
"}",
",",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event3\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"3",
"assert",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_gt_45 - event - test_event1\"",
"assert",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_lt_90 - event - test_event2\"",
"assert",
"calls",
"[",
"2",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_gt_45_lt_90 - event - test_event3\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"ent",
".",
"entity_id",
",",
"STATE_CLOSED",
",",
"attributes",
"=",
"{",
"\"current_position\"",
":",
"45",
"}",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event3\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"4",
"assert",
"calls",
"[",
"3",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_lt_90 - event - test_event2\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"ent",
".",
"entity_id",
",",
"STATE_CLOSED",
",",
"attributes",
"=",
"{",
"\"current_position\"",
":",
"90",
"}",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event3\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"5",
"assert",
"calls",
"[",
"4",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_gt_45 - event - test_event1\""
] | [
450,
0
] | [
551,
72
] | python | en | ['en', 'en', 'en'] | True |
test_if_tilt_position | (hass, calls) | Test for tilt position conditions. | Test for tilt position conditions. | async def test_if_tilt_position(hass, calls):
"""Test for tilt position conditions."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
ent = platform.ENTITIES[2]
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done()
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {"platform": "event", "event_type": "test_event1"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent.entity_id,
"type": "is_tilt_position",
"above": 45,
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_pos_gt_45 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event2"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent.entity_id,
"type": "is_tilt_position",
"below": 90,
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_pos_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
{
"trigger": {"platform": "event", "event_type": "test_event3"},
"condition": [
{
"condition": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent.entity_id,
"type": "is_tilt_position",
"above": 45,
"below": 90,
}
],
"action": {
"service": "test.automation",
"data_template": {
"some": "is_pos_gt_45_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}"
},
},
},
]
},
)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert len(calls) == 3
assert calls[0].data["some"] == "is_pos_gt_45 - event - test_event1"
assert calls[1].data["some"] == "is_pos_lt_90 - event - test_event2"
assert calls[2].data["some"] == "is_pos_gt_45_lt_90 - event - test_event3"
hass.states.async_set(
ent.entity_id, STATE_CLOSED, attributes={"current_tilt_position": 45}
)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert len(calls) == 4
assert calls[3].data["some"] == "is_pos_lt_90 - event - test_event2"
hass.states.async_set(
ent.entity_id, STATE_CLOSED, attributes={"current_tilt_position": 90}
)
hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
await hass.async_block_till_done()
assert len(calls) == 5
assert calls[4].data["some"] == "is_pos_gt_45 - event - test_event1" | [
"async",
"def",
"test_if_tilt_position",
"(",
"hass",
",",
"calls",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"ent",
"=",
"platform",
".",
"ENTITIES",
"[",
"2",
"]",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"automation",
".",
"DOMAIN",
",",
"{",
"automation",
".",
"DOMAIN",
":",
"[",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event1\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent",
".",
"entity_id",
",",
"\"type\"",
":",
"\"is_tilt_position\"",
",",
"\"above\"",
":",
"45",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_pos_gt_45 - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event2\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent",
".",
"entity_id",
",",
"\"type\"",
":",
"\"is_tilt_position\"",
",",
"\"below\"",
":",
"90",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_pos_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"event\"",
",",
"\"event_type\"",
":",
"\"test_event3\"",
"}",
",",
"\"condition\"",
":",
"[",
"{",
"\"condition\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent",
".",
"entity_id",
",",
"\"type\"",
":",
"\"is_tilt_position\"",
",",
"\"above\"",
":",
"45",
",",
"\"below\"",
":",
"90",
",",
"}",
"]",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"is_pos_gt_45_lt_90 - {{ trigger.platform }} - {{ trigger.event.event_type }}\"",
"}",
",",
"}",
",",
"}",
",",
"]",
"}",
",",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event3\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"3",
"assert",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_gt_45 - event - test_event1\"",
"assert",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_lt_90 - event - test_event2\"",
"assert",
"calls",
"[",
"2",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_gt_45_lt_90 - event - test_event3\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"ent",
".",
"entity_id",
",",
"STATE_CLOSED",
",",
"attributes",
"=",
"{",
"\"current_tilt_position\"",
":",
"45",
"}",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event3\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"4",
"assert",
"calls",
"[",
"3",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_lt_90 - event - test_event2\"",
"hass",
".",
"states",
".",
"async_set",
"(",
"ent",
".",
"entity_id",
",",
"STATE_CLOSED",
",",
"attributes",
"=",
"{",
"\"current_tilt_position\"",
":",
"90",
"}",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event1\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event2\"",
")",
"hass",
".",
"bus",
".",
"async_fire",
"(",
"\"test_event3\"",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"5",
"assert",
"calls",
"[",
"4",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"is_pos_gt_45 - event - test_event1\""
] | [
554,
0
] | [
655,
72
] | python | en | ['da', 'en', 'en'] | True |
load_data | () | Load dataset, use boston dataset | Load dataset, use boston dataset | def load_data():
'''Load dataset, use boston dataset'''
boston = load_boston()
X_train, X_test, y_train, y_test = train_test_split(
boston.data, boston.target, random_state=99, test_size=0.25)
#normalize data
ss_X = StandardScaler()
ss_y = StandardScaler()
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)
y_train = ss_y.fit_transform(y_train[:, None])[:, 0]
y_test = ss_y.transform(y_test[:, None])[:, 0]
return X_train, X_test, y_train, y_test | [
"def",
"load_data",
"(",
")",
":",
"boston",
"=",
"load_boston",
"(",
")",
"X_train",
",",
"X_test",
",",
"y_train",
",",
"y_test",
"=",
"train_test_split",
"(",
"boston",
".",
"data",
",",
"boston",
".",
"target",
",",
"random_state",
"=",
"99",
",",
"test_size",
"=",
"0.25",
")",
"#normalize data",
"ss_X",
"=",
"StandardScaler",
"(",
")",
"ss_y",
"=",
"StandardScaler",
"(",
")",
"X_train",
"=",
"ss_X",
".",
"fit_transform",
"(",
"X_train",
")",
"X_test",
"=",
"ss_X",
".",
"transform",
"(",
"X_test",
")",
"y_train",
"=",
"ss_y",
".",
"fit_transform",
"(",
"y_train",
"[",
":",
",",
"None",
"]",
")",
"[",
":",
",",
"0",
"]",
"y_test",
"=",
"ss_y",
".",
"transform",
"(",
"y_test",
"[",
":",
",",
"None",
"]",
")",
"[",
":",
",",
"0",
"]",
"return",
"X_train",
",",
"X_test",
",",
"y_train",
",",
"y_test"
] | [
32,
0
] | [
46,
43
] | python | en | ['es', 'zu', 'en'] | False |
get_default_parameters | () | get default parameters | get default parameters | def get_default_parameters():
'''get default parameters'''
params = {'model_name': 'LinearRegression'}
return params | [
"def",
"get_default_parameters",
"(",
")",
":",
"params",
"=",
"{",
"'model_name'",
":",
"'LinearRegression'",
"}",
"return",
"params"
] | [
48,
0
] | [
51,
17
] | python | fr | ['fr', 'fr', 'en'] | True |
get_model | (PARAMS) | Get model according to parameters | Get model according to parameters | def get_model(PARAMS):
'''Get model according to parameters'''
model_dict = {
'LinearRegression': LinearRegression(),
'Ridge': Ridge(),
'Lars': Lars(),
'ARDRegression': ARDRegression()
}
if not model_dict.get(PARAMS['model_name']):
LOG.exception('Not supported model!')
exit(1)
model = model_dict[PARAMS['model_name']]
model.normalize = bool(PARAMS['normalize'])
return model | [
"def",
"get_model",
"(",
"PARAMS",
")",
":",
"model_dict",
"=",
"{",
"'LinearRegression'",
":",
"LinearRegression",
"(",
")",
",",
"'Ridge'",
":",
"Ridge",
"(",
")",
",",
"'Lars'",
":",
"Lars",
"(",
")",
",",
"'ARDRegression'",
":",
"ARDRegression",
"(",
")",
"}",
"if",
"not",
"model_dict",
".",
"get",
"(",
"PARAMS",
"[",
"'model_name'",
"]",
")",
":",
"LOG",
".",
"exception",
"(",
"'Not supported model!'",
")",
"exit",
"(",
"1",
")",
"model",
"=",
"model_dict",
"[",
"PARAMS",
"[",
"'model_name'",
"]",
"]",
"model",
".",
"normalize",
"=",
"bool",
"(",
"PARAMS",
"[",
"'normalize'",
"]",
")",
"return",
"model"
] | [
53,
0
] | [
69,
16
] | python | en | ['en', 'en', 'en'] | True |
run | (X_train, X_test, y_train, y_test, model) | Train model and predict result | Train model and predict result | def run(X_train, X_test, y_train, y_test, model):
'''Train model and predict result'''
model.fit(X_train, y_train)
predict_y = model.predict(X_test)
score = r2_score(y_test, predict_y)
LOG.debug('r2 score: %s', score)
nni.report_final_result(score) | [
"def",
"run",
"(",
"X_train",
",",
"X_test",
",",
"y_train",
",",
"y_test",
",",
"model",
")",
":",
"model",
".",
"fit",
"(",
"X_train",
",",
"y_train",
")",
"predict_y",
"=",
"model",
".",
"predict",
"(",
"X_test",
")",
"score",
"=",
"r2_score",
"(",
"y_test",
",",
"predict_y",
")",
"LOG",
".",
"debug",
"(",
"'r2 score: %s'",
",",
"score",
")",
"nni",
".",
"report_final_result",
"(",
"score",
")"
] | [
71,
0
] | [
77,
34
] | python | en | ['en', 'it', 'en'] | True |
setup_platform | (hass, config, add_entities, discovery_info=None) | Set up the platform for a Skybell device. | Set up the platform for a Skybell device. | def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the platform for a Skybell device."""
skybell = hass.data.get(SKYBELL_DOMAIN)
sensors = []
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
for device in skybell.get_devices():
sensors.append(SkybellBinarySensor(device, sensor_type))
add_entities(sensors, True) | [
"def",
"setup_platform",
"(",
"hass",
",",
"config",
",",
"add_entities",
",",
"discovery_info",
"=",
"None",
")",
":",
"skybell",
"=",
"hass",
".",
"data",
".",
"get",
"(",
"SKYBELL_DOMAIN",
")",
"sensors",
"=",
"[",
"]",
"for",
"sensor_type",
"in",
"config",
".",
"get",
"(",
"CONF_MONITORED_CONDITIONS",
")",
":",
"for",
"device",
"in",
"skybell",
".",
"get_devices",
"(",
")",
":",
"sensors",
".",
"append",
"(",
"SkybellBinarySensor",
"(",
"device",
",",
"sensor_type",
")",
")",
"add_entities",
"(",
"sensors",
",",
"True",
")"
] | [
36,
0
] | [
45,
31
] | python | en | ['en', 'en', 'en'] | True |
SkybellBinarySensor.__init__ | (self, device, sensor_type) | Initialize a binary sensor for a Skybell device. | Initialize a binary sensor for a Skybell device. | def __init__(self, device, sensor_type):
"""Initialize a binary sensor for a Skybell device."""
super().__init__(device)
self._sensor_type = sensor_type
self._name = "{} {}".format(
self._device.name, SENSOR_TYPES[self._sensor_type][0]
)
self._device_class = SENSOR_TYPES[self._sensor_type][1]
self._event = {}
self._state = None | [
"def",
"__init__",
"(",
"self",
",",
"device",
",",
"sensor_type",
")",
":",
"super",
"(",
")",
".",
"__init__",
"(",
"device",
")",
"self",
".",
"_sensor_type",
"=",
"sensor_type",
"self",
".",
"_name",
"=",
"\"{} {}\"",
".",
"format",
"(",
"self",
".",
"_device",
".",
"name",
",",
"SENSOR_TYPES",
"[",
"self",
".",
"_sensor_type",
"]",
"[",
"0",
"]",
")",
"self",
".",
"_device_class",
"=",
"SENSOR_TYPES",
"[",
"self",
".",
"_sensor_type",
"]",
"[",
"1",
"]",
"self",
".",
"_event",
"=",
"{",
"}",
"self",
".",
"_state",
"=",
"None"
] | [
51,
4
] | [
60,
26
] | python | en | ['en', 'en', 'en'] | True |
SkybellBinarySensor.name | (self) | Return the name of the sensor. | Return the name of the sensor. | def name(self):
"""Return the name of the sensor."""
return self._name | [
"def",
"name",
"(",
"self",
")",
":",
"return",
"self",
".",
"_name"
] | [
63,
4
] | [
65,
25
] | python | en | ['en', 'mi', 'en'] | True |
SkybellBinarySensor.is_on | (self) | Return True if the binary sensor is on. | Return True if the binary sensor is on. | def is_on(self):
"""Return True if the binary sensor is on."""
return self._state | [
"def",
"is_on",
"(",
"self",
")",
":",
"return",
"self",
".",
"_state"
] | [
68,
4
] | [
70,
26
] | python | en | ['en', 'fy', 'en'] | True |
SkybellBinarySensor.device_class | (self) | Return the class of the binary sensor. | Return the class of the binary sensor. | def device_class(self):
"""Return the class of the binary sensor."""
return self._device_class | [
"def",
"device_class",
"(",
"self",
")",
":",
"return",
"self",
".",
"_device_class"
] | [
73,
4
] | [
75,
33
] | python | en | ['en', 'tg', 'en'] | True |
SkybellBinarySensor.device_state_attributes | (self) | Return the state attributes. | Return the state attributes. | def device_state_attributes(self):
"""Return the state attributes."""
attrs = super().device_state_attributes
attrs["event_date"] = self._event.get("createdAt")
return attrs | [
"def",
"device_state_attributes",
"(",
"self",
")",
":",
"attrs",
"=",
"super",
"(",
")",
".",
"device_state_attributes",
"attrs",
"[",
"\"event_date\"",
"]",
"=",
"self",
".",
"_event",
".",
"get",
"(",
"\"createdAt\"",
")",
"return",
"attrs"
] | [
78,
4
] | [
84,
20
] | python | en | ['en', 'en', 'en'] | True |
SkybellBinarySensor.update | (self) | Get the latest data and updates the state. | Get the latest data and updates the state. | def update(self):
"""Get the latest data and updates the state."""
super().update()
event = self._device.latest(SENSOR_TYPES[self._sensor_type][2])
self._state = bool(event and event.get("id") != self._event.get("id"))
self._event = event or {} | [
"def",
"update",
"(",
"self",
")",
":",
"super",
"(",
")",
".",
"update",
"(",
")",
"event",
"=",
"self",
".",
"_device",
".",
"latest",
"(",
"SENSOR_TYPES",
"[",
"self",
".",
"_sensor_type",
"]",
"[",
"2",
"]",
")",
"self",
".",
"_state",
"=",
"bool",
"(",
"event",
"and",
"event",
".",
"get",
"(",
"\"id\"",
")",
"!=",
"self",
".",
"_event",
".",
"get",
"(",
"\"id\"",
")",
")",
"self",
".",
"_event",
"=",
"event",
"or",
"{",
"}"
] | [
86,
4
] | [
94,
33
] | python | en | ['en', 'en', 'en'] | True |
async_setup_platform | (
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable[[Sequence[Entity], bool], None],
discovery_info: Optional[DiscoveryInfoType] = None,
) | Initialize Light Switch platform. | Initialize Light Switch platform. | async def async_setup_platform(
hass: HomeAssistantType,
config: ConfigType,
async_add_entities: Callable[[Sequence[Entity], bool], None],
discovery_info: Optional[DiscoveryInfoType] = None,
) -> None:
"""Initialize Light Switch platform."""
registry = await hass.helpers.entity_registry.async_get_registry()
wrapped_switch = registry.async_get(config[CONF_ENTITY_ID])
unique_id = wrapped_switch.unique_id if wrapped_switch else None
async_add_entities(
[
LightSwitch(
cast(str, config.get(CONF_NAME)),
config[CONF_ENTITY_ID],
unique_id,
)
],
True,
) | [
"async",
"def",
"async_setup_platform",
"(",
"hass",
":",
"HomeAssistantType",
",",
"config",
":",
"ConfigType",
",",
"async_add_entities",
":",
"Callable",
"[",
"[",
"Sequence",
"[",
"Entity",
"]",
",",
"bool",
"]",
",",
"None",
"]",
",",
"discovery_info",
":",
"Optional",
"[",
"DiscoveryInfoType",
"]",
"=",
"None",
",",
")",
"->",
"None",
":",
"registry",
"=",
"await",
"hass",
".",
"helpers",
".",
"entity_registry",
".",
"async_get_registry",
"(",
")",
"wrapped_switch",
"=",
"registry",
".",
"async_get",
"(",
"config",
"[",
"CONF_ENTITY_ID",
"]",
")",
"unique_id",
"=",
"wrapped_switch",
".",
"unique_id",
"if",
"wrapped_switch",
"else",
"None",
"async_add_entities",
"(",
"[",
"LightSwitch",
"(",
"cast",
"(",
"str",
",",
"config",
".",
"get",
"(",
"CONF_NAME",
")",
")",
",",
"config",
"[",
"CONF_ENTITY_ID",
"]",
",",
"unique_id",
",",
")",
"]",
",",
"True",
",",
")"
] | [
36,
0
] | [
57,
5
] | python | en | ['en', 'pl', 'en'] | True |
LightSwitch.__init__ | (self, name: str, switch_entity_id: str, unique_id: str) | Initialize Light Switch. | Initialize Light Switch. | def __init__(self, name: str, switch_entity_id: str, unique_id: str) -> None:
"""Initialize Light Switch."""
self._name = name
self._switch_entity_id = switch_entity_id
self._unique_id = unique_id
self._is_on = False
self._available = False
self._async_unsub_state_changed: Optional[CALLBACK_TYPE] = None | [
"def",
"__init__",
"(",
"self",
",",
"name",
":",
"str",
",",
"switch_entity_id",
":",
"str",
",",
"unique_id",
":",
"str",
")",
"->",
"None",
":",
"self",
".",
"_name",
"=",
"name",
"self",
".",
"_switch_entity_id",
"=",
"switch_entity_id",
"self",
".",
"_unique_id",
"=",
"unique_id",
"self",
".",
"_is_on",
"=",
"False",
"self",
".",
"_available",
"=",
"False",
"self",
".",
"_async_unsub_state_changed",
":",
"Optional",
"[",
"CALLBACK_TYPE",
"]",
"=",
"None"
] | [
63,
4
] | [
70,
71
] | python | en | ['en', 'pl', 'en'] | True |
LightSwitch.name | (self) | Return the name of the entity. | Return the name of the entity. | def name(self) -> str:
"""Return the name of the entity."""
return self._name | [
"def",
"name",
"(",
"self",
")",
"->",
"str",
":",
"return",
"self",
".",
"_name"
] | [
73,
4
] | [
75,
25
] | python | en | ['en', 'en', 'en'] | True |
LightSwitch.is_on | (self) | Return true if light switch is on. | Return true if light switch is on. | def is_on(self) -> bool:
"""Return true if light switch is on."""
return self._is_on | [
"def",
"is_on",
"(",
"self",
")",
"->",
"bool",
":",
"return",
"self",
".",
"_is_on"
] | [
78,
4
] | [
80,
26
] | python | en | ['en', 'fy', 'en'] | True |
LightSwitch.available | (self) | Return true if light switch is on. | Return true if light switch is on. | def available(self) -> bool:
"""Return true if light switch is on."""
return self._available | [
"def",
"available",
"(",
"self",
")",
"->",
"bool",
":",
"return",
"self",
".",
"_available"
] | [
83,
4
] | [
85,
30
] | python | en | ['en', 'fy', 'en'] | True |
LightSwitch.should_poll | (self) | No polling needed for a light switch. | No polling needed for a light switch. | def should_poll(self) -> bool:
"""No polling needed for a light switch."""
return False | [
"def",
"should_poll",
"(",
"self",
")",
"->",
"bool",
":",
"return",
"False"
] | [
88,
4
] | [
90,
20
] | python | en | ['en', 'en', 'en'] | True |
LightSwitch.unique_id | (self) | Return the unique id of the light switch. | Return the unique id of the light switch. | def unique_id(self):
"""Return the unique id of the light switch."""
return self._unique_id | [
"def",
"unique_id",
"(",
"self",
")",
":",
"return",
"self",
".",
"_unique_id"
] | [
93,
4
] | [
95,
30
] | python | en | ['en', 'en', 'en'] | True |
LightSwitch.async_turn_on | (self, **kwargs) | Forward the turn_on command to the switch in this light switch. | Forward the turn_on command to the switch in this light switch. | async def async_turn_on(self, **kwargs):
"""Forward the turn_on command to the switch in this light switch."""
data = {ATTR_ENTITY_ID: self._switch_entity_id}
await self.hass.services.async_call(
switch.DOMAIN,
switch.SERVICE_TURN_ON,
data,
blocking=True,
context=self._context,
) | [
"async",
"def",
"async_turn_on",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"data",
"=",
"{",
"ATTR_ENTITY_ID",
":",
"self",
".",
"_switch_entity_id",
"}",
"await",
"self",
".",
"hass",
".",
"services",
".",
"async_call",
"(",
"switch",
".",
"DOMAIN",
",",
"switch",
".",
"SERVICE_TURN_ON",
",",
"data",
",",
"blocking",
"=",
"True",
",",
"context",
"=",
"self",
".",
"_context",
",",
")"
] | [
97,
4
] | [
106,
9
] | python | en | ['en', 'en', 'en'] | True |
LightSwitch.async_turn_off | (self, **kwargs) | Forward the turn_off command to the switch in this light switch. | Forward the turn_off command to the switch in this light switch. | async def async_turn_off(self, **kwargs):
"""Forward the turn_off command to the switch in this light switch."""
data = {ATTR_ENTITY_ID: self._switch_entity_id}
await self.hass.services.async_call(
switch.DOMAIN,
switch.SERVICE_TURN_OFF,
data,
blocking=True,
context=self._context,
) | [
"async",
"def",
"async_turn_off",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"data",
"=",
"{",
"ATTR_ENTITY_ID",
":",
"self",
".",
"_switch_entity_id",
"}",
"await",
"self",
".",
"hass",
".",
"services",
".",
"async_call",
"(",
"switch",
".",
"DOMAIN",
",",
"switch",
".",
"SERVICE_TURN_OFF",
",",
"data",
",",
"blocking",
"=",
"True",
",",
"context",
"=",
"self",
".",
"_context",
",",
")"
] | [
108,
4
] | [
117,
9
] | python | en | ['en', 'en', 'en'] | True |
LightSwitch.async_update | (self) | Query the switch in this light switch and determine the state. | Query the switch in this light switch and determine the state. | async def async_update(self):
"""Query the switch in this light switch and determine the state."""
switch_state = self.hass.states.get(self._switch_entity_id)
if switch_state is None:
self._available = False
return
self._is_on = switch_state.state == STATE_ON
self._available = switch_state.state != STATE_UNAVAILABLE | [
"async",
"def",
"async_update",
"(",
"self",
")",
":",
"switch_state",
"=",
"self",
".",
"hass",
".",
"states",
".",
"get",
"(",
"self",
".",
"_switch_entity_id",
")",
"if",
"switch_state",
"is",
"None",
":",
"self",
".",
"_available",
"=",
"False",
"return",
"self",
".",
"_is_on",
"=",
"switch_state",
".",
"state",
"==",
"STATE_ON",
"self",
".",
"_available",
"=",
"switch_state",
".",
"state",
"!=",
"STATE_UNAVAILABLE"
] | [
119,
4
] | [
128,
65
] | python | en | ['en', 'en', 'en'] | True |
LightSwitch.async_added_to_hass | (self) | Register callbacks. | Register callbacks. | async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@callback
def async_state_changed_listener(*_: Any) -> None:
"""Handle child updates."""
self.async_schedule_update_ha_state(True)
assert self.hass is not None
self._async_unsub_state_changed = async_track_state_change_event(
self.hass, [self._switch_entity_id], async_state_changed_listener
) | [
"async",
"def",
"async_added_to_hass",
"(",
"self",
")",
"->",
"None",
":",
"@",
"callback",
"def",
"async_state_changed_listener",
"(",
"*",
"_",
":",
"Any",
")",
"->",
"None",
":",
"\"\"\"Handle child updates.\"\"\"",
"self",
".",
"async_schedule_update_ha_state",
"(",
"True",
")",
"assert",
"self",
".",
"hass",
"is",
"not",
"None",
"self",
".",
"_async_unsub_state_changed",
"=",
"async_track_state_change_event",
"(",
"self",
".",
"hass",
",",
"[",
"self",
".",
"_switch_entity_id",
"]",
",",
"async_state_changed_listener",
")"
] | [
130,
4
] | [
141,
9
] | python | en | ['en', 'no', 'en'] | False |
LightSwitch.async_will_remove_from_hass | (self) | Handle removal from Home Assistant. | Handle removal from Home Assistant. | async def async_will_remove_from_hass(self):
"""Handle removal from Home Assistant."""
if self._async_unsub_state_changed is not None:
self._async_unsub_state_changed()
self._async_unsub_state_changed = None
self._available = False | [
"async",
"def",
"async_will_remove_from_hass",
"(",
"self",
")",
":",
"if",
"self",
".",
"_async_unsub_state_changed",
"is",
"not",
"None",
":",
"self",
".",
"_async_unsub_state_changed",
"(",
")",
"self",
".",
"_async_unsub_state_changed",
"=",
"None",
"self",
".",
"_available",
"=",
"False"
] | [
143,
4
] | [
148,
35
] | python | en | ['en', 'en', 'en'] | True |
device_reg | (hass) | Return an empty, loaded, registry. | Return an empty, loaded, registry. | def device_reg(hass):
"""Return an empty, loaded, registry."""
return mock_device_registry(hass) | [
"def",
"device_reg",
"(",
"hass",
")",
":",
"return",
"mock_device_registry",
"(",
"hass",
")"
] | [
24,
0
] | [
26,
37
] | python | en | ['en', 'fy', 'en'] | True |
entity_reg | (hass) | Return an empty, loaded, registry. | Return an empty, loaded, registry. | def entity_reg(hass):
"""Return an empty, loaded, registry."""
return mock_registry(hass) | [
"def",
"entity_reg",
"(",
"hass",
")",
":",
"return",
"mock_registry",
"(",
"hass",
")"
] | [
30,
0
] | [
32,
30
] | python | en | ['en', 'fy', 'en'] | True |
calls | (hass) | Track calls to a mock service. | Track calls to a mock service. | def calls(hass):
"""Track calls to a mock service."""
return async_mock_service(hass, "test", "automation") | [
"def",
"calls",
"(",
"hass",
")",
":",
"return",
"async_mock_service",
"(",
"hass",
",",
"\"test\"",
",",
"\"automation\"",
")"
] | [
36,
0
] | [
38,
57
] | python | en | ['en', 'en', 'en'] | True |
test_get_triggers | (hass, device_reg, entity_reg) | Test we get the expected triggers from a switch. | Test we get the expected triggers from a switch. | async def test_get_triggers(hass, device_reg, entity_reg):
"""Test we get the expected triggers from a switch."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_triggers = [
{
"platform": "device",
"domain": DOMAIN,
"type": "turned_off",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
{
"platform": "device",
"domain": DOMAIN,
"type": "turned_on",
"device_id": device_entry.id,
"entity_id": f"{DOMAIN}.test_5678",
},
]
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
assert triggers == expected_triggers | [
"async",
"def",
"test_get_triggers",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"\"5678\"",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"expected_triggers",
"=",
"[",
"{",
"\"platform\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"turned_off\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_5678\"",
",",
"}",
",",
"{",
"\"platform\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"type\"",
":",
"\"turned_on\"",
",",
"\"device_id\"",
":",
"device_entry",
".",
"id",
",",
"\"entity_id\"",
":",
"f\"{DOMAIN}.test_5678\"",
",",
"}",
",",
"]",
"triggers",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"trigger\"",
",",
"device_entry",
".",
"id",
")",
"assert",
"triggers",
"==",
"expected_triggers"
] | [
41,
0
] | [
67,
40
] | python | en | ['en', 'en', 'en'] | True |
test_get_trigger_capabilities | (hass, device_reg, entity_reg) | Test we get the expected capabilities from a switch trigger. | Test we get the expected capabilities from a switch trigger. | async def test_get_trigger_capabilities(hass, device_reg, entity_reg):
"""Test we get the expected capabilities from a switch trigger."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id)
expected_capabilities = {
"extra_fields": [
{"name": "for", "optional": True, "type": "positive_time_period_dict"}
]
}
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
for trigger in triggers:
capabilities = await async_get_device_automation_capabilities(
hass, "trigger", trigger
)
assert capabilities == expected_capabilities | [
"async",
"def",
"test_get_trigger_capabilities",
"(",
"hass",
",",
"device_reg",
",",
"entity_reg",
")",
":",
"config_entry",
"=",
"MockConfigEntry",
"(",
"domain",
"=",
"\"test\"",
",",
"data",
"=",
"{",
"}",
")",
"config_entry",
".",
"add_to_hass",
"(",
"hass",
")",
"device_entry",
"=",
"device_reg",
".",
"async_get_or_create",
"(",
"config_entry_id",
"=",
"config_entry",
".",
"entry_id",
",",
"connections",
"=",
"{",
"(",
"device_registry",
".",
"CONNECTION_NETWORK_MAC",
",",
"\"12:34:56:AB:CD:EF\"",
")",
"}",
",",
")",
"entity_reg",
".",
"async_get_or_create",
"(",
"DOMAIN",
",",
"\"test\"",
",",
"\"5678\"",
",",
"device_id",
"=",
"device_entry",
".",
"id",
")",
"expected_capabilities",
"=",
"{",
"\"extra_fields\"",
":",
"[",
"{",
"\"name\"",
":",
"\"for\"",
",",
"\"optional\"",
":",
"True",
",",
"\"type\"",
":",
"\"positive_time_period_dict\"",
"}",
"]",
"}",
"triggers",
"=",
"await",
"async_get_device_automations",
"(",
"hass",
",",
"\"trigger\"",
",",
"device_entry",
".",
"id",
")",
"for",
"trigger",
"in",
"triggers",
":",
"capabilities",
"=",
"await",
"async_get_device_automation_capabilities",
"(",
"hass",
",",
"\"trigger\"",
",",
"trigger",
")",
"assert",
"capabilities",
"==",
"expected_capabilities"
] | [
70,
0
] | [
89,
52
] | python | en | ['en', 'en', 'en'] | True |
test_if_fires_on_state_change | (hass, calls) | Test for turn_on and turn_off triggers firing. | Test for turn_on and turn_off triggers firing. | async def test_if_fires_on_state_change(hass, calls):
"""Test for turn_on and turn_off triggers firing."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done()
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent1.entity_id,
"type": "turned_on",
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_on {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
},
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent1.entity_id,
"type": "turned_off",
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_off {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
},
]
},
)
await hass.async_block_till_done()
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.states.async_set(ent1.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data["some"] == "turn_off device - {} - on - off - None".format(
ent1.entity_id
)
hass.states.async_set(ent1.entity_id, STATE_ON)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data["some"] == "turn_on device - {} - off - on - None".format(
ent1.entity_id
) | [
"async",
"def",
"test_if_fires_on_state_change",
"(",
"hass",
",",
"calls",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"ent1",
",",
"ent2",
",",
"ent3",
"=",
"platform",
".",
"ENTITIES",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"automation",
".",
"DOMAIN",
",",
"{",
"automation",
".",
"DOMAIN",
":",
"[",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent1",
".",
"entity_id",
",",
"\"type\"",
":",
"\"turned_on\"",
",",
"}",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"turn_on {{ trigger.%s }}\"",
"%",
"\"}} - {{ trigger.\"",
".",
"join",
"(",
"(",
"\"platform\"",
",",
"\"entity_id\"",
",",
"\"from_state.state\"",
",",
"\"to_state.state\"",
",",
"\"for\"",
",",
")",
")",
"}",
",",
"}",
",",
"}",
",",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent1",
".",
"entity_id",
",",
"\"type\"",
":",
"\"turned_off\"",
",",
"}",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"turn_off {{ trigger.%s }}\"",
"%",
"\"}} - {{ trigger.\"",
".",
"join",
"(",
"(",
"\"platform\"",
",",
"\"entity_id\"",
",",
"\"from_state.state\"",
",",
"\"to_state.state\"",
",",
"\"for\"",
",",
")",
")",
"}",
",",
"}",
",",
"}",
",",
"]",
"}",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"hass",
".",
"states",
".",
"get",
"(",
"ent1",
".",
"entity_id",
")",
".",
"state",
"==",
"STATE_ON",
"assert",
"len",
"(",
"calls",
")",
"==",
"0",
"hass",
".",
"states",
".",
"async_set",
"(",
"ent1",
".",
"entity_id",
",",
"STATE_OFF",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"1",
"assert",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"turn_off device - {} - on - off - None\"",
".",
"format",
"(",
"ent1",
".",
"entity_id",
")",
"hass",
".",
"states",
".",
"async_set",
"(",
"ent1",
".",
"entity_id",
",",
"STATE_ON",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"2",
"assert",
"calls",
"[",
"1",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"turn_on device - {} - off - on - None\"",
".",
"format",
"(",
"ent1",
".",
"entity_id",
")"
] | [
92,
0
] | [
174,
5
] | python | en | ['en', 'en', 'en'] | True |
test_if_fires_on_state_change_with_for | (hass, calls) | Test for triggers firing with delay. | Test for triggers firing with delay. | async def test_if_fires_on_state_change_with_for(hass, calls):
"""Test for triggers firing with delay."""
platform = getattr(hass.components, f"test.{DOMAIN}")
platform.init()
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
await hass.async_block_till_done()
ent1, ent2, ent3 = platform.ENTITIES
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "device",
"domain": DOMAIN,
"device_id": "",
"entity_id": ent1.entity_id,
"type": "turned_off",
"for": {"seconds": 5},
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_off {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
}
]
},
)
await hass.async_block_till_done()
assert hass.states.get(ent1.entity_id).state == STATE_ON
assert len(calls) == 0
hass.states.async_set(ent1.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 0
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
await hass.async_block_till_done()
assert len(calls) == 1
await hass.async_block_till_done()
assert calls[0].data["some"] == "turn_off device - {} - on - off - 0:00:05".format(
ent1.entity_id
) | [
"async",
"def",
"test_if_fires_on_state_change_with_for",
"(",
"hass",
",",
"calls",
")",
":",
"platform",
"=",
"getattr",
"(",
"hass",
".",
"components",
",",
"f\"test.{DOMAIN}\"",
")",
"platform",
".",
"init",
"(",
")",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"DOMAIN",
",",
"{",
"DOMAIN",
":",
"{",
"CONF_PLATFORM",
":",
"\"test\"",
"}",
"}",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"ent1",
",",
"ent2",
",",
"ent3",
"=",
"platform",
".",
"ENTITIES",
"assert",
"await",
"async_setup_component",
"(",
"hass",
",",
"automation",
".",
"DOMAIN",
",",
"{",
"automation",
".",
"DOMAIN",
":",
"[",
"{",
"\"trigger\"",
":",
"{",
"\"platform\"",
":",
"\"device\"",
",",
"\"domain\"",
":",
"DOMAIN",
",",
"\"device_id\"",
":",
"\"\"",
",",
"\"entity_id\"",
":",
"ent1",
".",
"entity_id",
",",
"\"type\"",
":",
"\"turned_off\"",
",",
"\"for\"",
":",
"{",
"\"seconds\"",
":",
"5",
"}",
",",
"}",
",",
"\"action\"",
":",
"{",
"\"service\"",
":",
"\"test.automation\"",
",",
"\"data_template\"",
":",
"{",
"\"some\"",
":",
"\"turn_off {{ trigger.%s }}\"",
"%",
"\"}} - {{ trigger.\"",
".",
"join",
"(",
"(",
"\"platform\"",
",",
"\"entity_id\"",
",",
"\"from_state.state\"",
",",
"\"to_state.state\"",
",",
"\"for\"",
",",
")",
")",
"}",
",",
"}",
",",
"}",
"]",
"}",
",",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"hass",
".",
"states",
".",
"get",
"(",
"ent1",
".",
"entity_id",
")",
".",
"state",
"==",
"STATE_ON",
"assert",
"len",
"(",
"calls",
")",
"==",
"0",
"hass",
".",
"states",
".",
"async_set",
"(",
"ent1",
".",
"entity_id",
",",
"STATE_OFF",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"0",
"async_fire_time_changed",
"(",
"hass",
",",
"dt_util",
".",
"utcnow",
"(",
")",
"+",
"timedelta",
"(",
"seconds",
"=",
"10",
")",
")",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"len",
"(",
"calls",
")",
"==",
"1",
"await",
"hass",
".",
"async_block_till_done",
"(",
")",
"assert",
"calls",
"[",
"0",
"]",
".",
"data",
"[",
"\"some\"",
"]",
"==",
"\"turn_off device - {} - on - off - 0:00:05\"",
".",
"format",
"(",
"ent1",
".",
"entity_id",
")"
] | [
177,
0
] | [
233,
5
] | python | en | ['en', 'en', 'en'] | True |
async_setup_platform | (
hass, hass_config, async_add_entities, discovery_info=None
) | Setups the LCN cover platform. | Setups the LCN cover platform. | async def async_setup_platform(
hass, hass_config, async_add_entities, discovery_info=None
):
"""Setups the LCN cover platform."""
if discovery_info is None:
return
devices = []
for config in discovery_info:
address, connection_id = config[CONF_ADDRESS]
addr = pypck.lcn_addr.LcnAddr(*address)
connections = hass.data[DATA_LCN][CONF_CONNECTIONS]
connection = get_connection(connections, connection_id)
address_connection = connection.get_address_conn(addr)
if config[CONF_MOTOR] == "OUTPUTS":
devices.append(LcnOutputsCover(config, address_connection))
else: # RELAYS
devices.append(LcnRelayCover(config, address_connection))
async_add_entities(devices) | [
"async",
"def",
"async_setup_platform",
"(",
"hass",
",",
"hass_config",
",",
"async_add_entities",
",",
"discovery_info",
"=",
"None",
")",
":",
"if",
"discovery_info",
"is",
"None",
":",
"return",
"devices",
"=",
"[",
"]",
"for",
"config",
"in",
"discovery_info",
":",
"address",
",",
"connection_id",
"=",
"config",
"[",
"CONF_ADDRESS",
"]",
"addr",
"=",
"pypck",
".",
"lcn_addr",
".",
"LcnAddr",
"(",
"*",
"address",
")",
"connections",
"=",
"hass",
".",
"data",
"[",
"DATA_LCN",
"]",
"[",
"CONF_CONNECTIONS",
"]",
"connection",
"=",
"get_connection",
"(",
"connections",
",",
"connection_id",
")",
"address_connection",
"=",
"connection",
".",
"get_address_conn",
"(",
"addr",
")",
"if",
"config",
"[",
"CONF_MOTOR",
"]",
"==",
"\"OUTPUTS\"",
":",
"devices",
".",
"append",
"(",
"LcnOutputsCover",
"(",
"config",
",",
"address_connection",
")",
")",
"else",
":",
"# RELAYS",
"devices",
".",
"append",
"(",
"LcnRelayCover",
"(",
"config",
",",
"address_connection",
")",
")",
"async_add_entities",
"(",
"devices",
")"
] | [
11,
0
] | [
31,
31
] | python | en | ['en', 'da', 'en'] | True |
LcnOutputsCover.__init__ | (self, config, address_connection) | Initialize the LCN cover. | Initialize the LCN cover. | def __init__(self, config, address_connection):
"""Initialize the LCN cover."""
super().__init__(config, address_connection)
self.output_ids = [
pypck.lcn_defs.OutputPort["OUTPUTUP"].value,
pypck.lcn_defs.OutputPort["OUTPUTDOWN"].value,
]
if CONF_REVERSE_TIME in config:
self.reverse_time = pypck.lcn_defs.MotorReverseTime[
config[CONF_REVERSE_TIME]
]
else:
self.reverse_time = None
self._is_closed = False
self._is_closing = False
self._is_opening = False | [
"def",
"__init__",
"(",
"self",
",",
"config",
",",
"address_connection",
")",
":",
"super",
"(",
")",
".",
"__init__",
"(",
"config",
",",
"address_connection",
")",
"self",
".",
"output_ids",
"=",
"[",
"pypck",
".",
"lcn_defs",
".",
"OutputPort",
"[",
"\"OUTPUTUP\"",
"]",
".",
"value",
",",
"pypck",
".",
"lcn_defs",
".",
"OutputPort",
"[",
"\"OUTPUTDOWN\"",
"]",
".",
"value",
",",
"]",
"if",
"CONF_REVERSE_TIME",
"in",
"config",
":",
"self",
".",
"reverse_time",
"=",
"pypck",
".",
"lcn_defs",
".",
"MotorReverseTime",
"[",
"config",
"[",
"CONF_REVERSE_TIME",
"]",
"]",
"else",
":",
"self",
".",
"reverse_time",
"=",
"None",
"self",
".",
"_is_closed",
"=",
"False",
"self",
".",
"_is_closing",
"=",
"False",
"self",
".",
"_is_opening",
"=",
"False"
] | [
37,
4
] | [
54,
32
] | python | en | ['en', 'en', 'en'] | True |
LcnOutputsCover.async_added_to_hass | (self) | Run when entity about to be added to hass. | Run when entity about to be added to hass. | async def async_added_to_hass(self):
"""Run when entity about to be added to hass."""
await super().async_added_to_hass()
await self.address_connection.activate_status_request_handler(
pypck.lcn_defs.OutputPort["OUTPUTUP"]
)
await self.address_connection.activate_status_request_handler(
pypck.lcn_defs.OutputPort["OUTPUTDOWN"]
) | [
"async",
"def",
"async_added_to_hass",
"(",
"self",
")",
":",
"await",
"super",
"(",
")",
".",
"async_added_to_hass",
"(",
")",
"await",
"self",
".",
"address_connection",
".",
"activate_status_request_handler",
"(",
"pypck",
".",
"lcn_defs",
".",
"OutputPort",
"[",
"\"OUTPUTUP\"",
"]",
")",
"await",
"self",
".",
"address_connection",
".",
"activate_status_request_handler",
"(",
"pypck",
".",
"lcn_defs",
".",
"OutputPort",
"[",
"\"OUTPUTDOWN\"",
"]",
")"
] | [
56,
4
] | [
64,
9
] | python | en | ['en', 'en', 'en'] | True |
LcnOutputsCover.is_closed | (self) | Return if the cover is closed. | Return if the cover is closed. | def is_closed(self):
"""Return if the cover is closed."""
return self._is_closed | [
"def",
"is_closed",
"(",
"self",
")",
":",
"return",
"self",
".",
"_is_closed"
] | [
67,
4
] | [
69,
30
] | python | en | ['en', 'en', 'en'] | True |
LcnOutputsCover.is_opening | (self) | Return if the cover is opening or not. | Return if the cover is opening or not. | def is_opening(self):
"""Return if the cover is opening or not."""
return self._is_opening | [
"def",
"is_opening",
"(",
"self",
")",
":",
"return",
"self",
".",
"_is_opening"
] | [
72,
4
] | [
74,
31
] | python | en | ['en', 'en', 'en'] | True |
LcnOutputsCover.is_closing | (self) | Return if the cover is closing or not. | Return if the cover is closing or not. | def is_closing(self):
"""Return if the cover is closing or not."""
return self._is_closing | [
"def",
"is_closing",
"(",
"self",
")",
":",
"return",
"self",
".",
"_is_closing"
] | [
77,
4
] | [
79,
31
] | python | en | ['en', 'en', 'en'] | True |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.