mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Fix cover template: optimistic mode is ignored (#87925)
* Fix cover template: optimistic mode is ignored (#84334) * Fix invalid check in is_closed (make test pass) * Add test for non optimistic cover * Update homeassistant/components/template/cover.py Co-authored-by: Erik Montnemery <erik@montnemery.com> * Update test_cover.py * Fix format * Fix tests --------- Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
parent
f40b712664
commit
2314d15761
@ -172,7 +172,7 @@ class CoverTemplate(TemplateEntity, CoverEntity):
|
|||||||
self._tilt_script = Script(hass, tilt_action, friendly_name, DOMAIN)
|
self._tilt_script = Script(hass, tilt_action, friendly_name, DOMAIN)
|
||||||
optimistic = config.get(CONF_OPTIMISTIC)
|
optimistic = config.get(CONF_OPTIMISTIC)
|
||||||
self._optimistic = optimistic or (
|
self._optimistic = optimistic or (
|
||||||
not self._template and not self._position_template
|
optimistic is None and not self._template and not self._position_template
|
||||||
)
|
)
|
||||||
tilt_optimistic = config.get(CONF_TILT_OPTIMISTIC)
|
tilt_optimistic = config.get(CONF_TILT_OPTIMISTIC)
|
||||||
self._tilt_optimistic = tilt_optimistic or not self._tilt_template
|
self._tilt_optimistic = tilt_optimistic or not self._tilt_template
|
||||||
@ -270,8 +270,11 @@ class CoverTemplate(TemplateEntity, CoverEntity):
|
|||||||
self._tilt_value = state
|
self._tilt_value = state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_closed(self) -> bool:
|
def is_closed(self) -> bool | None:
|
||||||
"""Return if the cover is closed."""
|
"""Return if the cover is closed."""
|
||||||
|
if self._position is None:
|
||||||
|
return None
|
||||||
|
|
||||||
return self._position == 0
|
return self._position == 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -21,6 +21,7 @@ from homeassistant.const import (
|
|||||||
STATE_OPEN,
|
STATE_OPEN,
|
||||||
STATE_OPENING,
|
STATE_OPENING,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
@ -80,7 +81,7 @@ OPEN_CLOSE_COVER_CONFIG = {
|
|||||||
(
|
(
|
||||||
"cover.test_state",
|
"cover.test_state",
|
||||||
"cat",
|
"cat",
|
||||||
STATE_OPEN,
|
STATE_UNKNOWN,
|
||||||
{},
|
{},
|
||||||
-1,
|
-1,
|
||||||
"Received invalid cover is_on state: cat",
|
"Received invalid cover is_on state: cat",
|
||||||
@ -89,7 +90,7 @@ OPEN_CLOSE_COVER_CONFIG = {
|
|||||||
(
|
(
|
||||||
"cover.test_state",
|
"cover.test_state",
|
||||||
"bear",
|
"bear",
|
||||||
STATE_OPEN,
|
STATE_UNKNOWN,
|
||||||
{},
|
{},
|
||||||
-1,
|
-1,
|
||||||
"Received invalid cover is_on state: bear",
|
"Received invalid cover is_on state: bear",
|
||||||
@ -112,8 +113,8 @@ OPEN_CLOSE_COVER_CONFIG = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
("cover.test_state", STATE_OPEN, STATE_OPEN, {}, -1, ""),
|
("cover.test_state", STATE_OPEN, STATE_UNKNOWN, {}, -1, ""),
|
||||||
("cover.test_state", STATE_CLOSED, STATE_OPEN, {}, -1, ""),
|
("cover.test_state", STATE_CLOSED, STATE_UNKNOWN, {}, -1, ""),
|
||||||
("cover.test_state", STATE_OPENING, STATE_OPENING, {}, -1, ""),
|
("cover.test_state", STATE_OPENING, STATE_OPENING, {}, -1, ""),
|
||||||
("cover.test_state", STATE_CLOSING, STATE_CLOSING, {}, -1, ""),
|
("cover.test_state", STATE_CLOSING, STATE_CLOSING, {}, -1, ""),
|
||||||
("cover.test", STATE_CLOSED, STATE_CLOSING, {"position": 0}, 0, ""),
|
("cover.test", STATE_CLOSED, STATE_CLOSING, {"position": 0}, 0, ""),
|
||||||
@ -136,7 +137,7 @@ async def test_template_state_text(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Test the state text of a template."""
|
"""Test the state text of a template."""
|
||||||
state = hass.states.get("cover.test_template_cover")
|
state = hass.states.get("cover.test_template_cover")
|
||||||
assert state.state == STATE_OPEN
|
assert state.state == STATE_UNKNOWN
|
||||||
|
|
||||||
for entity, set_state, test_state, attr, pos, text in states:
|
for entity, set_state, test_state, attr, pos, text in states:
|
||||||
hass.states.async_set(entity, set_state, attributes=attr)
|
hass.states.async_set(entity, set_state, attributes=attr)
|
||||||
@ -207,6 +208,29 @@ async def test_template_position(hass: HomeAssistant, start_ha) -> None:
|
|||||||
assert state.state == test_state
|
assert state.state == test_state
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(("count", "domain"), [(1, DOMAIN)])
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"config",
|
||||||
|
[
|
||||||
|
{
|
||||||
|
DOMAIN: {
|
||||||
|
"platform": "template",
|
||||||
|
"covers": {
|
||||||
|
"test_template_cover": {
|
||||||
|
**OPEN_CLOSE_COVER_CONFIG,
|
||||||
|
"optimistic": False,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_template_not_optimistic(hass, start_ha):
|
||||||
|
"""Test the is_closed attribute."""
|
||||||
|
state = hass.states.get("cover.test_template_cover")
|
||||||
|
assert state.state == STATE_UNKNOWN
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(("count", "domain"), [(1, DOMAIN)])
|
@pytest.mark.parametrize(("count", "domain"), [(1, DOMAIN)])
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"config",
|
"config",
|
||||||
@ -428,7 +452,7 @@ async def test_set_position(hass: HomeAssistant, start_ha, calls) -> None:
|
|||||||
state = hass.states.async_set("input_number.test", 42)
|
state = hass.states.async_set("input_number.test", 42)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get("cover.test_template_cover")
|
state = hass.states.get("cover.test_template_cover")
|
||||||
assert state.state == STATE_OPEN
|
assert state.state == STATE_UNKNOWN
|
||||||
|
|
||||||
await hass.services.async_call(
|
await hass.services.async_call(
|
||||||
DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True
|
DOMAIN, SERVICE_OPEN_COVER, {ATTR_ENTITY_ID: ENTITY_COVER}, blocking=True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user