Add unique_id to jewish_calendar entities (#39025)
This commit is contained in:
parent
c74f187b1f
commit
d0af3339fc
6 changed files with 93 additions and 2 deletions
|
@ -1,5 +1,6 @@
|
|||
"""The jewish_calendar component."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import hdate
|
||||
import voluptuous as vol
|
||||
|
@ -77,6 +78,27 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
)
|
||||
|
||||
|
||||
def get_unique_prefix(
|
||||
location: hdate.Location,
|
||||
language: str,
|
||||
candle_lighting_offset: Optional[int],
|
||||
havdalah_offset: Optional[int],
|
||||
) -> str:
|
||||
"""Create a prefix for unique ids."""
|
||||
config_properties = [
|
||||
location.latitude,
|
||||
location.longitude,
|
||||
location.timezone,
|
||||
location.altitude,
|
||||
location.diaspora,
|
||||
language,
|
||||
candle_lighting_offset,
|
||||
havdalah_offset,
|
||||
]
|
||||
prefix = "_".join(map(str, config_properties))
|
||||
return f"{prefix}"
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the Jewish Calendar component."""
|
||||
name = config[DOMAIN][CONF_NAME]
|
||||
|
@ -96,6 +118,9 @@ async def async_setup(hass, config):
|
|||
diaspora=diaspora,
|
||||
)
|
||||
|
||||
prefix = get_unique_prefix(
|
||||
location, language, candle_lighting_offset, havdalah_offset
|
||||
)
|
||||
hass.data[DOMAIN] = {
|
||||
"location": location,
|
||||
"name": name,
|
||||
|
@ -103,6 +128,7 @@ async def async_setup(hass, config):
|
|||
"candle_lighting_offset": candle_lighting_offset,
|
||||
"havdalah_offset": havdalah_offset,
|
||||
"diaspora": diaspora,
|
||||
"prefix": prefix,
|
||||
}
|
||||
|
||||
hass.async_create_task(async_load_platform(hass, "sensor", DOMAIN, {}, config))
|
||||
|
|
|
@ -37,12 +37,18 @@ class JewishCalendarBinarySensor(BinarySensorEntity):
|
|||
self._candle_lighting_offset = data["candle_lighting_offset"]
|
||||
self._havdalah_offset = data["havdalah_offset"]
|
||||
self._state = False
|
||||
self._prefix = data["prefix"]
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon of the entity."""
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Generate a unique id."""
|
||||
return f"{self._prefix}_{self._type}"
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the entity."""
|
||||
|
|
|
@ -44,6 +44,7 @@ class JewishCalendarSensor(Entity):
|
|||
self._havdalah_offset = data["havdalah_offset"]
|
||||
self._diaspora = data["diaspora"]
|
||||
self._state = None
|
||||
self._prefix = data["prefix"]
|
||||
self._holiday_attrs = {}
|
||||
|
||||
@property
|
||||
|
@ -51,6 +52,11 @@ class JewishCalendarSensor(Entity):
|
|||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Generate a unique id."""
|
||||
return f"{self._prefix}_{self._type}"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon to display in the front end."""
|
||||
|
|
|
@ -10,6 +10,7 @@ from tests.async_mock import patch
|
|||
|
||||
_LatLng = namedtuple("_LatLng", ["lat", "lng"])
|
||||
|
||||
HDATE_DEFAULT_ALTITUDE = 754
|
||||
NYC_LATLNG = _LatLng(40.7128, -74.0060)
|
||||
JERUSALEM_LATLNG = _LatLng(31.778, 35.235)
|
||||
|
||||
|
|
|
@ -8,7 +8,12 @@ from homeassistant.const import STATE_OFF, STATE_ON
|
|||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import alter_time, make_jerusalem_test_params, make_nyc_test_params
|
||||
from . import (
|
||||
HDATE_DEFAULT_ALTITUDE,
|
||||
alter_time,
|
||||
make_jerusalem_test_params,
|
||||
make_nyc_test_params,
|
||||
)
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
|
@ -79,6 +84,8 @@ async def test_issur_melacha_sensor(
|
|||
hass.config.latitude = latitude
|
||||
hass.config.longitude = longitude
|
||||
|
||||
registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
with alter_time(test_time):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
|
@ -103,3 +110,21 @@ async def test_issur_melacha_sensor(
|
|||
hass.states.get("binary_sensor.test_issur_melacha_in_effect").state
|
||||
== result
|
||||
)
|
||||
entity = registry.async_get("binary_sensor.test_issur_melacha_in_effect")
|
||||
target_uid = "_".join(
|
||||
map(
|
||||
str,
|
||||
[
|
||||
latitude,
|
||||
longitude,
|
||||
time_zone,
|
||||
HDATE_DEFAULT_ALTITUDE,
|
||||
diaspora,
|
||||
"english",
|
||||
candle_lighting,
|
||||
havdalah,
|
||||
"issur_melacha_in_effect",
|
||||
],
|
||||
)
|
||||
)
|
||||
assert entity.unique_id == target_uid
|
||||
|
|
|
@ -7,7 +7,12 @@ from homeassistant.components import jewish_calendar
|
|||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import alter_time, make_jerusalem_test_params, make_nyc_test_params
|
||||
from . import (
|
||||
HDATE_DEFAULT_ALTITUDE,
|
||||
alter_time,
|
||||
make_jerusalem_test_params,
|
||||
make_nyc_test_params,
|
||||
)
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
|
@ -506,6 +511,8 @@ async def test_shabbat_times_sensor(
|
|||
hass.config.latitude = latitude
|
||||
hass.config.longitude = longitude
|
||||
|
||||
registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
with alter_time(test_time):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
|
@ -543,6 +550,26 @@ async def test_shabbat_times_sensor(
|
|||
result_value
|
||||
), f"Value for {sensor_type}"
|
||||
|
||||
entity = registry.async_get(f"sensor.test_{sensor_type}")
|
||||
target_sensor_type = sensor_type.replace("parshat_hashavua", "weekly_portion")
|
||||
target_uid = "_".join(
|
||||
map(
|
||||
str,
|
||||
[
|
||||
latitude,
|
||||
longitude,
|
||||
time_zone,
|
||||
HDATE_DEFAULT_ALTITUDE,
|
||||
diaspora,
|
||||
language,
|
||||
candle_lighting,
|
||||
havdalah,
|
||||
target_sensor_type,
|
||||
],
|
||||
)
|
||||
)
|
||||
assert entity.unique_id == target_uid
|
||||
|
||||
|
||||
OMER_PARAMS = [
|
||||
(dt(2019, 4, 21, 0), "1"),
|
||||
|
|
Loading…
Add table
Reference in a new issue