2021-11-04 16:50:43 +01:00
|
|
|
"""The tests for the Button component."""
|
2024-03-23 00:27:57 +01:00
|
|
|
|
2024-07-01 11:58:49 +02:00
|
|
|
from collections.abc import Generator
|
2024-01-17 21:53:55 -10:00
|
|
|
from datetime import timedelta
|
|
|
|
from unittest.mock import MagicMock
|
2021-11-04 16:50:43 +01:00
|
|
|
|
2024-01-17 21:53:55 -10:00
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
2021-11-04 16:50:43 +01:00
|
|
|
import pytest
|
|
|
|
|
2023-06-22 23:51:41 +02:00
|
|
|
from homeassistant.components.button import (
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_PRESS,
|
|
|
|
ButtonDeviceClass,
|
|
|
|
ButtonEntity,
|
|
|
|
ButtonEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
2024-01-21 01:04:13 -10:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_PLATFORM,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
STATE_UNKNOWN,
|
|
|
|
)
|
2021-11-04 16:50:43 +01:00
|
|
|
from homeassistant.core import HomeAssistant, State
|
2023-06-22 23:51:41 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-11-04 16:50:43 +01:00
|
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
|
2024-03-16 23:47:59 +01:00
|
|
|
from .const import TEST_DOMAIN
|
|
|
|
|
2023-06-22 23:51:41 +02:00
|
|
|
from tests.common import (
|
|
|
|
MockConfigEntry,
|
|
|
|
MockModule,
|
|
|
|
MockPlatform,
|
|
|
|
mock_config_flow,
|
|
|
|
mock_integration,
|
|
|
|
mock_platform,
|
|
|
|
mock_restore_cache,
|
|
|
|
)
|
|
|
|
|
2021-11-04 16:50:43 +01:00
|
|
|
|
|
|
|
async def test_button(hass: HomeAssistant) -> None:
|
|
|
|
"""Test getting data from the mocked button entity."""
|
|
|
|
button = ButtonEntity()
|
|
|
|
assert button.state is None
|
|
|
|
|
|
|
|
button.hass = hass
|
|
|
|
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
await button.async_press()
|
|
|
|
|
|
|
|
button.press = MagicMock()
|
|
|
|
await button.async_press()
|
|
|
|
|
|
|
|
assert button.press.called
|
|
|
|
|
|
|
|
|
2024-06-05 09:20:08 +02:00
|
|
|
@pytest.mark.usefixtures("enable_custom_integrations", "setup_platform")
|
2023-02-10 16:05:01 +01:00
|
|
|
async def test_custom_integration(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
caplog: pytest.LogCaptureFixture,
|
2024-01-17 21:53:55 -10:00
|
|
|
freezer: FrozenDateTimeFactory,
|
2023-02-10 16:05:01 +01:00
|
|
|
) -> None:
|
2021-11-04 16:50:43 +01:00
|
|
|
"""Test we integration."""
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("button.button_1").state == STATE_UNKNOWN
|
|
|
|
|
|
|
|
now = dt_util.utcnow()
|
2024-01-17 21:53:55 -10:00
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_PRESS,
|
|
|
|
{ATTR_ENTITY_ID: "button.button_1"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
2021-11-04 16:50:43 +01:00
|
|
|
|
|
|
|
assert hass.states.get("button.button_1").state == now.isoformat()
|
|
|
|
assert "The button has been pressed" in caplog.text
|
|
|
|
|
2024-01-17 21:53:55 -10:00
|
|
|
now_isoformat = dt_util.utcnow().isoformat()
|
|
|
|
assert hass.states.get("button.button_1").state == now_isoformat
|
|
|
|
|
|
|
|
new_time = dt_util.utcnow() + timedelta(weeks=1)
|
|
|
|
freezer.move_to(new_time)
|
|
|
|
await hass.services.async_call(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_PRESS,
|
|
|
|
{ATTR_ENTITY_ID: "button.button_1"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
new_time_isoformat = new_time.isoformat()
|
|
|
|
assert hass.states.get("button.button_1").state == new_time_isoformat
|
|
|
|
|
2021-11-04 16:50:43 +01:00
|
|
|
|
2024-06-05 09:20:08 +02:00
|
|
|
@pytest.mark.usefixtures("enable_custom_integrations", "setup_platform")
|
|
|
|
async def test_restore_state(hass: HomeAssistant) -> None:
|
2021-11-04 16:50:43 +01:00
|
|
|
"""Test we restore state integration."""
|
|
|
|
mock_restore_cache(hass, (State("button.button_1", "2021-01-01T23:59:59+00:00"),))
|
|
|
|
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("button.button_1").state == "2021-01-01T23:59:59+00:00"
|
2023-06-22 23:51:41 +02:00
|
|
|
|
|
|
|
|
2024-06-05 09:20:08 +02:00
|
|
|
@pytest.mark.usefixtures("enable_custom_integrations", "setup_platform")
|
|
|
|
async def test_restore_state_does_not_restore_unavailable(hass: HomeAssistant) -> None:
|
2024-01-21 01:04:13 -10:00
|
|
|
"""Test we restore state integration except for unavailable."""
|
|
|
|
mock_restore_cache(hass, (State("button.button_1", STATE_UNAVAILABLE),))
|
|
|
|
|
|
|
|
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get("button.button_1").state == STATE_UNKNOWN
|
|
|
|
|
|
|
|
|
2023-06-22 23:51:41 +02:00
|
|
|
class MockFlow(ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2024-06-06 17:24:22 +02:00
|
|
|
def config_flow_fixture(hass: HomeAssistant) -> Generator[None]:
|
2023-06-22 23:51:41 +02:00
|
|
|
"""Mock config flow."""
|
|
|
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
|
|
|
|
|
|
|
with mock_config_flow(TEST_DOMAIN, MockFlow):
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
async def test_name(hass: HomeAssistant) -> None:
|
|
|
|
"""Test button name."""
|
|
|
|
|
|
|
|
async def async_setup_entry_init(
|
|
|
|
hass: HomeAssistant, config_entry: ConfigEntry
|
|
|
|
) -> bool:
|
|
|
|
"""Set up test config entry."""
|
Ensure config entries are not unloaded while their platforms are setting up (#118767)
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* run with error on to find them
* cert_exp, hold lock
* cert_exp, hold lock
* shelly async_late_forward_entry_setups
* compact
* compact
* found another
* patch up mobileapp
* patch up hue tests
* patch up smartthings
* fix mqtt
* fix esphome
* zwave_js
* mqtt
* rework
* fixes
* fix mocking
* fix mocking
* do not call async_forward_entry_setup directly
* docstrings
* docstrings
* docstrings
* add comments
* doc strings
* fixed all in core, turn off strict
* coverage
* coverage
* missing
* coverage
2024-06-04 20:34:39 -05:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN])
|
2023-06-22 23:51:41 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
TEST_DOMAIN,
|
|
|
|
async_setup_entry=async_setup_entry_init,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# Unnamed button without device class -> no name
|
|
|
|
entity1 = ButtonEntity()
|
|
|
|
entity1.entity_id = "button.test1"
|
|
|
|
|
|
|
|
# Unnamed button with device class but has_entity_name False -> no name
|
|
|
|
entity2 = ButtonEntity()
|
|
|
|
entity2.entity_id = "button.test2"
|
|
|
|
entity2._attr_device_class = ButtonDeviceClass.RESTART
|
|
|
|
|
|
|
|
# Unnamed button with device class and has_entity_name True -> named
|
|
|
|
entity3 = ButtonEntity()
|
|
|
|
entity3.entity_id = "button.test3"
|
|
|
|
entity3._attr_device_class = ButtonDeviceClass.RESTART
|
|
|
|
entity3._attr_has_entity_name = True
|
|
|
|
|
|
|
|
# Unnamed button with device class and has_entity_name True -> named
|
|
|
|
entity4 = ButtonEntity()
|
|
|
|
entity4.entity_id = "sensor.test4"
|
|
|
|
entity4.entity_description = ButtonEntityDescription(
|
|
|
|
"test",
|
|
|
|
ButtonDeviceClass.RESTART,
|
|
|
|
has_entity_name=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_setup_entry_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up test button platform via config entry."""
|
|
|
|
async_add_entities([entity1, entity2, entity3, entity4])
|
|
|
|
|
|
|
|
mock_platform(
|
|
|
|
hass,
|
|
|
|
f"{TEST_DOMAIN}.{DOMAIN}",
|
|
|
|
MockPlatform(async_setup_entry=async_setup_entry_platform),
|
|
|
|
)
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain=TEST_DOMAIN)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
state = hass.states.get(entity1.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {}
|
|
|
|
|
|
|
|
state = hass.states.get(entity2.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {"device_class": "restart"}
|
|
|
|
|
|
|
|
state = hass.states.get(entity3.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {"device_class": "restart", "friendly_name": "Restart"}
|
|
|
|
|
|
|
|
state = hass.states.get(entity4.entity_id)
|
|
|
|
assert state
|
|
|
|
assert state.attributes == {"device_class": "restart", "friendly_name": "Restart"}
|