From 6423501498cedc6a11bed9e9a07fde0d3426379b Mon Sep 17 00:00:00 2001 From: Lenn <78048721+LennP@users.noreply.github.com> Date: Wed, 27 Mar 2024 11:05:26 +0100 Subject: [PATCH] Add buttons to Motionblinds BLE integration (#114227) Co-authored-by: Joost Lekkerkerker Co-authored-by: Robert Resch --- .coveragerc | 1 + .../components/motionblinds_ble/__init__.py | 2 +- .../components/motionblinds_ble/button.py | 88 +++++++++++++++++++ .../components/motionblinds_ble/const.py | 3 + .../components/motionblinds_ble/icon.json | 15 ++++ .../components/motionblinds_ble/strings.json | 11 +++ 6 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/motionblinds_ble/button.py create mode 100644 homeassistant/components/motionblinds_ble/icon.json diff --git a/.coveragerc b/.coveragerc index 9da1e6fb0a7..7fd6ab5defe 100644 --- a/.coveragerc +++ b/.coveragerc @@ -807,6 +807,7 @@ omit = homeassistant/components/motion_blinds/entity.py homeassistant/components/motion_blinds/sensor.py homeassistant/components/motionblinds_ble/__init__.py + homeassistant/components/motionblinds_ble/button.py homeassistant/components/motionblinds_ble/cover.py homeassistant/components/motionblinds_ble/entity.py homeassistant/components/motionblinds_ble/select.py diff --git a/homeassistant/components/motionblinds_ble/__init__.py b/homeassistant/components/motionblinds_ble/__init__.py index bb89b468a5b..f70625cd36d 100644 --- a/homeassistant/components/motionblinds_ble/__init__.py +++ b/homeassistant/components/motionblinds_ble/__init__.py @@ -28,7 +28,7 @@ from .const import CONF_BLIND_TYPE, CONF_MAC_CODE, DOMAIN _LOGGER = logging.getLogger(__name__) -PLATFORMS: list[Platform] = [Platform.COVER, Platform.SELECT] +PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.COVER, Platform.SELECT] CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) diff --git a/homeassistant/components/motionblinds_ble/button.py b/homeassistant/components/motionblinds_ble/button.py new file mode 100644 index 00000000000..d3bd22e9276 --- /dev/null +++ b/homeassistant/components/motionblinds_ble/button.py @@ -0,0 +1,88 @@ +"""Button entities for the Motionblinds BLE integration.""" + +from __future__ import annotations + +from collections.abc import Callable, Coroutine +from dataclasses import dataclass +import logging +from typing import Any + +from motionblindsble.device import MotionDevice + +from homeassistant.components.button import ButtonEntity, ButtonEntityDescription +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import ATTR_CONNECT, ATTR_DISCONNECT, ATTR_FAVORITE, CONF_MAC_CODE, DOMAIN +from .entity import MotionblindsBLEEntity + +_LOGGER = logging.getLogger(__name__) + +PARALLEL_UPDATES = 0 + + +@dataclass(frozen=True, kw_only=True) +class MotionblindsBLEButtonEntityDescription(ButtonEntityDescription): + """Entity description of a button entity with command attribute.""" + + command: Callable[[MotionDevice], Coroutine[Any, Any, None]] + + +BUTTON_TYPES: list[MotionblindsBLEButtonEntityDescription] = [ + MotionblindsBLEButtonEntityDescription( + key=ATTR_CONNECT, + translation_key=ATTR_CONNECT, + entity_category=EntityCategory.CONFIG, + command=lambda device: device.connect(), + ), + MotionblindsBLEButtonEntityDescription( + key=ATTR_DISCONNECT, + translation_key=ATTR_DISCONNECT, + entity_category=EntityCategory.CONFIG, + command=lambda device: device.disconnect(), + ), + MotionblindsBLEButtonEntityDescription( + key=ATTR_FAVORITE, + translation_key=ATTR_FAVORITE, + entity_category=EntityCategory.CONFIG, + command=lambda device: device.favorite(), + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up button entities based on a config entry.""" + + device: MotionDevice = hass.data[DOMAIN][entry.entry_id] + + async_add_entities( + MotionblindsBLEButtonEntity( + device, + entry, + entity_description, + unique_id_suffix=entity_description.key, + ) + for entity_description in BUTTON_TYPES + ) + + +class MotionblindsBLEButtonEntity(MotionblindsBLEEntity, ButtonEntity): + """Representation of a button entity.""" + + entity_description: MotionblindsBLEButtonEntityDescription + + async def async_added_to_hass(self) -> None: + """Log button entity information.""" + _LOGGER.debug( + "(%s) Setting up %s button entity", + self.entry.data[CONF_MAC_CODE], + self.entity_description.key, + ) + + async def async_press(self) -> None: + """Handle the button press.""" + await self.entity_description.command(self.device) diff --git a/homeassistant/components/motionblinds_ble/const.py b/homeassistant/components/motionblinds_ble/const.py index 5feaf59845f..d2eb5821b9f 100644 --- a/homeassistant/components/motionblinds_ble/const.py +++ b/homeassistant/components/motionblinds_ble/const.py @@ -1,5 +1,8 @@ """Constants for the Motionblinds BLE integration.""" +ATTR_CONNECT = "connect" +ATTR_DISCONNECT = "disconnect" +ATTR_FAVORITE = "favorite" ATTR_SPEED = "speed" CONF_LOCAL_NAME = "local_name" diff --git a/homeassistant/components/motionblinds_ble/icon.json b/homeassistant/components/motionblinds_ble/icon.json new file mode 100644 index 00000000000..109606ab474 --- /dev/null +++ b/homeassistant/components/motionblinds_ble/icon.json @@ -0,0 +1,15 @@ +{ + "entity": { + "button": { + "connect": { + "default": "mdi:bluetooth" + }, + "disconnect": { + "default": "mdi:bluetooth-off" + }, + "favorite": { + "default": "mdi:star" + } + } + } +} diff --git a/homeassistant/components/motionblinds_ble/strings.json b/homeassistant/components/motionblinds_ble/strings.json index bcc06eeb181..0bc9ad4c012 100644 --- a/homeassistant/components/motionblinds_ble/strings.json +++ b/homeassistant/components/motionblinds_ble/strings.json @@ -35,6 +35,17 @@ } }, "entity": { + "button": { + "connect": { + "name": "[%key:common::action::connect%]" + }, + "disconnect": { + "name": "[%key:common::action::disconnect%]" + }, + "favorite": { + "name": "Favorite" + } + }, "select": { "speed": { "name": "Speed",