Add button platform to LaMetric (#76768)
* Add button platform to LaMetric * coveragerc * Fix docstring Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
619e99d24c
commit
c26d6879ae
3 changed files with 88 additions and 1 deletions
|
@ -641,6 +641,7 @@ omit =
|
||||||
homeassistant/components/kwb/sensor.py
|
homeassistant/components/kwb/sensor.py
|
||||||
homeassistant/components/lacrosse/sensor.py
|
homeassistant/components/lacrosse/sensor.py
|
||||||
homeassistant/components/lametric/__init__.py
|
homeassistant/components/lametric/__init__.py
|
||||||
|
homeassistant/components/lametric/button.py
|
||||||
homeassistant/components/lametric/coordinator.py
|
homeassistant/components/lametric/coordinator.py
|
||||||
homeassistant/components/lametric/entity.py
|
homeassistant/components/lametric/entity.py
|
||||||
homeassistant/components/lametric/notify.py
|
homeassistant/components/lametric/notify.py
|
||||||
|
|
86
homeassistant/components/lametric/button.py
Normal file
86
homeassistant/components/lametric/button.py
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
"""Support for LaMetric buttons."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Awaitable, Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from demetriek import LaMetricDevice
|
||||||
|
|
||||||
|
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .coordinator import LaMetricDataUpdateCoordinator
|
||||||
|
from .entity import LaMetricEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LaMetricButtonEntityDescriptionMixin:
|
||||||
|
"""Mixin values for LaMetric entities."""
|
||||||
|
|
||||||
|
press_fn: Callable[[LaMetricDevice], Awaitable[Any]]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class LaMetricButtonEntityDescription(
|
||||||
|
ButtonEntityDescription, LaMetricButtonEntityDescriptionMixin
|
||||||
|
):
|
||||||
|
"""Class describing LaMetric button entities."""
|
||||||
|
|
||||||
|
|
||||||
|
BUTTONS = [
|
||||||
|
LaMetricButtonEntityDescription(
|
||||||
|
key="app_next",
|
||||||
|
name="Next app",
|
||||||
|
icon="mdi:arrow-right-bold",
|
||||||
|
entity_category=EntityCategory.CONFIG,
|
||||||
|
press_fn=lambda api: api.app_next(),
|
||||||
|
),
|
||||||
|
LaMetricButtonEntityDescription(
|
||||||
|
key="app_previous",
|
||||||
|
name="Previous app",
|
||||||
|
icon="mdi:arrow-left-bold",
|
||||||
|
entity_category=EntityCategory.CONFIG,
|
||||||
|
press_fn=lambda api: api.app_previous(),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up LaMetric button based on a config entry."""
|
||||||
|
coordinator: LaMetricDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
async_add_entities(
|
||||||
|
LaMetricButtonEntity(
|
||||||
|
coordinator=coordinator,
|
||||||
|
description=description,
|
||||||
|
)
|
||||||
|
for description in BUTTONS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class LaMetricButtonEntity(LaMetricEntity, ButtonEntity):
|
||||||
|
"""Representation of a LaMetric number."""
|
||||||
|
|
||||||
|
entity_description: LaMetricButtonEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: LaMetricDataUpdateCoordinator,
|
||||||
|
description: LaMetricButtonEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the button entity."""
|
||||||
|
super().__init__(coordinator=coordinator)
|
||||||
|
self.entity_description = description
|
||||||
|
self._attr_unique_id = f"{coordinator.data.serial_number}-{description.key}"
|
||||||
|
|
||||||
|
async def async_press(self) -> None:
|
||||||
|
"""Send out a command to LaMetric."""
|
||||||
|
await self.entity_description.press_fn(self.coordinator.lametric)
|
|
@ -7,7 +7,7 @@ from typing import Final
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
|
|
||||||
DOMAIN: Final = "lametric"
|
DOMAIN: Final = "lametric"
|
||||||
PLATFORMS = [Platform.NUMBER]
|
PLATFORMS = [Platform.BUTTON, Platform.NUMBER]
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
SCAN_INTERVAL = timedelta(seconds=30)
|
SCAN_INTERVAL = timedelta(seconds=30)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue