From c97817bb0eaa7595d1c7fc0fffbc7192132f2e67 Mon Sep 17 00:00:00 2001 From: Jafar Atili Date: Thu, 22 Sep 2022 15:20:32 +0300 Subject: [PATCH] Add Button platform to switchbee integration (#78386) * Added Button platform to switchbee integration * fixed review comments * Addressed CR comments * Update homeassistant/components/switchbee/button.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/switchbee/button.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/switchbee/button.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * removed the zone name from the entity name * Re-add space Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- .coveragerc | 1 + .../components/switchbee/__init__.py | 2 +- homeassistant/components/switchbee/button.py | 50 +++++++++++++++++++ .../components/switchbee/coordinator.py | 1 + 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/switchbee/button.py diff --git a/.coveragerc b/.coveragerc index fa31616541d..54d79c3aeda 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1218,6 +1218,7 @@ omit = homeassistant/components/swiss_public_transport/sensor.py homeassistant/components/swisscom/device_tracker.py homeassistant/components/switchbee/__init__.py + homeassistant/components/switchbee/button.py homeassistant/components/switchbee/const.py homeassistant/components/switchbee/coordinator.py homeassistant/components/switchbee/switch.py diff --git a/homeassistant/components/switchbee/__init__.py b/homeassistant/components/switchbee/__init__.py index a5523c51a7d..12cce234bf1 100644 --- a/homeassistant/components/switchbee/__init__.py +++ b/homeassistant/components/switchbee/__init__.py @@ -13,7 +13,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .const import DOMAIN from .coordinator import SwitchBeeCoordinator -PLATFORMS: list[Platform] = [Platform.SWITCH] +PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SWITCH] async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: diff --git a/homeassistant/components/switchbee/button.py b/homeassistant/components/switchbee/button.py new file mode 100644 index 00000000000..fc2c7373b7e --- /dev/null +++ b/homeassistant/components/switchbee/button.py @@ -0,0 +1,50 @@ +"""Support for SwitchBee scenario button.""" + +from switchbee.api import SwitchBeeError +from switchbee.device import ApiStateCommand, DeviceType, SwitchBeeBaseDevice + +from homeassistant.components.button import ButtonEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import SwitchBeeCoordinator + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up Switchbee button.""" + coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id] + async_add_entities( + SwitchBeeButton(switchbee_device, coordinator) + for switchbee_device in coordinator.data.values() + if switchbee_device.type == DeviceType.Scenario + ) + + +class SwitchBeeButton(CoordinatorEntity[SwitchBeeCoordinator], ButtonEntity): + """Representation of an Switchbee button.""" + + def __init__( + self, + device: SwitchBeeBaseDevice, + coordinator: SwitchBeeCoordinator, + ) -> None: + """Initialize the Switchbee switch.""" + super().__init__(coordinator) + self._attr_name = device.name + self._device_id = device.id + self._attr_unique_id = f"{coordinator.mac_formated}-{device.id}" + + async def async_press(self) -> None: + """Fire the scenario in the SwitchBee hub.""" + try: + await self.coordinator.api.set_state(self._device_id, ApiStateCommand.ON) + except SwitchBeeError as exp: + raise HomeAssistantError( + f"Failed to fire scenario {self.name}, {str(exp)}" + ) from exp diff --git a/homeassistant/components/switchbee/coordinator.py b/homeassistant/components/switchbee/coordinator.py index d43f3602e29..72a487d812b 100644 --- a/homeassistant/components/switchbee/coordinator.py +++ b/homeassistant/components/switchbee/coordinator.py @@ -54,6 +54,7 @@ class SwitchBeeCoordinator(DataUpdateCoordinator[dict[int, SwitchBeeBaseDevice]] DeviceType.TimedSwitch, DeviceType.GroupSwitch, DeviceType.TimedPowerSwitch, + DeviceType.Scenario, ] ) except SwitchBeeError as exp: