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.pypull/82820/head
parent
b65d8cc1b3
commit
367b5e586b
|
@ -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()
|
|
@ -10,7 +10,7 @@ from homewizard_energy.models import Data, Device, State, System
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
|
|
||||||
DOMAIN = "homewizard"
|
DOMAIN = "homewizard"
|
||||||
PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER]
|
PLATFORMS = [Platform.SENSOR, Platform.SWITCH, Platform.NUMBER, Platform.BUTTON]
|
||||||
|
|
||||||
# Platform config.
|
# Platform config.
|
||||||
CONF_API_ENABLED = "api_enabled"
|
CONF_API_ENABLED = "api_enabled"
|
||||||
|
|
|
@ -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"
|
Loading…
Reference in New Issue