Skip to content

Commit e4c7935

Browse files
dlashuaraman325
authored andcommitted
simplify context retrieval code
1 parent f7af97e commit e4c7935

File tree

3 files changed

+6
-14
lines changed

3 files changed

+6
-14
lines changed

custom_components/pyscript/function.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ async def event_fire(cls, event_type, **kwargs):
125125
if "context" in kwargs and isinstance(kwargs["context"], Context):
126126
context = kwargs["context"]
127127
del kwargs["context"]
128-
elif curr_task in cls.task2context:
129-
context = cls.task2context[curr_task]
130128
else:
131-
context = Context()
129+
context = cls.task2context.get(curr_task, None)
132130

133131
cls.hass.bus.async_fire(event_type, kwargs, context=context)
134132

@@ -195,10 +193,8 @@ async def service_call(cls, domain, name, **kwargs):
195193
if "context" in kwargs and isinstance(kwargs["context"], Context):
196194
context = kwargs["context"]
197195
del kwargs["context"]
198-
elif curr_task in cls.task2context:
199-
context = cls.task2context[curr_task]
200196
else:
201-
context = Context()
197+
context = cls.task2context.get(curr_task, None)
202198

203199
await cls.hass.services.async_call(domain, name, kwargs, context=context)
204200

@@ -261,10 +257,8 @@ async def service_call(*args, **kwargs):
261257
if "context" in kwargs and isinstance(kwargs["context"], Context):
262258
context = kwargs["context"]
263259
del kwargs["context"]
264-
elif curr_task in cls.task2context:
265-
context = cls.task2context[curr_task]
266260
else:
267-
context = Context()
261+
context = cls.task2context.get(curr_task, None)
268262

269263
await cls.hass.services.async_call(domain, service, kwargs, context=context)
270264

custom_components/pyscript/state.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ async def set(cls, var_name, value, new_attributes=None, **kwargs):
122122
if "context" in kwargs and isinstance(kwargs["context"], Context):
123123
context = kwargs["context"]
124124
del kwargs["context"]
125-
elif curr_task in Function.task2context:
126-
context = Function.task2context[curr_task]
127125
else:
128-
context = Context()
126+
context = Function.task2context.get(curr_task, None)
129127

130128
if kwargs:
131129
new_attributes = new_attributes.copy()

custom_components/pyscript/trigger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ async def trigger_watch(self):
831831
if "context" in func_args and isinstance(func_args["context"], Context):
832832
hass_context = Context(parent_id=func_args["context"].id)
833833
else:
834-
hass_context = Context()
834+
hass_context = None
835835

836836
# Fire an event indicating that pyscript is running
837837
# Note: the event must have an entity_id for logbook to work correctly.
@@ -851,7 +851,7 @@ async def trigger_watch(self):
851851
async def do_func_call(func, ast_ctx, task_unique, task_unique_func, hass_context, **kwargs):
852852
# Store HASS Context for this Task
853853
Function.store_hass_context(hass_context)
854-
854+
855855
if task_unique and task_unique_func:
856856
await task_unique_func(task_unique)
857857
await func.call(ast_ctx, **kwargs)

0 commit comments

Comments
 (0)