mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Refactor Command line binary sensor to inherit TemplateEntity (#81212)
* Refactor binary sensor * Align
This commit is contained in:
parent
5e73ad9cb0
commit
5c42261210
@ -23,8 +23,12 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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 import Template
|
||||||
|
from homeassistant.helpers.template_entity import (
|
||||||
|
TEMPLATE_ENTITY_BASE_SCHEMA,
|
||||||
|
TemplateEntity,
|
||||||
|
)
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, PLATFORMS
|
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,
|
hass: HomeAssistant,
|
||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Command line Binary Sensor."""
|
"""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]
|
command: str = config[CONF_COMMAND]
|
||||||
payload_off: str = config[CONF_PAYLOAD_OFF]
|
payload_off: str = config[CONF_PAYLOAD_OFF]
|
||||||
payload_on: str = config[CONF_PAYLOAD_ON]
|
payload_on: str = config[CONF_PAYLOAD_ON]
|
||||||
@ -73,9 +81,11 @@ def setup_platform(
|
|||||||
value_template.hass = hass
|
value_template.hass = hass
|
||||||
data = CommandSensorData(hass, command, command_timeout)
|
data = CommandSensorData(hass, command, command_timeout)
|
||||||
|
|
||||||
add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
CommandBinarySensor(
|
CommandBinarySensor(
|
||||||
|
hass,
|
||||||
|
binary_sensor_config,
|
||||||
data,
|
data,
|
||||||
name,
|
name,
|
||||||
device_class,
|
device_class,
|
||||||
@ -89,11 +99,13 @@ def setup_platform(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class CommandBinarySensor(BinarySensorEntity):
|
class CommandBinarySensor(TemplateEntity, BinarySensorEntity):
|
||||||
"""Representation of a command line binary sensor."""
|
"""Representation of a command line binary sensor."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: ConfigType,
|
||||||
data: CommandSensorData,
|
data: CommandSensorData,
|
||||||
name: str,
|
name: str,
|
||||||
device_class: BinarySensorDeviceClass | None,
|
device_class: BinarySensorDeviceClass | None,
|
||||||
@ -103,22 +115,29 @@ class CommandBinarySensor(BinarySensorEntity):
|
|||||||
unique_id: str | None,
|
unique_id: str | None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Command line binary sensor."""
|
"""Initialize the Command line binary sensor."""
|
||||||
|
TemplateEntity.__init__(
|
||||||
|
self,
|
||||||
|
hass,
|
||||||
|
config=config,
|
||||||
|
fallback_name=name,
|
||||||
|
unique_id=unique_id,
|
||||||
|
)
|
||||||
self.data = data
|
self.data = data
|
||||||
self._attr_name = name
|
|
||||||
self._attr_device_class = device_class
|
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
|
|
||||||
|
|
||||||
def update(self) -> None:
|
async def async_update(self) -> None:
|
||||||
"""Get the latest data and updates the state."""
|
"""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
|
value = self.data.value
|
||||||
|
|
||||||
if self._value_template is not None:
|
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:
|
if value == self._payload_on:
|
||||||
self._attr_is_on = True
|
self._attr_is_on = True
|
||||||
elif value == self._payload_off:
|
elif value == self._payload_off:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user