Skip to content

Commit

Permalink
Add button to homeworks (home-assistant#112269)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Mar 5, 2024
1 parent 8e2de51 commit a515603
Show file tree
Hide file tree
Showing 9 changed files with 763 additions and 9 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ omit =
homeassistant/components/homematic/sensor.py
homeassistant/components/homematic/switch.py
homeassistant/components/homeworks/__init__.py
homeassistant/components/homeworks/button.py
homeassistant/components/homeworks/light.py
homeassistant/components/horizon/media_player.py
homeassistant/components/hp_ilo/sensor.py
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/homeworks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

_LOGGER = logging.getLogger(__name__)

PLATFORMS: list[Platform] = [Platform.LIGHT]
PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.LIGHT]

EVENT_BUTTON_PRESS = "homeworks_button_press"
EVENT_BUTTON_RELEASE = "homeworks_button_release"
Expand Down Expand Up @@ -186,6 +186,7 @@ def __init__(
self._controller_id, self._addr, self._idx
)
self._controller = controller
self._attr_extra_state_attributes = {"homeworks_address": self._addr}


class HomeworksKeypad:
Expand Down
78 changes: 78 additions & 0 deletions homeassistant/components/homeworks/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Support for Lutron Homeworks buttons."""
from __future__ import annotations

from time import sleep

from pyhomeworks.pyhomeworks import Homeworks

from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HomeworksData, HomeworksEntity
from .const import (
CONF_ADDR,
CONF_BUTTONS,
CONF_CONTROLLER_ID,
CONF_KEYPADS,
CONF_NUMBER,
CONF_RELEASE_DELAY,
DOMAIN,
)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Homeworks buttons."""
data: HomeworksData = hass.data[DOMAIN][entry.entry_id]
controller = data.controller
controller_id = entry.options[CONF_CONTROLLER_ID]
devs = []
for keypad in entry.options.get(CONF_KEYPADS, []):
for button in keypad[CONF_BUTTONS]:
dev = HomeworksButton(
controller,
controller_id,
keypad[CONF_ADDR],
keypad[CONF_NAME],
button[CONF_NAME],
button[CONF_NUMBER],
button[CONF_RELEASE_DELAY],
)
devs.append(dev)
async_add_entities(devs, True)


class HomeworksButton(HomeworksEntity, ButtonEntity):
"""Homeworks Button."""

def __init__(
self,
controller: Homeworks,
controller_id: str,
addr: str,
keypad_name: str,
button_name: str,
button_number: int,
release_delay: float,
) -> None:
"""Create device with Addr, name, and rate."""
super().__init__(controller, controller_id, addr, button_number, button_name)
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, f"{controller_id}.{addr}")}, name=keypad_name
)
self._release_delay = release_delay

def press(self) -> None:
"""Press the button."""
# pylint: disable-next=protected-access
self._controller._send(f"KBP, {self._addr}, {self._idx}")
if not self._release_delay:
return
sleep(self._release_delay)
# pylint: disable-next=protected-access
self._controller._send(f"KBR, {self._addr}, {self._idx}")
Loading

0 comments on commit a515603

Please sign in to comment.