Make home_assistant imported sensors internal by default (#3372)

This commit is contained in:
Jesse Hills 2022-04-11 12:45:15 +12:00 committed by GitHub
parent 9e3e34acf5
commit efa6fd03e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 46 deletions

View file

@ -1,4 +1,20 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_INTERNAL
CODEOWNERS = ["@OttoWinter"] CODEOWNERS = ["@OttoWinter"]
homeassistant_ns = cg.esphome_ns.namespace("homeassistant") homeassistant_ns = cg.esphome_ns.namespace("homeassistant")
HOME_ASSISTANT_IMPORT_SCHEMA = cv.Schema(
{
cv.Required(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
cv.Optional(CONF_INTERNAL, default=True): cv.boolean,
}
)
def setup_home_assistant_entity(var, config):
cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config:
cg.add(var.set_attribute(config[CONF_ATTRIBUTE]))

View file

@ -1,30 +1,24 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor from esphome.components import binary_sensor
from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID
from .. import homeassistant_ns from .. import (
HOME_ASSISTANT_IMPORT_SCHEMA,
homeassistant_ns,
setup_home_assistant_entity,
)
DEPENDENCIES = ["api"] DEPENDENCIES = ["api"]
HomeassistantBinarySensor = homeassistant_ns.class_( HomeassistantBinarySensor = homeassistant_ns.class_(
"HomeassistantBinarySensor", binary_sensor.BinarySensor, cg.Component "HomeassistantBinarySensor", binary_sensor.BinarySensor, cg.Component
) )
CONFIG_SCHEMA = ( CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(HomeassistantBinarySensor).extend(
binary_sensor.binary_sensor_schema(HomeassistantBinarySensor) HOME_ASSISTANT_IMPORT_SCHEMA
.extend(
{
cv.Required(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
}
)
.extend(cv.COMPONENT_SCHEMA)
) )
async def to_code(config): async def to_code(config):
var = await binary_sensor.new_binary_sensor(config) var = await binary_sensor.new_binary_sensor(config)
await cg.register_component(var, config) await cg.register_component(var, config)
setup_home_assistant_entity(var, config)
cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config:
cg.add(var.set_attribute(config[CONF_ATTRIBUTE]))

View file

@ -1,12 +1,11 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor from esphome.components import sensor
from esphome.const import (
CONF_ATTRIBUTE, from .. import (
CONF_ENTITY_ID, HOME_ASSISTANT_IMPORT_SCHEMA,
CONF_ID, homeassistant_ns,
setup_home_assistant_entity,
) )
from .. import homeassistant_ns
DEPENDENCIES = ["api"] DEPENDENCIES = ["api"]
@ -14,19 +13,12 @@ HomeassistantSensor = homeassistant_ns.class_(
"HomeassistantSensor", sensor.Sensor, cg.Component "HomeassistantSensor", sensor.Sensor, cg.Component
) )
CONFIG_SCHEMA = sensor.sensor_schema(HomeassistantSensor, accuracy_decimals=1,).extend( CONFIG_SCHEMA = sensor.sensor_schema(HomeassistantSensor, accuracy_decimals=1).extend(
{ HOME_ASSISTANT_IMPORT_SCHEMA
cv.Required(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
}
) )
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = await sensor.new_sensor(config)
await cg.register_component(var, config) await cg.register_component(var, config)
await sensor.register_sensor(var, config) setup_home_assistant_entity(var, config)
cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config:
cg.add(var.set_attribute(config[CONF_ATTRIBUTE]))

View file

@ -1,9 +1,11 @@
import esphome.codegen as cg import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor from esphome.components import text_sensor
from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID
from .. import homeassistant_ns from .. import (
HOME_ASSISTANT_IMPORT_SCHEMA,
homeassistant_ns,
setup_home_assistant_entity,
)
DEPENDENCIES = ["api"] DEPENDENCIES = ["api"]
@ -11,19 +13,12 @@ HomeassistantTextSensor = homeassistant_ns.class_(
"HomeassistantTextSensor", text_sensor.TextSensor, cg.Component "HomeassistantTextSensor", text_sensor.TextSensor, cg.Component
) )
CONFIG_SCHEMA = text_sensor.text_sensor_schema().extend( CONFIG_SCHEMA = text_sensor.text_sensor_schema(HomeassistantTextSensor).extend(
{ HOME_ASSISTANT_IMPORT_SCHEMA
cv.GenerateID(): cv.declare_id(HomeassistantTextSensor),
cv.Required(CONF_ENTITY_ID): cv.entity_id,
cv.Optional(CONF_ATTRIBUTE): cv.string,
}
) )
async def to_code(config): async def to_code(config):
var = await text_sensor.new_text_sensor(config) var = await text_sensor.new_text_sensor(config)
await cg.register_component(var, config) await cg.register_component(var, config)
setup_home_assistant_entity(var, config)
cg.add(var.set_entity_id(config[CONF_ENTITY_ID]))
if CONF_ATTRIBUTE in config:
cg.add(var.set_attribute(config[CONF_ATTRIBUTE]))