Fix cover group to handle unknown state properly (#56739)

* fix cover group unknown state

* fix cover grup state

* fix cover group issue
This commit is contained in:
Regev Brody 2021-09-29 09:37:16 +03:00 committed by GitHub
parent 34ef47db55
commit 115bb39c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View File

@ -37,6 +37,7 @@ from homeassistant.const import (
CONF_ENTITIES,
CONF_NAME,
CONF_UNIQUE_ID,
STATE_CLOSED,
STATE_CLOSING,
STATE_OPEN,
STATE_OPENING,
@ -85,7 +86,7 @@ async def async_setup_platform(
class CoverGroup(GroupEntity, CoverEntity):
"""Representation of a CoverGroup."""
_attr_is_closed: bool | None = False
_attr_is_closed: bool | None = None
_attr_is_opening: bool | None = False
_attr_is_closing: bool | None = False
_attr_current_cover_position: int | None = 100
@ -258,7 +259,7 @@ class CoverGroup(GroupEntity, CoverEntity):
"""Update state and attributes."""
self._attr_assumed_state = False
self._attr_is_closed = True
self._attr_is_closed = None
self._attr_is_closing = False
self._attr_is_opening = False
for entity_id in self._entities:
@ -268,6 +269,9 @@ class CoverGroup(GroupEntity, CoverEntity):
if state.state == STATE_OPEN:
self._attr_is_closed = False
continue
if state.state == STATE_CLOSED:
self._attr_is_closed = True
continue
if state.state == STATE_CLOSING:
self._attr_is_closing = True
continue

View File

@ -32,6 +32,7 @@ from homeassistant.const import (
STATE_CLOSING,
STATE_OPEN,
STATE_OPENING,
STATE_UNKNOWN,
)
from homeassistant.helpers import entity_registry as er
from homeassistant.setup import async_setup_component
@ -99,7 +100,7 @@ async def setup_comp(hass, config_count):
async def test_attributes(hass, setup_comp):
"""Test handling of state attributes."""
state = hass.states.get(COVER_GROUP)
assert state.state == STATE_CLOSED
assert state.state == STATE_UNKNOWN
assert state.attributes[ATTR_FRIENDLY_NAME] == DEFAULT_NAME
assert state.attributes[ATTR_ENTITY_ID] == [
DEMO_COVER,
@ -112,6 +113,34 @@ async def test_attributes(hass, setup_comp):
assert ATTR_CURRENT_POSITION not in state.attributes
assert ATTR_CURRENT_TILT_POSITION not in state.attributes
# Set entity as closed
hass.states.async_set(DEMO_COVER, STATE_CLOSED, {})
await hass.async_block_till_done()
state = hass.states.get(COVER_GROUP)
assert state.state == STATE_CLOSED
# Set entity as opening
hass.states.async_set(DEMO_COVER, STATE_OPENING, {})
await hass.async_block_till_done()
state = hass.states.get(COVER_GROUP)
assert state.state == STATE_OPENING
# Set entity as closing
hass.states.async_set(DEMO_COVER, STATE_CLOSING, {})
await hass.async_block_till_done()
state = hass.states.get(COVER_GROUP)
assert state.state == STATE_CLOSING
# Set entity as unknown again
hass.states.async_set(DEMO_COVER, STATE_UNKNOWN, {})
await hass.async_block_till_done()
state = hass.states.get(COVER_GROUP)
assert state.state == STATE_UNKNOWN
# Add Entity that supports open / close / stop
hass.states.async_set(DEMO_COVER, STATE_OPEN, {ATTR_SUPPORTED_FEATURES: 11})
await hass.async_block_till_done()