Skip to main content

Switch

The Switch interface represents switch entities in Home Assistant. When you define a field of type Switch in your SmartBean class and annotate it with @Entity, the framework automatically injects an object that lets you control the corresponding switch entity in Home Assistant. This provides a type-safe way to interact with your smart switches.

@Entity("switch.outlet")
private Switch outlet;

State

The switch's state is represented by the Switch.State enum, which mirrors the possible states in Home Assistant: ON, OFF, UNKNOWN and UNAVAILABLE. You can query the switch's current state using these methods:

MethodDescription
getState()Get the current state of the switch.
isOn()Shortcut to check if the switch is currently switched on.
getStateAsString()Returns the original state from the Home Assistant entity.

Attributes

The following attributes of a switch entity can be accessed through simple getter methods:

HA attributeMethodDescription
friendly_namegetFriendlyName()Friendly name of the entity.
icongetIcon()Icon of the entity.

You can access any additional attributes that are not directly supported through the getAttributes() method.

Services

The Switch interface provides several methods to control your switch through Home Assistant services. Here are the supported operations:

HA serviceMethodDescription
switch.turn_onturnOn()Turns on the switch.
switch.turn_offturnOff()Turns off the switch.
switch.toggletoggle()Toggles the switch.

Example

public class ASampleBean implements SmartBean {

@Entity("switch.outlet")
private Switch outlet;

public void someBeanMethod() {
outlet.toggle();
}
}

Access Entities Programmatically

In addition to the annotation-based approach, you can programmatically access switch entities using the getSwitch() method of the SmartBeans API. You might prefer this programmatic approach over annotations for example when the entity ID is dynamically generated through business logic and cannot be determined at compile time.

public class ASampleBean implements SmartBean {

private SmartBeans sb;

public void someBeanMethod() {
Switch outlet = sb.getSwitch("switch.outlet");
if(outlet.isOn()) {
outlet.turnOff();
}
}
}
note

For improved efficiency, it is recommended to cache entity objects as member variables rather than retrieving them repeatedly. Since the state and attributes of entity objects are cached internally, no additional backend communication is required for multiple state retrievals. Note that initial entity object creation always requires at least one request to the Home Assistant backend.