Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
req = mock.call_args[0][0]
assert req.full_url == (
f"http://localhost:8123/api/services/{device.domain}/{signal}"
)
assert (
req.data.decode()
== f'{{"entity_id": "{device_conf["entity_id"]}"}}'
)
assert req.headers == {
"Authorization": "Bearer abc123",
"Content-type": "application/json",
}
assert int(req.host.rsplit(":")[-1]) == plugin_vars["ha_port"]
assert req.get_method() == "POST"
signal = HomeAssistantPlugin.service_map[device.domain.lower()]["off"]
assert device.off() is True
req = mock.call_args[0][0]
assert req.full_url == (
f"http://localhost:8123/api/services/{device.domain}/{signal}"
)
assert (
req.data.decode()
== f'{{"entity_id": "{device_conf["entity_id"]}"}}'
)
assert req.headers == {
"Authorization": "Bearer abc123",
"Content-type": "application/json",
}
assert int(req.host.rsplit(":")[-1]) == plugin_vars["ha_port"]
assert req.get_method() == "POST"
"""Ensure a reasonable HomeAssistant REST endpoint is called."""
with open(config_path_str) as f:
config = json.load(f)
response = MagicMock(spec=HTTPResponse)
response.status = 200
response.read.return_value = b'{"state": "off"}'
mock.return_value.__enter__.return_value = response
plugin_vars = {
k: v
for k, v in config["PLUGINS"]["HomeAssistantPlugin"].items()
if k not in {"DEVICES", "path"}
}
for device_conf in config["PLUGINS"]["HomeAssistantPlugin"]["DEVICES"]:
device = HomeAssistantPlugin(**{**plugin_vars, **device_conf})
signal = HomeAssistantPlugin.service_map[device.domain.lower()]["on"]
assert device.on() is True
req = mock.call_args[0][0]
assert req.full_url == (
f"http://localhost:8123/api/services/{device.domain}/{signal}"
)
assert (
req.data.decode()
== f'{{"entity_id": "{device_conf["entity_id"]}"}}'
)
assert req.headers == {
"Authorization": "Bearer abc123",
"Content-type": "application/json",
}
assert int(req.host.rsplit(":")[-1]) == plugin_vars["ha_port"]