-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2806 from bramstroker/feat/profile-composite
Allow to use composite config in library profiles
- Loading branch information
Showing
16 changed files
with
171 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
tests/testing_config/powercalc_profiles/composite/model.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "IKEA Control outlet", | ||
"measure_method": "manual", | ||
"measure_device": "IKEA Control outlet", | ||
"device_type": "smart_switch", | ||
"calculation_strategy": "composite", | ||
"composite_config": [ | ||
{ | ||
"condition": { | ||
"condition": "state", | ||
"entity_id": "light.test", | ||
"state": "on" | ||
}, | ||
"fixed": { | ||
"power": 0.82 | ||
} | ||
}, | ||
{ | ||
"fixed": { | ||
"power": 0.52 | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import glob | ||
import json | ||
import os | ||
|
||
from jsonschema import ValidationError, validate | ||
|
||
|
||
def load_json(file_path: str) -> dict: | ||
"""Load a JSON file from the given file path.""" | ||
with open(file_path) as file: | ||
return json.load(file) | ||
|
||
|
||
def validate_model(model_path: str, schema: dict) -> None: | ||
"""Validate a JSON model against the schema.""" | ||
try: | ||
model = load_json(model_path) | ||
validate(instance=model, schema=schema) | ||
print(f"VALID: {model_path}") # noqa: T201 | ||
except ValidationError as e: | ||
print(f"INVALID: {model_path}\nError: {e.message}") # noqa: T201 | ||
except Exception as e: # noqa: BLE001 | ||
print(f"ERROR: {model_path}\nError: {e}") # noqa: T201 | ||
|
||
|
||
def validate_models_with_glob(directory: str, schema_path: str) -> None: | ||
"""Validate model.json files up to 2 subdirectory levels using glob.""" | ||
schema = load_json(schema_path) | ||
pattern = os.path.join(directory, "*/*/model.json") | ||
for model_path in glob.glob(pattern): | ||
validate_model(model_path, schema) | ||
|
||
|
||
if __name__ == "__main__": | ||
directory = os.path.join(os.path.dirname(__file__), "../../profile_library") | ||
schema_file_path = os.path.join(os.path.dirname(__file__), "../../profile_library/model_schema.json") | ||
|
||
validate_models_with_glob(directory, schema_file_path) |