From 3f92519a1a71136a4bdcb880a7fa2563e2d52219 Mon Sep 17 00:00:00 2001 From: jimtng <2554958+jimtng@users.noreply.github.com> Date: Fri, 8 Sep 2023 08:15:10 +1000 Subject: [PATCH] Add event attributes for TimerEvent (#131) This may be useful especially for UI rules Signed-off-by: Jimmy Tanagra --- lib/openhab/core/events/abstract_event.rb | 11 +++++ lib/openhab/core/events/timer_event.rb | 48 ++++++++++++++++++++ spec/openhab/core/events/timer_event_spec.rb | 33 ++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 lib/openhab/core/events/timer_event.rb create mode 100644 spec/openhab/core/events/timer_event_spec.rb diff --git a/lib/openhab/core/events/abstract_event.rb b/lib/openhab/core/events/abstract_event.rb index 23cb7147d9..45f87a58eb 100644 --- a/lib/openhab/core/events/abstract_event.rb +++ b/lib/openhab/core/events/abstract_event.rb @@ -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 diff --git a/lib/openhab/core/events/timer_event.rb b/lib/openhab/core/events/timer_event.rb new file mode 100644 index 0000000000..256adf2303 --- /dev/null +++ b/lib/openhab/core/events/timer_event.rb @@ -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 diff --git a/spec/openhab/core/events/timer_event_spec.rb b/spec/openhab/core/events/timer_event_spec.rb new file mode 100644 index 0000000000..6111fde393 --- /dev/null +++ b/spec/openhab/core/events/timer_event_spec.rb @@ -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