[homeassistant-switch] Support different entity domains (#7331)

This commit is contained in:
Jesse Hills 2024-09-05 13:09:49 +12:00 committed by GitHub
parent b496233425
commit 1548fa0811
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 4 deletions

View File

@ -5,6 +5,19 @@ from esphome.const import CONF_ATTRIBUTE, CONF_ENTITY_ID, CONF_INTERNAL
CODEOWNERS = ["@OttoWinter", "@esphome/core"]
homeassistant_ns = cg.esphome_ns.namespace("homeassistant")
def validate_entity_domain(platform, supported_domains):
def validator(config):
domain = config[CONF_ENTITY_ID].split(".", 1)[0]
if domain not in supported_domains:
raise cv.Invalid(
f"Entity ID {config[CONF_ENTITY_ID]} is not supported by the {platform} platform."
)
return config
return validator
HOME_ASSISTANT_IMPORT_SCHEMA = cv.Schema(
{
cv.Required(CONF_ENTITY_ID): cv.entity_id,

View File

@ -7,19 +7,32 @@ from .. import (
HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA,
homeassistant_ns,
setup_home_assistant_entity,
validate_entity_domain,
)
CODEOWNERS = ["@Links2004"]
DEPENDENCIES = ["api"]
SUPPORTED_DOMAINS = [
"automation",
"fan",
"humidifier",
"input_boolean",
"light",
"remote",
"siren",
"switch",
]
HomeassistantSwitch = homeassistant_ns.class_(
"HomeassistantSwitch", switch.Switch, cg.Component
)
CONFIG_SCHEMA = (
CONFIG_SCHEMA = cv.All(
switch.switch_schema(HomeassistantSwitch)
.extend(cv.COMPONENT_SCHEMA)
.extend(HOME_ASSISTANT_IMPORT_CONTROL_SCHEMA)
.extend(cv.COMPONENT_SCHEMA),
validate_entity_domain("switch", SUPPORTED_DOMAINS),
)

View File

@ -42,9 +42,9 @@ void HomeassistantSwitch::write_state(bool state) {
api::HomeassistantServiceResponse resp;
if (state) {
resp.service = "switch.turn_on";
resp.service = "homeassistant.turn_on";
} else {
resp.service = "switch.turn_off";
resp.service = "homeassistant.turn_off";
}
api::HomeassistantServiceMap entity_id_kv;

View File

@ -33,6 +33,27 @@ wifi:
api:
switch:
- platform: homeassistant
entity_id: automation.my_cool_automation
id: my_cool_automation
- platform: homeassistant
entity_id: fan.my_cool_fan
id: my_cool_fan
- platform: homeassistant
entity_id: humidifier.my_cool_humidifier
id: my_cool_humidifier
- platform: homeassistant
entity_id: input_boolean.my_cool_input_boolean
id: my_cool_input_boolean
- platform: homeassistant
entity_id: light.my_cool_light
id: my_cool_light
- platform: homeassistant
entity_id: remote.my_cool_remote
id: my_cool_remote
- platform: homeassistant
entity_id: siren.my_cool_siren
id: my_cool_siren
- platform: homeassistant
entity_id: switch.my_cool_switch
id: my_cool_switch