Skip to content

Commit afa0bb7

Browse files
committed
better unit handling in semantic detection
1 parent 550006b commit afa0bb7

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

python/shared/helper.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ def __call__(self, clazz):
6868
def init(self, *args, **kwargs):
6969
SimpleRule.__init__(self)
7070
#proxy.set_uid_prefix(self,classPackage,filePackage)
71-
clazz.__init__(self, *args, **kwargs)
71+
try:
72+
clazz.__init__(self, *args, **kwargs)
73+
except Exception as e:
74+
self.log.error("Rule init failed:\n" + traceback.format_exc())
75+
raise e
7276

7377
self.triggerItems = {}
7478
_triggers = []

python/shared/semantic/command_processor.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
import re
33

4+
from org.openhab.core.library.items import NumberItem
5+
46
from shared.helper import sendCommandIfChanged, getItemState, getLanguage
57

68
import importlib
@@ -428,11 +430,17 @@ def getAnswer(self,semantic_item):
428430
for tag in semantic_item.getItem().getTags():
429431
if tag not in SemanticConfig["answers"]:
430432
continue
431-
return SemanticConfig["answers"][tag].format(room=semantic_location.getItem().getLabel(),state=value)
433+
434+
answer = SemanticConfig["answers"][tag]["answer"]
435+
unit = SemanticConfig["answers"][tag]["unit"]
436+
if isinstance(semantic_item.getItem(), NumberItem):
437+
if semantic_item.getItem().getUnit() is None:
438+
value = u"{} {}".format(value, unit)
439+
return answer.format(room=semantic_location.getItem().getLabel(),state=value)
432440

433441
semantic_reference = semantic_equipment if len(semantic_equipment.getChildren()) == 1 else semantic_item
434442

435-
return SemanticConfig["answers"]["Default"].format(equipment=semantic_reference.getItem().getLabel(),room=semantic_location.getItem().getLabel(),state=value)
443+
return SemanticConfig["answers"]["Default"]["answer"].format(equipment=semantic_reference.getItem().getLabel(),room=semantic_location.getItem().getLabel(),state=value)
436444

437445
def applyActions(self,actions,voice_command,dry_run):
438446
missing_locations = []

python/shared/semantic/config/semantic_config.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
"ok_message": "ok"
1515
},
1616
"answers": {
17-
"Temperature": u"The temperature in the {room} is {state} °C",
18-
"Humidity": u"The humidity in the {room} is {state} %",
19-
#"Light": u"The light in the {room} is {state}",
20-
"Default": u"{equipment} in the {room} is {state}"
17+
"Temperature": { "answer": u"The temperature in the {room} is {state}", "unit": "°C" },
18+
"Humidity": { "answer": u"The humidity in the {room} is {state}", "unit": "%" },
19+
#"Light": { "answer": u"The light in the {room} is {state}" },
20+
"Default": { "answer": u"{equipment} in the {room} is {state}" }
2121
},
2222
"states": {
2323
"ON": "on",

python/shared/semantic/config/semantic_config_de.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
"ok_message": "ok"
1717
},
1818
"answers": {
19-
"Temperature": u"Die Temperatur im {room} beträgt {state} °C",
20-
"Humidity": u"Die Luftfeuchtigkeit im {room} beträgt {state} %",
21-
#"Light": u"Das Licht im {room} ist {state}",
22-
"Default": u"{equipment} im {room} ist {state}"
19+
"Temperature": { "answer": u"Die Temperatur im {room} beträgt {state}", "unit": "°C" },
20+
"Humidity": { "answer": u"Die Luftfeuchtigkeit im {room} beträgt {state}", "unit": "%" },
21+
"Default": { "answer": u"{equipment} im {room} ist {state}" }
2322
},
2423
"states": {
2524
"ON": "on",

python/shared/semantic/model/semantic_model.py

+3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def __init__(self,log,item_registry,config):
117117
for semantic_item in self.semantic_items.values():
118118
if semantic_item.item.getType() == "Group":
119119
children = semantic_item.item.getMembers()
120+
#self.log.info("-------------")
121+
#self.log.info(semantic_item.item.getName())
120122
for item in children:
123+
#self.log.info(item.getName())
121124
semantic_item.children.append(self.semantic_items[item.getName()])
122125

123126
if semantic_item.getSemanticType() == "Location":

0 commit comments

Comments
 (0)