Skip to content

Commit

Permalink
Add sua as optimistic switch (Closes: #26) (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Feb 24, 2022
1 parent ac5f7fd commit 2dc27c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions custom_components/goecharger_mqtt/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class GoEChargerSwitchEntityDescription(
domain: str = "switch"
payload_on: str = "true"
payload_off: str = "false"
optimistic: bool = False


@dataclass
Expand Down Expand Up @@ -2618,6 +2619,15 @@ def transform_code(value, mapping_table) -> str:
entity_registry_enabled_default=True,
disabled=False,
),
GoEChargerSwitchEntityDescription(
key="sua",
name="Simulate unplugging permanently",
optimistic=True,
entity_category=EntityCategory.CONFIG,
device_class=None,
entity_registry_enabled_default=True,
disabled=False,
),
)

NUMBERS: tuple[GoEChargerNumberEntityDescription, ...] = (
Expand Down
19 changes: 18 additions & 1 deletion custom_components/goecharger_mqtt/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,40 @@ def __init__(
super().__init__(config_entry, description)

self.entity_description = description
self._optimistic = self.entity_description.optimistic

@property
def available(self):
"""Return True if entity is available."""
return self._attr_is_on is not None
if self._optimistic:
return self._topic is not None

return self._topic is not None

@property
def assumed_state(self):
"""Return true if we do optimistic updates."""
return self._optimistic

async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
await mqtt.async_publish(
self.hass, f"{self._topic}/set", self.entity_description.payload_on
)
if self._optimistic:
# Optimistically assume that switch has changed state.
self._attr_is_on = True
self.async_write_ha_state()

async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
await mqtt.async_publish(
self.hass, f"{self._topic}/set", self.entity_description.payload_off
)
if self._optimistic:
# Optimistically assume that switch has changed state.
self._attr_is_on = False
self.async_write_ha_state()

async def async_added_to_hass(self):
"""Subscribe to MQTT events."""
Expand Down

0 comments on commit 2dc27c8

Please sign in to comment.