Fix constraints on nusach and language + Make independent of config_entry

This commit is contained in:
Tsvi Mostovicz
2025-03-25 12:55:43 +00:00
parent 56c58931f2
commit b41c3ac30f
3 changed files with 29 additions and 17 deletions

View File

@@ -17,6 +17,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.typing import ConfigType
from .const import ( from .const import (
CONF_CANDLE_LIGHT_MINUTES, CONF_CANDLE_LIGHT_MINUTES,
@@ -34,6 +35,13 @@ _LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] 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( async def async_setup_entry(
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
) -> bool: ) -> bool:
@@ -68,7 +76,6 @@ async def async_setup_entry(
) )
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
async_setup_services(hass, config_entry)
async def update_listener( async def update_listener(
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry hass: HomeAssistant, config_entry: JewishCalendarConfigEntry

View File

@@ -4,37 +4,48 @@ import datetime
from hdate import HebrewDate from hdate import HebrewDate
from hdate.omer import Nusach, Omer from hdate.omer import Nusach, Omer
from hdate.translator import Language
import voluptuous as vol import voluptuous as vol
from homeassistant.const import CONF_LANGUAGE
from homeassistant.core import ( from homeassistant.core import (
HomeAssistant, HomeAssistant,
ServiceCall, ServiceCall,
ServiceResponse, ServiceResponse,
SupportsResponse, SupportsResponse,
) )
from homeassistant.helpers import config_validation as cv
from . import JewishCalendarConfigEntry
from .const import DOMAIN from .const import DOMAIN
OMER_SCHEMA = vol.Schema( OMER_SCHEMA = vol.Schema(
{ {
vol.Required("date", default=datetime.date.today): datetime.date, vol.Required("date", default=datetime.date.today): datetime.date,
vol.Required("nusach", default="sfarad"): str, vol.Required("nusach", default="sfarad"): vol.In(
vol.Required("language"): str, [nusach.name.lower() for nusach in Nusach]
),
vol.Required(CONF_LANGUAGE, default="he"): cv.language,
} }
) )
def async_setup_services( def async_setup_services(hass: HomeAssistant) -> None:
hass: HomeAssistant, config_entry: JewishCalendarConfigEntry
) -> None:
"""Set up the Jewish Calendar services.""" """Set up the Jewish Calendar services."""
async def get_omer_count(call: ServiceCall) -> ServiceResponse: async def get_omer_count(call: ServiceCall) -> ServiceResponse:
"""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.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) omer = Omer(date=hebrew_date, nusach=nusach, language=language)
return { return {
"message": str(omer.count_str()), "message": str(omer.count_str()),

View File

@@ -14,24 +14,18 @@ from tests.common import MockConfigEntry
@pytest.mark.parametrize( @pytest.mark.parametrize(
("test_date", "nusach", "language", "expected"), ("test_date", "nusach", "language", "expected"),
[ [
pytest.param( pytest.param(dt.date(2025, 3, 20), "sfarad", "he", "", id="no_blessing"),
dt.date(2025, 3, 20),
"sfarad",
"hebrew",
"",
id="no_blessing",
),
pytest.param( pytest.param(
dt.date(2025, 5, 20), dt.date(2025, 5, 20),
"ashkenaz", "ashkenaz",
"hebrew", "he",
"היום שבעה ושלושים יום שהם חמישה שבועות ושני ימים בעומר", "היום שבעה ושלושים יום שהם חמישה שבועות ושני ימים בעומר",
id="ahskenaz-hebrew", id="ahskenaz-hebrew",
), ),
pytest.param( pytest.param(
dt.date(2025, 5, 20), dt.date(2025, 5, 20),
"sfarad", "sfarad",
"english", "en",
"Today is the thirty-seventh day, which are five weeks and two days of the Omer", "Today is the thirty-seventh day, which are five weeks and two days of the Omer",
id="sefarad-english", id="sefarad-english",
), ),