Minor refactor of template vacuum (#61860)

pull/62147/head
Erik Montnemery 2021-12-16 20:53:19 +01:00 committed by GitHub
parent 80833aa7fb
commit 0e28fbbe83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 74 deletions

View File

@ -58,6 +58,14 @@ TEMPLATE_ENTITY_COMMON_SCHEMA = vol.Schema(
}
)
TEMPLATE_ENTITY_ATTRIBUTES_SCHEMA_LEGACY = vol.Schema(
{
vol.Optional(CONF_ATTRIBUTE_TEMPLATES, default={}): vol.Schema(
{cv.string: cv.template}
),
}
)
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY = vol.Schema(
{
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,

View File

@ -43,8 +43,13 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
from .template_entity import TemplateEntity
from .const import DOMAIN
from .template_entity import (
TEMPLATE_ENTITY_ATTRIBUTES_SCHEMA_LEGACY,
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY,
TemplateEntity,
rewrite_common_legacy_to_modern_conf,
)
_LOGGER = logging.getLogger(__name__)
@ -52,7 +57,6 @@ CONF_VACUUMS = "vacuums"
CONF_BATTERY_LEVEL_TEMPLATE = "battery_level_template"
CONF_FAN_SPEED_LIST = "fan_speeds"
CONF_FAN_SPEED_TEMPLATE = "fan_speed_template"
CONF_ATTRIBUTE_TEMPLATES = "attribute_templates"
ENTITY_ID_FORMAT = VACUUM_DOMAIN + ".{}"
_VALID_STATES = [
@ -72,10 +76,6 @@ VACUUM_SCHEMA = vol.All(
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_BATTERY_LEVEL_TEMPLATE): cv.template,
vol.Optional(CONF_FAN_SPEED_TEMPLATE): cv.template,
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
vol.Optional(CONF_ATTRIBUTE_TEMPLATES, default={}): vol.Schema(
{cv.string: cv.template}
),
vol.Required(SERVICE_START): cv.SCRIPT_SCHEMA,
vol.Optional(SERVICE_PAUSE): cv.SCRIPT_SCHEMA,
vol.Optional(SERVICE_STOP): cv.SCRIPT_SCHEMA,
@ -87,7 +87,9 @@ VACUUM_SCHEMA = vol.All(
vol.Optional(CONF_ENTITY_ID): cv.entity_ids,
vol.Optional(CONF_UNIQUE_ID): cv.string,
}
),
)
.extend(TEMPLATE_ENTITY_ATTRIBUTES_SCHEMA_LEGACY.schema)
.extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA_LEGACY.schema),
)
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
@ -99,44 +101,15 @@ async def _async_create_entities(hass, config):
"""Create the Template Vacuums."""
vacuums = []
for device, device_config in config[CONF_VACUUMS].items():
friendly_name = device_config.get(CONF_FRIENDLY_NAME, device)
state_template = device_config.get(CONF_VALUE_TEMPLATE)
battery_level_template = device_config.get(CONF_BATTERY_LEVEL_TEMPLATE)
fan_speed_template = device_config.get(CONF_FAN_SPEED_TEMPLATE)
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
attribute_templates = device_config.get(CONF_ATTRIBUTE_TEMPLATES)
start_action = device_config[SERVICE_START]
pause_action = device_config.get(SERVICE_PAUSE)
stop_action = device_config.get(SERVICE_STOP)
return_to_base_action = device_config.get(SERVICE_RETURN_TO_BASE)
clean_spot_action = device_config.get(SERVICE_CLEAN_SPOT)
locate_action = device_config.get(SERVICE_LOCATE)
set_fan_speed_action = device_config.get(SERVICE_SET_FAN_SPEED)
fan_speed_list = device_config[CONF_FAN_SPEED_LIST]
unique_id = device_config.get(CONF_UNIQUE_ID)
for object_id, entity_config in config[CONF_VACUUMS].items():
entity_config = rewrite_common_legacy_to_modern_conf(entity_config)
unique_id = entity_config.get(CONF_UNIQUE_ID)
vacuums.append(
TemplateVacuum(
hass,
device,
friendly_name,
state_template,
battery_level_template,
fan_speed_template,
availability_template,
start_action,
pause_action,
stop_action,
return_to_base_action,
clean_spot_action,
locate_action,
set_fan_speed_action,
fan_speed_list,
attribute_templates,
object_id,
entity_config,
unique_id,
)
)
@ -155,71 +128,55 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
def __init__(
self,
hass,
device_id,
friendly_name,
state_template,
battery_level_template,
fan_speed_template,
availability_template,
start_action,
pause_action,
stop_action,
return_to_base_action,
clean_spot_action,
locate_action,
set_fan_speed_action,
fan_speed_list,
attribute_templates,
object_id,
config,
unique_id,
):
"""Initialize the vacuum."""
super().__init__(
attribute_templates=attribute_templates,
availability_template=availability_template,
)
super().__init__(config=config)
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, device_id, hass=hass
ENTITY_ID_FORMAT, object_id, hass=hass
)
self._name = friendly_name
self._name = friendly_name = config.get(CONF_FRIENDLY_NAME, object_id)
self._template = state_template
self._battery_level_template = battery_level_template
self._fan_speed_template = fan_speed_template
self._template = config.get(CONF_VALUE_TEMPLATE)
self._battery_level_template = config.get(CONF_BATTERY_LEVEL_TEMPLATE)
self._fan_speed_template = config.get(CONF_FAN_SPEED_TEMPLATE)
self._supported_features = SUPPORT_START
self._start_script = Script(hass, start_action, friendly_name, DOMAIN)
self._start_script = Script(hass, config[SERVICE_START], friendly_name, DOMAIN)
self._pause_script = None
if pause_action:
if pause_action := config.get(SERVICE_PAUSE):
self._pause_script = Script(hass, pause_action, friendly_name, DOMAIN)
self._supported_features |= SUPPORT_PAUSE
self._stop_script = None
if stop_action:
if stop_action := config.get(SERVICE_STOP):
self._stop_script = Script(hass, stop_action, friendly_name, DOMAIN)
self._supported_features |= SUPPORT_STOP
self._return_to_base_script = None
if return_to_base_action:
if return_to_base_action := config.get(SERVICE_RETURN_TO_BASE):
self._return_to_base_script = Script(
hass, return_to_base_action, friendly_name, DOMAIN
)
self._supported_features |= SUPPORT_RETURN_HOME
self._clean_spot_script = None
if clean_spot_action:
if clean_spot_action := config.get(SERVICE_CLEAN_SPOT):
self._clean_spot_script = Script(
hass, clean_spot_action, friendly_name, DOMAIN
)
self._supported_features |= SUPPORT_CLEAN_SPOT
self._locate_script = None
if locate_action:
if locate_action := config.get(SERVICE_LOCATE):
self._locate_script = Script(hass, locate_action, friendly_name, DOMAIN)
self._supported_features |= SUPPORT_LOCATE
self._set_fan_speed_script = None
if set_fan_speed_action:
if set_fan_speed_action := config.get(SERVICE_SET_FAN_SPEED):
self._set_fan_speed_script = Script(
hass, set_fan_speed_action, friendly_name, DOMAIN
)
@ -237,7 +194,7 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
self._unique_id = unique_id
# List of valid fan speeds
self._fan_speed_list = fan_speed_list
self._fan_speed_list = config[CONF_FAN_SPEED_LIST]
@property
def name(self):