From b41c3ac30f28aaf58aa637a50b4a06695b1bdd26 Mon Sep 17 00:00:00 2001 From: Tsvi Mostovicz Date: Tue, 25 Mar 2025 12:55:43 +0000 Subject: [PATCH] Fix constraints on nusach and language + Make independent of config_entry --- .../components/jewish_calendar/__init__.py | 9 ++++++- .../components/jewish_calendar/service.py | 25 +++++++++++++------ .../jewish_calendar/test_service.py | 12 +++------ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/jewish_calendar/__init__.py b/homeassistant/components/jewish_calendar/__init__.py index 4f930f0b0b2..ebb28a069d2 100644 --- a/homeassistant/components/jewish_calendar/__init__.py +++ b/homeassistant/components/jewish_calendar/__init__.py @@ -17,6 +17,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry as er +from homeassistant.helpers.typing import ConfigType from .const import ( CONF_CANDLE_LIGHT_MINUTES, @@ -34,6 +35,13 @@ _LOGGER = logging.getLogger(__name__) PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the Jewish Calendar service.""" + async_setup_services(hass) + + return True + + async def async_setup_entry( hass: HomeAssistant, config_entry: JewishCalendarConfigEntry ) -> bool: @@ -68,7 +76,6 @@ async def async_setup_entry( ) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) - async_setup_services(hass, config_entry) async def update_listener( hass: HomeAssistant, config_entry: JewishCalendarConfigEntry diff --git a/homeassistant/components/jewish_calendar/service.py b/homeassistant/components/jewish_calendar/service.py index 9afd60c51f7..78855864c6c 100644 --- a/homeassistant/components/jewish_calendar/service.py +++ b/homeassistant/components/jewish_calendar/service.py @@ -4,37 +4,48 @@ import datetime from hdate import HebrewDate from hdate.omer import Nusach, Omer +from hdate.translator import Language import voluptuous as vol +from homeassistant.const import CONF_LANGUAGE from homeassistant.core import ( HomeAssistant, ServiceCall, ServiceResponse, SupportsResponse, ) +from homeassistant.helpers import config_validation as cv -from . import JewishCalendarConfigEntry from .const import DOMAIN OMER_SCHEMA = vol.Schema( { vol.Required("date", default=datetime.date.today): datetime.date, - vol.Required("nusach", default="sfarad"): str, - vol.Required("language"): str, + vol.Required("nusach", default="sfarad"): vol.In( + [nusach.name.lower() for nusach in Nusach] + ), + vol.Required(CONF_LANGUAGE, default="he"): cv.language, } ) -def async_setup_services( - hass: HomeAssistant, config_entry: JewishCalendarConfigEntry -) -> None: +def async_setup_services(hass: HomeAssistant) -> None: """Set up the Jewish Calendar services.""" async def get_omer_count(call: ServiceCall) -> ServiceResponse: """Return the Omer blessing for a given date.""" hebrew_date = HebrewDate.from_gdate(call.data["date"]) nusach = Nusach[call.data["nusach"].upper()] - language = call.data.get("language", config_entry.runtime_data.language) + _language = call.data[CONF_LANGUAGE] + + # Currently Omer only supports Hebrew, English, and French and requires + # the full language name + language: Language = "hebrew" + if _language == "en": + language = "english" + elif _language == "fr": + language = "french" + omer = Omer(date=hebrew_date, nusach=nusach, language=language) return { "message": str(omer.count_str()), diff --git a/tests/components/jewish_calendar/test_service.py b/tests/components/jewish_calendar/test_service.py index c1a5557808a..9eb80e5e7f0 100644 --- a/tests/components/jewish_calendar/test_service.py +++ b/tests/components/jewish_calendar/test_service.py @@ -14,24 +14,18 @@ from tests.common import MockConfigEntry @pytest.mark.parametrize( ("test_date", "nusach", "language", "expected"), [ - pytest.param( - dt.date(2025, 3, 20), - "sfarad", - "hebrew", - "", - id="no_blessing", - ), + pytest.param(dt.date(2025, 3, 20), "sfarad", "he", "", id="no_blessing"), pytest.param( dt.date(2025, 5, 20), "ashkenaz", - "hebrew", + "he", "היום שבעה ושלושים יום שהם חמישה שבועות ושני ימים בעומר", id="ahskenaz-hebrew", ), pytest.param( dt.date(2025, 5, 20), "sfarad", - "english", + "en", "Today is the thirty-seventh day, which are five weeks and two days of the Omer", id="sefarad-english", ),