Add button entity to Overkiz integration (#62719)

This commit is contained in:
Mick Vleeshouwer 2021-12-23 16:21:47 -08:00 committed by GitHub
parent f07030c425
commit fb04b19960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 1 deletions

View File

@ -803,6 +803,7 @@ omit =
homeassistant/components/osramlightify/light.py homeassistant/components/osramlightify/light.py
homeassistant/components/otp/sensor.py homeassistant/components/otp/sensor.py
homeassistant/components/overkiz/__init__.py homeassistant/components/overkiz/__init__.py
homeassistant/components/overkiz/button.py
homeassistant/components/overkiz/coordinator.py homeassistant/components/overkiz/coordinator.py
homeassistant/components/overkiz/entity.py homeassistant/components/overkiz/entity.py
homeassistant/components/overkiz/executor.py homeassistant/components/overkiz/executor.py

View File

@ -0,0 +1,81 @@
"""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_platform import AddEntitiesCallback
from . import HomeAssistantOverkizData
from .const import DOMAIN, IGNORED_OVERKIZ_DEVICES
from .entity import OverkizDescriptiveEntity
BUTTON_DESCRIPTIONS: list[ButtonEntityDescription] = [
# My Position (cover, light)
ButtonEntityDescription(
key="my",
name="My Position",
icon="mdi:star",
),
# Identify
ButtonEntityDescription(
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_registry_enabled_default=False,
),
ButtonEntityDescription(
key="stopIdentify",
name="Stop Identify",
icon="mdi:human-greeting-variant",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
entity_registry_enabled_default=False,
),
ButtonEntityDescription(
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,
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
):
"""Set up the Overkiz button from a config entry."""
data: HomeAssistantOverkizData = hass.data[DOMAIN][entry.entry_id]
entities: list[ButtonEntity] = []
supported_commands = {
description.key: description for description in BUTTON_DESCRIPTIONS
}
for device in data.coordinator.data.values():
if (
device.widget not in IGNORED_OVERKIZ_DEVICES
and device.ui_class not 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,
)
)
async_add_entities(entities)
class OverkizButton(OverkizDescriptiveEntity, ButtonEntity):
"""Representation of an Overkiz Button."""
async def async_press(self) -> None:
"""Handle the button press."""
await self.executor.async_execute_command(self.entity_description.key)

View File

@ -18,6 +18,7 @@ UPDATE_INTERVAL: Final = timedelta(seconds=30)
UPDATE_INTERVAL_ALL_ASSUMED_STATE: Final = timedelta(minutes=60) UPDATE_INTERVAL_ALL_ASSUMED_STATE: Final = timedelta(minutes=60)
PLATFORMS: list[Platform] = [ PLATFORMS: list[Platform] = [
Platform.BUTTON,
Platform.LOCK, Platform.LOCK,
Platform.SENSOR, Platform.SENSOR,
] ]

View File

@ -7,6 +7,7 @@ from dataclasses import dataclass
from pyoverkiz.enums import OverkizAttribute, OverkizState from pyoverkiz.enums import OverkizAttribute, OverkizState
from pyoverkiz.models import Device from pyoverkiz.models import Device
from homeassistant.components.button import ButtonEntityDescription
from homeassistant.components.sensor import SensorEntityDescription from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
@ -99,7 +100,7 @@ class OverkizDescriptiveEntity(OverkizEntity):
self, self,
device_url: str, device_url: str,
coordinator: OverkizDataUpdateCoordinator, coordinator: OverkizDataUpdateCoordinator,
description: OverkizSensorDescription, description: OverkizSensorDescription | ButtonEntityDescription,
) -> None: ) -> None:
"""Initialize the device.""" """Initialize the device."""
super().__init__(device_url, coordinator) super().__init__(device_url, coordinator)