Skip to content

Commit

Permalink
Backport button (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl committed Feb 20, 2022
1 parent 6cb325c commit d93ebc7
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions custom_components/tahoma/button.py
@@ -1,15 +1,17 @@
"""Support for Overkiz number devices."""
"""Support for Overkiz (virtual) buttons."""
from __future__ import annotations

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeAssistantOverkizData
from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES
from .entity import OverkizDescriptiveEntity

BUTTON_DESCRIPTIONS = [
BUTTON_DESCRIPTIONS: list[ButtonEntityDescription] = [
# My Position (cover, light)
ButtonEntityDescription(
key="my",
Expand All @@ -18,62 +20,70 @@
),
# Identify
ButtonEntityDescription(
key="identify", # startIdentify and identify are reversed... Remove when fixed server side.
key="identify", # startIdentify and identify are reversed... Swap this when fixed in API.
name="Start Identify",
icon="mdi:human-greeting-variant",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
ButtonEntityDescription(
key="stopIdentify",
name="Stop Identify",
icon="mdi:human-greeting-variant",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
),
ButtonEntityDescription(
key="startIdentify", # startIdentify and identify are reversed... Remove when fixed server side.
key="startIdentify", # startIdentify and identify are reversed... Swap this when fixed in API.
name="Identify",
icon="mdi:human-greeting-variant",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_category=EntityCategory.DIAGNOSTIC,
),
# RTDIndoorSiren / RTDOutdoorSiren
ButtonEntityDescription(key="dingDong", name="Ding Dong", icon="mdi:bell-ring"),
ButtonEntityDescription(key="bip", name="Bip", icon="mdi:bell-ring"),
ButtonEntityDescription(
key="fastBipSequence", name="Fast Bip Sequence", icon="mdi:bell-ring"
),
ButtonEntityDescription(key="ring", name="Ring", icon="mdi:bell-ring"),
]

SUPPORTED_COMMANDS = {
description.key: description for description in BUTTON_DESCRIPTIONS
}


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
):
"""Set up the Overkiz number from a config entry."""
) -> None:
"""Set up the Overkiz button from a config entry."""
data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id]

entities = []

supported_commands = {
description.key: description for description in BUTTON_DESCRIPTIONS
}
entities: list[ButtonEntity] = []

for device in data.coordinator.data.values():
if (
device.widget not in IGNORED_OVERKIZ_DEVICES
and device.ui_class not in IGNORED_OVERKIZ_DEVICES
device.widget in IGNORED_OVERKIZ_DEVICES
or device.ui_class in IGNORED_OVERKIZ_DEVICES
):
for command in device.definition.commands:
if description := supported_commands.get(command.command_name):
entities.append(
OverkizButton(
device.device_url,
data.coordinator,
description,
)
continue

for command in device.definition.commands:
if description := SUPPORTED_COMMANDS.get(command.command_name):
entities.append(
OverkizButton(
device.device_url,
data.coordinator,
description,
)
)

async_add_entities(entities)


class OverkizButton(OverkizDescriptiveEntity, ButtonEntity):
"""Representation of an Overkiz Button entity."""
"""Representation of an Overkiz Button."""

async def async_press(self) -> None:
"""Handle the button press."""
Expand Down

0 comments on commit d93ebc7

Please sign in to comment.