openhab-scripting 2.22.1 → 2.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/openhab/dsl/items/datetime_item.rb +6 -31
- data/lib/openhab/dsl/items/item_delegate.rb +122 -0
- data/lib/openhab/dsl/items/number_item.rb +4 -36
- data/lib/openhab/dsl/items/rollershutter_item.rb +6 -46
- data/lib/openhab/dsl/items/string_item.rb +8 -37
- data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -0
- data/lib/openhab/dsl/monkey_patch/events/item_event.rb +28 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state.rb +23 -0
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -11
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +23 -75
- data/lib/openhab/dsl/monkey_patch/items/items.rb +9 -0
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +3 -5
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +52 -23
- data/lib/openhab/dsl/rules/automation_rule.rb +4 -20
- data/lib/openhab/version.rb +1 -1
- metadata +5 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 4ce40856625691eb49a22ee19ac02bd030dba8cdf6e62f86736f9974cf7e2162
         | 
| 4 | 
            +
              data.tar.gz: 98e2226b8d4286f16a7f3b4ae40ef45246489df9464d1b3a6f5f7eea37ed93cb
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: d3e2329d4a8b1ef612348f135a827e0011ffa0fa6b2958d2d333def5777db2ed76bd91e3cd0310c2075fe0b941b6936482a21c3ef20cca4a14283780629704b8
         | 
| 7 | 
            +
              data.tar.gz: d809054def4036f6b42faf9aa069a9f201fd0fd01120df1ebb32df91a5b7069a8073237153b1f0ac2ced5477e47a24547496fab48b317d57fbb100dc286eb59f
         | 
| @@ -4,6 +4,7 @@ require 'forwardable' | |
| 4 4 | 
             
            require 'java'
         | 
| 5 5 | 
             
            require 'time'
         | 
| 6 6 | 
             
            require 'openhab/dsl/types/datetime'
         | 
| 7 | 
            +
            require 'openhab/dsl/items/item_delegate'
         | 
| 7 8 |  | 
| 8 9 | 
             
            module OpenHAB
         | 
| 9 10 | 
             
              module DSL
         | 
| @@ -15,9 +16,11 @@ module OpenHAB | |
| 15 16 | 
             
                  #
         | 
| 16 17 | 
             
                  class DateTimeItem
         | 
| 17 18 | 
             
                    extend Forwardable
         | 
| 19 | 
            +
                    extend OpenHAB::DSL::Items::ItemDelegate
         | 
| 20 | 
            +
             | 
| 18 21 | 
             
                    include Comparable
         | 
| 19 22 |  | 
| 20 | 
            -
                     | 
| 23 | 
            +
                    def_item_delegator :@datetime_item
         | 
| 21 24 |  | 
| 22 25 | 
             
                    #
         | 
| 23 26 | 
             
                    # Create a new DateTimeItem
         | 
| @@ -27,6 +30,8 @@ module OpenHAB | |
| 27 30 | 
             
                    #
         | 
| 28 31 | 
             
                    def initialize(datetime_item)
         | 
| 29 32 | 
             
                      @datetime_item = datetime_item
         | 
| 33 | 
            +
                      item_missing_delegate { @datetime_item }
         | 
| 34 | 
            +
                      item_missing_delegate { to_dt }
         | 
| 30 35 | 
             
                    end
         | 
| 31 36 |  | 
| 32 37 | 
             
                    #
         | 
| @@ -61,36 +66,6 @@ module OpenHAB | |
| 61 66 | 
             
                    def zone
         | 
| 62 67 | 
             
                      to_dt.zone if state?
         | 
| 63 68 | 
             
                    end
         | 
| 64 | 
            -
             | 
| 65 | 
            -
                    #
         | 
| 66 | 
            -
                    # Check if missing method can be delegated to other contained objects
         | 
| 67 | 
            -
                    #
         | 
| 68 | 
            -
                    # @param [String, Symbol] meth The method name to check for
         | 
| 69 | 
            -
                    #
         | 
| 70 | 
            -
                    # @return [Boolean] true if DateTimeItem or DateTime responds to the method, false otherwise
         | 
| 71 | 
            -
                    #
         | 
| 72 | 
            -
                    def respond_to_missing?(meth, *)
         | 
| 73 | 
            -
                      @datetime_item.respond_to?(meth) || to_dt.respond_to?(meth)
         | 
| 74 | 
            -
                    end
         | 
| 75 | 
            -
             | 
| 76 | 
            -
                    #
         | 
| 77 | 
            -
                    # Forward missing methods to the OpenHAB Item, or a DateTime object wrapping its state
         | 
| 78 | 
            -
                    #
         | 
| 79 | 
            -
                    # @param [String] meth method name
         | 
| 80 | 
            -
                    # @param [Array] args arguments for method
         | 
| 81 | 
            -
                    # @param [Proc] block <description>
         | 
| 82 | 
            -
                    #
         | 
| 83 | 
            -
                    # @return [Object] Value from delegated method in OpenHAB NumberItem
         | 
| 84 | 
            -
                    #
         | 
| 85 | 
            -
                    def method_missing(meth, *args, &block)
         | 
| 86 | 
            -
                      if @datetime_item.respond_to?(meth)
         | 
| 87 | 
            -
                        @datetime_item.__send__(meth, *args, &block)
         | 
| 88 | 
            -
                      elsif state?
         | 
| 89 | 
            -
                        to_dt.send(meth, *args, &block)
         | 
| 90 | 
            -
                      else
         | 
| 91 | 
            -
                        raise NoMethodError, "undefined method `#{meth}' for #{self.class}"
         | 
| 92 | 
            -
                      end
         | 
| 93 | 
            -
                    end
         | 
| 94 69 | 
             
                  end
         | 
| 95 70 | 
             
                end
         | 
| 96 71 | 
             
              end
         | 
| @@ -0,0 +1,122 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'java'
         | 
| 4 | 
            +
            require 'openhab/log/logger'
         | 
| 5 | 
            +
            require 'forwardable'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            module OpenHAB
         | 
| 8 | 
            +
              module DSL
         | 
| 9 | 
            +
                module Items
         | 
| 10 | 
            +
                  #
         | 
| 11 | 
            +
                  # Holds methods to delegate to items
         | 
| 12 | 
            +
                  #
         | 
| 13 | 
            +
                  module ItemDelegate
         | 
| 14 | 
            +
                    include OpenHAB::Log
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    # @return [Array<Symbol>] Array of methods that by default will be forwarded to an OpenHAB Item.
         | 
| 17 | 
            +
                    DEFAULT_DELEGATION_METHODS = %i[to_s groups].freeze
         | 
| 18 | 
            +
                    private_constant :DEFAULT_DELEGATION_METHODS
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                    #
         | 
| 21 | 
            +
                    # Extends calling object with DSL and helper methods
         | 
| 22 | 
            +
                    #
         | 
| 23 | 
            +
                    # @param [Object] base Object to decorate with DSL and helper methods
         | 
| 24 | 
            +
                    #
         | 
| 25 | 
            +
                    #
         | 
| 26 | 
            +
                    def self.extended(base)
         | 
| 27 | 
            +
                      base.extend Forwardable
         | 
| 28 | 
            +
                      base.include OpenHAB::DSL::Items::ItemDelegate
         | 
| 29 | 
            +
                    end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                    #
         | 
| 32 | 
            +
                    # Delegate a set of methods to the supplied delegator
         | 
| 33 | 
            +
                    #
         | 
| 34 | 
            +
                    # @param [Java::OrgOpenhabCoreItems::GenericItem] item to delegate methods to
         | 
| 35 | 
            +
                    # @param [Symbol] methods to delegate, if not supplied default methods are used
         | 
| 36 | 
            +
                    #
         | 
| 37 | 
            +
                    #
         | 
| 38 | 
            +
                    def def_item_delegator(item, *methods)
         | 
| 39 | 
            +
                      methods = DEFAULT_DELEGATION_METHODS if methods.size.zero?
         | 
| 40 | 
            +
                      methods.each do |method|
         | 
| 41 | 
            +
                        logger.trace("Creating delegate for (#{method}) to #{item}) for #{self.class}")
         | 
| 42 | 
            +
                        def_delegator item, method
         | 
| 43 | 
            +
                      end
         | 
| 44 | 
            +
                    end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                    #
         | 
| 47 | 
            +
                    # Delegates methods to the object returned from the supplied block if
         | 
| 48 | 
            +
                    #   they don't exist in the object this is included in.  If the supplied block returns nil, no delegation occurs
         | 
| 49 | 
            +
                    # If this item is called more than once delegation occurs in the order of invocation, i.e. the object returned
         | 
| 50 | 
            +
                    #   by the first block is delegated to if it responds to the missing method,
         | 
| 51 | 
            +
                    #   then the second block is processed, etc.
         | 
| 52 | 
            +
                    #
         | 
| 53 | 
            +
                    # @param [Proc] &delegate delgegate block
         | 
| 54 | 
            +
                    #
         | 
| 55 | 
            +
                    #
         | 
| 56 | 
            +
                    def item_missing_delegate(&delegate)
         | 
| 57 | 
            +
                      @delegates ||= []
         | 
| 58 | 
            +
                      @delegates << delegate
         | 
| 59 | 
            +
                    end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                    #
         | 
| 62 | 
            +
                    # Delegate missing method calls to delegates supplied to item_delgate method
         | 
| 63 | 
            +
                    #  if no delegates exist or respond to missing method, super is invoked which will
         | 
| 64 | 
            +
                    #  throw the appropriate method missing error
         | 
| 65 | 
            +
                    #
         | 
| 66 | 
            +
                    # @param [String] meth misisng method
         | 
| 67 | 
            +
                    # @param [Array] *args Arguments to the missing method
         | 
| 68 | 
            +
                    # @param [Proc] &block supplied to the missing method
         | 
| 69 | 
            +
                    #
         | 
| 70 | 
            +
                    # @return [Object] Result of missing method invocation
         | 
| 71 | 
            +
                    #
         | 
| 72 | 
            +
                    def method_missing(meth, *args, &block)
         | 
| 73 | 
            +
                      logger.trace("Method (#{meth}) missing for item #{self.class}")
         | 
| 74 | 
            +
                      delegate = delegate_for(meth)
         | 
| 75 | 
            +
                      if delegate
         | 
| 76 | 
            +
                        logger.trace("Delegating #{meth} to #{delegate.class}")
         | 
| 77 | 
            +
                        delegate.__send__(meth, *args, &block)
         | 
| 78 | 
            +
                      else
         | 
| 79 | 
            +
                        super
         | 
| 80 | 
            +
                      end
         | 
| 81 | 
            +
                    end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                    #
         | 
| 84 | 
            +
                    # Checks if any of the supplied delgates respond to a specific method
         | 
| 85 | 
            +
                    #
         | 
| 86 | 
            +
                    # @param [String] meth method to check for
         | 
| 87 | 
            +
                    # @param [Boolean] _include_private if private methods should be checked
         | 
| 88 | 
            +
                    #
         | 
| 89 | 
            +
                    # @return [Boolean] True if any delegates respond to method, false otherwise
         | 
| 90 | 
            +
                    #
         | 
| 91 | 
            +
                    def respond_to_missing?(meth, _include_private = false)
         | 
| 92 | 
            +
                      logger.trace("Checking if (#{self.class}) responds to (#{meth})")
         | 
| 93 | 
            +
                      responds = !delegate_for(meth).nil?
         | 
| 94 | 
            +
                      logger.trace("(#{self.class}) responds to (#{meth}) (#{responds})")
         | 
| 95 | 
            +
                      responds
         | 
| 96 | 
            +
                    end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                    private
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                    #
         | 
| 101 | 
            +
                    # Find a delegate for the supplied method
         | 
| 102 | 
            +
                    #
         | 
| 103 | 
            +
                    # @param [String] meth method to find delegate for
         | 
| 104 | 
            +
                    #
         | 
| 105 | 
            +
                    # @return [Boolean] True if any method responds to the supplied delegate, false otherwise
         | 
| 106 | 
            +
                    #
         | 
| 107 | 
            +
                    def delegate_for(meth)
         | 
| 108 | 
            +
                      (@delegates || []).each do |delegate_block|
         | 
| 109 | 
            +
                        delegate = delegate_block.call(meth)
         | 
| 110 | 
            +
                        logger.trace("Checking if delegate (#{delegate.class}) responds to (#{meth})")
         | 
| 111 | 
            +
                        if delegate.respond_to? meth
         | 
| 112 | 
            +
                          logger.trace("Delegate (#{delegate.class}) found for method (#{meth})")
         | 
| 113 | 
            +
                          return delegate
         | 
| 114 | 
            +
                        end
         | 
| 115 | 
            +
                      end
         | 
| 116 | 
            +
                      logger.trace("No delegate found for method (#{meth})")
         | 
| 117 | 
            +
                      nil
         | 
| 118 | 
            +
                    end
         | 
| 119 | 
            +
                  end
         | 
| 120 | 
            +
                end
         | 
| 121 | 
            +
              end
         | 
| 122 | 
            +
            end
         | 
| @@ -4,6 +4,7 @@ require 'bigdecimal' | |
| 4 4 | 
             
            require 'forwardable'
         | 
| 5 5 | 
             
            require 'java'
         | 
| 6 6 | 
             
            require 'openhab/dsl/types/quantity'
         | 
| 7 | 
            +
            require 'openhab/dsl/items/item_delegate'
         | 
| 7 8 |  | 
| 8 9 | 
             
            module OpenHAB
         | 
| 9 10 | 
             
              module DSL
         | 
| @@ -15,9 +16,9 @@ module OpenHAB | |
| 15 16 | 
             
                  # Disabled because this class has a single responsibility, there does not appear a logical
         | 
| 16 17 | 
             
                  # way of breaking it up into multiple classes
         | 
| 17 18 | 
             
                  class NumberItem < Numeric
         | 
| 18 | 
            -
                    extend  | 
| 19 | 
            +
                    extend OpenHAB::DSL::Items::ItemDelegate
         | 
| 19 20 |  | 
| 20 | 
            -
                     | 
| 21 | 
            +
                    def_item_delegator :@number_item
         | 
| 21 22 |  | 
| 22 23 | 
             
                    java_import org.openhab.core.library.types.DecimalType
         | 
| 23 24 | 
             
                    java_import org.openhab.core.library.types.QuantityType
         | 
| @@ -31,6 +32,7 @@ module OpenHAB | |
| 31 32 | 
             
                    #
         | 
| 32 33 | 
             
                    def initialize(number_item)
         | 
| 33 34 | 
             
                      @number_item = number_item
         | 
| 35 | 
            +
                      item_missing_delegate { @number_item }
         | 
| 34 36 | 
             
                      super()
         | 
| 35 37 | 
             
                    end
         | 
| 36 38 |  | 
| @@ -146,40 +148,6 @@ module OpenHAB | |
| 146 148 | 
             
                      @number_item.dimension
         | 
| 147 149 | 
             
                    end
         | 
| 148 150 |  | 
| 149 | 
            -
                    #
         | 
| 150 | 
            -
                    # Forward missing methods to Openhab Number Item if they are defined
         | 
| 151 | 
            -
                    #
         | 
| 152 | 
            -
                    # @param [String] meth method name
         | 
| 153 | 
            -
                    # @param [Array] args arguments for method
         | 
| 154 | 
            -
                    # @param [Proc] block <description>
         | 
| 155 | 
            -
                    #
         | 
| 156 | 
            -
                    # @return [Object] Value from delegated method in OpenHAB NumberItem
         | 
| 157 | 
            -
                    #
         | 
| 158 | 
            -
                    def method_missing(meth, *args, &block)
         | 
| 159 | 
            -
                      logger.trace("Method missing, performing dynamic lookup for: #{meth}")
         | 
| 160 | 
            -
                      if @number_item.respond_to?(meth)
         | 
| 161 | 
            -
                        @number_item.__send__(meth, *args, &block)
         | 
| 162 | 
            -
                      elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
         | 
| 163 | 
            -
                        ::Kernel.instance_method(meth).bind_call(self, *args, &block)
         | 
| 164 | 
            -
                      else
         | 
| 165 | 
            -
                        super(meth, *args, &block)
         | 
| 166 | 
            -
                      end
         | 
| 167 | 
            -
                    end
         | 
| 168 | 
            -
             | 
| 169 | 
            -
                    #
         | 
| 170 | 
            -
                    # Checks if this method responds to the missing method
         | 
| 171 | 
            -
                    #
         | 
| 172 | 
            -
                    # @param [String] method_name Name of the method to check
         | 
| 173 | 
            -
                    # @param [Boolean] _include_private boolean if private methods should be checked
         | 
| 174 | 
            -
                    #
         | 
| 175 | 
            -
                    # @return [Boolean] true if this object will respond to the supplied method, false otherwise
         | 
| 176 | 
            -
                    #
         | 
| 177 | 
            -
                    def respond_to_missing?(method_name, _include_private = false)
         | 
| 178 | 
            -
                      @number_item.respond_to?(method_name) ||
         | 
| 179 | 
            -
                        ::Kernel.method_defined?(method_name) ||
         | 
| 180 | 
            -
                        ::Kernel.private_method_defined?(method_name)
         | 
| 181 | 
            -
                    end
         | 
| 182 | 
            -
             | 
| 183 151 | 
             
                    %w[+ - * /].each do |operation|
         | 
| 184 152 | 
             
                      define_method(operation) do |other|
         | 
| 185 153 | 
             
                        logger.trace("Execution math operation '#{operation}' on #{inspect} with #{other.inspect}")
         | 
| @@ -3,6 +3,7 @@ | |
| 3 3 | 
             
            require 'forwardable'
         | 
| 4 4 | 
             
            require 'java'
         | 
| 5 5 | 
             
            require 'openhab/dsl/items/item_command'
         | 
| 6 | 
            +
            require 'openhab/dsl/items/item_delegate'
         | 
| 6 7 |  | 
| 7 8 | 
             
            module OpenHAB
         | 
| 8 9 | 
             
              module DSL
         | 
| @@ -13,9 +14,10 @@ module OpenHAB | |
| 13 14 | 
             
                  class RollershutterItem < Numeric
         | 
| 14 15 | 
             
                    extend Forwardable
         | 
| 15 16 | 
             
                    extend OpenHAB::DSL::Items::ItemCommand
         | 
| 17 | 
            +
                    extend OpenHAB::DSL::Items::ItemDelegate
         | 
| 16 18 | 
             
                    include Comparable
         | 
| 17 19 |  | 
| 18 | 
            -
                     | 
| 20 | 
            +
                    def_item_delegator :@rollershutter_item
         | 
| 19 21 |  | 
| 20 22 | 
             
                    java_import Java::OrgOpenhabCoreLibraryTypes::PercentType
         | 
| 21 23 | 
             
                    java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
         | 
| @@ -35,6 +37,9 @@ module OpenHAB | |
| 35 37 | 
             
                      logger.trace("Wrapping #{rollershutter_item}")
         | 
| 36 38 | 
             
                      @rollershutter_item = rollershutter_item
         | 
| 37 39 |  | 
| 40 | 
            +
                      item_missing_delegate { @rollershutter_item }
         | 
| 41 | 
            +
                      item_missing_delegate { position }
         | 
| 42 | 
            +
             | 
| 38 43 | 
             
                      super()
         | 
| 39 44 | 
             
                    end
         | 
| 40 45 |  | 
| @@ -79,19 +84,6 @@ module OpenHAB | |
| 79 84 | 
             
                      end
         | 
| 80 85 | 
             
                    end
         | 
| 81 86 |  | 
| 82 | 
            -
                    #
         | 
| 83 | 
            -
                    # Case equality
         | 
| 84 | 
            -
                    #
         | 
| 85 | 
            -
                    # @param [Java::OrgOpenhabCoreLibraryTypes::UpDownType, Numeric] other Other object to compare against
         | 
| 86 | 
            -
                    #
         | 
| 87 | 
            -
                    # @return [Boolean] true if self can be defined as other, false otherwise
         | 
| 88 | 
            -
                    #
         | 
| 89 | 
            -
                    def ===(other)
         | 
| 90 | 
            -
                      super unless other.is_a? UpDownType
         | 
| 91 | 
            -
             | 
| 92 | 
            -
                      state.as(UpDownType).equals(other)
         | 
| 93 | 
            -
                    end
         | 
| 94 | 
            -
             | 
| 95 87 | 
             
                    #
         | 
| 96 88 | 
             
                    # Define math operations
         | 
| 97 89 | 
             
                    #
         | 
| @@ -101,38 +93,6 @@ module OpenHAB | |
| 101 93 | 
             
                        left.send(operator, right)
         | 
| 102 94 | 
             
                      end
         | 
| 103 95 | 
             
                    end
         | 
| 104 | 
            -
             | 
| 105 | 
            -
                    #
         | 
| 106 | 
            -
                    # Checks if this method responds to the missing method
         | 
| 107 | 
            -
                    #
         | 
| 108 | 
            -
                    # @param [String] meth Name of the method to check
         | 
| 109 | 
            -
                    # @param [Boolean] _include_private boolean if private methods should be checked
         | 
| 110 | 
            -
                    #
         | 
| 111 | 
            -
                    # @return [Boolean] true if this object will respond to the supplied method, false otherwise
         | 
| 112 | 
            -
                    #
         | 
| 113 | 
            -
                    def respond_to_missing?(meth, _include_private = false)
         | 
| 114 | 
            -
                      @rollershutter_item.respond_to?(meth) || position.respond_to?(meth)
         | 
| 115 | 
            -
                    end
         | 
| 116 | 
            -
             | 
| 117 | 
            -
                    #
         | 
| 118 | 
            -
                    # Forward missing methods to Openhab RollershutterItem or the PercentType representing it's state
         | 
| 119 | 
            -
                    #   if they are defined
         | 
| 120 | 
            -
                    #
         | 
| 121 | 
            -
                    # @param [String] meth method name
         | 
| 122 | 
            -
                    # @param [Array] args arguments for method
         | 
| 123 | 
            -
                    # @param [Proc] block <description>
         | 
| 124 | 
            -
                    #
         | 
| 125 | 
            -
                    # @return [Object] Value from delegated method in OpenHAB NumberItem
         | 
| 126 | 
            -
                    #
         | 
| 127 | 
            -
                    def method_missing(meth, *args, &block)
         | 
| 128 | 
            -
                      if @rollershutter_item.respond_to?(meth)
         | 
| 129 | 
            -
                        @rollershutter_item.__send__(meth, *args, &block)
         | 
| 130 | 
            -
                      elsif position.respond_to?(meth)
         | 
| 131 | 
            -
                        position.__send__(meth, *args, &block)
         | 
| 132 | 
            -
                      else
         | 
| 133 | 
            -
                        raise NoMethodError, "No method `#{meth}' defined for #{self.class}"
         | 
| 134 | 
            -
                      end
         | 
| 135 | 
            -
                    end
         | 
| 136 96 | 
             
                  end
         | 
| 137 97 | 
             
                end
         | 
| 138 98 | 
             
              end
         | 
| @@ -3,6 +3,7 @@ | |
| 3 3 | 
             
            require 'bigdecimal'
         | 
| 4 4 | 
             
            require 'forwardable'
         | 
| 5 5 | 
             
            require 'java'
         | 
| 6 | 
            +
            require 'openhab/dsl/items/item_delegate'
         | 
| 6 7 |  | 
| 7 8 | 
             
            module OpenHAB
         | 
| 8 9 | 
             
              module DSL
         | 
| @@ -12,13 +13,15 @@ module OpenHAB | |
| 12 13 | 
             
                  #
         | 
| 13 14 | 
             
                  class StringItem
         | 
| 14 15 | 
             
                    extend Forwardable
         | 
| 16 | 
            +
             | 
| 15 17 | 
             
                    include Comparable
         | 
| 18 | 
            +
                    extend OpenHAB::DSL::Items::ItemDelegate
         | 
| 16 19 |  | 
| 17 20 | 
             
                    # @return [Regex] Regular expression matching blank strings
         | 
| 18 21 | 
             
                    BLANK_RE = /\A[[:space:]]*\z/.freeze
         | 
| 19 22 | 
             
                    private_constant :BLANK_RE
         | 
| 20 23 |  | 
| 21 | 
            -
                     | 
| 24 | 
            +
                    def_item_delegator :@string_item
         | 
| 22 25 |  | 
| 23 26 | 
             
                    #
         | 
| 24 27 | 
             
                    # Create a new StringItem
         | 
| @@ -27,6 +30,10 @@ module OpenHAB | |
| 27 30 | 
             
                    #
         | 
| 28 31 | 
             
                    def initialize(string_item)
         | 
| 29 32 | 
             
                      @string_item = string_item
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                      item_missing_delegate { @string_item }
         | 
| 35 | 
            +
                      item_missing_delegate { @string_item.state&.to_full_string&.to_s }
         | 
| 36 | 
            +
             | 
| 30 37 | 
             
                      super()
         | 
| 31 38 | 
             
                    end
         | 
| 32 39 |  | 
| @@ -78,42 +85,6 @@ module OpenHAB | |
| 78 85 | 
             
                        @string_item.state <=> other
         | 
| 79 86 | 
             
                      end
         | 
| 80 87 | 
             
                    end
         | 
| 81 | 
            -
             | 
| 82 | 
            -
                    #
         | 
| 83 | 
            -
                    # Forward missing methods to Openhab String Item or String representation of the item if they are defined
         | 
| 84 | 
            -
                    #
         | 
| 85 | 
            -
                    # @param [String] meth method name
         | 
| 86 | 
            -
                    # @param [Array] args arguments for method
         | 
| 87 | 
            -
                    # @param [Proc] block <description>
         | 
| 88 | 
            -
                    #
         | 
| 89 | 
            -
                    # @return [Object] Value from delegated method in OpenHAB StringItem or Ruby String
         | 
| 90 | 
            -
                    #
         | 
| 91 | 
            -
                    def method_missing(meth, *args, &block)
         | 
| 92 | 
            -
                      if @string_item.respond_to?(meth)
         | 
| 93 | 
            -
                        @string_item.__send__(meth, *args, &block)
         | 
| 94 | 
            -
                      elsif @string_item.state&.to_full_string&.to_s.respond_to?(meth)
         | 
| 95 | 
            -
                        @string_item.state.to_full_string.to_s.__send__(meth, *args, &block)
         | 
| 96 | 
            -
                      elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
         | 
| 97 | 
            -
                        ::Kernel.instance_method(meth).bind_call(self, *args, &block)
         | 
| 98 | 
            -
                      else
         | 
| 99 | 
            -
                        super(meth, *args, &block)
         | 
| 100 | 
            -
                      end
         | 
| 101 | 
            -
                    end
         | 
| 102 | 
            -
             | 
| 103 | 
            -
                    #
         | 
| 104 | 
            -
                    # Checks if this method responds to the missing method
         | 
| 105 | 
            -
                    #
         | 
| 106 | 
            -
                    # @param [String] method_name Name of the method to check
         | 
| 107 | 
            -
                    # @param [Boolean] _include_private boolean if private methods should be checked
         | 
| 108 | 
            -
                    #
         | 
| 109 | 
            -
                    # @return [Boolean] true if this object will respond to the supplied method, false otherwise
         | 
| 110 | 
            -
                    #
         | 
| 111 | 
            -
                    def respond_to_missing?(method_name, _include_private = false)
         | 
| 112 | 
            -
                      @string_item.respond_to?(method_name) ||
         | 
| 113 | 
            -
                        @string_item.state&.to_full_string&.to_s.respond_to?(method_name) ||
         | 
| 114 | 
            -
                        ::Kernel.method_defined?(method_name) ||
         | 
| 115 | 
            -
                        ::Kernel.private_method_defined?(method_name)
         | 
| 116 | 
            -
                    end
         | 
| 117 88 | 
             
                  end
         | 
| 118 89 | 
             
                end
         | 
| 119 90 | 
             
              end
         | 
| @@ -1,5 +1,7 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 | 
            +
            require 'openhab/dsl/monkey_patch/events/item_state'
         | 
| 4 | 
            +
            require 'openhab/dsl/monkey_patch/events/item_event'
         | 
| 3 5 | 
             
            require 'openhab/dsl/monkey_patch/events/item_state_changed'
         | 
| 4 6 | 
             
            require 'openhab/dsl/monkey_patch/events/thing_status_info'
         | 
| 5 7 | 
             
            require 'openhab/dsl/monkey_patch/events/item_command'
         | 
| @@ -0,0 +1,28 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'java'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module OpenHAB
         | 
| 6 | 
            +
              module DSL
         | 
| 7 | 
            +
                module MonkeyPatch
         | 
| 8 | 
            +
                  #
         | 
| 9 | 
            +
                  # Patches OpenHAB events
         | 
| 10 | 
            +
                  #
         | 
| 11 | 
            +
                  module Events
         | 
| 12 | 
            +
                    java_import Java::OrgOpenhabCoreItemsEvents::ItemEvent
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    #
         | 
| 15 | 
            +
                    # MonkeyPatch to add item
         | 
| 16 | 
            +
                    #
         | 
| 17 | 
            +
                    class ItemEvent
         | 
| 18 | 
            +
                      #
         | 
| 19 | 
            +
                      # Return a decorated item
         | 
| 20 | 
            +
                      #
         | 
| 21 | 
            +
                      def item
         | 
| 22 | 
            +
                        OpenHAB::Core::EntityLookup.lookup_item(item_name)
         | 
| 23 | 
            +
                      end
         | 
| 24 | 
            +
                    end
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'java'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module OpenHAB
         | 
| 6 | 
            +
              module DSL
         | 
| 7 | 
            +
                module MonkeyPatch
         | 
| 8 | 
            +
                  #
         | 
| 9 | 
            +
                  # Patches OpenHAB events
         | 
| 10 | 
            +
                  #
         | 
| 11 | 
            +
                  module Events
         | 
| 12 | 
            +
                    java_import Java::OrgOpenhabCoreItemsEvents::ItemStateEvent
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    #
         | 
| 15 | 
            +
                    # MonkeyPatch with ruby style accessors
         | 
| 16 | 
            +
                    #
         | 
| 17 | 
            +
                    class ItemStateEvent
         | 
| 18 | 
            +
                      alias state item_state
         | 
| 19 | 
            +
                    end
         | 
| 20 | 
            +
                  end
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| @@ -15,17 +15,6 @@ module OpenHAB | |
| 15 15 | 
             
                    # MonkeyPatch with ruby style accessors
         | 
| 16 16 | 
             
                    #
         | 
| 17 17 | 
             
                    class ItemStateChangedEvent
         | 
| 18 | 
            -
                      #
         | 
| 19 | 
            -
                      # Get the item that caused the state change
         | 
| 20 | 
            -
                      #
         | 
| 21 | 
            -
                      # @return [Item] Item that caused state change
         | 
| 22 | 
            -
                      #
         | 
| 23 | 
            -
                      def item
         | 
| 24 | 
            -
                        # rubocop:disable Style/GlobalVars
         | 
| 25 | 
            -
                        $ir.get(item_name)
         | 
| 26 | 
            -
                        # rubocop:enable Style/GlobalVars
         | 
| 27 | 
            -
                      end
         | 
| 28 | 
            -
             | 
| 29 18 | 
             
                      alias state item_state
         | 
| 30 19 | 
             
                      alias last old_item_state
         | 
| 31 20 | 
             
                    end
         | 
| @@ -1,6 +1,8 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 3 | 
             
            require 'java'
         | 
| 4 | 
            +
            require 'openhab/dsl/items/item_command'
         | 
| 5 | 
            +
            require 'forwardable'
         | 
| 4 6 |  | 
| 5 7 | 
             
            module OpenHAB
         | 
| 6 8 | 
             
              module DSL
         | 
| @@ -25,31 +27,13 @@ module OpenHAB | |
| 25 27 | 
             
                      java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
         | 
| 26 28 | 
             
                      java_import Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
         | 
| 27 29 |  | 
| 28 | 
            -
                       | 
| 29 | 
            -
                       | 
| 30 | 
            -
                      #
         | 
| 31 | 
            -
                      # @param [Object] other object to add the dimmer value to
         | 
| 32 | 
            -
                      #
         | 
| 33 | 
            -
                      # @return [Integer] Current dimmer value plus value of supplied object
         | 
| 34 | 
            -
                      #
         | 
| 35 | 
            -
                      def +(other)
         | 
| 36 | 
            -
                        return unless state?
         | 
| 30 | 
            +
                      extend Forwardable
         | 
| 31 | 
            +
                      extend OpenHAB::DSL::Items::ItemCommand
         | 
| 37 32 |  | 
| 38 | 
            -
             | 
| 39 | 
            -
                       | 
| 33 | 
            +
                      item_state Java::OrgOpenhabCoreLibraryTypes::OnOffType
         | 
| 34 | 
            +
                      item_command Java::OrgOpenhabCoreLibraryTypes::IncreaseDecreaseType
         | 
| 40 35 |  | 
| 41 | 
            -
                       | 
| 42 | 
            -
                      # Subtract the supplied object from the current value of the dimmer
         | 
| 43 | 
            -
                      #
         | 
| 44 | 
            -
                      # @param [Object] other object to subtract from the dimmer value
         | 
| 45 | 
            -
                      #
         | 
| 46 | 
            -
                      # @return [Integer] Current dimmer value minus value of supplied object
         | 
| 47 | 
            -
                      #
         | 
| 48 | 
            -
                      def -(other)
         | 
| 49 | 
            -
                        return unless state?
         | 
| 50 | 
            -
             | 
| 51 | 
            -
                        state.to_big_decimal.intValue - other
         | 
| 52 | 
            -
                      end
         | 
| 36 | 
            +
                      def_delegator :state, :to_s
         | 
| 53 37 |  | 
| 54 38 | 
             
                      #
         | 
| 55 39 | 
             
                      # Dim the dimmer
         | 
| @@ -59,17 +43,9 @@ module OpenHAB | |
| 59 43 | 
             
                      # @return [Integer] level target for dimmer
         | 
| 60 44 | 
             
                      #
         | 
| 61 45 | 
             
                      def dim(amount = 1)
         | 
| 62 | 
            -
                         | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
                        if amount == 1
         | 
| 67 | 
            -
                          command(IncreaseDecreaseType::DECREASE)
         | 
| 68 | 
            -
                        else
         | 
| 69 | 
            -
                          command(target)
         | 
| 70 | 
            -
                        end
         | 
| 71 | 
            -
             | 
| 72 | 
            -
                        target
         | 
| 46 | 
            +
                        [state&.to_big_decimal&.intValue&.-(amount), 0].compact
         | 
| 47 | 
            +
                                                                       .max
         | 
| 48 | 
            +
                                                                       .tap { |target| command(target) }
         | 
| 73 49 | 
             
                      end
         | 
| 74 50 |  | 
| 75 51 | 
             
                      #
         | 
| @@ -80,16 +56,7 @@ module OpenHAB | |
| 80 56 | 
             
                      # @return [Integer] level target for dimmer
         | 
| 81 57 | 
             
                      #
         | 
| 82 58 | 
             
                      def brighten(amount = 1)
         | 
| 83 | 
            -
                         | 
| 84 | 
            -
             | 
| 85 | 
            -
                        target = state.to_big_decimal.intValue + amount
         | 
| 86 | 
            -
             | 
| 87 | 
            -
                        if amount == 1
         | 
| 88 | 
            -
                          command(IncreaseDecreaseType::INCREASE)
         | 
| 89 | 
            -
                        else
         | 
| 90 | 
            -
                          command(target)
         | 
| 91 | 
            -
                        end
         | 
| 92 | 
            -
                        target
         | 
| 59 | 
            +
                        state&.to_big_decimal&.intValue&.+(amount)&.tap { |target| command(target) }
         | 
| 93 60 | 
             
                      end
         | 
| 94 61 |  | 
| 95 62 | 
             
                      #
         | 
| @@ -120,10 +87,8 @@ module OpenHAB | |
| 120 87 | 
             
                      def coerce(other)
         | 
| 121 88 | 
             
                        logger.trace("Coercing #{self} as a request from  #{other.class}")
         | 
| 122 89 | 
             
                        case other
         | 
| 123 | 
            -
                        when Numeric
         | 
| 124 | 
            -
             | 
| 125 | 
            -
                        else
         | 
| 126 | 
            -
                          [other, state]
         | 
| 90 | 
            +
                        when Numeric then [other, state.to_big_decimal.to_d]
         | 
| 91 | 
            +
                        else [other, state]
         | 
| 127 92 | 
             
                        end
         | 
| 128 93 | 
             
                      end
         | 
| 129 94 |  | 
| @@ -140,6 +105,16 @@ module OpenHAB | |
| 140 105 | 
             
                        (self <=> other).zero?
         | 
| 141 106 | 
             
                      end
         | 
| 142 107 |  | 
| 108 | 
            +
                      #
         | 
| 109 | 
            +
                      # Define math operations
         | 
| 110 | 
            +
                      #
         | 
| 111 | 
            +
                      %i[+ - / *].each do |operator|
         | 
| 112 | 
            +
                        define_method(operator) do |other|
         | 
| 113 | 
            +
                          right, left = coerce(other)
         | 
| 114 | 
            +
                          left.send(operator, right)
         | 
| 115 | 
            +
                        end
         | 
| 116 | 
            +
                      end
         | 
| 117 | 
            +
             | 
| 143 118 | 
             
                      #
         | 
| 144 119 | 
             
                      # Check if dimmer has a state and state is not zero
         | 
| 145 120 | 
             
                      #
         | 
| @@ -159,33 +134,6 @@ module OpenHAB | |
| 159 134 | 
             
                      end
         | 
| 160 135 |  | 
| 161 136 | 
             
                      alias to_int to_i
         | 
| 162 | 
            -
             | 
| 163 | 
            -
                      #
         | 
| 164 | 
            -
                      # Return the string representation of the dimmer item
         | 
| 165 | 
            -
                      #
         | 
| 166 | 
            -
                      # @return [String] String version of the dimmer value
         | 
| 167 | 
            -
                      #
         | 
| 168 | 
            -
                      def to_s
         | 
| 169 | 
            -
                        to_i.to_s
         | 
| 170 | 
            -
                      end
         | 
| 171 | 
            -
             | 
| 172 | 
            -
                      #
         | 
| 173 | 
            -
                      # Check if dimmer is on
         | 
| 174 | 
            -
                      #
         | 
| 175 | 
            -
                      # @return [Boolean] True if item is not UNDEF or NULL and has a value greater than 0
         | 
| 176 | 
            -
                      #
         | 
| 177 | 
            -
                      def on?
         | 
| 178 | 
            -
                        state&.to_big_decimal&.intValue&.positive?
         | 
| 179 | 
            -
                      end
         | 
| 180 | 
            -
             | 
| 181 | 
            -
                      #
         | 
| 182 | 
            -
                      # Check if dimmer is off
         | 
| 183 | 
            -
                      #
         | 
| 184 | 
            -
                      # @return [Boolean] True if item is not UNDEF or NULL and has a state of 0
         | 
| 185 | 
            -
                      #
         | 
| 186 | 
            -
                      def off?
         | 
| 187 | 
            -
                        state&.to_big_decimal&.intValue&.zero?
         | 
| 188 | 
            -
                      end
         | 
| 189 137 | 
             
                    end
         | 
| 190 138 | 
             
                  end
         | 
| 191 139 | 
             
                end
         | 
| @@ -115,6 +115,15 @@ module OpenHAB | |
| 115 115 | 
             
                      def inspect
         | 
| 116 116 | 
             
                        toString
         | 
| 117 117 | 
             
                      end
         | 
| 118 | 
            +
             | 
| 119 | 
            +
                      #
         | 
| 120 | 
            +
                      # Return all groups that this item is part of
         | 
| 121 | 
            +
                      #
         | 
| 122 | 
            +
                      # @return [Array<Group>] All groups that this item is part of
         | 
| 123 | 
            +
                      #
         | 
| 124 | 
            +
                      def groups
         | 
| 125 | 
            +
                        group_names.map { |name| OpenHAB::DSL::Groups.groups[name] }
         | 
| 126 | 
            +
                      end
         | 
| 118 127 | 
             
                    end
         | 
| 119 128 |  | 
| 120 129 | 
             
                    java_import Java::OrgOpenhabCoreItems::GenericItem
         | 
| @@ -29,7 +29,7 @@ module OpenHAB | |
| 29 29 | 
             
                        def_delegator :@metadata, :value
         | 
| 30 30 |  | 
| 31 31 | 
             
                        def initialize(metadata: nil, key: nil, value: nil, config: nil)
         | 
| 32 | 
            -
                          @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value, config)
         | 
| 32 | 
            +
                          @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value&.to_s, config)
         | 
| 33 33 | 
             
                          super(@metadata&.configuration)
         | 
| 34 34 | 
             
                        end
         | 
| 35 35 |  | 
| @@ -60,9 +60,7 @@ module OpenHAB | |
| 60 60 | 
             
                        # @return [Java::Org::openhab::core::items::Metadata] the old metadata
         | 
| 61 61 | 
             
                        #
         | 
| 62 62 | 
             
                        def value=(value)
         | 
| 63 | 
            -
                           | 
| 64 | 
            -
             | 
| 65 | 
            -
                          metadata = Metadata.new(@metadata&.uID, value, @metadata&.configuration)
         | 
| 63 | 
            +
                          metadata = Metadata.new(@metadata&.uID, value&.to_s, @metadata&.configuration)
         | 
| 66 64 | 
             
                          NamespaceAccessor.registry.update(metadata) if @metadata&.uID
         | 
| 67 65 | 
             
                        end
         | 
| 68 66 |  | 
| @@ -125,7 +123,7 @@ module OpenHAB | |
| 125 123 | 
             
                          meta_value, configuration = update_from_value(value)
         | 
| 126 124 |  | 
| 127 125 | 
             
                          key = MetadataKey.new(namespace, @item_name)
         | 
| 128 | 
            -
                          metadata = Metadata.new(key, meta_value, configuration)
         | 
| 126 | 
            +
                          metadata = Metadata.new(key, meta_value&.to_s, configuration)
         | 
| 129 127 | 
             
                          # registry.get can be omitted, but registry.update will log a warning for nonexistent metadata
         | 
| 130 128 | 
             
                          if NamespaceAccessor.registry.get(key)
         | 
| 131 129 | 
             
                            NamespaceAccessor.registry.update(metadata)
         | 
| @@ -8,7 +8,23 @@ module OpenHAB | |
| 8 8 | 
             
                    # Persistence extension for Items
         | 
| 9 9 | 
             
                    #
         | 
| 10 10 | 
             
                    module Persistence
         | 
| 11 | 
            -
                       | 
| 11 | 
            +
                      # All persistence methods that could return a Quantity
         | 
| 12 | 
            +
                      QUANTITY_METHODS = %i[average_since
         | 
| 13 | 
            +
                                            delta_since
         | 
| 14 | 
            +
                                            deviation_since
         | 
| 15 | 
            +
                                            sum_since
         | 
| 16 | 
            +
                                            variance_since].freeze
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                      # All persistence methods that require a timestamp
         | 
| 19 | 
            +
                      PERSISTENCE_METHODS = (QUANTITY_METHODS +
         | 
| 20 | 
            +
                                            %i[changed_since
         | 
| 21 | 
            +
                                               evolution_rate
         | 
| 22 | 
            +
                                               historic_state
         | 
| 23 | 
            +
                                               maximum_since
         | 
| 24 | 
            +
                                               minimum_since
         | 
| 25 | 
            +
                                               updated_since]).freeze
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                      %i[persist last_update].each do |method|
         | 
| 12 28 | 
             
                        define_method(method) do |service = nil|
         | 
| 13 29 | 
             
                          service ||= persistence_service
         | 
| 14 30 | 
             
                          PersistenceExtensions.public_send(method, self, service&.to_s)
         | 
| @@ -31,35 +47,48 @@ module OpenHAB | |
| 31 47 | 
             
                        PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
         | 
| 32 48 | 
             
                      end
         | 
| 33 49 |  | 
| 34 | 
            -
                       | 
| 35 | 
            -
                        average_since
         | 
| 36 | 
            -
                        changed_since
         | 
| 37 | 
            -
                        delta_since
         | 
| 38 | 
            -
                        deviation_since
         | 
| 39 | 
            -
                        evolution_rate
         | 
| 40 | 
            -
                        historic_state
         | 
| 41 | 
            -
                        maximum_since
         | 
| 42 | 
            -
                        minimum_since
         | 
| 43 | 
            -
                        sum_since
         | 
| 44 | 
            -
                        updated_since
         | 
| 45 | 
            -
                        variance_since
         | 
| 46 | 
            -
                      ].each do |method|
         | 
| 50 | 
            +
                      PERSISTENCE_METHODS.each do |method|
         | 
| 47 51 | 
             
                        define_method(method) do |timestamp, service = nil|
         | 
| 48 52 | 
             
                          service ||= persistence_service
         | 
| 49 | 
            -
                           | 
| 50 | 
            -
             | 
| 51 | 
            -
                          end
         | 
| 52 | 
            -
                          result = PersistenceExtensions.public_send(method, self, timestamp, service&.to_s)
         | 
| 53 | 
            -
                          if result.is_a?(Java::OrgOpenhabCoreLibraryTypes::DecimalType) && respond_to?(:unit) && unit
         | 
| 54 | 
            -
                            Quantity.new(Java::OrgOpenhabCoreLibraryTypes::QuantityType.new(result.to_big_decimal, unit))
         | 
| 55 | 
            -
                          else
         | 
| 56 | 
            -
                            result
         | 
| 57 | 
            -
                          end
         | 
| 53 | 
            +
                          result = PersistenceExtensions.public_send(method, self, to_zdt(timestamp), service&.to_s)
         | 
| 54 | 
            +
                          QUANTITY_METHODS.include?(method) ? quantify(result) : result
         | 
| 58 55 | 
             
                        end
         | 
| 59 56 | 
             
                      end
         | 
| 60 57 |  | 
| 61 58 | 
             
                      private
         | 
| 62 59 |  | 
| 60 | 
            +
                      #
         | 
| 61 | 
            +
                      # Convert timestamp to ZonedDateTime if it's a TemporalAmount
         | 
| 62 | 
            +
                      #
         | 
| 63 | 
            +
                      # @param [Object] timestamp to convert
         | 
| 64 | 
            +
                      #
         | 
| 65 | 
            +
                      # @return [ZonedDateTime]
         | 
| 66 | 
            +
                      #
         | 
| 67 | 
            +
                      def to_zdt(timestamp)
         | 
| 68 | 
            +
                        if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
         | 
| 69 | 
            +
                          logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
         | 
| 70 | 
            +
                          Java::JavaTime::ZonedDateTime.now.minus(timestamp)
         | 
| 71 | 
            +
                        else
         | 
| 72 | 
            +
                          timestamp
         | 
| 73 | 
            +
                        end
         | 
| 74 | 
            +
                      end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                      #
         | 
| 77 | 
            +
                      # Convert value to Quantity if it is a DecimalType and a unit is defined
         | 
| 78 | 
            +
                      #
         | 
| 79 | 
            +
                      # @param [Object] value The value to convert
         | 
| 80 | 
            +
                      #
         | 
| 81 | 
            +
                      # @return [Object] Quantity or the original value
         | 
| 82 | 
            +
                      #
         | 
| 83 | 
            +
                      def quantify(value)
         | 
| 84 | 
            +
                        if value.is_a?(Java::OrgOpenhabCoreLibraryTypes::DecimalType) && respond_to?(:unit) && unit
         | 
| 85 | 
            +
                          logger.trace("Unitizing #{value} with unit #{unit}")
         | 
| 86 | 
            +
                          Quantity.new(Java::OrgOpenhabCoreLibraryTypes::QuantityType.new(value.to_big_decimal, unit))
         | 
| 87 | 
            +
                        else
         | 
| 88 | 
            +
                          value
         | 
| 89 | 
            +
                        end
         | 
| 90 | 
            +
                      end
         | 
| 91 | 
            +
             | 
| 63 92 | 
             
                      #
         | 
| 64 93 | 
             
                      # Get the specified persistence service from the current thread local variable
         | 
| 65 94 | 
             
                      #
         | 
| @@ -243,21 +243,6 @@ module OpenHAB | |
| 243 243 | 
             
                      false
         | 
| 244 244 | 
             
                    end
         | 
| 245 245 |  | 
| 246 | 
            -
                    #
         | 
| 247 | 
            -
                    # Patch event to decorate event.item with our item wrapper
         | 
| 248 | 
            -
                    #
         | 
| 249 | 
            -
                    # @param [OpenHAB Event] event patch
         | 
| 250 | 
            -
                    #
         | 
| 251 | 
            -
                    def decorate_event_item(event)
         | 
| 252 | 
            -
                      return if event.nil?
         | 
| 253 | 
            -
             | 
| 254 | 
            -
                      class << event
         | 
| 255 | 
            -
                        def item
         | 
| 256 | 
            -
                          OpenHAB::Core::EntityLookup.lookup_item(item_name)
         | 
| 257 | 
            -
                        end
         | 
| 258 | 
            -
                      end
         | 
| 259 | 
            -
                    end
         | 
| 260 | 
            -
             | 
| 261 246 | 
             
                    #
         | 
| 262 247 | 
             
                    # Process the run queue
         | 
| 263 248 | 
             
                    #
         | 
| @@ -287,7 +272,6 @@ module OpenHAB | |
| 287 272 | 
             
                    #
         | 
| 288 273 | 
             
                    #
         | 
| 289 274 | 
             
                    def process_otherwise_task(event, task)
         | 
| 290 | 
            -
                      decorate_event_item(event)
         | 
| 291 275 | 
             
                      logger.trace { "Executing rule '#{name}' otherwise block with event(#{event})" }
         | 
| 292 276 | 
             
                      task.block.call(event)
         | 
| 293 277 | 
             
                    end
         | 
| @@ -314,9 +298,10 @@ module OpenHAB | |
| 314 298 | 
             
                    #
         | 
| 315 299 | 
             
                    #
         | 
| 316 300 | 
             
                    def process_trigger_task(event, task)
         | 
| 317 | 
            -
                       | 
| 318 | 
            -
             | 
| 319 | 
            -
                       | 
| 301 | 
            +
                      return unless event&.item
         | 
| 302 | 
            +
             | 
| 303 | 
            +
                      logger.trace { "Executing rule '#{name}' trigger block with item (#{event.item})" }
         | 
| 304 | 
            +
                      task.block.call(event.item)
         | 
| 320 305 | 
             
                    end
         | 
| 321 306 |  | 
| 322 307 | 
             
                    #
         | 
| @@ -327,7 +312,6 @@ module OpenHAB | |
| 327 312 | 
             
                    #
         | 
| 328 313 | 
             
                    #
         | 
| 329 314 | 
             
                    def process_run_task(event, task)
         | 
| 330 | 
            -
                      decorate_event_item(event)
         | 
| 331 315 | 
             
                      logger.trace { "Executing rule '#{name}' run block with event(#{event})" }
         | 
| 332 316 | 
             
                      task.block.call(event)
         | 
| 333 317 | 
             
                    end
         | 
    
        data/lib/openhab/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: openhab-scripting
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 2. | 
| 4 | 
            +
              version: 2.24.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Brian O'Connell
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2021-02- | 
| 11 | 
            +
            date: 2021-02-23 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         | 
| @@ -41,6 +41,7 @@ files: | |
| 41 41 | 
             
            - lib/openhab/dsl/group.rb
         | 
| 42 42 | 
             
            - lib/openhab/dsl/items/datetime_item.rb
         | 
| 43 43 | 
             
            - lib/openhab/dsl/items/item_command.rb
         | 
| 44 | 
            +
            - lib/openhab/dsl/items/item_delegate.rb
         | 
| 44 45 | 
             
            - lib/openhab/dsl/items/items.rb
         | 
| 45 46 | 
             
            - lib/openhab/dsl/items/number_item.rb
         | 
| 46 47 | 
             
            - lib/openhab/dsl/items/rollershutter_item.rb
         | 
| @@ -49,6 +50,8 @@ files: | |
| 49 50 | 
             
            - lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb
         | 
| 50 51 | 
             
            - lib/openhab/dsl/monkey_patch/events/events.rb
         | 
| 51 52 | 
             
            - lib/openhab/dsl/monkey_patch/events/item_command.rb
         | 
| 53 | 
            +
            - lib/openhab/dsl/monkey_patch/events/item_event.rb
         | 
| 54 | 
            +
            - lib/openhab/dsl/monkey_patch/events/item_state.rb
         | 
| 52 55 | 
             
            - lib/openhab/dsl/monkey_patch/events/item_state_changed.rb
         | 
| 53 56 | 
             
            - lib/openhab/dsl/monkey_patch/events/thing_status_info.rb
         | 
| 54 57 | 
             
            - lib/openhab/dsl/monkey_patch/items/contact_item.rb
         |