-
Notifications
You must be signed in to change notification settings - Fork 103
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 #776 from onkelandy/executor
Executor Plugin: Improvements and fixes
- Loading branch information
Showing
9 changed files
with
375 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import os | ||
|
||
with os.popen('ip neigh show') as result: | ||
# permanent, noarp, reachable, stale, none, incomplete, delay, probe, failed | ||
ip = '192.168.10.56' | ||
mac = "b4:b5:2f:ce:6d:29" | ||
value = False | ||
lines = str(result.read()).splitlines() | ||
for line in lines: | ||
if (ip in line or mac in line) and ("REACHABLE" in line or "STALE" in line): | ||
value = True | ||
break | ||
#sh.devices.laptop.status(value) | ||
print(f"set item to {value}") |
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,95 @@ | ||
""" | ||
given following items within a yaml: | ||
MyItem: | ||
MyChildItem: | ||
type: num | ||
initial_value: 12 | ||
MyGrandchildItem: | ||
type: str | ||
initial_value: "foo" | ||
Within a logic it is possible to set the value of MyChildItem to 42 with | ||
``sh.MyItem.MyChildItem(42)`` and retrieve the Items value with | ||
``value = sh.MyItem.MyChildItem()`` | ||
Often beginners forget the parentheses and instead write | ||
``sh.MyItem.MyChildItem = 42`` when they really intend to assign the value ``42`` | ||
to the item or write ``value = sh.MyItem.MyChildItem`` when they really want to | ||
retrieve the item's value. | ||
But using ``sh.MyItem.MyChildItem = 42`` destroys the structure here and makes | ||
it impossible to retrieve the value of the child | ||
``MyItem.MyChildItem.MyGrandchildItem`` | ||
Alike, an instruction as ``value = sh.MyItem.MyChildItem`` will not assign the | ||
value of ``sh.MyItem.MyChildItem`` but assign a reference to the item object | ||
``sh.MyItem.MyChildItem`` | ||
It is not possible with Python to intercept an assignment to a variable or an | ||
objects' attribute. The only thing one can do is search all items for a | ||
mismatching item type. | ||
This logic checks all items returned by SmartHomeNG, and if it encounters one | ||
which seems to be damaged like described before, it attempts to repair the | ||
broken assignment. | ||
""" | ||
from lib.item import Items | ||
from lib.item.item import Item | ||
|
||
def repair_item(sh, item): | ||
path = item.id() | ||
path_elems = path.split('.') | ||
ref = sh | ||
|
||
# traverse through object structure sh.path1.path2... | ||
try: | ||
for path_part in path_elems[:-1]: | ||
ref = getattr(ref, path_part) | ||
|
||
setattr(ref, path_elems[-1], item) | ||
print(f'Item reference repaired for {path}') | ||
return True | ||
except NameError: | ||
print(f'Error: item traversal for {path} failed at part {path_part}. Item list not sorted?') | ||
|
||
return False | ||
|
||
|
||
def get_item_type(sh, path): | ||
expr = f'type(sh.{path})' | ||
return str(eval(expr)) | ||
|
||
|
||
def check_item(sh, path): | ||
global get_item_type | ||
|
||
return get_item_type(sh, path) == "<class 'lib.item.item.Item'>" | ||
|
||
|
||
# to get access to the object instance: | ||
items = Items.get_instance() | ||
|
||
# to access a method (eg. to get the list of Items): | ||
# allitems = items.return_items() | ||
problems_found = 0 | ||
problems_fixed = 0 | ||
|
||
for one in items.return_items(ordered=True): | ||
# get the items full path | ||
path = one.property.path | ||
try: | ||
if not check_item(sh, path): | ||
logger.error(f"Error: item {path} has type {get_item_type(sh, path)} but should be an Item Object") | ||
problems_found += 1 | ||
if repair_item(sh, one): | ||
if check_item(sh, path): | ||
problems_fixed += 1 | ||
except ValueError as e: | ||
logger.error(f'Error {e} while processing item {path}, parent defective? Items not sorted?') | ||
|
||
if problems_found: | ||
logger.error(f"{problems_found} problematic item assignment{'' if problems_found == 1 else 's'} found, {problems_fixed} item assignment{'' if problems_fixed == 1 else 's'} fixed") | ||
else: | ||
logger.warning("no problems found") |
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,9 @@ | ||
from lib.item import Items | ||
items = Items.get_instance() | ||
myfiller = " " | ||
allItems = items.return_items() | ||
for myItem in allItems: | ||
if not hasattr(myItem,'db'): | ||
continue | ||
mycount = myItem.db('countall', 0) | ||
print (myItem.property.name + myfiller[0:len(myfiller)-len(myItem.property.name)]+ ' - Anzahl Datensätze :'+str(mycount)) |
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,9 @@ | ||
import json | ||
|
||
def myconverter(o): | ||
import datetime | ||
if isinstance(o, datetime.datetime): | ||
return o.__str__() | ||
data = sh.<your item here>.series('max','1d','now') | ||
pretty = json.dumps(data, default = myconverter, indent = 2, separators=(',', ': ')) | ||
print(pretty) |
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
Oops, something went wrong.