Add buttons to Motionblinds BLE integration (#114227)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Lenn 2024-03-27 11:05:26 +01:00 committed by GitHub
parent 7b56643278
commit 6423501498
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 119 additions and 1 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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"

View File

@ -0,0 +1,15 @@
{
"entity": {
"button": {
"connect": {
"default": "mdi:bluetooth"
},
"disconnect": {
"default": "mdi:bluetooth-off"
},
"favorite": {
"default": "mdi:star"
}
}
}
}

View File

@ -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",