Add Jewish Calendar attributes for non-date sensors (#116252)
parent
2ce510357d
commit
2dfa0a3c90
|
@ -4,9 +4,9 @@ from __future__ import annotations
|
|||
|
||||
from datetime import date as Date
|
||||
import logging
|
||||
from typing import Any
|
||||
from typing import Any, cast
|
||||
|
||||
from hdate import HDate
|
||||
from hdate import HDate, HebrewDate, htables
|
||||
from hdate.zmanim import Zmanim
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
|
@ -36,16 +36,19 @@ INFO_SENSORS: tuple[SensorEntityDescription, ...] = (
|
|||
key="date",
|
||||
name="Date",
|
||||
icon="mdi:star-david",
|
||||
translation_key="hebrew_date",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="weekly_portion",
|
||||
name="Parshat Hashavua",
|
||||
icon="mdi:book-open-variant",
|
||||
device_class=SensorDeviceClass.ENUM,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="holiday",
|
||||
name="Holiday",
|
||||
icon="mdi:calendar-star",
|
||||
device_class=SensorDeviceClass.ENUM,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="omer_count",
|
||||
|
@ -190,7 +193,7 @@ class JewishCalendarSensor(SensorEntity):
|
|||
self._candle_lighting_offset = data[CONF_CANDLE_LIGHT_MINUTES]
|
||||
self._havdalah_offset = data[CONF_HAVDALAH_OFFSET_MINUTES]
|
||||
self._diaspora = data[CONF_DIASPORA]
|
||||
self._holiday_attrs: dict[str, str] = {}
|
||||
self._attrs: dict[str, str] = {}
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Update the state of the sensor."""
|
||||
|
@ -247,9 +250,7 @@ class JewishCalendarSensor(SensorEntity):
|
|||
@property
|
||||
def extra_state_attributes(self) -> dict[str, str]:
|
||||
"""Return the state attributes."""
|
||||
if self.entity_description.key != "holiday":
|
||||
return {}
|
||||
return self._holiday_attrs
|
||||
return self._attrs
|
||||
|
||||
def get_state(
|
||||
self, daytime_date: HDate, after_shkia_date: HDate, after_tzais_date: HDate
|
||||
|
@ -258,16 +259,31 @@ class JewishCalendarSensor(SensorEntity):
|
|||
# Terminology note: by convention in py-libhdate library, "upcoming"
|
||||
# refers to "current" or "upcoming" dates.
|
||||
if self.entity_description.key == "date":
|
||||
hdate = cast(HebrewDate, after_shkia_date.hdate)
|
||||
month = htables.MONTHS[hdate.month.value - 1]
|
||||
self._attrs = {
|
||||
"hebrew_year": hdate.year,
|
||||
"hebrew_month_name": month.hebrew if self._hebrew else month.english,
|
||||
"hebrew_day": hdate.day,
|
||||
}
|
||||
return after_shkia_date.hebrew_date
|
||||
if self.entity_description.key == "weekly_portion":
|
||||
self._attr_options = [
|
||||
(p.hebrew if self._hebrew else p.english) for p in htables.PARASHAOT
|
||||
]
|
||||
# Compute the weekly portion based on the upcoming shabbat.
|
||||
return after_tzais_date.upcoming_shabbat.parasha
|
||||
if self.entity_description.key == "holiday":
|
||||
self._holiday_attrs = {
|
||||
self._attrs = {
|
||||
"id": after_shkia_date.holiday_name,
|
||||
"type": after_shkia_date.holiday_type.name,
|
||||
"type_id": after_shkia_date.holiday_type.value,
|
||||
}
|
||||
self._attr_options = [
|
||||
h.description.hebrew.long if self._hebrew else h.description.english
|
||||
for h in htables.HOLIDAYS
|
||||
]
|
||||
|
||||
return after_shkia_date.holiday_description
|
||||
if self.entity_description.key == "omer_count":
|
||||
return after_shkia_date.omer_day
|
||||
|
|
|
@ -1,4 +1,15 @@
|
|||
{
|
||||
"entity": {
|
||||
"sensor": {
|
||||
"hebrew_date": {
|
||||
"state_attributes": {
|
||||
"hebrew_year": { "name": "Hebrew Year" },
|
||||
"hebrew_month_name": { "name": "Hebrew Month Name" },
|
||||
"hebrew_day": { "name": "Hebrew Day" }
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
from datetime import datetime as dt, timedelta
|
||||
|
||||
from hdate import htables
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN as SENSOR_DOMAIN
|
||||
|
@ -40,7 +41,17 @@ async def test_jewish_calendar_hebrew(hass: HomeAssistant) -> None:
|
|||
|
||||
|
||||
TEST_PARAMS = [
|
||||
(dt(2018, 9, 3), "UTC", 31.778, 35.235, "english", "date", False, "23 Elul 5778"),
|
||||
(
|
||||
dt(2018, 9, 3),
|
||||
"UTC",
|
||||
31.778,
|
||||
35.235,
|
||||
"english",
|
||||
"date",
|
||||
False,
|
||||
"23 Elul 5778",
|
||||
None,
|
||||
),
|
||||
(
|
||||
dt(2018, 9, 3),
|
||||
"UTC",
|
||||
|
@ -50,8 +61,19 @@ TEST_PARAMS = [
|
|||
"date",
|
||||
False,
|
||||
'כ"ג אלול ה\' תשע"ח',
|
||||
None,
|
||||
),
|
||||
(
|
||||
dt(2018, 9, 10),
|
||||
"UTC",
|
||||
31.778,
|
||||
35.235,
|
||||
"hebrew",
|
||||
"holiday",
|
||||
False,
|
||||
"א' ראש השנה",
|
||||
None,
|
||||
),
|
||||
(dt(2018, 9, 10), "UTC", 31.778, 35.235, "hebrew", "holiday", False, "א' ראש השנה"),
|
||||
(
|
||||
dt(2018, 9, 10),
|
||||
"UTC",
|
||||
|
@ -61,6 +83,15 @@ TEST_PARAMS = [
|
|||
"holiday",
|
||||
False,
|
||||
"Rosh Hashana I",
|
||||
{
|
||||
"device_class": "enum",
|
||||
"friendly_name": "Jewish Calendar Holiday",
|
||||
"icon": "mdi:calendar-star",
|
||||
"id": "rosh_hashana_i",
|
||||
"type": "YOM_TOV",
|
||||
"type_id": 1,
|
||||
"options": [h.description.english for h in htables.HOLIDAYS],
|
||||
},
|
||||
),
|
||||
(
|
||||
dt(2018, 9, 8),
|
||||
|
@ -71,6 +102,12 @@ TEST_PARAMS = [
|
|||
"parshat_hashavua",
|
||||
False,
|
||||
"נצבים",
|
||||
{
|
||||
"device_class": "enum",
|
||||
"friendly_name": "Jewish Calendar Parshat Hashavua",
|
||||
"icon": "mdi:book-open-variant",
|
||||
"options": [p.hebrew for p in htables.PARASHAOT],
|
||||
},
|
||||
),
|
||||
(
|
||||
dt(2018, 9, 8),
|
||||
|
@ -81,6 +118,7 @@ TEST_PARAMS = [
|
|||
"t_set_hakochavim",
|
||||
True,
|
||||
dt(2018, 9, 8, 19, 45),
|
||||
None,
|
||||
),
|
||||
(
|
||||
dt(2018, 9, 8),
|
||||
|
@ -91,6 +129,7 @@ TEST_PARAMS = [
|
|||
"t_set_hakochavim",
|
||||
False,
|
||||
dt(2018, 9, 8, 19, 19),
|
||||
None,
|
||||
),
|
||||
(
|
||||
dt(2018, 10, 14),
|
||||
|
@ -101,6 +140,7 @@ TEST_PARAMS = [
|
|||
"parshat_hashavua",
|
||||
False,
|
||||
"לך לך",
|
||||
None,
|
||||
),
|
||||
(
|
||||
dt(2018, 10, 14, 17, 0, 0),
|
||||
|
@ -111,6 +151,7 @@ TEST_PARAMS = [
|
|||
"date",
|
||||
False,
|
||||
"ה' מרחשוון ה' תשע\"ט",
|
||||
None,
|
||||
),
|
||||
(
|
||||
dt(2018, 10, 14, 19, 0, 0),
|
||||
|
@ -121,6 +162,13 @@ TEST_PARAMS = [
|
|||
"date",
|
||||
False,
|
||||
"ו' מרחשוון ה' תשע\"ט",
|
||||
{
|
||||
"hebrew_year": 5779,
|
||||
"hebrew_month_name": "מרחשוון",
|
||||
"hebrew_day": 6,
|
||||
"icon": "mdi:star-david",
|
||||
"friendly_name": "Jewish Calendar Date",
|
||||
},
|
||||
),
|
||||
]
|
||||
|
||||
|
@ -148,6 +196,7 @@ TEST_IDS = [
|
|||
"sensor",
|
||||
"diaspora",
|
||||
"result",
|
||||
"attrs",
|
||||
),
|
||||
TEST_PARAMS,
|
||||
ids=TEST_IDS,
|
||||
|
@ -162,6 +211,7 @@ async def test_jewish_calendar_sensor(
|
|||
sensor,
|
||||
diaspora,
|
||||
result,
|
||||
attrs,
|
||||
) -> None:
|
||||
"""Test Jewish calendar sensor output."""
|
||||
time_zone = dt_util.get_time_zone(tzname)
|
||||
|
@ -196,10 +246,8 @@ async def test_jewish_calendar_sensor(
|
|||
sensor_object = hass.states.get(f"sensor.jewish_calendar_{sensor}")
|
||||
assert sensor_object.state == result
|
||||
|
||||
if sensor == "holiday":
|
||||
assert sensor_object.attributes.get("id") == "rosh_hashana_i"
|
||||
assert sensor_object.attributes.get("type") == "YOM_TOV"
|
||||
assert sensor_object.attributes.get("type_id") == 1
|
||||
if attrs:
|
||||
assert sensor_object.attributes == attrs
|
||||
|
||||
|
||||
SHABBAT_PARAMS = [
|
||||
|
|
Loading…
Reference in New Issue