1 """Support for custom shell commands to retrieve values."""
3 from __future__
import annotations
6 from datetime
import datetime, timedelta
7 from typing
import cast
26 from .const
import CONF_COMMAND_TIMEOUT, LOGGER, TRIGGER_ENTITY_OPTIONS
27 from .sensor
import CommandSensorData
29 DEFAULT_NAME =
"Binary Command Sensor"
30 DEFAULT_PAYLOAD_ON =
"ON"
31 DEFAULT_PAYLOAD_OFF =
"OFF"
39 async_add_entities: AddEntitiesCallback,
40 discovery_info: DiscoveryInfoType |
None =
None,
42 """Set up the Command line Binary Sensor."""
43 if not discovery_info:
46 discovery_info = cast(DiscoveryInfoType, discovery_info)
47 binary_sensor_config = discovery_info
49 command: str = binary_sensor_config[CONF_COMMAND]
50 payload_off: str = binary_sensor_config[CONF_PAYLOAD_OFF]
51 payload_on: str = binary_sensor_config[CONF_PAYLOAD_ON]
52 command_timeout: int = binary_sensor_config[CONF_COMMAND_TIMEOUT]
53 scan_interval: timedelta = binary_sensor_config.get(
54 CONF_SCAN_INTERVAL, SCAN_INTERVAL
56 value_template: Template |
None = binary_sensor_config.get(CONF_VALUE_TEMPLATE)
60 trigger_entity_config = {
61 CONF_NAME:
Template(binary_sensor_config.get(CONF_NAME, DEFAULT_NAME), hass),
63 k: v
for k, v
in binary_sensor_config.items()
if k
in TRIGGER_ENTITY_OPTIONS
71 trigger_entity_config,
82 """Representation of a command line binary sensor."""
84 _attr_should_poll =
False
88 data: CommandSensorData,
92 value_template: Template |
None,
93 scan_interval: timedelta,
95 """Initialize the Command line binary sensor."""
106 """Call when entity about to be added to hass."""
114 name=f
"Command Line Binary Sensor - {self.name}",
115 cancel_on_shutdown=
True,
120 """Update the state of the entity."""
125 "Updating Command Line Binary Sensor %s took longer than the scheduled update interval %s",
135 """Get the latest data and updates the state."""
137 value = self.
datadata.value
140 value = self.
_value_template_value_template.async_render_with_possible_json_value(
153 """Update the entity.
155 Only used by the generic entity update service.
None async_added_to_hass(self)
None _update_entity_state(self, datetime|None now=None)
None __init__(self, CommandSensorData data, ConfigType config, str payload_on, str payload_off, Template|None value_template, timedelta scan_interval)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
str|UndefinedType|None name(self)
None _process_manual_data(self, Any|None value=None)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)