Skip to content

Commit 6c56861

Browse files
committed
made #828 backward compatible for HA < 2026.5
also down-revd ruff target-version to py313
1 parent 6ad0e19 commit 6c56861

4 files changed

Lines changed: 58 additions & 20 deletions

File tree

custom_components/pyscript/jupyter_kernel.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ async def control_listen(self, reader, writer):
610610
await self.housekeep_q.put(["shutdown"])
611611
except asyncio.CancelledError:
612612
raise
613-
except EOFError, ConnectionResetError:
613+
except (EOFError, ConnectionResetError):
614614
_LOGGER.debug("control_listen got eof")
615615
await self.housekeep_q.put(["unregister", "control", asyncio.current_task()])
616616
control_socket.close()
@@ -630,7 +630,7 @@ async def stdin_listen(self, reader, writer):
630630
# _LOGGER.debug("stdin_listen received %s", _)
631631
except asyncio.CancelledError:
632632
raise
633-
except EOFError, ConnectionResetError:
633+
except (EOFError, ConnectionResetError):
634634
_LOGGER.debug("stdin_listen got eof")
635635
await self.housekeep_q.put(["unregister", "stdin", asyncio.current_task()])
636636
stdin_socket.close()
@@ -651,7 +651,7 @@ async def shell_listen(self, reader, writer):
651651
except asyncio.CancelledError:
652652
shell_socket.close()
653653
raise
654-
except EOFError, ConnectionResetError:
654+
except (EOFError, ConnectionResetError):
655655
_LOGGER.debug("shell_listen got eof")
656656
await self.housekeep_q.put(["unregister", "shell", asyncio.current_task()])
657657
shell_socket.close()
@@ -672,7 +672,7 @@ async def heartbeat_listen(self, reader, writer):
672672
await heartbeat_socket.send(msg)
673673
except asyncio.CancelledError:
674674
raise
675-
except EOFError, ConnectionResetError:
675+
except (EOFError, ConnectionResetError):
676676
_LOGGER.debug("heartbeat_listen got eof")
677677
await self.housekeep_q.put(["unregister", "heartbeat", asyncio.current_task()])
678678
heartbeat_socket.close()
@@ -693,7 +693,7 @@ async def iopub_listen(self, reader, writer):
693693
# _LOGGER.debug("iopub received %s", _)
694694
except asyncio.CancelledError:
695695
raise
696-
except EOFError, ConnectionResetError:
696+
except (EOFError, ConnectionResetError):
697697
await self.housekeep_q.put(["unregister", "iopub", asyncio.current_task()])
698698
iopub_socket.close()
699699
self.iopub_socket.discard(iopub_socket)

custom_components/pyscript/state.py

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,55 @@
99
from homeassistant.core import Context, HomeAssistant, State as CoreState
1010
from homeassistant.helpers.restore_state import DATA_RESTORE_STATE
1111
from homeassistant.helpers.service import async_get_all_descriptions
12-
from homeassistant.helpers.template.extensions.math import _SENTINEL as MATH_SENTINEL, MathExtension
13-
from homeassistant.helpers.template.extensions.type_cast import (
14-
_SENTINEL as TYPECAST_SENTINEL,
15-
TypeCastExtension,
16-
)
17-
from homeassistant.helpers.template.helpers import (
18-
_SENTINEL,
19-
forgiving_boolean,
20-
raise_no_default,
21-
)
12+
13+
try:
14+
# HA >= 2026.5
15+
from homeassistant.helpers.template.extensions.math import _SENTINEL as MATH_SENTINEL, MathExtension
16+
from homeassistant.helpers.template.extensions.type_cast import (
17+
_SENTINEL as TYPECAST_SENTINEL,
18+
TypeCastExtension,
19+
)
20+
from homeassistant.helpers.template.helpers import (
21+
_SENTINEL,
22+
forgiving_boolean,
23+
raise_no_default,
24+
)
25+
except ImportError:
26+
# HA < 2026.5
27+
from homeassistant.helpers.template import ( # type: ignore[no-redef]
28+
_SENTINEL,
29+
forgiving_boolean,
30+
forgiving_float as _forgiving_float,
31+
forgiving_int as _forgiving_int,
32+
forgiving_round as _forgiving_round,
33+
raise_no_default,
34+
)
35+
36+
TYPECAST_SENTINEL = _SENTINEL
37+
MATH_SENTINEL = _SENTINEL
38+
39+
class TypeCastExtension: # type: ignore[no-redef]
40+
"""Shim for older HA versions that export forgiving_* as module-level functions."""
41+
42+
@staticmethod
43+
def forgiving_float(value, default=_SENTINEL):
44+
"""Shim for forgiving_float."""
45+
return _forgiving_float(value, default=default)
46+
47+
@staticmethod
48+
def forgiving_int(value, default=_SENTINEL, base=10):
49+
"""Shim for forgiving_int."""
50+
return _forgiving_int(value, default=default, base=base)
51+
52+
class MathExtension: # type: ignore[no-redef]
53+
"""Shim for older HA versions that export forgiving_round as a module-level function."""
54+
55+
@staticmethod
56+
def forgiving_round(value, precision=0, method="common", default=_SENTINEL):
57+
"""Shim for forgiving_round."""
58+
return _forgiving_round(value, precision=precision, method=method, default=default)
59+
60+
2261
from homeassistant.util import dt as dt_util
2362

2463
from .const import LOGGER_PATH
@@ -63,7 +102,7 @@ def as_datetime(self, default: datetime = _SENTINEL) -> datetime:
63102
"""Return the state converted to a datetime, matching the forgiving template behaviour."""
64103
try:
65104
return dt_util.parse_datetime(self, raise_on_error=True)
66-
except ValueError, TypeError:
105+
except (ValueError, TypeError):
67106
if default is _SENTINEL:
68107
raise_no_default("as_datetime", self)
69108
return default

hacs.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
"content_in_root": false,
44
"domains": ["automation", "script", "timer"],
55
"zip_release": true,
6-
"filename": "hass-custom-pyscript.zip",
7-
"homeassistant": "2026.5.0"
6+
"filename": "hass-custom-pyscript.zip"
87
}

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ line-length = 109
66
#asyncio_default_fixture_loop_scope = "function"
77

88
[tool.ruff]
9-
target-version = "py314"
9+
target-version = "py313"
1010
line-length = 109
1111
src = ["custom_components", "tests"]
1212
include = ["custom_components/**/*.py", "tests/**/*.py"]
@@ -84,4 +84,4 @@ ignore = [
8484
[dependency-groups]
8585
dev = [
8686
"ruff==0.15.12",
87-
]
87+
]

0 commit comments

Comments
 (0)