Skip to content

Commit

Permalink
add button entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuen Lee committed Jul 9, 2023
1 parent eaf4695 commit a044059
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
10 changes: 6 additions & 4 deletions custom_components/alfen_wallbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .alfen import AlfenDevice

Expand All @@ -31,6 +32,7 @@
Platform.BINARY_SENSOR,
Platform.SWITCH,
Platform.NUMBER,
Platform.BUTTON
]
SCAN_INTERVAL = timedelta(seconds=60)

Expand All @@ -43,7 +45,7 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
conf = entry.data
device = await alfen_setup(
hass, conf[CONF_HOST], conf[CONF_NAME], conf[CONF_USERNAME], conf[CONF_PASSWORD]
Expand All @@ -61,7 +63,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
return True


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload a config entry."""
_LOGGER.debug("async_unload_entry: %s", config_entry)

Expand All @@ -74,10 +76,10 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
return unload_ok


async def alfen_setup(hass: HomeAssistant, host: str, name: str, username: str, password: str):
async def alfen_setup(hass: HomeAssistant, host: str, name: str, username: str, password: str) -> AlfenDevice | None:
"""Create a Alfen instance only once."""

session = hass.helpers.aiohttp_client.async_get_clientsession()
session = async_get_clientsession(hass, verify_ssl=False)
try:
with timeout(TIMEOUT):
device = AlfenDevice(host, name, session, username, password)
Expand Down
83 changes: 83 additions & 0 deletions custom_components/alfen_wallbox/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

from dataclasses import dataclass
import logging
from typing import Final
from .alfen import POST_HEADER_JSON, AlfenDevice
from .entity import AlfenEntity

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import CMD, METHOD_POST, PARAM_COMMAND, COMMAND_REBOOT

from . import DOMAIN as ALFEN_DOMAIN


_LOGGER = logging.getLogger(__name__)


@dataclass
class AlfenButtonDescriptionMixin:
"""Define an entity description mixin for button entities."""

method: str
url_action: str
json_action: str


@dataclass
class AlfenButtonDescription(ButtonEntityDescription, AlfenButtonDescriptionMixin):
"""Class to describe an Alfen button entity."""


ALFEN_BUTTON_TYPES: Final[tuple[AlfenButtonDescription, ...]] = (
AlfenButtonDescription(
key="reboot_wallbox",
name="Reboot Wallbox",
method=METHOD_POST,
url_action=CMD,
json_action={PARAM_COMMAND: COMMAND_REBOOT},
),
)


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Alfen switch entities from a config entry."""
device = hass.data[ALFEN_DOMAIN][entry.entry_id]
buttons = [AlfenButton(device, description)
for description in ALFEN_BUTTON_TYPES]

async_add_entities(buttons)


class AlfenButton(AlfenEntity, ButtonEntity):
"""Representation of a Alfen button entity."""

def __init__(
self,
device: AlfenDevice,
description: AlfenButtonDescription,
) -> None:
"""Initialize the Alfen button entity."""
super().__init__(device)
self._device = device
self._attr_name = f"{device.name} {description.name}"
self._attr_unique_id = f"{device.id}-{description.key}"
self.entity_description = description

async def async_press(self) -> None:
"""Press the button."""
await self._device.login()
response = await self._device.request(
self.entity_description.method,
POST_HEADER_JSON,
self.entity_description.url_action,
self.entity_description.json_action
)
await self._device.logout()
_LOGGER.debug("Response: %s", response)

0 comments on commit a044059

Please sign in to comment.