Add input_datetime reload service. (#29581)
* Add input_datetime reload service. * Add reload service test.
This commit is contained in:
parent
ccb0fd5e32
commit
256056430e
3 changed files with 116 additions and 19 deletions
|
@ -2,20 +2,23 @@
|
|||
# pylint: disable=protected-access
|
||||
import asyncio
|
||||
import datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.core import CoreState, State, Context
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.components.input_datetime import (
|
||||
DOMAIN,
|
||||
ATTR_DATE,
|
||||
ATTR_DATETIME,
|
||||
ATTR_TIME,
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
SERVICE_SET_DATETIME,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import Context, CoreState, State
|
||||
from homeassistant.exceptions import Unauthorized
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import mock_restore_cache
|
||||
|
||||
|
@ -310,3 +313,65 @@ async def test_input_datetime_context(hass, hass_admin_user):
|
|||
assert state2 is not None
|
||||
assert state.state != state2.state
|
||||
assert state2.context.user_id == hass_admin_user.id
|
||||
|
||||
|
||||
async def test_reload(hass, hass_admin_user, hass_read_only_user):
|
||||
"""Test reload service."""
|
||||
count_start = len(hass.states.async_entity_ids())
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
DOMAIN,
|
||||
{
|
||||
DOMAIN: {
|
||||
"dt1": {"has_time": False, "has_date": True, "initial": "2019-1-1"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
assert count_start + 1 == len(hass.states.async_entity_ids())
|
||||
|
||||
state_1 = hass.states.get("input_datetime.dt1")
|
||||
state_2 = hass.states.get("input_datetime.dt2")
|
||||
|
||||
dt_obj = datetime.datetime(2019, 1, 1, 0, 0)
|
||||
assert state_1 is not None
|
||||
assert state_2 is None
|
||||
assert str(dt_obj.date()) == state_1.state
|
||||
|
||||
with patch(
|
||||
"homeassistant.config.load_yaml_config_file",
|
||||
autospec=True,
|
||||
return_value={
|
||||
DOMAIN: {
|
||||
"dt1": {"has_time": True, "has_date": False, "initial": "23:32"},
|
||||
"dt2": {"has_time": True, "has_date": True},
|
||||
}
|
||||
},
|
||||
):
|
||||
with patch("homeassistant.config.find_config_file", return_value=""):
|
||||
with pytest.raises(Unauthorized):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_read_only_user.id),
|
||||
)
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
blocking=True,
|
||||
context=Context(user_id=hass_admin_user.id),
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert count_start + 2 == len(hass.states.async_entity_ids())
|
||||
|
||||
state_1 = hass.states.get("input_datetime.dt1")
|
||||
state_2 = hass.states.get("input_datetime.dt2")
|
||||
|
||||
dt_obj = datetime.datetime(2019, 1, 1, 23, 32)
|
||||
assert state_1 is not None
|
||||
assert state_2 is not None
|
||||
assert str(dt_obj.time()) == state_1.state
|
||||
assert str(datetime.datetime(1970, 1, 1, 0, 0)) == state_2.state
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue