-
-
Notifications
You must be signed in to change notification settings - Fork 471
State machine to model lights in Thing handlers #4995
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
Merged
Merged
Changes from 62 commits
Commits
Show all changes
74 commits
Select commit
Hold shift + click to select a range
104712e
Create LightUtil.java
andrewfg caf4219
javadoc and min/max code style
andrewfg 64c68d9
spotless
andrewfg ed4f05c
fix javadoc errors
andrewfg f50fa22
use quantitytype for color temperatures
andrewfg 4d591f1
refactoring
andrewfg 12299f6
bug fix
andrewfg 3509a44
Create LightStateMachine.java
andrewfg 1fbc5db
Merge remote-tracking branch 'upstream/main' into light-helpers
andrewfg 875e6c7
work in progress
andrewfg 5aab892
unit tests and refactoring
andrewfg 931dcf1
refactoring and testing
andrewfg 1d2edc0
fix typos
andrewfg 4ec5a81
Merge branch 'openhab:main' into light-helpers
andrewfg ce30be9
fix typos
andrewfg e0d9c11
various
andrewfg 95db4ad
tweaks to javadoc
andrewfg 774d6db
fix javadoc build error
andrewfg aa04db2
remove more bad link fields
andrewfg 7d46b42
implement OH sparse channel policy
andrewfg f53b895
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg 10ab72c
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg 2dc69ab
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg 61846cd
fix spotless and copilot suggestions
andrewfg 7e4d837
rename methods
andrewfg 7ec362a
first stab at RGBCW support
andrewfg e3d2544
fix javadoc issues
andrewfg 8bc9750
multiple fixes
andrewfg 6e1a1e1
fix javadoc error
andrewfg 3dbedd1
rework RGBCW and tests; refactor into one class
andrewfg e97fddb
more javadoc stuff
andrewfg d115ef6
fix copilot suggestions
andrewfg 71c6702
add TODO for RGBCW JUnit test cases
andrewfg 8966895
RGBCW tests and fixes
andrewfg de8f1d5
fix javadoc error
andrewfg 78af403
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg 39266a6
refactor RGBCW; write correct unit tests
andrewfg 211abd4
refactor RgbcwMath for possible move to ColorUtil
andrewfg 2a89dd1
fix javadoc and typo
andrewfg 170d591
simplify rgb data type configuration
andrewfg 8d958cc
fix javadoc link fields
andrewfg 76aa10d
adopt copilot suggestions
andrewfg 3f31050
improve rgb to rgbcw conversion
andrewfg 3519450
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg 330f396
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg 05c4076
adopt copilot suggestion about comparing enums
andrewfg beb0b3c
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg bd55090
adopt more copilot suggestions
andrewfg fa67275
tweak
andrewfg d683058
add BaseLightThingHandler prototype
andrewfg 9c2a779
javadoc and copilot stuff
andrewfg 3182226
Update bundles/org.openhab.core/src/main/java/org/openhab/core/util/L…
andrewfg 2987971
typos; javadoc; clone method
andrewfg aa5093b
adopt reviewer suggestions
andrewfg 63e0623
fix javadoc
andrewfg 3b05f61
Merge remote-tracking branch 'upstream/main' into light-helpers
andrewfg 4598e5a
support various led operating modes; add javadoc
andrewfg 2c84d8b
tracking implementation in HomeKit addon
andrewfg c68c722
tracking HomeKit addon proof of concept
andrewfg 0ffc2bb
Merge branch 'openhab:main' into light-helpers
andrewfg ecf4b2a
Merge branch 'openhab:main' into light-helpers
andrewfg 87b8855
Merge branch 'openhab:main' into light-helpers
andrewfg 9c3d0b9
update date headers
andrewfg e05556f
Merge remote-tracking branch 'upstream/main' into light-helpers
andrewfg d93f592
adopt reviewer suggestions
andrewfg 684d821
avoid divide by zero case
andrewfg c844ce2
various
andrewfg 5b6683c
Merge branch 'openhab:main' into light-helpers
andrewfg d8bd8b0
tweak javadoc
andrewfg 489e300
Merge branch 'openhab:main' into light-helpers
andrewfg d88dbaf
Merge remote-tracking branch 'upstream/main' into light-helpers
andrewfg 2772cd5
adopt copilot suggestions
andrewfg 4e92ddc
typo
andrewfg 2058858
handle some real and some faffy copilot comments
andrewfg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
215 changes: 215 additions & 0 deletions
215
...penhab.core.thing/src/main/java/org/openhab/core/thing/binding/BaseLightThingHandler.java
This file contains hidden or 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,215 @@ | ||
| /* | ||
| * Copyright (c) 2010-2025 Contributors to the openHAB project | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0 | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| */ | ||
| package org.openhab.core.thing.binding; | ||
|
|
||
| import org.eclipse.jdt.annotation.NonNullByDefault; | ||
| import org.openhab.core.thing.ChannelUID; | ||
| import org.openhab.core.thing.Thing; | ||
| import org.openhab.core.types.Command; | ||
| import org.openhab.core.util.LightModel; | ||
|
|
||
| /** | ||
| * {@link BaseLightThingHandler} provides an abstract base implementation for a {@link ThingHandler} for a light. | ||
| * | ||
| * @author Andrew Fiddian-Green - Initial contribution | ||
| */ | ||
| @NonNullByDefault | ||
| public abstract class BaseLightThingHandler extends BaseThingHandler { | ||
|
|
||
| /** | ||
| * Light state machine model to manage light capabilities, configuration, and runtime state | ||
| */ | ||
| private final LightModel model = new LightModel(); | ||
|
andrewfg marked this conversation as resolved.
Outdated
|
||
|
|
||
| public BaseLightThingHandler(Thing thing) { | ||
| super(thing); | ||
| } | ||
|
|
||
| /** | ||
| * Override this method to handle commands from OH core. | ||
| * <p> | ||
| * Example: (implementation will depend on the specific binding and device). | ||
| * | ||
| * <pre> | ||
| * {@code | ||
| * | ||
| * // update the model state based on the command from OpenHAB | ||
| * model.handleCommand(command); | ||
| * | ||
| * // or if it is a color temperature command | ||
| * model.handleColorTemperatureCommand(command); | ||
| * | ||
| * // and transmit the appropriate command to the remote light device based on the model state | ||
| * doTransmitBindingSpecificRemoteLightData(model); | ||
| * | ||
| * } | ||
| * </pre> | ||
| */ | ||
| @Override | ||
| public abstract void handleCommand(ChannelUID channelUID, Command command); | ||
|
|
||
| /** | ||
| * Override this method to provide initialization of the light state machine capabilities and configuration | ||
| * parameters. | ||
| * <p> | ||
| * Example: (implementation will depend on the specific binding and device). | ||
| * | ||
| * <pre> | ||
| * {@code | ||
| * | ||
| * // STEP 1: Set up the light state machine capabilities. | ||
| * model.configSetLightCapabilities(LightCapabilities.COLOR_WITH_COLOR_TEMPERATURE); | ||
| * | ||
| * // STEP 2: optionally set up the light state machine configuration parameters. | ||
| * // These would typically be read from the thing configuration or read from the remote device. | ||
| * model.configSetRgbDataType(RgbDataType.RGB_NO_BRIGHTNESS); // RGB data type | ||
| * model.configSetMinimumOnBrightness(2); // minimum brightness level in % when on | ||
| * model.configSetIncreaseDecreaseStep(10); // step size for increase/decrease commands | ||
| * model.configSetMiredControlCoolest(153); // color temperature control range coolest | ||
| * model.configSetMiredControlWarmest(500); // color temperature control range warmest | ||
| * | ||
| * // STEP 3: optionally if the light has warm and cool white LEDS then set up their LED color temperatures. | ||
| * // These would typically be read from the thing configuration or read from the remote device. | ||
| * model.configSetMiredCoolWhiteLED(153); | ||
| * model.configSetMiredWarmWhiteLED(500); | ||
|
andrewfg marked this conversation as resolved.
Outdated
|
||
| * | ||
| * // STEP 4: now set the status to UNKNOWN to indicate that we are initialized | ||
| * updateStatus(ThingStatus.UNKNOWN); | ||
| * | ||
| * // STEP 5: finally provide further initialization, e.g. connecting to the remote device | ||
| * ... | ||
| * | ||
| * } | ||
| * </pre> | ||
| */ | ||
| @Override | ||
| public abstract void initialize(); | ||
|
|
||
| /** | ||
| * Transmit the appropriate command to the remote light device based on the model state. | ||
| * This method must be overridden in the concrete implementation to transmit the appropriate command(s) | ||
| * to the remote light device based on the model state. | ||
| * <p> | ||
| * Example: (implementation will depend on the specific binding and device). | ||
| * | ||
| * <pre> | ||
| * {@code | ||
| * | ||
| * if (model.getOnOff() == OnOffType.ON) { | ||
| * transmit command to turn on the light | ||
| * } else { | ||
| * transmit command to turn off the light | ||
| * } | ||
| * | ||
| * if (model.getBrightness() != null) { | ||
| * transmit command to set brightness to model.getBrightness() | ||
| * } | ||
| * | ||
| * if (model.getColor() != null) { | ||
| * transmit command to set color to model.getColor() | ||
| * } | ||
| * | ||
| * if (model.getColorTemperature() != null) { | ||
| * transmit command to set color temperature to model.getColorTemperature() | ||
| * } | ||
| * | ||
| * if (model.getColorTemperaturePercent() != null) { | ||
| * transmit command to set color temperature percent to model.getColorTemperaturePercent() | ||
| * } | ||
| * | ||
| * if (model.getRGBx().length == 3) { | ||
| * transmit command to set RGB value to model.getRGBx() | ||
| * } | ||
| * | ||
| * if (model.getXY() != null) { | ||
| * transmit command to set XY value to model.getXY() | ||
| * } | ||
| * } | ||
|
andrewfg marked this conversation as resolved.
Outdated
|
||
| * </pre> | ||
| * | ||
| * @param model the light model containing the current state | ||
| */ | ||
| protected abstract void doTransmitBindingSpecificRemoteLightData(LightModel model); | ||
|
|
||
| /** | ||
| * Receive data from the remote light device and update the model state accordingly. | ||
| * This method must be overridden in the concrete implementation to 1) receive data from the remote light device | ||
| * 2) update the model state accordingly, and 3) update the openHAB channels. | ||
| * <P> | ||
| * Example: (implementation will depend on the specific binding and device). | ||
| * | ||
| * <pre> | ||
| * {@code | ||
| * | ||
| * STEP 1: Parse the remoteData to extract the relevant information. Depends on specific binding / device | ||
| * | ||
| * OnOffType onOff = ...; // extract on/off state from remoteData | ||
| * Integer brightness = ...; // extract brightness from remoteData | ||
| * HSBType color = ...; // extract color from remoteData | ||
| * Integer colorTemperature = ...; // extract color temperature from remoteData | ||
| * Integer colorTemperaturePercent = ...; // extract color temperature percent from remoteData | ||
| * RGBType rgb = ...; // extract RGB value from remoteData | ||
| * XYType xy = ...; // extract XY value from remoteData | ||
| * | ||
| * STEP 2: Update the model state based on the received data | ||
| * | ||
| * if (onOff != null) { | ||
| * model.setOnOff(onOff); | ||
|
andrewfg marked this conversation as resolved.
Outdated
|
||
| * } | ||
| * | ||
| * if (brightness != null) { | ||
| * model.setBrightness(brightness); | ||
| * } | ||
| * | ||
| * if (color != null) { | ||
| * model.setColor(color); | ||
|
andrewfg marked this conversation as resolved.
|
||
| * } | ||
| * | ||
| * if (colorTemperature != null) { | ||
| * model.setColorTemperature(colorTemperature); | ||
| * } | ||
| * | ||
| * if (colorTemperaturePercent != null) { | ||
| * model.setColorTemperaturePercent(colorTemperaturePercent); | ||
|
andrewfg marked this conversation as resolved.
Outdated
|
||
| * } | ||
| * | ||
| * if (rgb != null) { | ||
| * model.setRGBx(rgb); | ||
| * } | ||
| * | ||
| * if (xy != null) { | ||
| * model.setXY(xy); | ||
| * } | ||
| * | ||
| * STEP 3: After updating the model, update the channel states in OpenHAB | ||
| * Note: Ensure that the channel IDs used in updateState() match those defined in the thing type. | ||
| * | ||
| * if (model.configGetLightCapabilities().supportsColor()) { | ||
| * updateState(CHANNEL_COLOR, model.getColor()); | ||
| * } else if (model.configGetLightCapabilities().supportsBrightness()) { | ||
| * updateState(CHANNEL_BRIGHTNESS, model.getBrightness()); | ||
| * } else { | ||
| * updateState(CHANNEL_ON_OFF, model.getOnOff()); | ||
| * } | ||
| * | ||
| * if (model.configGetLightCapabilities().supportsColorTemperature()) { | ||
| * updateState(CHANNEL_COLOR_TEMPERATURE_ABS, model.getColorTemperature()); | ||
| * updateState(CHANNEL_COLOR_TEMPERATURE_PERCENT, model.getColorTemperaturePercent()); | ||
| * } | ||
| * } | ||
| * </pre> | ||
| * | ||
| * @param remoteData the data received from the remote light device | ||
| */ | ||
| protected abstract void onReceiveBindingSpecificRemoteLightData(Object... remoteData); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.