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
This commit is contained in:
Duco Sebel 2022-11-30 10:28:28 +01:00 committed by GitHub
parent 663482fb10
commit c2d372dd13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 13 deletions

View File

@ -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, Platform.BUTTON] PLATFORMS = [Platform.BUTTON, Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
# Platform config. # Platform config.
CONF_API_ENABLED = "api_enabled" CONF_API_ENABLED = "api_enabled"

View File

@ -22,20 +22,16 @@ async def async_setup_entry(
"""Set up switches.""" """Set up switches."""
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
entities: list[SwitchEntity] = []
if coordinator.data["state"]: if coordinator.data["state"]:
async_add_entities( entities.append(HWEnergyMainSwitchEntity(coordinator, entry))
[ entities.append(HWEnergySwitchLockEntity(coordinator, entry))
HWEnergyMainSwitchEntity(coordinator, entry),
HWEnergySwitchLockEntity(coordinator, entry),
]
)
if coordinator.data["system"]: if coordinator.data["system"]:
async_add_entities( entities.append(HWEnergyEnableCloudEntity(hass, coordinator, entry))
[
HWEnergyEnableCloudEntity(hass, coordinator, entry), async_add_entities(entities)
]
)
class HWEnergySwitchEntity( class HWEnergySwitchEntity(

View File

@ -1,7 +1,8 @@
"""Test the identify button for HomeWizard.""" """Test the identify button for HomeWizard."""
from unittest.mock import patch 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 homeassistant.helpers import entity_registry as er
from .generator import get_mock_device 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") entry = entity_registry.async_get("button.product_name_aabbccddeeff_identify")
assert entry assert entry
assert entry.unique_id == "aabbccddeeff_identify" 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