mirror of https://github.com/home-assistant/core
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""Support for switches through the SmartThings cloud API."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Sequence
|
|
from typing import Any
|
|
|
|
from pysmartthings import Capability
|
|
|
|
from homeassistant.components.switch import SwitchEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .const import DATA_BROKERS, DOMAIN
|
|
from .entity import SmartThingsEntity
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Add switches for a config entry."""
|
|
broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
|
|
async_add_entities(
|
|
SmartThingsSwitch(device)
|
|
for device in broker.devices.values()
|
|
if broker.any_assigned(device.device_id, "switch")
|
|
)
|
|
|
|
|
|
def get_capabilities(capabilities: Sequence[str]) -> Sequence[str] | None:
|
|
"""Return all capabilities supported if minimum required are present."""
|
|
# Must be able to be turned on/off.
|
|
if Capability.switch in capabilities:
|
|
return [Capability.switch, Capability.energy_meter, Capability.power_meter]
|
|
return None
|
|
|
|
|
|
class SmartThingsSwitch(SmartThingsEntity, SwitchEntity):
|
|
"""Define a SmartThings switch."""
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
"""Turn the switch off."""
|
|
await self._device.switch_off(set_status=True)
|
|
# State is set optimistically in the command above, therefore update
|
|
# the entity state ahead of receiving the confirming push updates
|
|
self.async_write_ha_state()
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
"""Turn the switch on."""
|
|
await self._device.switch_on(set_status=True)
|
|
# State is set optimistically in the command above, therefore update
|
|
# the entity state ahead of receiving the confirming push updates
|
|
self.async_write_ha_state()
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
"""Return true if light is on."""
|
|
return self._device.status.switch
|