From 391c63640a2c76f1e9b555c94e79834de613d286 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sat, 3 Jun 2023 20:11:42 +0200 Subject: [PATCH] Refactor Command Line binary sensor to use ManualTriggerEntity (#94000) Refactor binary sensor ManualTriggerEntity --- .../components/command_line/binary_sensor.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/command_line/binary_sensor.py b/homeassistant/components/command_line/binary_sensor.py index 9c5a1ce1bbe..d18321d6f02 100644 --- a/homeassistant/components/command_line/binary_sensor.py +++ b/homeassistant/components/command_line/binary_sensor.py @@ -29,6 +29,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback 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 .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, LOGGER @@ -94,16 +95,20 @@ async def async_setup_platform( value_template.hass = hass data = CommandSensorData(hass, command, command_timeout) + trigger_entity_config = { + CONF_UNIQUE_ID: unique_id, + CONF_NAME: Template(name, hass), + CONF_DEVICE_CLASS: device_class, + } + async_add_entities( [ CommandBinarySensor( data, - name, - device_class, + trigger_entity_config, payload_on, payload_off, value_template, - unique_id, scan_interval, ) ], @@ -111,7 +116,7 @@ async def async_setup_platform( ) -class CommandBinarySensor(BinarySensorEntity): +class CommandBinarySensor(ManualTriggerEntity, BinarySensorEntity): """Representation of a command line binary sensor.""" _attr_should_poll = False @@ -119,23 +124,19 @@ class CommandBinarySensor(BinarySensorEntity): def __init__( self, data: CommandSensorData, - name: str, - device_class: BinarySensorDeviceClass | None, + config: ConfigType, payload_on: str, payload_off: str, value_template: Template | None, - unique_id: str | None, scan_interval: timedelta, ) -> None: """Initialize the Command line binary sensor.""" + super().__init__(self.hass, config) self.data = data - self._attr_name = name - self._attr_device_class = device_class self._attr_is_on = None self._payload_on = payload_on self._payload_off = payload_off self._value_template = value_template - self._attr_unique_id = unique_id self._scan_interval = scan_interval self._process_updates: asyncio.Lock | None = None @@ -183,4 +184,5 @@ class CommandBinarySensor(BinarySensorEntity): elif value == self._payload_off: self._attr_is_on = False + self._process_manual_data(value) self.async_write_ha_state()