Refactor Command line binary sensor to inherit TemplateEntity (#81212)
* Refactor binary sensor * Alignpull/88140/head^2
parent
5e73ad9cb0
commit
5c42261210
|
@ -23,8 +23,12 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.reload import setup_reload_service
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
from homeassistant.helpers.template import Template
|
||||
from homeassistant.helpers.template_entity import (
|
||||
TEMPLATE_ENTITY_BASE_SCHEMA,
|
||||
TemplateEntity,
|
||||
)
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, PLATFORMS
|
||||
|
@ -51,17 +55,21 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
)
|
||||
|
||||
|
||||
def setup_platform(
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Command line Binary Sensor."""
|
||||
|
||||
setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
|
||||
name: str = config[CONF_NAME]
|
||||
binary_sensor_config = vol.Schema(
|
||||
TEMPLATE_ENTITY_BASE_SCHEMA.schema, extra=vol.REMOVE_EXTRA
|
||||
)(config)
|
||||
|
||||
name: str = config.get(CONF_NAME, DEFAULT_NAME)
|
||||
command: str = config[CONF_COMMAND]
|
||||
payload_off: str = config[CONF_PAYLOAD_OFF]
|
||||
payload_on: str = config[CONF_PAYLOAD_ON]
|
||||
|
@ -73,9 +81,11 @@ def setup_platform(
|
|||
value_template.hass = hass
|
||||
data = CommandSensorData(hass, command, command_timeout)
|
||||
|
||||
add_entities(
|
||||
async_add_entities(
|
||||
[
|
||||
CommandBinarySensor(
|
||||
hass,
|
||||
binary_sensor_config,
|
||||
data,
|
||||
name,
|
||||
device_class,
|
||||
|
@ -89,11 +99,13 @@ def setup_platform(
|
|||
)
|
||||
|
||||
|
||||
class CommandBinarySensor(BinarySensorEntity):
|
||||
class CommandBinarySensor(TemplateEntity, BinarySensorEntity):
|
||||
"""Representation of a command line binary sensor."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
data: CommandSensorData,
|
||||
name: str,
|
||||
device_class: BinarySensorDeviceClass | None,
|
||||
|
@ -103,22 +115,29 @@ class CommandBinarySensor(BinarySensorEntity):
|
|||
unique_id: str | None,
|
||||
) -> None:
|
||||
"""Initialize the Command line binary sensor."""
|
||||
TemplateEntity.__init__(
|
||||
self,
|
||||
hass,
|
||||
config=config,
|
||||
fallback_name=name,
|
||||
unique_id=unique_id,
|
||||
)
|
||||
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
|
||||
|
||||
def update(self) -> None:
|
||||
async def async_update(self) -> None:
|
||||
"""Get the latest data and updates the state."""
|
||||
self.data.update()
|
||||
await self.hass.async_add_executor_job(self.data.update)
|
||||
value = self.data.value
|
||||
|
||||
if self._value_template is not None:
|
||||
value = self._value_template.render_with_possible_json_value(value, False)
|
||||
value = await self.hass.async_add_executor_job(
|
||||
self._value_template.render_with_possible_json_value, value, False
|
||||
)
|
||||
if value == self._payload_on:
|
||||
self._attr_is_on = True
|
||||
elif value == self._payload_off:
|
||||
|
|
Loading…
Reference in New Issue