Use setup_test_component_platform
helper for time entity component tests instead of hass.components
(#114411)
This commit is contained in:
parent
ae635a5586
commit
a102230498
3 changed files with 30 additions and 66 deletions
20
tests/components/time/common.py
Normal file
20
tests/components/time/common.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
"""Common helpers for time entity component tests."""
|
||||
|
||||
from datetime import time
|
||||
|
||||
from homeassistant.components.time import TimeEntity
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
|
||||
class MockTimeEntity(MockEntity, TimeEntity):
|
||||
"""Mock time class."""
|
||||
|
||||
@property
|
||||
def native_value(self) -> time | None:
|
||||
"""Return the current time."""
|
||||
return self._handle("native_value")
|
||||
|
||||
def set_value(self, value: time) -> None:
|
||||
"""Change the time."""
|
||||
self._values["native_value"] = value
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
from datetime import time
|
||||
|
||||
from homeassistant.components.time import DOMAIN, SERVICE_SET_VALUE, TimeEntity
|
||||
from homeassistant.components.time import DOMAIN, SERVICE_SET_VALUE
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
|
@ -12,23 +12,18 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
class MockTimeEntity(TimeEntity):
|
||||
"""Mock time device to use in tests."""
|
||||
|
||||
def __init__(self, native_value=time(12, 0, 0)) -> None:
|
||||
"""Initialize mock time entity."""
|
||||
self._attr_native_value = native_value
|
||||
|
||||
async def async_set_value(self, value: time) -> None:
|
||||
"""Set the value of the time."""
|
||||
self._attr_native_value = value
|
||||
from tests.common import setup_test_component_platform
|
||||
from tests.components.time.common import MockTimeEntity
|
||||
|
||||
|
||||
async def test_date(hass: HomeAssistant, enable_custom_integrations: None) -> None:
|
||||
async def test_date(hass: HomeAssistant) -> None:
|
||||
"""Test time entity."""
|
||||
platform = getattr(hass.components, f"test.{DOMAIN}")
|
||||
platform.init()
|
||||
entity = MockTimeEntity(
|
||||
name="test",
|
||||
unique_id="unique_time",
|
||||
native_value=time(1, 2, 3),
|
||||
)
|
||||
setup_test_component_platform(hass, DOMAIN, [entity])
|
||||
|
||||
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 time
|
||||
|
||||
from homeassistant.components.time import TimeEntity
|
||||
|
||||
from tests.common import MockEntity
|
||||
|
||||
UNIQUE_TIME = "unique_time"
|
||||
|
||||
ENTITIES = []
|
||||
|
||||
|
||||
class MockTimeEntity(MockEntity, TimeEntity):
|
||||
"""Mock time class."""
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the native value of this time."""
|
||||
return self._handle("native_value")
|
||||
|
||||
def set_value(self, value: time) -> None:
|
||||
"""Change the time."""
|
||||
self._values["native_value"] = value
|
||||
|
||||
|
||||
def init(empty=False):
|
||||
"""Initialize the platform with entities."""
|
||||
global ENTITIES
|
||||
|
||||
ENTITIES = (
|
||||
[]
|
||||
if empty
|
||||
else [
|
||||
MockTimeEntity(
|
||||
name="test",
|
||||
unique_id=UNIQUE_TIME,
|
||||
native_value=time(1, 2, 3),
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass, config, async_add_entities_callback, discovery_info=None
|
||||
):
|
||||
"""Return mock entities."""
|
||||
async_add_entities_callback(ENTITIES)
|
Loading…
Add table
Reference in a new issue