Package com.codename1.home
A cross-platform smart-home API: read the home's accessories, read and write what they can do, watch them for change, run scenes, and add new Matter accessories.
SmartHome.getInstance() is the single entry point and
never returns null. Ports without smart-home support return a fallback
whose operations fail fast with HomeError.NOT_SUPPORTED
and whose graph accessors return empty lists, so calling code needs no
platform-specific if.
The model
A HomeStructure holds HomeRooms
and Accessorys. An accessory has one or more
AccessoryServices -- a two-gang wall switch is one
accessory with two services -- and a service exposes
Traits. A trait is a canonical capability, not a
platform identifier: Trait.BRIGHTNESS is the same
constant whether the accessory is behind HomeKit or Matter, and the port
maps it to HMCharacteristicTypeBrightness or Level Control's
CurrentLevel on your behalf.
Graph objects are immutable snapshots. Nothing on an
Accessory crosses into the platform when you call a
getter; when the topology moves, a
HomeStructureListener tells you to fetch it again.
Three things that will surprise you
Android's default answer is not "available". With no extra setup an
Android app can commission a Matter accessory into the user's Google Home
and can do nothing else -- the graph is empty and no trait can be read or
written. That state is
HomeAvailability.COMMISSIONING_ONLY, and it is
reported honestly rather than dressed up as a working home. The full
accessory graph on Android needs the Google Home APIs, which need a Google
Cloud project and a Home Developer Console registration that only you can
create. See SmartHome.getConfigurationProblems().
Nothing wakes your app for an accessory change. HomeKit delivers
changes only while your app is running in the foreground, and the Google
Home APIs need a live signed-in client. The home hub, not your app, is what
runs automations while the phone sleeps. Ask
TraitSubscription.isPushDelivery() rather than
assuming; where it answers false, changes arrive when you call
SmartHome.drainChanges() and at no other time.
Commissioning may not give you a device you can control. Adding an
accessory through Google Play services puts it in the user's Google Home
and tells your app nothing more. Check
CommissioningResult.wasCommissionedToThisApp()
instead of assuming the returned accessory id is usable.
Threading
Every method may be called from the EDT and returns immediately. Every result and every listener delivery arrives on the EDT, on every platform -- including the desktop, simulator and JavaScript ports, which marshal rather than answering on whichever thread happened to ask. A callback may touch components directly.
Not claimed in this release
Automations, triggers and conditions (scenes only); topology writes
(creating homes, renaming rooms, moving accessories); cameras and video;
security and alarm panels; Matter events, which is why
LockState.JAMMED is unreachable outside HomeKit;
energy, appliance and diagnostic clusters. Codename One is not a Matter
controller: everything Matter goes through the OS ecosystem, so the Apple
Home or Google Home app has to be installed and set up.
-
ClassDescriptionOne physical device in the home: HomeKit's
HMAccessory, a Google Home device, one Matter node.Roughly what anAccessoryis, for picking an icon and grouping a list.One functional endpoint of anAccessory: HomeKit'sHMService, one Matter endpoint.A summarized air-quality rating, forTrait.AIR_QUALITY.The state of a life-safety alarm, forTrait.SMOKE_DETECTEDandTrait.CO_DETECTED.Whether an accessory's battery is charging, forTrait.BATTERY_CHARGING.The state of a door or garage door, forTrait.DOOR_STATEandTrait.TARGET_DOOR_STATE.How a fan is running, forTrait.FAN_MODE.What a thermostat is doing, forTrait.CURRENT_HEATING_COOLING, or what it has been asked to do, forTrait.TARGET_HEATING_COOLING.Whether the user has granted this app access to their home.Whether a home graph is usable right now, and when it is not, why.Which platform service is behind thisSmartHomeinstance.Told when a watchedTraitchanges value.Raised when the app itself is missing something the backend needs -- an entitlement, a build hint, a Google Cloud project id -- rather than the user or the device being at fault.Typed failure reasons carried byHomeException.The failure delivered through anAsyncResourcewhen a smart-home operation does not succeed.A room in aHomeStructure: HomeKit'sHMRoom, a Google Home room.One home: HomeKit'sHMHome, a Google Home structure.Notification that the home graph moved, delivered to aHomeStructureListeneron the EDT.Told when the home graph moves -- an accessory added, removed, renamed, moved between rooms, or its reachability changing.A named grouping of rooms -- "upstairs", "the annexe": HomeKit'sHMZone.The state of a door lock, forTrait.LOCK_STATEandTrait.TARGET_LOCK_STATE.Which way a window covering is moving, forTrait.COVERING_MOTION.A named set of accessory states that can be applied in one go: HomeKit'sHMActionSet, a Google Home scene.What kind ofScenethis is.What oneAccessoryServiceis: HomeKit'sHMServicetype, Matter's device type on an endpoint.Entry point for the Codename One smart-home API -- reading the accessories in a user's home, reading and writing what they can do, watching them for change, running scenes, and adding new Matter accessories.What moved in the home, forHomeStructureEvent.Which traits to watch, and how often you are willing to hear about them.One capability an accessory can have: a light's brightness, a lock's state, a sensor's reading.A coalesced set of trait changes, delivered to aHomeChangeListeneron the EDT.What one particular accessory will actually accept for one particularTrait: whether it can be read or written, the range it works over, the step it moves in, and for an enum trait, which constants it can produce.One trait's value at one moment, or the reason there isn't one.Which traits to read, and how fresh they have to be.A live watch on a set of traits, returned bySmartHome.subscribe(SubscriptionRequest, HomeChangeListener).A unit of measure for aTraitvalue.What aTraitUnitmeasures.One value of oneTrait: immutable, typed, and carrying its unit.What sort of value aTraitcarries, and therefore whichTraitValuegetter is the one that works.One change to make: set oneTraiton oneAccessoryServiceto one value.What happened to oneTraitWrite.