-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: Parameterized thing label in thing builder. #323
Comments
Another usefull thing I found is that passing extra symbols to config hash works very well and I use it to store extra properties for each thing. |
I don't fully understand the use case, but you could do something like this without the whole custom class:
|
Sure but this makes
|
Behind this, the thing This is quite practical and grandly reduces maintenance when renaming things or groups over large amount of the same |
Here's what I currently do after the thing builder if it's of any interest for you :
|
I don't have a lot of time this week to look into this. I'm more than happy to extend things where they make sense. But I do want to point out that your example code does not create global (or even top-level local) variables. In Ruby, every block (i.e. the |
Sorry, should have said external to the thing instead of global. Happy to ear from you once you'll have time to take a look at it. |
I've read through this more fully now. I think how you're doing it is exactly how you should do it - you're adding additional meaning to things like the label, and in order to facilitate that you're wrapping classes already in the library. Most of your additions are not generically applicable to openHAB users as a whole - only someone that specifically structures their things and labels like you do, and then wants to derive additional meaning from that structure would get use out of this. My own rules files have several similar things that I know only apply to how I do things, and wouldn't be useful for others. To enumerate your additions to
|
|
element = thing.configuration[key.to_sym] # there's no need to call to_sym here, because it gets converted back to string anyway inside the helper library So thing.configuration[:original_label] = @original_label
# stored as thing.configuration["original_label"] Some suggestions, mainly as general Ruby tips, and not specifically about what you're trying to achieve: def parameterized_string(label)
new_label = label
label.scan(/<([^>]+)>/).flatten.each do |key|
new_label.gsub!("<#{key}>", parameter_to_string(key))
end
new_label
end
Proof:
To avoid this, you could do
A better implementation of that method: def parameterized_string(label)
label.gsub(/<([^>]+)>/) { parameter_to_string($1) }
end |
As for your main goal, you could probably do this:
def format_thing_label(thing)
return unless thing.label.include? "<"
thing.label = thing.label.gsub(/<([^>]+)>/) do
case $1
when "location" then thing.location
when "type" then thing.thing_type.uid.id.capitalize
when "binding" then thing.uid.binding_id.capitalize
when "id" then thing.uid.id.capitalize
else thing.configuration[$1].to_s
end
end
end
things.each { |thing| format_thing_label(thing) } |
Thanks for your insights @jimtng - I'll integrate them. |
I'm going my way with JRuby and I love it.
So much that I completely switched Thing creation to its ruby version taking the advantage of dynamically create things and using variables for this - far more flexible than traditional
.things
files.I produced the following code that could maybe be interesting to consider for insertion in the thing builder:
This enables me to make this type of thing creation:
This produces a thing labelled
"Welcome Dining Room"
The text was updated successfully, but these errors were encountered: