Use test helpers for acaia buttons (#130626)

This commit is contained in:
Josef Zweck 2024-11-14 16:34:17 +01:00 committed by GitHub
parent cd12720085
commit 1ce8bfdaa4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 43 deletions

View file

@ -1,5 +1,5 @@
# serializer version: 1 # serializer version: 1
# name: test_buttons[entry_button_reset_timer] # name: test_buttons[button.lunar_ddeeff_reset_timer-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -32,7 +32,20 @@
'unit_of_measurement': None, 'unit_of_measurement': None,
}) })
# --- # ---
# name: test_buttons[entry_button_start_stop_timer] # name: test_buttons[button.lunar_ddeeff_reset_timer-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'LUNAR-DDEEFF Reset timer',
}),
'context': <ANY>,
'entity_id': 'button.lunar_ddeeff_reset_timer',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_buttons[button.lunar_ddeeff_start_stop_timer-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -65,7 +78,20 @@
'unit_of_measurement': None, 'unit_of_measurement': None,
}) })
# --- # ---
# name: test_buttons[entry_button_tare] # name: test_buttons[button.lunar_ddeeff_start_stop_timer-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'LUNAR-DDEEFF Start/stop timer',
}),
'context': <ANY>,
'entity_id': 'button.lunar_ddeeff_start_stop_timer',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_buttons[button.lunar_ddeeff_tare-entry]
EntityRegistryEntrySnapshot({ EntityRegistryEntrySnapshot({
'aliases': set({ 'aliases': set({
}), }),
@ -98,33 +124,7 @@
'unit_of_measurement': None, 'unit_of_measurement': None,
}) })
# --- # ---
# name: test_buttons[state_button_reset_timer] # name: test_buttons[button.lunar_ddeeff_tare-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'LUNAR-DDEEFF Reset timer',
}),
'context': <ANY>,
'entity_id': 'button.lunar_ddeeff_reset_timer',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_buttons[state_button_start_stop_timer]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'LUNAR-DDEEFF Start/stop timer',
}),
'context': <ANY>,
'entity_id': 'button.lunar_ddeeff_start_stop_timer',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_buttons[state_button_tare]
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'friendly_name': 'LUNAR-DDEEFF Tare', 'friendly_name': 'LUNAR-DDEEFF Tare',

View file

@ -1,21 +1,24 @@
"""Tests for the acaia buttons.""" """Tests for the acaia buttons."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy import SnapshotAssertion from syrupy import SnapshotAssertion
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, STATE_UNKNOWN from homeassistant.const import (
ATTR_ENTITY_ID,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
Platform,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from tests.common import async_fire_time_changed from . import setup_integration
pytestmark = pytest.mark.usefixtures("init_integration")
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform
BUTTONS = ( BUTTONS = (
"tare", "tare",
@ -28,24 +31,25 @@ async def test_buttons(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
mock_scale: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test the acaia buttons.""" """Test the acaia buttons."""
for button in BUTTONS:
state = hass.states.get(f"button.lunar_ddeeff_{button}")
assert state
assert state == snapshot(name=f"state_button_{button}")
entry = entity_registry.async_get(state.entity_id) with patch("homeassistant.components.acaia.PLATFORMS", [Platform.BUTTON]):
assert entry await setup_integration(hass, mock_config_entry)
assert entry == snapshot(name=f"entry_button_{button}") await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_button_presses( async def test_button_presses(
hass: HomeAssistant, hass: HomeAssistant,
mock_scale: MagicMock, mock_scale: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test the acaia button presses.""" """Test the acaia button presses."""
await setup_integration(hass, mock_config_entry)
for button in BUTTONS: for button in BUTTONS:
await hass.services.async_call( await hass.services.async_call(
BUTTON_DOMAIN, BUTTON_DOMAIN,
@ -63,10 +67,13 @@ async def test_button_presses(
async def test_buttons_unavailable_on_disconnected_scale( async def test_buttons_unavailable_on_disconnected_scale(
hass: HomeAssistant, hass: HomeAssistant,
mock_scale: MagicMock, mock_scale: MagicMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test the acaia buttons are unavailable when the scale is disconnected.""" """Test the acaia buttons are unavailable when the scale is disconnected."""
await setup_integration(hass, mock_config_entry)
for button in BUTTONS: for button in BUTTONS:
state = hass.states.get(f"button.lunar_ddeeff_{button}") state = hass.states.get(f"button.lunar_ddeeff_{button}")
assert state assert state