mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Add shabat sensors to jewish_calendar (#57866)
* add shabat sensors * add shabat sensors * add shabat sensors * add shabat sensors * add shabat sensors * Remove redundunt classes and combine sensors * Update homeassistant/components/jewish_calendar/binary_sensor.py Co-authored-by: Yuval Aboulafia <yuval.abou@gmail.com> * Update homeassistant/components/jewish_calendar/binary_sensor.py Co-authored-by: Yuval Aboulafia <yuval.abou@gmail.com> * updated requirements * call get_zmanim once * add type hint to entity description * fix errors resulted from type hints introduction * fix mypy error * use attr for state * Update homeassistant/components/jewish_calendar/binary_sensor.py Co-authored-by: Teemu R. <tpr@iki.fi> * Fix typing Co-authored-by: Yuval Aboulafia <yuval.abou@gmail.com> Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
parent
4f56028491
commit
7251445ffb
@ -1,9 +1,10 @@
|
|||||||
"""Support for Jewish Calendar binary sensors."""
|
"""Support for Jewish Calendar binary sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import cast
|
|
||||||
|
|
||||||
import hdate
|
import hdate
|
||||||
from hdate.zmanim import Zmanim
|
from hdate.zmanim import Zmanim
|
||||||
@ -20,10 +21,38 @@ import homeassistant.util.dt as dt_util
|
|||||||
|
|
||||||
from . import DOMAIN
|
from . import DOMAIN
|
||||||
|
|
||||||
BINARY_SENSORS = BinarySensorEntityDescription(
|
|
||||||
key="issur_melacha_in_effect",
|
@dataclass
|
||||||
name="Issur Melacha in Effect",
|
class JewishCalendarBinarySensorMixIns(BinarySensorEntityDescription):
|
||||||
icon="mdi:power-plug-off",
|
"""Binary Sensor description mixin class for Jewish Calendar."""
|
||||||
|
|
||||||
|
is_on: Callable[..., bool] = lambda _: False
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class JewishCalendarBinarySensorEntityDescription(
|
||||||
|
JewishCalendarBinarySensorMixIns, BinarySensorEntityDescription
|
||||||
|
):
|
||||||
|
"""Binary Sensor Entity description for Jewish Calendar."""
|
||||||
|
|
||||||
|
|
||||||
|
BINARY_SENSORS: tuple[JewishCalendarBinarySensorEntityDescription, ...] = (
|
||||||
|
JewishCalendarBinarySensorEntityDescription(
|
||||||
|
key="issur_melacha_in_effect",
|
||||||
|
name="Issur Melacha in Effect",
|
||||||
|
icon="mdi:power-plug-off",
|
||||||
|
is_on=lambda state: bool(state.issur_melacha_in_effect),
|
||||||
|
),
|
||||||
|
JewishCalendarBinarySensorEntityDescription(
|
||||||
|
key="erev_shabbat_hag",
|
||||||
|
name="Erev Shabbat/Hag",
|
||||||
|
is_on=lambda state: bool(state.erev_shabbat_hag),
|
||||||
|
),
|
||||||
|
JewishCalendarBinarySensorEntityDescription(
|
||||||
|
key="motzei_shabbat_hag",
|
||||||
|
name="Motzei Shabbat/Hag",
|
||||||
|
is_on=lambda state: bool(state.motzei_shabbat_hag),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -37,20 +66,27 @@ async def async_setup_platform(
|
|||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
async_add_entities([JewishCalendarBinarySensor(hass.data[DOMAIN], BINARY_SENSORS)])
|
async_add_entities(
|
||||||
|
[
|
||||||
|
JewishCalendarBinarySensor(hass.data[DOMAIN], description)
|
||||||
|
for description in BINARY_SENSORS
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class JewishCalendarBinarySensor(BinarySensorEntity):
|
class JewishCalendarBinarySensor(BinarySensorEntity):
|
||||||
"""Representation of an Jewish Calendar binary sensor."""
|
"""Representation of an Jewish Calendar binary sensor."""
|
||||||
|
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
entity_description: JewishCalendarBinarySensorEntityDescription
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
data: dict[str, str | bool | int | float],
|
data: dict[str, str | bool | int | float],
|
||||||
description: BinarySensorEntityDescription,
|
description: JewishCalendarBinarySensorEntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the binary sensor."""
|
"""Initialize the binary sensor."""
|
||||||
|
self.entity_description = description
|
||||||
self._attr_name = f"{data['name']} {description.name}"
|
self._attr_name = f"{data['name']} {description.name}"
|
||||||
self._attr_unique_id = f"{data['prefix']}_{description.key}"
|
self._attr_unique_id = f"{data['prefix']}_{description.key}"
|
||||||
self._location = data["location"]
|
self._location = data["location"]
|
||||||
@ -62,7 +98,8 @@ class JewishCalendarBinarySensor(BinarySensorEntity):
|
|||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
"""Return true if sensor is on."""
|
"""Return true if sensor is on."""
|
||||||
return cast(bool, self._get_zmanim().issur_melacha_in_effect)
|
zmanim = self._get_zmanim()
|
||||||
|
return self.entity_description.is_on(zmanim)
|
||||||
|
|
||||||
def _get_zmanim(self) -> Zmanim:
|
def _get_zmanim(self) -> Zmanim:
|
||||||
"""Return the Zmanim object for now()."""
|
"""Return the Zmanim object for now()."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user