Skip to main content
The switch platform represents entities that can be turned on or off. Switches are used for outlets, wall switches, and other binary controllable devices.

Base Class

All switch entities inherit from SwitchEntity, which extends ToggleEntity. The class is defined in homeassistant.components.switch.

Entity Description

Switch entities can optionally use SwitchEntityDescription to define static metadata:

Required Methods

turn_on

Turn the switch on.

turn_off

Turn the switch off.

Optional Properties

is_on

Return the current state of the switch.

device_class

Indicates the type of switch.
Available device classes:
  • OUTLET - Power outlet
  • SWITCH - Generic switch

Services

The switch platform automatically registers these services:
  • switch.turn_on - Turn the switch on
  • switch.turn_off - Turn the switch off
  • switch.toggle - Toggle the switch state

Example Implementation

Async Implementation

For async devices, override the async methods:

Real-World Example

From the demo integration (homeassistant/components/demo/switch.py):

Toggle Support

Switches automatically support toggling via the inherited ToggleEntity base class:

Assumed State

For devices where the state cannot be read:

Optimistic Updates

For fast UI response while waiting for device confirmation:

Important Notes

  • Always call schedule_update_ha_state() (sync) or async_write_ha_state() (async) after changing state
  • Switches inherit from ToggleEntity, which provides the toggle functionality
  • Set _attr_should_poll = False for switches that receive push updates
  • Use _attr_assumed_state = True when you can’t read back the actual state
  • The turn_on and turn_off methods should update _attr_is_on to reflect the new state

State Management

Switches can manage state in different ways:

Polling (Default)

Push Updates

See Also