mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Add unique_id to jewish_calendar entities (#39025)
This commit is contained in:
parent
c74f187b1f
commit
d0af3339fc
@ -1,5 +1,6 @@
|
|||||||
"""The jewish_calendar component."""
|
"""The jewish_calendar component."""
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
import hdate
|
import hdate
|
||||||
import voluptuous as vol
|
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):
|
async def async_setup(hass, config):
|
||||||
"""Set up the Jewish Calendar component."""
|
"""Set up the Jewish Calendar component."""
|
||||||
name = config[DOMAIN][CONF_NAME]
|
name = config[DOMAIN][CONF_NAME]
|
||||||
@ -96,6 +118,9 @@ async def async_setup(hass, config):
|
|||||||
diaspora=diaspora,
|
diaspora=diaspora,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
prefix = get_unique_prefix(
|
||||||
|
location, language, candle_lighting_offset, havdalah_offset
|
||||||
|
)
|
||||||
hass.data[DOMAIN] = {
|
hass.data[DOMAIN] = {
|
||||||
"location": location,
|
"location": location,
|
||||||
"name": name,
|
"name": name,
|
||||||
@ -103,6 +128,7 @@ async def async_setup(hass, config):
|
|||||||
"candle_lighting_offset": candle_lighting_offset,
|
"candle_lighting_offset": candle_lighting_offset,
|
||||||
"havdalah_offset": havdalah_offset,
|
"havdalah_offset": havdalah_offset,
|
||||||
"diaspora": diaspora,
|
"diaspora": diaspora,
|
||||||
|
"prefix": prefix,
|
||||||
}
|
}
|
||||||
|
|
||||||
hass.async_create_task(async_load_platform(hass, "sensor", DOMAIN, {}, config))
|
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._candle_lighting_offset = data["candle_lighting_offset"]
|
||||||
self._havdalah_offset = data["havdalah_offset"]
|
self._havdalah_offset = data["havdalah_offset"]
|
||||||
self._state = False
|
self._state = False
|
||||||
|
self._prefix = data["prefix"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon of the entity."""
|
"""Return the icon of the entity."""
|
||||||
return self._icon
|
return self._icon
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self) -> str:
|
||||||
|
"""Generate a unique id."""
|
||||||
|
return f"{self._prefix}_{self._type}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the entity."""
|
"""Return the name of the entity."""
|
||||||
|
@ -44,6 +44,7 @@ class JewishCalendarSensor(Entity):
|
|||||||
self._havdalah_offset = data["havdalah_offset"]
|
self._havdalah_offset = data["havdalah_offset"]
|
||||||
self._diaspora = data["diaspora"]
|
self._diaspora = data["diaspora"]
|
||||||
self._state = None
|
self._state = None
|
||||||
|
self._prefix = data["prefix"]
|
||||||
self._holiday_attrs = {}
|
self._holiday_attrs = {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -51,6 +52,11 @@ class JewishCalendarSensor(Entity):
|
|||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self) -> str:
|
||||||
|
"""Generate a unique id."""
|
||||||
|
return f"{self._prefix}_{self._type}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Icon to display in the front end."""
|
"""Icon to display in the front end."""
|
||||||
|
@ -10,6 +10,7 @@ from tests.async_mock import patch
|
|||||||
|
|
||||||
_LatLng = namedtuple("_LatLng", ["lat", "lng"])
|
_LatLng = namedtuple("_LatLng", ["lat", "lng"])
|
||||||
|
|
||||||
|
HDATE_DEFAULT_ALTITUDE = 754
|
||||||
NYC_LATLNG = _LatLng(40.7128, -74.0060)
|
NYC_LATLNG = _LatLng(40.7128, -74.0060)
|
||||||
JERUSALEM_LATLNG = _LatLng(31.778, 35.235)
|
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
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
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
|
from tests.common import async_fire_time_changed
|
||||||
|
|
||||||
@ -79,6 +84,8 @@ async def test_issur_melacha_sensor(
|
|||||||
hass.config.latitude = latitude
|
hass.config.latitude = latitude
|
||||||
hass.config.longitude = longitude
|
hass.config.longitude = longitude
|
||||||
|
|
||||||
|
registry = await hass.helpers.entity_registry.async_get_registry()
|
||||||
|
|
||||||
with alter_time(test_time):
|
with alter_time(test_time):
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
@ -103,3 +110,21 @@ async def test_issur_melacha_sensor(
|
|||||||
hass.states.get("binary_sensor.test_issur_melacha_in_effect").state
|
hass.states.get("binary_sensor.test_issur_melacha_in_effect").state
|
||||||
== result
|
== 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
|
from homeassistant.setup import async_setup_component
|
||||||
import homeassistant.util.dt as dt_util
|
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
|
from tests.common import async_fire_time_changed
|
||||||
|
|
||||||
@ -506,6 +511,8 @@ async def test_shabbat_times_sensor(
|
|||||||
hass.config.latitude = latitude
|
hass.config.latitude = latitude
|
||||||
hass.config.longitude = longitude
|
hass.config.longitude = longitude
|
||||||
|
|
||||||
|
registry = await hass.helpers.entity_registry.async_get_registry()
|
||||||
|
|
||||||
with alter_time(test_time):
|
with alter_time(test_time):
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
@ -543,6 +550,26 @@ async def test_shabbat_times_sensor(
|
|||||||
result_value
|
result_value
|
||||||
), f"Value for {sensor_type}"
|
), 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 = [
|
OMER_PARAMS = [
|
||||||
(dt(2019, 4, 21, 0), "1"),
|
(dt(2019, 4, 21, 0), "1"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user