From c2d372dd13bd0892f6fd3f4386efac6f7b5ce418 Mon Sep 17 00:00:00 2001 From: Duco Sebel <74970928+DCSBL@users.noreply.github.com> Date: Wed, 30 Nov 2022 10:28:28 +0100 Subject: [PATCH] Fix HomeWizard code quality issues (#82973) * Sort platforms a-z * Collect all switch entities and load in a single call * Add test for button press --- homeassistant/components/homewizard/const.py | 2 +- homeassistant/components/homewizard/switch.py | 18 ++++------ tests/components/homewizard/test_button.py | 34 ++++++++++++++++++- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/homewizard/const.py b/homeassistant/components/homewizard/const.py index 8e24bd19e44..e5ceb5ab23d 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, Platform.BUTTON] +PLATFORMS = [Platform.BUTTON, Platform.NUMBER, Platform.SENSOR, Platform.SWITCH] # Platform config. CONF_API_ENABLED = "api_enabled" diff --git a/homeassistant/components/homewizard/switch.py b/homeassistant/components/homewizard/switch.py index 8a40087403e..3b255d195b1 100644 --- a/homeassistant/components/homewizard/switch.py +++ b/homeassistant/components/homewizard/switch.py @@ -22,20 +22,16 @@ async def async_setup_entry( """Set up switches.""" coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + entities: list[SwitchEntity] = [] + if coordinator.data["state"]: - async_add_entities( - [ - HWEnergyMainSwitchEntity(coordinator, entry), - HWEnergySwitchLockEntity(coordinator, entry), - ] - ) + entities.append(HWEnergyMainSwitchEntity(coordinator, entry)) + entities.append(HWEnergySwitchLockEntity(coordinator, entry)) if coordinator.data["system"]: - async_add_entities( - [ - HWEnergyEnableCloudEntity(hass, coordinator, entry), - ] - ) + entities.append(HWEnergyEnableCloudEntity(hass, coordinator, entry)) + + async_add_entities(entities) class HWEnergySwitchEntity( diff --git a/tests/components/homewizard/test_button.py b/tests/components/homewizard/test_button.py index 5d1481f4c1d..79c6fe3c4a9 100644 --- a/tests/components/homewizard/test_button.py +++ b/tests/components/homewizard/test_button.py @@ -1,7 +1,8 @@ """Test the identify button for HomeWizard.""" from unittest.mock import patch -from homeassistant.const import ATTR_FRIENDLY_NAME +from homeassistant.components import button +from homeassistant.const import ATTR_FRIENDLY_NAME, STATE_UNKNOWN from homeassistant.helpers import entity_registry as er from .generator import get_mock_device @@ -57,3 +58,34 @@ async def test_identify_button_is_loaded( entry = entity_registry.async_get("button.product_name_aabbccddeeff_identify") assert entry assert entry.unique_id == "aabbccddeeff_identify" + + +async def test_cloud_connection_on_off(hass, mock_config_entry_data, mock_config_entry): + """Test the creation and values of the Litter-Robot button.""" + + 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() + + assert ( + hass.states.get("button.product_name_aabbccddeeff_identify").state + == STATE_UNKNOWN + ) + + assert api.identify.call_count == 0 + await hass.services.async_call( + button.DOMAIN, + button.SERVICE_PRESS, + {"entity_id": "button.product_name_aabbccddeeff_identify"}, + blocking=True, + ) + assert api.identify.call_count == 1