Add invert option to switch_as_x (#107535)

* Add invert option to switch_as_x

* Store invert flag in entity options

* Add options flow

* Update strings

* Add tests

* Address review comment

* Update homeassistant/components/switch_as_x/strings.json

Co-authored-by: G Johansson <goran.johansson@shiftit.se>

* Address review comments

* Inline get_suggested which was only used once in tests

* Address review comments

---------

Co-authored-by: G Johansson <goran.johansson@shiftit.se>
This commit is contained in:
Erik Montnemery 2024-01-24 16:35:08 +01:00 committed by GitHub
parent c3de193e2e
commit 4b2b4ae36b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 939 additions and 54 deletions

View file

@ -23,7 +23,8 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import EventStateChangedData
from homeassistant.helpers.typing import EventType
from .entity import BaseEntity
from .const import CONF_INVERT
from .entity import BaseInvertableEntity
async def async_setup_entry(
@ -43,6 +44,7 @@ async def async_setup_entry(
hass,
config_entry.title,
COVER_DOMAIN,
config_entry.options[CONF_INVERT],
entity_id,
config_entry.entry_id,
)
@ -50,7 +52,7 @@ async def async_setup_entry(
)
class CoverSwitch(BaseEntity, CoverEntity):
class CoverSwitch(BaseInvertableEntity, CoverEntity):
"""Represents a Switch as a Cover."""
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
@ -59,7 +61,7 @@ class CoverSwitch(BaseEntity, CoverEntity):
"""Open the cover."""
await self.hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
SERVICE_TURN_OFF if self._invert_state else SERVICE_TURN_ON,
{ATTR_ENTITY_ID: self._switch_entity_id},
blocking=True,
context=self._context,
@ -69,7 +71,7 @@ class CoverSwitch(BaseEntity, CoverEntity):
"""Close cover."""
await self.hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON if self._invert_state else SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: self._switch_entity_id},
blocking=True,
context=self._context,
@ -87,4 +89,7 @@ class CoverSwitch(BaseEntity, CoverEntity):
):
return
self._attr_is_closed = state.state != STATE_ON
if self._invert_state:
self._attr_is_closed = state.state == STATE_ON
else:
self._attr_is_closed = state.state != STATE_ON