Use LanguageSelector and some constants

This commit is contained in:
Tsvi Mostovicz
2025-03-25 17:18:16 +00:00
parent 549a40480a
commit caa3eecf31
2 changed files with 16 additions and 12 deletions

View File

@@ -2,6 +2,9 @@
DOMAIN = "jewish_calendar" DOMAIN = "jewish_calendar"
ATTR_DATE = "date"
ATTR_NUSACH = "nusach"
CONF_DIASPORA = "diaspora" CONF_DIASPORA = "diaspora"
CONF_CANDLE_LIGHT_MINUTES = "candle_lighting_minutes_before_sunset" CONF_CANDLE_LIGHT_MINUTES = "candle_lighting_minutes_before_sunset"
CONF_HAVDALAH_OFFSET_MINUTES = "havdalah_minutes_after_sunset" CONF_HAVDALAH_OFFSET_MINUTES = "havdalah_minutes_after_sunset"
@@ -11,3 +14,5 @@ DEFAULT_CANDLE_LIGHT = 18
DEFAULT_DIASPORA = False DEFAULT_DIASPORA = False
DEFAULT_HAVDALAH_OFFSET_MINUTES = 0 DEFAULT_HAVDALAH_OFFSET_MINUTES = 0
DEFAULT_LANGUAGE = "english" DEFAULT_LANGUAGE = "english"
SERVICE_COUNT_OMER = "count_omer"

View File

@@ -1,6 +1,7 @@
"""Services for Jewish Calendar.""" """Services for Jewish Calendar."""
import datetime import datetime
from typing import cast
from hdate import HebrewDate from hdate import HebrewDate
from hdate.omer import Nusach, Omer from hdate.omer import Nusach, Omer
@@ -14,17 +15,20 @@ from homeassistant.core import (
ServiceResponse, ServiceResponse,
SupportsResponse, SupportsResponse,
) )
from homeassistant.helpers import config_validation as cv from homeassistant.helpers.selector import LanguageSelector, LanguageSelectorConfig
from .const import DOMAIN from .const import ATTR_DATE, ATTR_NUSACH, DOMAIN, SERVICE_COUNT_OMER
SUPPORTED_LANGUAGES = {"en": "english", "fr": "french", "he": "hebrew"}
OMER_SCHEMA = vol.Schema( OMER_SCHEMA = vol.Schema(
{ {
vol.Required("date", default=datetime.date.today): datetime.date, vol.Required(ATTR_DATE, default=datetime.date.today): datetime.date,
vol.Required("nusach", default="sfarad"): vol.In( vol.Required(ATTR_NUSACH, default="sfarad"): vol.In(
[nusach.name.lower() for nusach in Nusach] [nusach.name.lower() for nusach in Nusach]
), ),
vol.Required(CONF_LANGUAGE, default="he"): cv.language, vol.Required(CONF_LANGUAGE, default="he"): LanguageSelector(
LanguageSelectorConfig(languages=list(SUPPORTED_LANGUAGES.keys()))
),
} }
) )
@@ -36,15 +40,10 @@ def async_setup_services(hass: HomeAssistant) -> None:
"""Return the Omer blessing for a given date.""" """Return the Omer blessing for a given date."""
hebrew_date = HebrewDate.from_gdate(call.data["date"]) hebrew_date = HebrewDate.from_gdate(call.data["date"])
nusach = Nusach[call.data["nusach"].upper()] nusach = Nusach[call.data["nusach"].upper()]
_language = call.data[CONF_LANGUAGE]
# Currently Omer only supports Hebrew, English, and French and requires # Currently Omer only supports Hebrew, English, and French and requires
# the full language name # the full language name
language: Language = "hebrew" language = cast(Language, SUPPORTED_LANGUAGES[call.data[CONF_LANGUAGE]])
if _language == "en":
language = "english"
elif _language == "fr":
language = "french"
omer = Omer(date=hebrew_date, nusach=nusach, language=language) omer = Omer(date=hebrew_date, nusach=nusach, language=language)
return { return {
@@ -57,7 +56,7 @@ def async_setup_services(hass: HomeAssistant) -> None:
hass.services.async_register( hass.services.async_register(
DOMAIN, DOMAIN,
"count_omer", SERVICE_COUNT_OMER,
get_omer_count, get_omer_count,
schema=OMER_SCHEMA, schema=OMER_SCHEMA,
supports_response=SupportsResponse.ONLY, supports_response=SupportsResponse.ONLY,