Skip to content

Commit 5dfa78a

Browse files
authored
Merge pull request #112 from akloeckner/feat/script
feat: allow full script syntax in event section
2 parents 801e5f6 + dc71991 commit 5dfa78a

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

custom_components/openhasp/__init__.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
1414
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
1515
from homeassistant.const import CONF_NAME, STATE_UNAVAILABLE, STATE_UNKNOWN
16-
from homeassistant.core import callback
16+
from homeassistant.core import callback, Context
1717
from homeassistant.exceptions import TemplateError
1818
from homeassistant.helpers import device_registry as dr, entity_registry
1919
import homeassistant.helpers.config_validation as cv
@@ -22,7 +22,7 @@
2222
from homeassistant.helpers.network import get_url
2323
from homeassistant.helpers.reload import async_integration_yaml_config
2424
from homeassistant.helpers.restore_state import RestoreEntity
25-
from homeassistant.helpers.service import async_call_from_config
25+
from homeassistant.helpers.script import Script
2626
from homeassistant.util import slugify
2727
import jsonschema
2828
import voluptuous as vol
@@ -103,7 +103,7 @@ def hasp_object(value):
103103

104104

105105
# Configuration YAML schemas
106-
EVENT_SCHEMA = cv.schema_with_slug_keys([cv.SERVICE_SCHEMA])
106+
EVENT_SCHEMA = cv.schema_with_slug_keys(cv.SCRIPT_SCHEMA)
107107

108108
PROPERTY_SCHEMA = cv.schema_with_slug_keys(cv.template)
109109

@@ -700,7 +700,10 @@ def __init__(self, hass, plate_topic, config):
700700
self.cached_properties = {}
701701

702702
self.properties = config.get(CONF_PROPERTIES)
703-
self.event_services = config.get(CONF_EVENT)
703+
self.event_services = {
704+
event: Script(hass, script, plate_topic, DOMAIN)
705+
for (event, script) in config[CONF_EVENT].items()
706+
}
704707
self._tracked_property_templates = []
705708
self._freeze_properties = []
706709
self._subscriptions = []
@@ -794,7 +797,7 @@ async def message_received(msg):
794797
elif message[HASP_EVENT] in [HASP_EVENT_UP, HASP_EVENT_RELEASE]:
795798
self._freeze_properties = []
796799

797-
for event in self.event_services:
800+
for event, script in self.event_services.items():
798801
if event in message[HASP_EVENT]:
799802
_LOGGER.debug(
800803
"Service call for '%s' triggered by '%s' on '%s' with variables %s",
@@ -803,13 +806,10 @@ async def message_received(msg):
803806
msg.topic,
804807
message,
805808
)
806-
for service in self.event_services[event]:
807-
await async_call_from_config(
808-
self.hass,
809-
service,
810-
validate_config=False,
811-
variables=message,
812-
)
809+
await script.async_run(
810+
run_variables=message,
811+
context=Context(),
812+
)
813813
except vol.error.Invalid:
814814
_LOGGER.debug(
815815
"Could not handle openHASP event: '%s' on '%s'",

0 commit comments

Comments
 (0)