mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 06:17:07 +00:00
Refactor Command Line binary sensor to use ManualTriggerEntity (#94000)
Refactor binary sensor ManualTriggerEntity
This commit is contained in:
parent
305fa128fb
commit
391c63640a
@ -29,6 +29,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 .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, LOGGER
|
from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, LOGGER
|
||||||
@ -94,16 +95,20 @@ async def async_setup_platform(
|
|||||||
value_template.hass = hass
|
value_template.hass = hass
|
||||||
data = CommandSensorData(hass, command, command_timeout)
|
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(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
CommandBinarySensor(
|
CommandBinarySensor(
|
||||||
data,
|
data,
|
||||||
name,
|
trigger_entity_config,
|
||||||
device_class,
|
|
||||||
payload_on,
|
payload_on,
|
||||||
payload_off,
|
payload_off,
|
||||||
value_template,
|
value_template,
|
||||||
unique_id,
|
|
||||||
scan_interval,
|
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."""
|
"""Representation of a command line binary sensor."""
|
||||||
|
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
@ -119,23 +124,19 @@ class CommandBinarySensor(BinarySensorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
data: CommandSensorData,
|
data: CommandSensorData,
|
||||||
name: str,
|
config: ConfigType,
|
||||||
device_class: BinarySensorDeviceClass | None,
|
|
||||||
payload_on: str,
|
payload_on: str,
|
||||||
payload_off: str,
|
payload_off: str,
|
||||||
value_template: Template | None,
|
value_template: Template | None,
|
||||||
unique_id: str | None,
|
|
||||||
scan_interval: timedelta,
|
scan_interval: timedelta,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Command line binary sensor."""
|
"""Initialize the Command line binary sensor."""
|
||||||
|
super().__init__(self.hass, config)
|
||||||
self.data = data
|
self.data = data
|
||||||
self._attr_name = name
|
|
||||||
self._attr_device_class = device_class
|
|
||||||
self._attr_is_on = None
|
self._attr_is_on = None
|
||||||
self._payload_on = payload_on
|
self._payload_on = payload_on
|
||||||
self._payload_off = payload_off
|
self._payload_off = payload_off
|
||||||
self._value_template = value_template
|
self._value_template = value_template
|
||||||
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
|
||||||
|
|
||||||
@ -183,4 +184,5 @@ class CommandBinarySensor(BinarySensorEntity):
|
|||||||
elif value == self._payload_off:
|
elif value == self._payload_off:
|
||||||
self._attr_is_on = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
|
self._process_manual_data(value)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user