Skip to main content

Switch Entity

A switch entity turns on or off something, for example a relay. Derive a platform entity from homeassistant.components.switch.SwitchEntity. To represent something which can be turned on or off but can't be controlled, for example a wall switch which transmits its state but can't be turned on or off from Home Assistant, a Binary Sensor is a better choice. To represent something which doesn't have a state, for example a door bell push button, a custom event or a Device Trigger is a better choice.

Properties

tip

Properties should always only return information from memory and not do I/O (like network requests). Implement update() or async_update() to fetch data.

NameTypeDefaultDescription
is_onbooleanNoneIf the switch is currently on or off.

Deprecated Properties

The following properties are deprecated and should not be used by new integrations. Provide them as sensors instead.

NameTypeDefaultDescription
current_power_wfloatNoneThe current power usage in W.
today_energy_kwhfloatNoneTotal energy usage in kWh.

Methods

Turn On

Turn the switch on.

class MySwitch(SwitchEntity):
# Implement one of these methods.

def turn_on(self, **kwargs) -> None:
"""Turn the entity on."""

async def async_turn_on(self, **kwargs):
"""Turn the entity on."""

Turn Off

Turn the switch off.

class MySwitch(SwitchEntity):
# Implement one of these methods.

def turn_off(self, **kwargs):
"""Turn the entity off."""

async def async_turn_off(self, **kwargs):
"""Turn the entity off."""

Toggle

Optional. If not implemented will default to checking what method to call using the is_on property.

class MySwitch(SwitchEntity):
# Implement one of these methods.

def toggle(self, **kwargs):
"""Toggle the entity."""

async def async_toggle(self, **kwargs):
"""Toggle the entity."""

Available device classes

Optional. What type of device this. It will possibly map to google device types.

ConstantDescription
SwitchDeviceClass.OUTLETDevice is an outlet for power.
SwitchDeviceClass.SWITCHDevice is switch for some type of entity.