Skip to content

fix: defer curl_cffi import + ko translation placeholder + legacy safety_alert schema migration#16

Merged
redchupa merged 3 commits into
mainfrom
fix/safety-alert-blocking-io-and-ko-translation
May 13, 2026
Merged

fix: defer curl_cffi import + ko translation placeholder + legacy safety_alert schema migration#16
redchupa merged 3 commits into
mainfrom
fix/safety-alert-blocking-io-and-ko-translation

Conversation

@redchupa
Copy link
Copy Markdown
Owner

HA 로그와 실제 entity 상태 검증 결과 발견된 3가지 작은 버그 수정.

1. safety_alert/api.py — blocking I/O 제거 (lazy import)

HA 로그에서 발견:

Detected blocking call to listdir/read_text/open inside the event loop
by custom integration 'kr_component_kit' at safety_alert/api.py, line 9:
import curl_cffi

import curl_cffi 가 module top-level에 있어서 setup 단계에서 disk I/O 발생. lazy import로 변경 — 첫 HTTP 요청 시점으로 미룸. 그땐 event loop가 executor에서 안전하게 처리.

2. translations/ko.json — cannot_connect placeholder 추가

HA validation 경고:

Validation of translation placeholders for localized (ko) string
component.kr_component_kit.config.error.cannot_connect failed:
(set() != {'error'})

en.json / ja.jsoncannot_connect{error} placeholder 포함인데 ko.json 만 빠져 있었음. 일관성 맞춤.

3. init.py — 안전알림 legacy area_codes schema 마이그레이션

실제 HA 인스턴스에서 발견: 이전 버전 안전알림 entry data가 area_codes (복수형 bare list) 로 저장되어 있어, 현재 코드가 regions=[] 로 떨어지고 entity 생성 0개. 결과적으로 이전 코드가 만든 17개 sensor entity가 orphan 상태로 남고 모두 unavailable.

이전 두 단계 fallback (regions / area_code 단일) 다음에 area_codes 케이스도 처리하도록 추가. 다음 HA 재시작 시 entry 가 자동 자가 치유.

Test plan

  • HA 로그에 Detected blocking call ... import curl_cffi 메시지가 사라지는지 (HA 재시작 후)
  • ko 사용자에게 cannot_connect 에러 메시지가 정상 표시되는지 (config_flow에서 시뮬레이션)
  • area_codes schema 가진 entry 가 다음 HA 재시작 시 entity 생성하는지 (사용자 실제 환경에서 검증 — 본 환경에서는 entry 삭제 후 재등록으로 새 schema 사용 중)

Notes

이 PR은 PR #15 (README 개선)와 독립적. main에 머지된 후 docs PR도 이어서 머지하면 됨.

🤖 Generated with Claude Code

ng410808-max and others added 3 commits May 14, 2026 02:44
…legacy safety_alert schema

Three small fixes discovered while writing the README guide:

1. safety_alert/api.py — defer ``import curl_cffi`` from module top-level
   to inside the request method. Importing curl_cffi triggers blocking
   disk I/O (listdir on site-packages, read_text on METADATA) which HA
   detects during async_setup_entry and warns:

      Detected blocking call to listdir/read_text/open inside the event
      loop by custom integration 'kr_component_kit' at
      safety_alert/api.py, line 9: import curl_cffi

   Lazy import defers those reads to the first HTTP request, by which
   point the event loop can run them on the executor cleanly.

2. translations/ko.json — add ``{error}`` placeholder to the
   cannot_connect error string so it matches en.json/ja.json. HA emits a
   validation error otherwise:

      Validation of translation placeholders for localized (ko) string
      component.kr_component_kit.config.error.cannot_connect failed:
      (set() != {'error'})

3. __init__.py — add backward-compat path for an even older
   safety_alert entry.data schema that stored regions under ``area_codes``
   (plural, bare list). Entries on that legacy schema produced a
   17-sensor entity layout that's now orphaned (state unavailable) and
   sets neither ``regions`` nor ``area_code`` (single), so they fall
   through both existing branches and end up with regions=[] (no
   coordinator setup, no entity creation). The new mapping turns each
   legacy code into the current {"code", "name"} shape so the entry
   auto-heals on the next HA start.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Critical regression fix. Previous config_flow exposed only:
  - 17 광역시도 (sido-level)
  - Seoul gu (autonomous districts within Seoul only)

This meant users outside Seoul could only register at sido granularity —
e.g. "경기도" but not "경기도 시흥시" — a hard downgrade from earlier
versions of the integration that used safekorea.go.kr's region API to
cascade through sido → sgg → emd.

The region client (safety_alert/region_api.py) was still in the
codebase but no longer wired into config_flow. This commit restores the
three-step cascading flow:

  Step 1 (safety_alert):     sido dropdown (hardcoded 17개)
  Step 2 (safety_alert_sgg): sgg dropdown for selected sido (live API)
  Step 3 (safety_alert_emd): emd dropdown for selected sgg (live API)

Each step's "leave blank" option lets users stop at sido- or sgg-level
granularity if they don't want per-동 alerts. Output is stored in the
existing `regions[]` schema with `code` / `code2` / `code3` matching
the __init__.py setup path that's been there all along.

Field names sido_code / sgg_code / emd_code match the en.json labels
that were already in the codebase from the original cascading flow
(those translation keys were never removed even though config_flow had
been simplified to a single dropdown).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Bundle with PR #16 fixes (curl_cffi lazy import, ko translation
placeholder, legacy schema migration, cascading region dropdown
restoration) for HACS update notification.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@redchupa redchupa merged commit 1e0f773 into main May 13, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants