From da1d6d38212509ed0448f15bb8b8b5ce816f6a8d Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Sun, 27 Jun 2021 11:59:11 -0700 Subject: [PATCH] Add service to reset SmartTub reminders (#51824) * Add service to reset SmartTub reminders * add test Co-authored-by: Franck Nijhof --- .../components/smarttub/binary_sensor.py | 15 ++++++++++++ .../components/smarttub/services.yaml | 19 +++++++++++++++ .../components/smarttub/test_binary_sensor.py | 24 +++++++++++++++++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/smarttub/binary_sensor.py b/homeassistant/components/smarttub/binary_sensor.py index 7bb4e8cc9e0..6ddeccadc74 100644 --- a/homeassistant/components/smarttub/binary_sensor.py +++ b/homeassistant/components/smarttub/binary_sensor.py @@ -30,6 +30,11 @@ ATTR_UPDATED_AT = "updated_at" # how many days to snooze the reminder for ATTR_REMINDER_DAYS = "days" +RESET_REMINDER_SCHEMA = { + vol.Required(ATTR_REMINDER_DAYS): vol.All( + vol.Coerce(int), vol.Range(min=30, max=365) + ) +} SNOOZE_REMINDER_SCHEMA = { vol.Required(ATTR_REMINDER_DAYS): vol.All( vol.Coerce(int), vol.Range(min=10, max=120) @@ -60,6 +65,11 @@ async def async_setup_entry(hass, entry, async_add_entities): SNOOZE_REMINDER_SCHEMA, "async_snooze", ) + platform.async_register_entity_service( + "reset_reminder", + RESET_REMINDER_SCHEMA, + "async_reset", + ) class SmartTubOnline(SmartTubSensorBase, BinarySensorEntity): @@ -127,6 +137,11 @@ class SmartTubReminder(SmartTubEntity, BinarySensorEntity): await self.reminder.snooze(days) await self.coordinator.async_request_refresh() + async def async_reset(self, days): + """Dismiss this reminder, and reset it to the specified number of days.""" + await self.reminder.reset(days) + await self.coordinator.async_request_refresh() + class SmartTubError(SmartTubEntity, BinarySensorEntity): """Indicates whether an error code is present. diff --git a/homeassistant/components/smarttub/services.yaml b/homeassistant/components/smarttub/services.yaml index bb5ee66f1d0..d9890dba35a 100644 --- a/homeassistant/components/smarttub/services.yaml +++ b/homeassistant/components/smarttub/services.yaml @@ -64,3 +64,22 @@ snooze_reminder: min: 10 max: 120 unit_of_measurement: days + +reset_reminder: + name: Reset a reminder + description: Reset a reminder, and set the next time it will be triggered. + target: + entity: + integration: smarttub + domain: binary_sensor + fields: + days: + name: Days + description: The number of days when the next reminder should trigger. + required: true + example: 180 + selector: + number: + min: 30 + max: 365 + unit_of_measurement: days diff --git a/tests/components/smarttub/test_binary_sensor.py b/tests/components/smarttub/test_binary_sensor.py index 98d404ef600..c84ef99328e 100644 --- a/tests/components/smarttub/test_binary_sensor.py +++ b/tests/components/smarttub/test_binary_sensor.py @@ -64,7 +64,7 @@ async def test_error(spa, hass, config_entry, mock_error): assert state.attributes["error_code"] == 11 -async def test_snooze(spa, setup_entry, hass): +async def test_snooze_reminder(spa, setup_entry, hass): """Test snoozing a reminder.""" entity_id = f"binary_sensor.{spa.brand}_{spa.model}_myfilter_reminder" @@ -76,9 +76,29 @@ async def test_snooze(spa, setup_entry, hass): "snooze_reminder", { "entity_id": entity_id, - "days": 30, + "days": days, }, blocking=True, ) reminder.snooze.assert_called_with(days) + + +async def test_reset_reminder(spa, setup_entry, hass): + """Test snoozing a reminder.""" + + entity_id = f"binary_sensor.{spa.brand}_{spa.model}_myfilter_reminder" + reminder = spa.get_reminders.return_value[0] + days = 180 + + await hass.services.async_call( + "smarttub", + "reset_reminder", + { + "entity_id": entity_id, + "days": days, + }, + blocking=True, + ) + + reminder.reset.assert_called_with(days)