Add `button` platform to NAM integration (#60410)

pull/60562/head
Maciej Bieniek 2021-11-29 22:58:04 +01:00 committed by GitHub
parent 8626de24fc
commit 847b10fa65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 1 deletions

View File

@ -42,7 +42,7 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["sensor"]
PLATFORMS = ["button", "sensor"]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

View File

@ -0,0 +1,58 @@
"""Support for the Nettigo Air Monitor service."""
from __future__ import annotations
import logging
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENTITY_CATEGORY_CONFIG
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import NAMDataUpdateCoordinator
from .const import DEFAULT_NAME, DOMAIN
PARALLEL_UPDATES = 1
_LOGGER = logging.getLogger(__name__)
RESTART_BUTTON: ButtonEntityDescription = ButtonEntityDescription(
key="restart",
name=f"{DEFAULT_NAME} Restart",
icon="mdi:restart",
entity_category=ENTITY_CATEGORY_CONFIG,
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Add a Nettigo Air Monitor entities from a config_entry."""
coordinator: NAMDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
buttons: list[NAMButton] = []
buttons.append(NAMButton(coordinator, RESTART_BUTTON))
async_add_entities(buttons, False)
class NAMButton(CoordinatorEntity, ButtonEntity):
"""Define an Nettigo Air Monitor button."""
coordinator: NAMDataUpdateCoordinator
def __init__(
self,
coordinator: NAMDataUpdateCoordinator,
description: ButtonEntityDescription,
) -> None:
"""Initialize."""
super().__init__(coordinator)
self._attr_device_info = coordinator.device_info
self._attr_unique_id = f"{coordinator.unique_id}-{description.key}"
self.entity_description = description
async def async_press(self) -> None:
"""Triggers the restart."""
await self.coordinator.nam.async_restart()

View File

@ -0,0 +1,48 @@
"""Test button of Nettigo Air Monitor integration."""
from unittest.mock import patch
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
from homeassistant.const import ATTR_ENTITY_ID, ATTR_ICON, STATE_UNKNOWN
from homeassistant.helpers import entity_registry as er
from homeassistant.util import dt as dt_util
from tests.components.nam import init_integration
async def test_button(hass):
"""Test states of the button."""
registry = er.async_get(hass)
await init_integration(hass)
state = hass.states.get("button.nettigo_air_monitor_restart")
assert state
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ICON) == "mdi:restart"
entry = registry.async_get("button.nettigo_air_monitor_restart")
assert entry
assert entry.unique_id == "aa:bb:cc:dd:ee:ff-restart"
async def test_button_press(hass):
"""Test button press."""
await init_integration(hass)
now = dt_util.utcnow()
with patch(
"homeassistant.components.nam.NettigoAirMonitor.async_restart"
) as mock_restart, patch("homeassistant.core.dt_util.utcnow", return_value=now):
await hass.services.async_call(
BUTTON_DOMAIN,
"press",
{ATTR_ENTITY_ID: "button.nettigo_air_monitor_restart"},
blocking=True,
)
await hass.async_block_till_done()
mock_restart.assert_called_once()
state = hass.states.get("button.nettigo_air_monitor_restart")
assert state
assert state.state == now.isoformat()