api: restructure exception generation to be more ergonomic in the code

This commit is contained in:
lilly 2026-05-14 16:23:52 +02:00
commit 2f991a6b02
Signed by: lilly
SSH key fingerprint: SHA256:y9T5GFw2A20WVklhetIxG1+kcg/Ce0shnQmbu1LQ37g
5 changed files with 153 additions and 81 deletions

View file

@ -9,8 +9,15 @@ logger = logging.getLogger(__name__)
DEVICE_TYPE_LOCK = "HmIP-DLD"
CHANNEL_TYPES_RELEVANT = [ "DOOR_LOCK_STATE_TRANSMITTER", "MAINTENANCE" ]
PARAM_ADDRESSES_RELEVANT = [ "ACTIVITY_STATE", "LOCK_STATE", "LOCK_TARGET_LEVEL", "ERROR_JAMMED", "LOW_BAT", "UNREACH" ]
CHANNEL_TYPES_RELEVANT = ["DOOR_LOCK_STATE_TRANSMITTER", "MAINTENANCE"]
PARAM_ADDRESSES_RELEVANT = [
"ACTIVITY_STATE",
"LOCK_STATE",
"LOCK_TARGET_LEVEL",
"ERROR_JAMMED",
"LOW_BAT",
"UNREACH",
]
class CCURef(BaseModel):
@ -57,12 +64,18 @@ class CCUValue(BaseModel):
LockData = List[Tuple[CCUDeviceInfo, List[Tuple[CCUChannelInfo, List[CCUParamInfo]]]]]
class CCUJackClient:
base_uri: str
locks: LockData
def __init__(self, base_uri: str, auth: BasicAuth):
self.http = ClientSession(base_url=base_uri, auth=auth, raise_for_status=True, connector=TCPConnector(ssl=False))
self.http = ClientSession(
base_url=base_uri,
auth=auth,
raise_for_status=True,
connector=TCPConnector(ssl=False),
)
self.locks = None
async def find_locks(self):
@ -70,18 +83,15 @@ class CCUJackClient:
async with self.http.get("/device") as resp:
devices = CCUDeviceList.model_validate(await resp.json())
device_infos = await asyncio.gather(*[
self._inspect_ccu_device(i)
for i in devices.links
if i.rel == "device"
])
self.locks = [
i
for i in device_infos
if i[0].type == DEVICE_TYPE_LOCK
]
device_infos = await asyncio.gather(
*[
self._inspect_ccu_device(i)
for i in devices.links
if i.rel == "device"
]
)
self.locks = [i for i in device_infos if i[0].type == DEVICE_TYPE_LOCK]
async def query_param_value(self, address: str):
logger.debug("Querying parameter value from '%s'", address)
@ -92,7 +102,9 @@ class CCUJackClient:
logger.debug("Writing parameter value '%s' to '%s'", value, address)
await self.http.put(f"/device/{address}/~pv", json={"v": value})
async def _inspect_ccu_device(self, device_ref: CCURef) -> Tuple[CCUDeviceInfo, List[Tuple[CCUChannelInfo, List[CCUParamInfo]]]]:
async def _inspect_ccu_device(
self, device_ref: CCURef
) -> Tuple[CCUDeviceInfo, List[Tuple[CCUChannelInfo, List[CCUParamInfo]]]]:
logger.debug("Inspecting device '%s' (%s)", device_ref.href, device_ref.title)
async with self.http.get(f"/device/{device_ref.href}") as resp:
device_info = CCUDeviceInfo.model_validate(await resp.json())
@ -100,33 +112,49 @@ class CCUJackClient:
if device_info.type != DEVICE_TYPE_LOCK:
return device_info, []
channel_infos = await asyncio.gather(*[
self._inspect_ccu_channel(device_ref, i)
for i in device_info.links
if i.rel == "channel"
])
channel_infos = await asyncio.gather(
*[
self._inspect_ccu_channel(device_ref, i)
for i in device_info.links
if i.rel == "channel"
]
)
return device_info, [
i
for i in channel_infos
if i[0].type in CHANNEL_TYPES_RELEVANT
i for i in channel_infos if i[0].type in CHANNEL_TYPES_RELEVANT
]
async def _inspect_ccu_channel(self, device_ref: CCURef, channel_ref: CCURef) -> Tuple[CCUChannelInfo, List[CCUParamInfo]]:
logger.debug("Inspecting device channel '%s/%s'", device_ref.href, channel_ref.href)
async with self.http.get(f"/device/{device_ref.href}/{channel_ref.href}") as resp:
async def _inspect_ccu_channel(
self, device_ref: CCURef, channel_ref: CCURef
) -> Tuple[CCUChannelInfo, List[CCUParamInfo]]:
logger.debug(
"Inspecting device channel '%s/%s'", device_ref.href, channel_ref.href
)
async with self.http.get(
f"/device/{device_ref.href}/{channel_ref.href}"
) as resp:
channel_info = CCUChannelInfo.model_validate(await resp.json())
param_infos = await asyncio.gather(*[
self._inspect_ccu_param(device_ref, channel_ref, i)
for i in channel_info.links
if i.rel == "parameter" and i.href in PARAM_ADDRESSES_RELEVANT
])
param_infos = await asyncio.gather(
*[
self._inspect_ccu_param(device_ref, channel_ref, i)
for i in channel_info.links
if i.rel == "parameter" and i.href in PARAM_ADDRESSES_RELEVANT
]
)
return channel_info, param_infos
async def _inspect_ccu_param(self, device_ref: CCURef, channel_ref: CCURef, param_ref: CCURef) -> CCUParamInfo:
logger.debug("Inspecting device parameter '%s/%s/%s'", device_ref.href, channel_ref.href, param_ref.href)
async with self.http.get(f"/device/{device_ref.href}/{channel_ref.href}/{param_ref.href}") as resp:
async def _inspect_ccu_param(
self, device_ref: CCURef, channel_ref: CCURef, param_ref: CCURef
) -> CCUParamInfo:
logger.debug(
"Inspecting device parameter '%s/%s/%s'",
device_ref.href,
channel_ref.href,
param_ref.href,
)
async with self.http.get(
f"/device/{device_ref.href}/{channel_ref.href}/{param_ref.href}"
) as resp:
return CCUParamInfo.model_validate(await resp.json())