ran linters

This commit is contained in:
ari committed 2022-05-16 10:42:47 -04:00
1 parent ef5e66a8f1
commit b17e4c75b3
27 files changed
+290 -266

No files matched your search

+169 -191
View File
@@ -1,8 +1,14 @@
from ruamel.yaml import YAML
from unittest import TestCase
from fastapi.testclient import TestClient
from ruamel.yaml import YAML
from src.api.app import make_app
from src.automations.types import AutomationData, AutomationMetdata, ExtenededAutomationData
from src.automations.types import (
AutomationData,
AutomationMetdata,
ExtenededAutomationData,
)
from tests.utils import HA_CONFIG2_EXAMPLE, get_example_automation_loader
@@ -17,12 +23,8 @@ class BaseTestCase(TestCase):
class automation_list_tests(BaseTestCase):
test_params = {
"total": 28,
"subtest2_found_items": 3,
"subtest1_found_items": 10
}
test_params = {"total": 28, "subtest2_found_items": 3, "subtest1_found_items": 10}
def test_get_list_of_everything(self):
data = self.client.post(
@@ -41,8 +43,8 @@ class automation_list_tests(BaseTestCase):
"limit": 50,
},
)
self.assertEqual(d["totalItems"], self.test_params['total'])
self.assertEqual(len(d["data"]), self.test_params['total'])
self.assertEqual(d["totalItems"], self.test_params["total"])
self.assertEqual(len(d["data"]), self.test_params["total"])
def test_subset1(self):
data = self.client.post(
@@ -61,7 +63,7 @@ class automation_list_tests(BaseTestCase):
"limit": 10,
},
)
self.assertEqual(d["totalItems"], self.test_params['total'])
self.assertEqual(d["totalItems"], self.test_params["total"])
self.assertEqual(len(d["data"]), self.test_params["subtest1_found_items"])
def test_subset2(self):
@@ -81,15 +83,17 @@ class automation_list_tests(BaseTestCase):
"limit": 10,
},
)
self.assertEqual(d["totalItems"], self.test_params['total'])
self.assertEqual(d["totalItems"], self.test_params["total"])
self.assertEqual(len(d["data"]), self.test_params["subtest2_found_items"])
class automation2_lists_tests(automation_list_tests):
test_params = {
"total": 1,
"subtest2_found_items": 0,
"subtest1_found_items": 1,
}
def setUp(self) -> None:
(
self.config_folder,
@@ -99,7 +103,6 @@ class automation2_lists_tests(automation_list_tests):
self.client = TestClient(make_app(self.automation_loader))
class automation_update_tests(BaseTestCase):
def test_insert_new(self):
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 0})
@@ -131,12 +134,14 @@ class automation_update_tests(BaseTestCase):
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 20})
new_auto = resp.json()["data"][0]
self.assertNotEqual(original_auto, new_auto)
self.assertEqual(ExtenededAutomationData(metadata=AutomationMetdata(id="testme")).dict(), new_auto)
self.assertEqual(
ExtenededAutomationData(metadata=AutomationMetdata(id="testme")).dict(), new_auto
)
def test_consistent_standard_1(self):
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 15})
original_auto = resp.json()["data"][0]
original_auto['metadata']['alias'] = "I CHANGED!"
original_auto["metadata"]["alias"] = "I CHANGED!"
resp = self.client.post(
"/automations",
json={
@@ -145,46 +150,49 @@ class automation_update_tests(BaseTestCase):
},
)
self.assertEqual(resp.status_code, 200)
with (self.config_folder / "automations.yaml").open('r') as fp:
with (self.config_folder / "automations.yaml").open("r") as fp:
disk_data = dict(YAML().load(fp)[15])
trigger = list(map(dict, disk_data.pop('trigger')))
condition = disk_data.pop('condition')
action = list(map(dict, disk_data.pop('action')))
self.assertDictEqual(disk_data, {
"id": "1629422438707",
"alias": "I CHANGED!",
"description": "",
"mode": "single"
})
self.assertListEqual(trigger, [
{"platform": "time", "at": "input_datetime.vita_night_pump_time"}
])
self.assertListEqual(condition, [{
"conditions": [
{"condition": "time", "after": "input_datetime.vita_night_pump_time", "before": "00:00:00"},
{"condition": "time", "after": "00:00:00", "before": "06:00:00"}
],
"condition": "and"
}, ])
self.assertListEqual(action, [
{
"service": "light.turn_on",
"target": {
"entity_id": "light.bulb_staircase",
"area_id": "living_room"
},
"data": {
"brightness_pct": 40,
"transition": 15
trigger = list(map(dict, disk_data.pop("trigger")))
condition = disk_data.pop("condition")
action = list(map(dict, disk_data.pop("action")))
self.assertDictEqual(
disk_data,
{"id": "1629422438707", "alias": "I CHANGED!", "description": "", "mode": "single"},
)
self.assertListEqual(
trigger, [{"platform": "time", "at": "input_datetime.vita_night_pump_time"}]
)
self.assertListEqual(
condition,
[
{
"conditions": [
{
"condition": "time",
"after": "input_datetime.vita_night_pump_time",
"before": "00:00:00",
},
{"condition": "time", "after": "00:00:00", "before": "06:00:00"},
],
"condition": "and",
},
],
)
self.assertListEqual(
action,
[
{
"service": "light.turn_on",
"target": {"entity_id": "light.bulb_staircase", "area_id": "living_room"},
"data": {"brightness_pct": 40, "transition": 15},
}
}
])
],
)
def test_consistent_standard_2(self):
resp = self.client.post("/automations/list", json={"limit": 1, "offset": 27})
original_auto = resp.json()["data"][0]
original_auto['metadata']['alias'] = "I CHANGED!"
original_auto["metadata"]["alias"] = "I CHANGED!"
resp = self.client.post(
"/automations",
json={
@@ -193,155 +201,125 @@ class automation_update_tests(BaseTestCase):
},
)
self.assertEqual(resp.status_code, 200)
with (self.config_folder / "automations.yaml").open('r') as fp:
with (self.config_folder / "automations.yaml").open("r") as fp:
disk_data = dict(YAML().load(fp)[27])
trigger = list(map(dict, disk_data.pop('trigger')))
condition = disk_data.pop('condition')
action = list(map(dict, disk_data.pop('action')))
self.assertDictEqual(disk_data, {
"id": "1649947692702",
"alias": "I CHANGED!",
"description": "",
"mode": "single"
})
self.assertListEqual(trigger, [
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "turn_on",
"id": "turn_on"
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "turn_off",
"id": "turn_off"
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "dim_up",
"id": "dim_up"
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "dim_down",
"id": "dim_down"
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_double_press",
"subtype": "turn_on",
"id": "up_dbclk"
}
])
trigger = list(map(dict, disk_data.pop("trigger")))
condition = disk_data.pop("condition")
action = list(map(dict, disk_data.pop("action")))
self.assertDictEqual(
disk_data,
{"id": "1649947692702", "alias": "I CHANGED!", "description": "", "mode": "single"},
)
self.assertListEqual(
trigger,
[
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "turn_on",
"id": "turn_on",
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "turn_off",
"id": "turn_off",
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "dim_up",
"id": "dim_up",
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_short_press",
"subtype": "dim_down",
"id": "dim_down",
},
{
"device_id": "das2qdasdasdasadaasd",
"domain": "zha",
"platform": "device",
"type": "remote_button_double_press",
"subtype": "turn_on",
"id": "up_dbclk",
},
],
)
self.assertListEqual(condition, [])
self.assertListEqual(action, [
{
"choose": [
self.assertListEqual(
action,
[
{
"conditions": [
"choose": [
{
"condition": "trigger",
"id": "turn_off"
}
"conditions": [{"condition": "trigger", "id": "turn_off"}],
"sequence": [
{
"type": "turn_off",
"device_id": "das2qdasdasdasadaasd",
"entity_id": "light.bulb_ari_lamp",
"domain": "light",
}
],
},
{
"conditions": [{"condition": "trigger", "id": "turn_on"}],
"sequence": [
{
"service": "light.turn_on",
"data": {"color_temp": 153, "brightness_pct": 100},
"target": {"device_id": "das2qdasdasdasadaasd"},
}
],
},
{
"conditions": [{"condition": "trigger", "id": "dim_up"}],
"sequence": [
{
"device_id": "das2qdasdasdasadaasd",
"domain": "light",
"entity_id": "light.bulb_ari_lamp",
"type": "brightness_increase",
}
],
},
{
"conditions": [{"condition": "trigger", "id": "dim_down"}],
"sequence": [
{
"device_id": "das2qdasdasdasadaasd",
"domain": "light",
"entity_id": "light.bulb_ari_lamp",
"type": "brightness_decrease",
}
],
},
{
"conditions": [{"condition": "trigger", "id": "up_dbclk"}],
"sequence": [
{
"service": "light.turn_on",
"data": {"color_temp": 500, "brightness_pct": 100},
"target": {"device_id": "das2qdasdasdasadaasd"},
}
],
},
],
"sequence": [
{
"type": "turn_off",
"device_id": "das2qdasdasdasadaasd",
"entity_id": "light.bulb_ari_lamp",
"domain": "light"
}
]
},
{
"conditions": [
{
"condition": "trigger",
"id": "turn_on"
}
],
"sequence": [
{
"service": "light.turn_on",
"data": {
"color_temp": 153,
"brightness_pct": 100
},
"target": {
"device_id": "das2qdasdasdasadaasd"
}
}
]
},
{
"conditions": [
{
"condition": "trigger",
"id": "dim_up"
}
],
"sequence": [
{
"device_id": "das2qdasdasdasadaasd",
"domain": "light",
"entity_id": "light.bulb_ari_lamp",
"type": "brightness_increase"
}
]
},
{
"conditions": [
{
"condition": "trigger",
"id": "dim_down"
}
],
"sequence": [
{
"device_id": "das2qdasdasdasadaasd",
"domain": "light",
"entity_id": "light.bulb_ari_lamp",
"type": "brightness_decrease"
}
]
},
{
"conditions": [
{
"condition": "trigger",
"id": "up_dbclk"
}
],
"sequence": [
{
"service": "light.turn_on",
"data": {
"color_temp": 500,
"brightness_pct": 100
},
"target": {
"device_id": "das2qdasdasdasadaasd"
}
}
]
"default": [],
}
],
"default": []
}
])
)
class automation_delete_tests(BaseTestCase):
def test_delete_some(self):
+11 -14
View File
@@ -1,13 +1,8 @@
from unittest import TestCase
from src.automations.types import (
AutomationMetdata,
ExtenededAutomationData,
)
from src.automations.loader import load_automation
from src.yaml_serializer.types import (
SecretValue,
NOT_IMPLEMENTED_SV_MSG,
)
from src.automations.types import AutomationMetdata, ExtenededAutomationData
from src.yaml_serializer.types import NOT_IMPLEMENTED_SV_MSG, SecretValue
class loader_tests(TestCase):
@@ -31,7 +26,7 @@ class loader_tests(TestCase):
],
"mode": "single",
},
{}
{},
)
)
self.assertEqual(
@@ -44,10 +39,12 @@ class loader_tests(TestCase):
mode="single",
),
trigger=[{"platform": "homeassistant", "event": "shutdown"}],
condition=[{
"condition": "template",
"value_template": "states(time.time) >= '10:00:00'",
}],
condition=[
{
"condition": "template",
"value_template": "states(time.time) >= '10:00:00'",
}
],
sequence=[
{
"device_id": NOT_IMPLEMENTED_SV_MSG,
@@ -57,6 +54,6 @@ class loader_tests(TestCase):
"message": "Hassio Is turning off...",
}
],
tags={}
tags={},
),
)
@@ -1,4 +1,5 @@
from unittest import TestCase
from src.automations.errors import FailedDeletion, InvalidAutomationFile
from src.automations.types import AutomationMetdata, ExtenededAutomationData
from tests.utils import (
+5 -6
View File
@@ -1,18 +1,17 @@
from pathlib import Path
from tempfile import mktemp
from unittest import TestCase
from src.automations.tags import TagManager
class tag_tests(TestCase):
class tag_tests(TestCase):
def test_load_and_save(self):
tmp_file = Path(mktemp()).with_suffix('.yml')
tmp_file = Path(mktemp()).with_suffix(".yml")
tags = TagManager()
tags[0] = {'Room': "Kitchen", "Type": 'Lights'}
tags[0] = {"Room": "Kitchen", "Type": "Lights"}
tags[1] = {}
tags.save(tmp_file)
loaded_tags = TagManager.load(tmp_file)
assert loaded_tags.keys() == tags.keys()
assert loaded_tags.keys() == tags.keys()
@@ -1,4 +1,5 @@
from unittest import TestCase
from src.automations.types import AutomationData, AutomationMetdata
from src.hass_config.errors import MissingFile
from src.hass_config.loader import HassConfig
+5 -3
View File
@@ -1,11 +1,13 @@
import json
from unittest import TestCase
from ruamel.yaml.scalarstring import ScalarString
from ruamel.yaml.scalarint import ScalarInt
from ruamel.yaml.scalarbool import ScalarBoolean
from ruamel.yaml.scalarfloat import ScalarFloat
from src.yaml_serializer.types import SecretValue, NOT_IMPLEMENTED_SV_MSG
from ruamel.yaml.scalarint import ScalarInt
from ruamel.yaml.scalarstring import ScalarString
from src.json_serializer import json_dumps
from src.yaml_serializer.types import NOT_IMPLEMENTED_SV_MSG, SecretValue
class json_serializer_tests(TestCase):
+4 -6
View File
@@ -1,18 +1,16 @@
import io
from unittest import TestCase
from src.yaml_serializer import (
dump_yaml,
load_yaml,
)
from src.yaml_serializer import dump_yaml, load_yaml
from src.yaml_serializer.types import (
IncludedYaml,
SecretValue,
IncludedYamlDir,
SecretValue,
)
from tests.utils import SAMPLES_FOLDER
class dumping_yamls_tests(TestCase):
class dumping_yamls_tests(TestCase):
def test_dumping_includes(self):
yaml = dump_yaml(
{