-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event attributes for TimerEvent (#131)
This may be useful especially for UI rules Signed-off-by: Jimmy Tanagra <[email protected]>
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |