mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Fix unbound variable in sql when session setup fails (#90439)
Traceback (most recent call last): File "/Users/bdraco/home-assistant/homeassistant/helpers/entity_platform.py", line 304, in _async_setup_platform await asyncio.shield(task) File "/Users/bdraco/home-assistant/homeassistant/components/sql/sensor.py", line 75, in async_setup_platform await async_setup_sensor( File "/Users/bdraco/home-assistant/homeassistant/components/sql/sensor.py", line 150, in async_setup_sensor sessmaker := await hass.async_add_executor_job( File "/opt/homebrew/Cellar/python@3.10/3.10.9/Frameworks/Python.framework/Versions/3.10/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/Users/bdraco/home-assistant/homeassistant/components/sql/sensor.py", line 205, in _validate_and_get_session_maker_for_db_url if sess: UnboundLocalError: local variable 'sess' referenced before assignment
This commit is contained in:
parent
885be98f8f
commit
5dc96a6952
@ -180,11 +180,12 @@ def _validate_and_get_session_maker_for_db_url(db_url: str) -> scoped_session |
|
|||||||
|
|
||||||
This does I/O and should be run in the executor.
|
This does I/O and should be run in the executor.
|
||||||
"""
|
"""
|
||||||
|
sess: Session | None = None
|
||||||
try:
|
try:
|
||||||
engine = sqlalchemy.create_engine(db_url, future=True)
|
engine = sqlalchemy.create_engine(db_url, future=True)
|
||||||
sessmaker = scoped_session(sessionmaker(bind=engine, future=True))
|
sessmaker = scoped_session(sessionmaker(bind=engine, future=True))
|
||||||
# Run a dummy query just to test the db_url
|
# Run a dummy query just to test the db_url
|
||||||
sess: Session = sessmaker()
|
sess = sessmaker()
|
||||||
sess.execute(sqlalchemy.text("SELECT 1;"))
|
sess.execute(sqlalchemy.text("SELECT 1;"))
|
||||||
|
|
||||||
except SQLAlchemyError as err:
|
except SQLAlchemyError as err:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user