Fix visualization by inverting open/closed state of patio awnings (#128079)

This commit is contained in:
Marc Hörsken 2024-10-29 14:09:49 +01:00 committed by GitHub
parent c264ee22e7
commit 2c9ad9562e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 50 deletions

View File

@ -46,12 +46,12 @@ class WebControlProAwning(WebControlProGenericEntity, CoverEntity):
def current_cover_position(self) -> int | None: def current_cover_position(self) -> int | None:
"""Return current position of cover.""" """Return current position of cover."""
action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive) action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive)
return action["percentage"] return 100 - action["percentage"]
async def async_set_cover_position(self, **kwargs: Any) -> None: async def async_set_cover_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position.""" """Move the cover to a specific position."""
action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive) action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive)
await action(percentage=kwargs[ATTR_POSITION]) await action(percentage=100 - kwargs[ATTR_POSITION])
@property @property
def is_closed(self) -> bool | None: def is_closed(self) -> bool | None:
@ -61,12 +61,12 @@ class WebControlProAwning(WebControlProGenericEntity, CoverEntity):
async def async_open_cover(self, **kwargs: Any) -> None: async def async_open_cover(self, **kwargs: Any) -> None:
"""Open the cover.""" """Open the cover."""
action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive) action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive)
await action(percentage=100) await action(percentage=0)
async def async_close_cover(self, **kwargs: Any) -> None: async def async_close_cover(self, **kwargs: Any) -> None:
"""Close the cover.""" """Close the cover."""
action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive) action = self._dest.action(WMS_WebControl_pro_API_actionDescription.AwningDrive)
await action(percentage=0) await action(percentage=100)
async def async_stop_cover(self, **kwargs: Any) -> None: async def async_stop_cover(self, **kwargs: Any) -> None:
"""Stop the device if in motion.""" """Stop the device if in motion."""

View File

@ -35,7 +35,7 @@
StateSnapshot({ StateSnapshot({
'attributes': ReadOnlyDict({ 'attributes': ReadOnlyDict({
'attribution': 'Data provided by WMS WebControl pro API', 'attribution': 'Data provided by WMS WebControl pro API',
'current_position': 100, 'current_position': 0,
'device_class': 'awning', 'device_class': 'awning',
'friendly_name': 'Markise', 'friendly_name': 'Markise',
'supported_features': <CoverEntityFeature: 15>, 'supported_features': <CoverEntityFeature: 15>,
@ -45,6 +45,6 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': 'open', 'state': 'closed',
}) })
# --- # ---

View File

@ -2,24 +2,27 @@
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from freezegun.api import FrozenDateTimeFactory
from syrupy import SnapshotAssertion from syrupy import SnapshotAssertion
from homeassistant.components.wmspro.const import DOMAIN from homeassistant.components.wmspro.const import DOMAIN
from homeassistant.components.wmspro.cover import SCAN_INTERVAL
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_ENTITY_ID,
SERVICE_CLOSE_COVER, SERVICE_CLOSE_COVER,
SERVICE_OPEN_COVER, SERVICE_OPEN_COVER,
SERVICE_SET_COVER_POSITION, SERVICE_SET_COVER_POSITION,
SERVICE_STOP_COVER, SERVICE_STOP_COVER,
STATE_CLOSED,
STATE_OPEN,
Platform, Platform,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.setup import async_setup_component
from . import setup_config_entry from . import setup_config_entry
from tests.common import MockConfigEntry from tests.common import MockConfigEntry, async_fire_time_changed
async def test_cover_device( async def test_cover_device(
@ -48,6 +51,7 @@ async def test_cover_update(
mock_hub_ping: AsyncMock, mock_hub_ping: AsyncMock,
mock_hub_configuration_prod: AsyncMock, mock_hub_configuration_prod: AsyncMock,
mock_hub_status_prod_awning: AsyncMock, mock_hub_status_prod_awning: AsyncMock,
freezer: FrozenDateTimeFactory,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
) -> None: ) -> None:
"""Test that a cover entity is created and updated correctly.""" """Test that a cover entity is created and updated correctly."""
@ -60,18 +64,15 @@ async def test_cover_update(
assert entity is not None assert entity is not None
assert entity == snapshot assert entity == snapshot
await async_setup_component(hass, "homeassistant", {}) # Move time to next update
await hass.services.async_call( freezer.tick(SCAN_INTERVAL)
"homeassistant", async_fire_time_changed(hass)
"update_entity", await hass.async_block_till_done(wait_background_tasks=True)
{ATTR_ENTITY_ID: entity.entity_id},
blocking=True,
)
assert len(mock_hub_status_prod_awning.mock_calls) == 3 assert len(mock_hub_status_prod_awning.mock_calls) >= 3
async def test_cover_close_and_open( async def test_cover_open_and_close(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_hub_ping: AsyncMock, mock_hub_ping: AsyncMock,
@ -87,27 +88,8 @@ async def test_cover_close_and_open(
entity = hass.states.get("cover.markise") entity = hass.states.get("cover.markise")
assert entity is not None assert entity is not None
assert entity.state == "open" assert entity.state == STATE_CLOSED
assert entity.attributes["current_position"] == 100 assert entity.attributes["current_position"] == 0
with patch(
"wmspro.destination.Destination.refresh",
return_value=True,
):
before = len(mock_hub_status_prod_awning.mock_calls)
await hass.services.async_call(
Platform.COVER,
SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: entity.entity_id},
blocking=True,
)
entity = hass.states.get("cover.markise")
assert entity is not None
assert entity.state == "closed"
assert entity.attributes["current_position"] == 0
assert len(mock_hub_status_prod_awning.mock_calls) == before
with patch( with patch(
"wmspro.destination.Destination.refresh", "wmspro.destination.Destination.refresh",
@ -124,12 +106,31 @@ async def test_cover_close_and_open(
entity = hass.states.get("cover.markise") entity = hass.states.get("cover.markise")
assert entity is not None assert entity is not None
assert entity.state == "open" assert entity.state == STATE_OPEN
assert entity.attributes["current_position"] == 100 assert entity.attributes["current_position"] == 100
assert len(mock_hub_status_prod_awning.mock_calls) == before assert len(mock_hub_status_prod_awning.mock_calls) == before
with patch(
"wmspro.destination.Destination.refresh",
return_value=True,
):
before = len(mock_hub_status_prod_awning.mock_calls)
async def test_cover_move( await hass.services.async_call(
Platform.COVER,
SERVICE_CLOSE_COVER,
{ATTR_ENTITY_ID: entity.entity_id},
blocking=True,
)
entity = hass.states.get("cover.markise")
assert entity is not None
assert entity.state == STATE_CLOSED
assert entity.attributes["current_position"] == 0
assert len(mock_hub_status_prod_awning.mock_calls) == before
async def test_cover_open_to_pos(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_hub_ping: AsyncMock, mock_hub_ping: AsyncMock,
@ -137,7 +138,7 @@ async def test_cover_move(
mock_hub_status_prod_awning: AsyncMock, mock_hub_status_prod_awning: AsyncMock,
mock_action_call: AsyncMock, mock_action_call: AsyncMock,
) -> None: ) -> None:
"""Test that a cover entity is moved and closed correctly.""" """Test that a cover entity is opened to correct position."""
assert await setup_config_entry(hass, mock_config_entry) assert await setup_config_entry(hass, mock_config_entry)
assert len(mock_hub_ping.mock_calls) == 1 assert len(mock_hub_ping.mock_calls) == 1
assert len(mock_hub_configuration_prod.mock_calls) == 1 assert len(mock_hub_configuration_prod.mock_calls) == 1
@ -145,8 +146,8 @@ async def test_cover_move(
entity = hass.states.get("cover.markise") entity = hass.states.get("cover.markise")
assert entity is not None assert entity is not None
assert entity.state == "open" assert entity.state == STATE_CLOSED
assert entity.attributes["current_position"] == 100 assert entity.attributes["current_position"] == 0
with patch( with patch(
"wmspro.destination.Destination.refresh", "wmspro.destination.Destination.refresh",
@ -163,12 +164,12 @@ async def test_cover_move(
entity = hass.states.get("cover.markise") entity = hass.states.get("cover.markise")
assert entity is not None assert entity is not None
assert entity.state == "open" assert entity.state == STATE_OPEN
assert entity.attributes["current_position"] == 50 assert entity.attributes["current_position"] == 50
assert len(mock_hub_status_prod_awning.mock_calls) == before assert len(mock_hub_status_prod_awning.mock_calls) == before
async def test_cover_move_and_stop( async def test_cover_open_and_stop(
hass: HomeAssistant, hass: HomeAssistant,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
mock_hub_ping: AsyncMock, mock_hub_ping: AsyncMock,
@ -176,7 +177,7 @@ async def test_cover_move_and_stop(
mock_hub_status_prod_awning: AsyncMock, mock_hub_status_prod_awning: AsyncMock,
mock_action_call: AsyncMock, mock_action_call: AsyncMock,
) -> None: ) -> None:
"""Test that a cover entity is moved and closed correctly.""" """Test that a cover entity is opened and stopped correctly."""
assert await setup_config_entry(hass, mock_config_entry) assert await setup_config_entry(hass, mock_config_entry)
assert len(mock_hub_ping.mock_calls) == 1 assert len(mock_hub_ping.mock_calls) == 1
assert len(mock_hub_configuration_prod.mock_calls) == 1 assert len(mock_hub_configuration_prod.mock_calls) == 1
@ -184,8 +185,8 @@ async def test_cover_move_and_stop(
entity = hass.states.get("cover.markise") entity = hass.states.get("cover.markise")
assert entity is not None assert entity is not None
assert entity.state == "open" assert entity.state == STATE_CLOSED
assert entity.attributes["current_position"] == 100 assert entity.attributes["current_position"] == 0
with patch( with patch(
"wmspro.destination.Destination.refresh", "wmspro.destination.Destination.refresh",
@ -202,7 +203,7 @@ async def test_cover_move_and_stop(
entity = hass.states.get("cover.markise") entity = hass.states.get("cover.markise")
assert entity is not None assert entity is not None
assert entity.state == "open" assert entity.state == STATE_OPEN
assert entity.attributes["current_position"] == 80 assert entity.attributes["current_position"] == 80
assert len(mock_hub_status_prod_awning.mock_calls) == before assert len(mock_hub_status_prod_awning.mock_calls) == before
@ -221,6 +222,6 @@ async def test_cover_move_and_stop(
entity = hass.states.get("cover.markise") entity = hass.states.get("cover.markise")
assert entity is not None assert entity is not None
assert entity.state == "open" assert entity.state == STATE_OPEN
assert entity.attributes["current_position"] == 80 assert entity.attributes["current_position"] == 80
assert len(mock_hub_status_prod_awning.mock_calls) == before assert len(mock_hub_status_prod_awning.mock_calls) == before