Add button entity to slide_local (#133141)
Co-authored-by: Joostlek <joostlek@outlook.com>pull/133201/head
parent
06391d4635
commit
d85d986075
|
@ -2,16 +2,14 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from goslideapi.goslideapi import GoSlideLocal as SlideLocalApi
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .coordinator import SlideCoordinator
|
||||
|
||||
PLATFORMS = [Platform.COVER]
|
||||
type SlideConfigEntry = ConfigEntry[SlideLocalApi]
|
||||
PLATFORMS = [Platform.BUTTON, Platform.COVER]
|
||||
type SlideConfigEntry = ConfigEntry[SlideCoordinator]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: SlideConfigEntry) -> bool:
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
"""Support for Slide button."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.button import ButtonEntity
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import SlideConfigEntry
|
||||
from .coordinator import SlideCoordinator
|
||||
from .entity import SlideEntity
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: SlideConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up button for Slide platform."""
|
||||
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities([SlideButton(coordinator)])
|
||||
|
||||
|
||||
class SlideButton(SlideEntity, ButtonEntity):
|
||||
"""Defines a Slide button."""
|
||||
|
||||
_attr_entity_category = EntityCategory.CONFIG
|
||||
_attr_translation_key = "calibrate"
|
||||
|
||||
def __init__(self, coordinator: SlideCoordinator) -> None:
|
||||
"""Initialize the slide button."""
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = f"{coordinator.data["mac"]}-calibrate"
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Send out a calibrate command."""
|
||||
await self.coordinator.slide.slide_calibrate(self.coordinator.host)
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"entity": {
|
||||
"button": {
|
||||
"calibrate": {
|
||||
"default": "mdi:tape-measure"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -41,6 +41,13 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"button": {
|
||||
"calibrate": {
|
||||
"name": "Calibrate"
|
||||
}
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"update_error": {
|
||||
"message": "Error while updating data from the API."
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
# serializer version: 1
|
||||
# name: test_all_entities[button.slide_bedroom_calibrate-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'button.slide_bedroom_calibrate',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Calibrate',
|
||||
'platform': 'slide_local',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'calibrate',
|
||||
'unique_id': '1234567890ab-calibrate',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[button.slide_bedroom_calibrate-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'slide bedroom Calibrate',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'button.slide_bedroom_calibrate',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
|
@ -0,0 +1,46 @@
|
|||
"""Tests for the Slide Local button platform."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_platform
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
async def test_all_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_slide_api: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test all entities."""
|
||||
await setup_platform(hass, mock_config_entry, [Platform.BUTTON])
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
async def test_pressing_button(
|
||||
hass: HomeAssistant,
|
||||
mock_slide_api: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test pressing button."""
|
||||
await setup_platform(hass, mock_config_entry, [Platform.BUTTON])
|
||||
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN,
|
||||
SERVICE_PRESS,
|
||||
{
|
||||
ATTR_ENTITY_ID: "button.slide_bedroom_calibrate",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
mock_slide_api.slide_calibrate.assert_called_once()
|
Loading…
Reference in New Issue