Skip to content

Commit

Permalink
Refactor Command Line cover to use ManualTriggerEntity (#93997)
Browse files Browse the repository at this point in the history
Refactor command_line cover
  • Loading branch information
gjohansson-ST authored Jun 3, 2023
1 parent 391c636 commit 76d8c04
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions homeassistant/components/command_line/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.template import Template
from homeassistant.helpers.template_entity import ManualTriggerEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify

Expand Down Expand Up @@ -90,16 +91,20 @@ async def async_setup_platform(
): # Backward compatibility. Can be removed after deprecation
device_config[CONF_NAME] = name

trigger_entity_config = {
CONF_UNIQUE_ID: device_config.get(CONF_UNIQUE_ID),
CONF_NAME: Template(device_config.get(CONF_NAME, device_name), hass),
}

covers.append(
CommandCover(
device_config.get(CONF_NAME, device_name),
trigger_entity_config,
device_config[CONF_COMMAND_OPEN],
device_config[CONF_COMMAND_CLOSE],
device_config[CONF_COMMAND_STOP],
device_config.get(CONF_COMMAND_STATE),
value_template,
device_config[CONF_COMMAND_TIMEOUT],
device_config.get(CONF_UNIQUE_ID),
device_config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL),
)
)
Expand All @@ -111,33 +116,31 @@ async def async_setup_platform(
async_add_entities(covers)


class CommandCover(CoverEntity):
class CommandCover(ManualTriggerEntity, CoverEntity):
"""Representation a command line cover."""

_attr_should_poll = False

def __init__(
self,
name: str,
config: ConfigType,
command_open: str,
command_close: str,
command_stop: str,
command_state: str | None,
value_template: Template | None,
timeout: int,
unique_id: str | None,
scan_interval: timedelta,
) -> None:
"""Initialize the cover."""
self._attr_name = name
super().__init__(self.hass, config)
self._state: int | None = None
self._command_open = command_open
self._command_close = command_close
self._command_stop = command_stop
self._command_state = command_state
self._value_template = value_template
self._timeout = timeout
self._attr_unique_id = unique_id
self._scan_interval = scan_interval
self._process_updates: asyncio.Lock | None = None

Expand Down Expand Up @@ -218,6 +221,7 @@ async def _async_update(self) -> None:
self._state = None
if payload:
self._state = int(payload)
self._process_manual_data(payload)
await self.async_update_ha_state(True)

async def async_open_cover(self, **kwargs: Any) -> None:
Expand Down

0 comments on commit 76d8c04

Please sign in to comment.