8000 Update "issur_melacha_in_effect" via time tracking by amitfin · Pull Request #42328 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update "issur_melacha_in_effect" via time tracking #42328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d6cbe1c
Add files via upload
amitfin Oct 5, 2020
dca2a38
Add files via upload
amitfin Oct 5, 2020
2049917
Changed state to be on/off
amitfin Oct 6, 2020
8d90f4e
Updated the cache file
amitfin Oct 6, 2020
064a387
Merge branch 'amitfin-input-schedule' of https://github.com/amitfin/c…
amitfin Oct 6, 2020
30363ac
Updating cache
amitfin Oct 6, 2020
11d2e48
Update CODEOWNERS
amitfin Oct 7, 2020
818370d
Fixed setting off an entire on period
amitfin Oct 7, 2020
27cb3f9
Add "input_schedule" to "default_config".
amitfin Oct 7, 2020
7ce12c8
Renamed "schedule" to "timetable"
amitfin Oct 7, 2020
9706b60
Fixed typos
amitfin Oct 7, 2020
52938e6
Fixed translation
amitfin Oct 7, 2020
117c3d0
Fixed "reload"
amitfin Oct 8, 2020
e7f8ff2
Move to events model
amitfin Oct 10, 2020
f806eb8
Use capital letters for the states
amitfin Oct 14, 2020
45a2099
Re-trigger checks
amitfin Oct 14, 2020
3139cde
Merge branch 'amitfin-input-schedule' of https://github.com/amitfin/c…
amitfin Oct 15, 2020
42b86a7
Add "reconfig" service
amitfin Oct 16, 2020
ac3e77c
Add "reconfig" description
amitfin Oct 16, 2020
23604fd
Rerun tests
amitfin Oct 16, 2020
003f9e5
Merge branch 'amitfin-input-schedule' of https://github.com/amitfin/c…
amitfin Oct 16, 2020
52771b9
Merge branch 'dev' into amitfin-input-schedule
amitfin Oct 22, 2020
a3e23d5
Update manifest.json
amitfin Oct 22, 2020
06988c4
Use @pytest.mark.parametrize
amitfin Oct 24, 2020
0bd2f0c
Use dt_util
amitfin Oct 24, 2020
25b46c1
Update "issur_melacha_in_effect" via time tracking
amitfin Oct 25, 2020
8d7aa2b
Decrease test window to 2 seconds (from 61)
amitfin Oct 25, 2020
8843cd7
Improve test_state cases
amitfin Oct 25, 2020
ee5a6a1
Merge branch 'amitfin-input-schedule' of https://github.com/amitfin/c…
amitfin Oct 26, 2020
82c2311
Decrease test window to 1 second (from 2)
amitfin Oct 26, 2020
77f6b37
Delete unrelated changes
amitfin Oct 26, 2020
6732a6e
Rerun test
amitfin Oct 26, 2020
8024866
Merge branch 'jewish-binary-sensor-time-tracking' of https://github.c…
amitfin Oct 26, 2020
1274c46
Rerun tests
amitfin Oct 26, 2020
47be404
Merge branch 'jewish-binary-sensor-time-tracking' of https://github.c…
amitfin Oct 26, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions homeassistant/components/jewish_calendar/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
"""Support for Jewish Calendar binary sensors."""
import datetime as dt

import hdate

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.core import callback
from homeassistant.helpers import event
import homeassistant.util.dt as dt_util

from . import DOMAIN, SENSOR_TYPES
Expand Down Expand Up @@ -32,8 +36,8 @@ def __init__(self, data, sensor, sensor_info):
self._hebrew = data["language"] == "hebrew"
self._candle_lighting_offset = data["candle_lighting_offset"]
self._havdalah_offset = data["havdalah_offset"]
self._state = False
self._prefix = data["prefix"]
self._next_update = None

@property
def icon(self):
Expand All @@ -53,16 +57,46 @@ def name(self):
@property
def is_on(self):
"""Return true if sensor is on."""
return self._state
return self._get_zmanim().issur_melacha_in_effect

async def async_update(self):
"""Update the state of the sensor."""
zmanim = hdate.Zmanim(
@property
def should_poll(self):
"""No polling needed."""
return False

def _get_zmanim(self):
"""Return the Zmanim object for now()."""
return hdate.Zmanim(
date=dt_util.now(),
location=self._location,
candle_lighting_offset=self._candle_lighting_offset,
havdalah_offset=self._havdalah_offset,
hebrew=self._hebrew,
)

self._state = zmanim.issur_melacha_in_effect
async def async_added_to_hass(self):
"""Run when entity about to be added to hass."""
await super().async_added_to_hass()
self._schedule_update()

@callback
async def _update(self, now=None):
"""Update the state of the sensor."""
self._schedule_update()
self.async_write_ha_state()

def _schedule_update(self):
"""Schedule the next update of the sensor."""
now = dt_util.now()
zmanim = self._get_zmanim()
update = zmanim.zmanim["sunrise"] + dt.timedelta(days=1)
candle_lighting = zmanim.candle_lighting
if candle_lighting is not None and now < candle_lighting < update:
update = candle_lighting
havdalah = zmanim.havdalah
if havdalah is not None and now < havdalah < update:
update = havdalah
if self._next_update == update:
return
self._next_update = update
event.async_track_point_in_time(self.hass, self._update, update)
2 changes: 1 addition & 1 deletion homeassistant/components/jewish_calendar/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"domain": "jewish_calendar",
"name": "Jewish Calendar",
"documentation": "https://www.home-assistant.io/integrations/jewish_calendar",
"requirements": ["hdate==0.9.5"],
"requirements": ["hdate==0.9.12"],
"codeowners": ["@tsvi"]
}
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ hass_splunk==0.1.1
hatasmota==0.0.22

# homeassistant.components.jewish_calendar
hdate==0.9.5
hdate==0.9.12

# homeassistant.components.heatmiser
heatmiserV3==1.1.18
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ hass-nabucasa==0.37.1
hatasmota==0.0.22

# homeassistant.components.jewish_calendar
hdate==0.9.5
hdate==0.9.12

# homeassistant.components.here_travel_time
herepy==2.0.0
Expand Down
196 changes: 176 additions & 20 deletions tests/components/jewish_calendar/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,108 @@
make_nyc_test_params,
)

from tests.async_mock import patch
from tests.common import async_fire_time_changed

MELACHA_PARAMS = [
make_nyc_test_params(dt(2018, 9, 1, 16, 0), STATE_ON),
make_nyc_test_params(dt(2018, 9, 1, 20, 21), STATE_OFF),
make_nyc_test_params(dt(2018, 9, 7, 13, 1), STATE_OFF),
make_nyc_test_params(dt(2018, 9, 8, 21, 25), STATE_OFF),
make_nyc_test_params(dt(2018, 9, 9, 21, 25), STATE_ON),
make_nyc_test_params(dt(2018, 9, 10, 21, 25), STATE_ON),
make_nyc_test_params(dt(2018, 9, 28, 21, 25), STATE_ON),
make_nyc_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF),
make_nyc_test_params(dt(2018, 9, 30, 21, 25), STATE_ON),
make_nyc_test_params(dt(2018, 10, 1, 21, 25), STATE_ON),
make_jerusalem_test_params(dt(2018, 9, 29, 21, 25), STATE_OFF),
make_jerusalem_test_params(dt(2018, 9, 30, 21, 25), STATE_ON),
make_jerusalem_test_params(dt(2018, 10, 1, 21, 25), STATE_OFF),
make_nyc_test_params(
dt(2018, 9, 1, 16, 0),
{
"state": STATE_ON,
"update": dt(2018, 9, 1, 20, 14),
},
),
make_nyc_test_params(
dt(2018, 9, 1, 20, 21),
{
"state": STATE_OFF,
"update": dt(2018, 9, 2, 6, 21),
},
),
make_nyc_test_params(
dt(2018, 9, 7, 13, 1),
{
"state": STATE_OFF,
"update": dt(2018, 9, 7, 19, 4),
},
),
make_nyc_test_params(
dt(2018, 9, 8, 21, 25),
{
"state": STATE_OFF,
"update": dt(2018, 9, 9, 6, 27),
},
),
make_nyc_test_params(
dt(2018, 9, 9, 21, 25),
{
"state": STATE_ON,
"update": dt(2018, 9, 10, 6, 28),
},
),
make_nyc_test_params(
dt(2018, 9, 10, 21, 25),
{
"state": STATE_ON,
"update": dt(2018, 9, 11, 6, 29),
},
),
make_nyc_test_params(
dt(2018, 9, 11, 11, 25),
{
"state": STATE_ON,
"update": dt(2018, 9, 11, 19, 57),
},
),
make_nyc_test_params(
dt(2018, 9, 29, 16, 25),
{
"state": STATE_ON,
"update": dt(2018, 9, 29, 19, 25),
},
),
make_nyc_test_params(
dt(2018, 9, 29, 21, 25),
{
"state": STATE_OFF,
"update": dt(2018, 9, 30, 6, 48),
},
),
make_nyc_test_params(
dt(2018, 9, 30, 21, 25),
{
"state": STATE_ON,
"update": dt(2018, 10, 1, 6, 49),
},
),
make_nyc_test_params(
dt(2018, 10, 1, 21, 25),
{
"state": STATE_ON,
"update": dt(2018, 10, 2, 6, 50),
},
),
make_jerusalem_test_params(
dt(2018, 9, 29, 21, 25),
{
"state": STATE_OFF,
"update": dt(2018, 9, 30, 6, 29),
},
),
make_jerusalem_test_params(
dt(2018, 10, 1, 11, 25),
{
"state": STATE_ON,
"update": dt(2018, 10, 1, 19, 2),
},
),
make_jerusalem_test_params(
dt(2018, 10, 1, 21, 25),
{
"state": STATE_OFF,
"update": dt(2018, 10, 2, 6, 31),
},
),
]

MELACHA_TEST_IDS = [
Expand All @@ -39,7 +125,8 @@
"friday_upcoming_shabbat",
"upcoming_rosh_hashana",
"currently_rosh_hashana",
"second_day_rosh_hashana",
"second_day_rosh_hashana_night",
"second_day_rosh_hashana_day",
"currently_shabbat_chol_hamoed",
"upcoming_two_day_yomtov_in_diaspora",
"currently_first_day_of_two_day_yomtov_in_diaspora",
Expand Down Expand Up @@ -86,7 +173,9 @@ async def test_issur_melacha_sensor(

registry = await hass.helpers.entity_registry.async_get_registry()

with alter_time(test_time):
with alter_time(test_time), patch(
"homeassistant.helpers.event.async_track_point_in_time"
) as async_track_point_in_time:
assert await async_setup_component(
hass,
jewish_calendar.DOMAIN,
Expand All @@ -102,13 +191,9 @@ async def test_issur_melacha_sensor(
)
await hass.async_block_till_done()

future = dt_util.utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, future)
await hass.async_block_till_done()

assert (
hass.states.get("binary_sensor.test_issur_melacha_in_effect").state
== result
== result["state"]
)
entity = registry.async_get("binary_sensor.test_issur_melacha_in_effect")
target_uid = "_".join(
Expand All @@ -128,3 +213,74 @@ async def test_issur_melacha_sensor(
)
)
assert entity.unique_id == target_uid

assert async_track_point_in_time.call_args[0][2] == result["update"]


@pytest.mark.parametrize(
[
"now",
"candle_lighting",
"havdalah",
"diaspora",
"tzname",
"latitude",
"longitude",
"result",
],
[
make_nyc_test_params(dt(2020, 10, 23, 17, 46, 59), [STATE_OFF, STATE_ON]),
make_nyc_test_params(dt(2020, 10, 24, 18, 44, 59), [STATE_ON, STATE_OFF]),
],
ids=["before_candle_lighting", "before_havdalah"],
)
async def test_issur_melacha_sensor_update(
hass,
legacy_patchable_time,
now,
candle_lighting,
havdalah,
diaspora,
tzname,
latitude,
longitude,
result,
):
"""Test Issur Melacha sensor output."""
time_zone = dt_util.get_time_zone(tzname)
test_time = time_zone.localize(now)

hass.config.time_zone = time_zone
hass.config.latitude = latitude
hass.config.longitude = longitude

with alter_time(test_time):
assert await async_setup_component(
hass,
jewish_calendar.DOMAIN,
{
"jewish_calendar": {
"name": "test",
"language": "english",
"diaspora": diaspora,
"candle_lighting_minutes_before_sunset": candle_lighting,
"havdalah_minutes_after_sunset": havdalah,
}
},
)
await hass.async_block_till_done()

assert (
hass.states.get("binary_sensor.test_issur_melacha_in_effect").state
== result[0]
)

test_time += timedelta(seconds=1)
with alter_time(test_time):
async_fire_time_changed(hass, test_time)
await hass.async_block_till_done()

assert (
hass.states.get("binary_sensor.test_issur_melacha_in_effect").state
== result[1]
)
0