Refactor Command Line cover to use ManualTriggerEntity (#93997)

Refactor command_line cover
This commit is contained in:
G Johansson 2023-06-03 20:19:59 +02:00 committed by GitHub
parent 391c63640a
commit 76d8c047ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,6 +30,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.template import Template from homeassistant.helpers.template import Template
from homeassistant.helpers.template_entity import ManualTriggerEntity
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify from homeassistant.util import slugify
@ -90,16 +91,20 @@ async def async_setup_platform(
): # Backward compatibility. Can be removed after deprecation ): # Backward compatibility. Can be removed after deprecation
device_config[CONF_NAME] = name 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( covers.append(
CommandCover( CommandCover(
device_config.get(CONF_NAME, device_name), trigger_entity_config,
device_config[CONF_COMMAND_OPEN], device_config[CONF_COMMAND_OPEN],
device_config[CONF_COMMAND_CLOSE], device_config[CONF_COMMAND_CLOSE],
device_config[CONF_COMMAND_STOP], device_config[CONF_COMMAND_STOP],
device_config.get(CONF_COMMAND_STATE), device_config.get(CONF_COMMAND_STATE),
value_template, value_template,
device_config[CONF_COMMAND_TIMEOUT], device_config[CONF_COMMAND_TIMEOUT],
device_config.get(CONF_UNIQUE_ID),
device_config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL), device_config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL),
) )
) )
@ -111,25 +116,24 @@ async def async_setup_platform(
async_add_entities(covers) async_add_entities(covers)
class CommandCover(CoverEntity): class CommandCover(ManualTriggerEntity, CoverEntity):
"""Representation a command line cover.""" """Representation a command line cover."""
_attr_should_poll = False _attr_should_poll = False
def __init__( def __init__(
self, self,
name: str, config: ConfigType,
command_open: str, command_open: str,
command_close: str, command_close: str,
command_stop: str, command_stop: str,
command_state: str | None, command_state: str | None,
value_template: Template | None, value_template: Template | None,
timeout: int, timeout: int,
unique_id: str | None,
scan_interval: timedelta, scan_interval: timedelta,
) -> None: ) -> None:
"""Initialize the cover.""" """Initialize the cover."""
self._attr_name = name super().__init__(self.hass, config)
self._state: int | None = None self._state: int | None = None
self._command_open = command_open self._command_open = command_open
self._command_close = command_close self._command_close = command_close
@ -137,7 +141,6 @@ class CommandCover(CoverEntity):
self._command_state = command_state self._command_state = command_state
self._value_template = value_template self._value_template = value_template
self._timeout = timeout self._timeout = timeout
self._attr_unique_id = unique_id
self._scan_interval = scan_interval self._scan_interval = scan_interval
self._process_updates: asyncio.Lock | None = None self._process_updates: asyncio.Lock | None = None
@ -218,6 +221,7 @@ class CommandCover(CoverEntity):
self._state = None self._state = None
if payload: if payload:
self._state = int(payload) self._state = int(payload)
self._process_manual_data(payload)
await self.async_update_ha_state(True) await self.async_update_ha_state(True)
async def async_open_cover(self, **kwargs: Any) -> None: async def async_open_cover(self, **kwargs: Any) -> None: