mirror of
https://github.com/home-assistant/core.git
synced 2025-04-19 14:57:52 +00:00
Use setup_test_component_platform
helper for datetime entity component tests instead of hass.components
(#114415)
This commit is contained in:
parent
74d614243b
commit
045dc3f1fb
20
tests/components/datetime/common.py
Normal file
20
tests/components/datetime/common.py
Normal file
@ -0,0 +1,20 @@
|
||||
"""Common helpers for the datetime entity component tests."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from homeassistant.components.datetime import DateTimeEntity
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
|
||||
class MockDateTimeEntity(MockEntity, DateTimeEntity):
|
||||
"""Mock date/time class."""
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the native value of this date/time."""
|
||||
return self._handle("native_value")
|
||||
|
||||
def set_value(self, value: datetime) -> None:
|
||||
"""Change the time."""
|
||||
self._values["native_value"] = value
|
@ -5,36 +5,31 @@ from zoneinfo import ZoneInfo
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.datetime import (
|
||||
ATTR_DATETIME,
|
||||
DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
DateTimeEntity,
|
||||
)
|
||||
from homeassistant.components.datetime import ATTR_DATETIME, DOMAIN, SERVICE_SET_VALUE
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, CONF_PLATFORM
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import setup_test_component_platform
|
||||
from tests.components.datetime.common import MockDateTimeEntity
|
||||
|
||||
DEFAULT_VALUE = datetime(2020, 1, 1, 12, 0, 0, tzinfo=UTC)
|
||||
|
||||
|
||||
class MockDateTimeEntity(DateTimeEntity):
|
||||
"""Mock datetime device to use in tests."""
|
||||
|
||||
def __init__(self, native_value: datetime | None = DEFAULT_VALUE) -> None:
|
||||
"""Initialize mock datetime entity."""
|
||||
self._attr_native_value = native_value
|
||||
|
||||
async def async_set_value(self, value: datetime) -> None:
|
||||
"""Change the date/time."""
|
||||
self._attr_native_value = value
|
||||
|
||||
|
||||
async def test_datetime(hass: HomeAssistant, enable_custom_integrations: None) -> None:
|
||||
async def test_datetime(hass: HomeAssistant) -> None:
|
||||
"""Test date/time entity."""
|
||||
hass.config.set_time_zone("UTC")
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
setup_test_component_platform(
|
||||
hass,
|
||||
DOMAIN,
|
||||
[
|
||||
MockDateTimeEntity(
|
||||
name="test",
|
||||
unique_id="unique_datetime",
|
||||
native_value=datetime(2020, 1, 1, 1, 2, 3, tzinfo=UTC),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}})
|
||||
await hass.async_block_till_done()
|
||||
|
@ -1,51 +0,0 @@
|
||||
"""Provide a mock time platform.
|
||||
|
||||
Call init before using it in your tests to ensure clean test data.
|
||||
"""
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from homeassistant.components.datetime import DateTimeEntity
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
UNIQUE_DATETIME = "unique_datetime"
|
||||
|
||||
ENTITIES = []
|
||||
|
||||
|
||||
class MockDateTimeEntity(MockEntity, DateTimeEntity):
|
||||
"""Mock date/time class."""
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the native value of this date/time."""
|
||||
return self._handle("native_value")
|
||||
|
||||
def set_value(self, value: datetime) -> None:
|
||||
"""Change the time."""
|
||||
self._values["native_value"] = value
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
global ENTITIES
|
||||
|
||||
ENTITIES = (
|
||||
[]
|
||||
if empty
|
||||
else [
|
||||
MockDateTimeEntity(
|
||||
name="test",
|
||||
unique_id=UNIQUE_DATETIME,
|
||||
native_value=datetime(2020, 1, 1, 1, 2, 3, tzinfo=UTC),
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass, config, async_add_entities_callback, discovery_info=None
|
||||
):
|
||||
"""Return mock entities."""
|
||||
async_add_entities_callback(ENTITIES)
|
Loading…
x
Reference in New Issue
Block a user