From 367b5e586b9a7c6f5c9d33283a8e92b3873d6e52 Mon Sep 17 00:00:00 2001 From: Duco Sebel <74970928+DCSBL@users.noreply.github.com> Date: Mon, 28 Nov 2022 02:48:35 +0100 Subject: [PATCH] Add support for HomeWizard identify feature (#82375) * Add support for Identify feature * Add tests for button * Use only identifiers for device_info * Update homeassistant/components/homewizard/button.py --- homeassistant/components/homewizard/button.py | 57 ++++++++++++++++++ homeassistant/components/homewizard/const.py | 2 +- tests/components/homewizard/test_button.py | 59 +++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/homewizard/button.py create mode 100644 tests/components/homewizard/test_button.py diff --git a/homeassistant/components/homewizard/button.py b/homeassistant/components/homewizard/button.py new file mode 100644 index 00000000000..4bcc5016dec --- /dev/null +++ b/homeassistant/components/homewizard/button.py @@ -0,0 +1,57 @@ +"""Support for HomeWizard buttons.""" + +import logging + +from homeassistant.components.button import ButtonEntity +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 homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import HWEnergyDeviceUpdateCoordinator + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback +) -> None: + """Set up the Identify button.""" + coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + + features = await coordinator.api.features() + if features.has_identify: + async_add_entities([HomeWizardIdentifyButton(coordinator, entry)]) + + +class HomeWizardIdentifyButton( + CoordinatorEntity[HWEnergyDeviceUpdateCoordinator], ButtonEntity +): + """Representation of a identify button.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: HWEnergyDeviceUpdateCoordinator, + entry: ConfigEntry, + ) -> None: + """Initialize button.""" + super().__init__(coordinator) + self._attr_unique_id = f"{entry.unique_id}_identify" + self._attr_device_info = { + "name": entry.title, + "manufacturer": "HomeWizard", + "sw_version": coordinator.data["device"].firmware_version, + "model": coordinator.data["device"].product_type, + "identifiers": {(DOMAIN, coordinator.data["device"].serial)}, + } + self._attr_name = "Identify" + self._attr_icon = "mdi:magnify" + self._attr_entity_category = EntityCategory.DIAGNOSTIC + + async def async_press(self) -> None: + """Identify the device.""" + await self.coordinator.api.identify() diff --git a/homeassistant/components/homewizard/const.py b/homeassistant/components/homewizard/const.py index f1aba6ca17a..8e24bd19e44 100644 --- a/homeassistant/components/homewizard/const.py +++ b/homeassistant/components/homewizard/const.py @@ -10,7 +10,7 @@ from homewizard_energy.models import Data, Device, State, System from homeassistant.const import Platform DOMAIN = "homewizard" -PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER] +PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER, Platform.BUTTON] # Platform config. CONF_API_ENABLED = "api_enabled" diff --git a/tests/components/homewizard/test_button.py b/tests/components/homewizard/test_button.py new file mode 100644 index 00000000000..5d1481f4c1d --- /dev/null +++ b/tests/components/homewizard/test_button.py @@ -0,0 +1,59 @@ +"""Test the identify button for HomeWizard.""" +from unittest.mock import patch + +from homeassistant.const import ATTR_FRIENDLY_NAME +from homeassistant.helpers import entity_registry as er + +from .generator import get_mock_device + + +async def test_identify_button_entity_not_loaded_when_not_available( + hass, mock_config_entry_data, mock_config_entry +): + """Does not load button when device has no support for it.""" + + api = get_mock_device(product_type="HWE-P1") + + with patch( + "homeassistant.components.homewizard.coordinator.HomeWizardEnergy", + return_value=api, + ): + entry = mock_config_entry + entry.data = mock_config_entry_data + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert hass.states.get("button.product_name_aabbccddeeff_identify") is None + + +async def test_identify_button_is_loaded( + hass, mock_config_entry_data, mock_config_entry +): + """Loads button when device has support.""" + + api = get_mock_device(product_type="HWE-SKT", firmware_version="3.02") + + with patch( + "homeassistant.components.homewizard.coordinator.HomeWizardEnergy", + return_value=api, + ): + entry = mock_config_entry + entry.data = mock_config_entry_data + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get("button.product_name_aabbccddeeff_identify") + assert state + assert ( + state.attributes.get(ATTR_FRIENDLY_NAME) + == "Product Name (aabbccddeeff) Identify" + ) + + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("button.product_name_aabbccddeeff_identify") + assert entry + assert entry.unique_id == "aabbccddeeff_identify"