Skip to content

Commit a368ad4

Browse files
authored
Set sensors to unknown when no next alarm is set in Sleep as Android (home-assistant#150558)
1 parent 254694b commit a368ad4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

homeassistant/components/sleep_as_android/sensor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ def _async_handle_event(self, webhook_id: str, data: dict[str, str]) -> None:
8585
):
8686
self._attr_native_value = label
8787

88+
if (
89+
data[ATTR_EVENT] == "alarm_rescheduled"
90+
and data.get(ATTR_VALUE1) is None
91+
):
92+
self._attr_native_value = None
93+
8894
self.async_write_ha_state()
8995

9096
async def async_added_to_hass(self) -> None:

tests/components/sleep_as_android/test_sensor.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,45 @@ async def test_webhook_sensor(
122122

123123
assert (state := hass.states.get("sensor.sleep_as_android_alarm_label"))
124124
assert state.state == "label"
125+
126+
127+
async def test_webhook_sensor_alarm_unset(
128+
hass: HomeAssistant,
129+
config_entry: MockConfigEntry,
130+
hass_client_no_auth: ClientSessionGenerator,
131+
) -> None:
132+
"""Test unsetting sensors if there is no next alarm."""
133+
134+
config_entry.add_to_hass(hass)
135+
await hass.config_entries.async_setup(config_entry.entry_id)
136+
await hass.async_block_till_done()
137+
138+
assert config_entry.state is ConfigEntryState.LOADED
139+
140+
client = await hass_client_no_auth()
141+
142+
response = await client.post(
143+
"/api/webhook/webhook_id",
144+
json={
145+
"event": "alarm_rescheduled",
146+
"value1": "1582719660934",
147+
"value2": "label",
148+
},
149+
)
150+
assert response.status == HTTPStatus.NO_CONTENT
151+
152+
assert (state := hass.states.get("sensor.sleep_as_android_next_alarm"))
153+
assert state.state == "2020-02-26T12:21:00+00:00"
154+
155+
assert (state := hass.states.get("sensor.sleep_as_android_alarm_label"))
156+
assert state.state == "label"
157+
158+
response = await client.post(
159+
"/api/webhook/webhook_id",
160+
json={"event": "alarm_rescheduled"},
161+
)
162+
assert (state := hass.states.get("sensor.sleep_as_android_next_alarm"))
163+
assert state.state == STATE_UNKNOWN
164+
165+
assert (state := hass.states.get("sensor.sleep_as_android_alarm_label"))
166+
assert state.state == STATE_UNKNOWN

0 commit comments

Comments
 (0)