Skip to content

Commit

Permalink
Add event attributes for TimerEvent (#131)
Browse files Browse the repository at this point in the history
This may be useful especially for UI rules

Signed-off-by: Jimmy Tanagra <[email protected]>
  • Loading branch information
jimtng authored Sep 7, 2023
1 parent 643bc8b commit 3f92519
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/openhab/core/events/abstract_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ class AbstractEvent

# @return [String]
alias_method :inspect, :to_s

#
# Returns the event payload as a Hash.
#
# @return [Hash, nil] The payload object parsed by JSON. The keys are symbolized.
# `nil` when the payload is empty.
#
def payload
require "json"
@payload ||= JSON.parse(get_payload, symbolize_names: true) unless get_payload.empty?
end
end
end
end
Expand Down
48 changes: 48 additions & 0 deletions lib/openhab/core/events/timer_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module OpenHAB
module Core
module Events
# @deprecated OH3.4 this guard is not needed on OH4
if Gem::Version.new(OpenHAB::Core::VERSION) >= Gem::Version.new("4.0.0")
java_import org.openhab.core.automation.events.TimerEvent

#
# Adds methods to core openHAB TimerEvent to make it more natural in Ruby
#
# This event can be triggered by a `DateTimeTrigger`, `cron`, or `TimeOfDay` trigger.
#
# @since openHAB 4.0
#
class TimerEvent < AbstractEvent
#
# @!attribute [r] cron_expression
# @return [String, nil] The cron expression that triggered this event.
# `nil` when this event wasn't triggered by a cron trigger.
#
def cron_expression
payload&.[](:cronExpression)
end

#
# @!attribute [r] item
# @return [Item, nil] The DateTime item that triggered this event.
# `nil` when this event wasn't triggered by a DateTimeItem trigger.
#
def item
payload&.[](:itemName)&.then { |item_name| EntityLookup.lookup_item(item_name) }
end

#
# @!attribute [r] time
# @return [LocalTime, nil] The configured time for this TimeOfDay trigger event.
# `nil` when this event wasn't triggered by a TimeOfDay trigger.
#
def time
payload&.[](:time)&.then { |time| LocalTime.parse(time) }
end
end
end
end
end
end
33 changes: 33 additions & 0 deletions spec/openhab/core/events/timer_event_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# @deprecated OH3.4 - this guard is not needed on OH4
if Gem::Version.new(OpenHAB::Core::VERSION) >= Gem::Version.new("4.0.0")

RSpec.describe OpenHAB::Core::Events::TimerEvent do
describe "#cron_expression" do
it "works" do
expression = (Time.now + 2).strftime("%S %M %H ? * ?")
event = described_class.new("topic", %({"cronExpression":"#{expression}"}), nil)
expect(event.cron_expression).to eql expression
end
end

describe "#item" do
it "works" do
items.build { date_time_item MyDateTimeItem }
event = described_class.new("topic", %({"itemName":"MyDateTimeItem"}), nil)
expect(event.item).to be_an(Item)
expect(event.item).to eql MyDateTimeItem
end
end

describe "#time" do
it "works" do
event = described_class.new("topic", %({"time":"12:34"}), nil)
expect(event.time).to be_a(java.time.LocalTime)
expect(event.time).to eq LocalTime.parse("12:34")
end
end
end

end

0 comments on commit 3f92519

Please sign in to comment.