From 9479a4fa240cd6d5a02baa8ac77c23bba60c68de Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Sun, 23 May 2021 14:55:54 -0700 Subject: [PATCH 1/5] Add service to snooze SmartTub reminders --- .../components/smarttub/binary_sensor.py | 22 +++++++++++++++++++ .../components/smarttub/services.yaml | 18 +++++++++++++++ .../components/smarttub/test_binary_sensor.py | 20 +++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/homeassistant/components/smarttub/binary_sensor.py b/homeassistant/components/smarttub/binary_sensor.py index 31c6f6d0bc0ce6..c710cb9d6b4442 100644 --- a/homeassistant/components/smarttub/binary_sensor.py +++ b/homeassistant/components/smarttub/binary_sensor.py @@ -2,12 +2,14 @@ import logging from smarttub import SpaReminder +import voluptuous as vol from homeassistant.components.binary_sensor import ( DEVICE_CLASS_CONNECTIVITY, DEVICE_CLASS_PROBLEM, BinarySensorEntity, ) +from homeassistant.helpers import entity_platform from .const import ATTR_REMINDERS, DOMAIN, SMARTTUB_CONTROLLER from .entity import SmartTubEntity, SmartTubSensorBase @@ -17,6 +19,12 @@ # whether the reminder has been snoozed (bool) ATTR_REMINDER_SNOOZED = "snoozed" +# how many days to snooze the reminder for +ATTR_SNOOZE_DAYS = "days" +SNOOZE_REMINDER_SCHEMA = { + vol.Required(ATTR_SNOOZE_DAYS): vol.All(vol.Coerce(int), vol.Range(min=1, max=120)) +} + async def async_setup_entry(hass, entry, async_add_entities): """Set up binary sensor entities for the binary sensors in the tub.""" @@ -33,6 +41,14 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(entities) + platform = entity_platform.current_platform.get() + + platform.async_register_entity_service( + "snooze_reminder", + SNOOZE_REMINDER_SCHEMA, + "async_snooze", + ) + class SmartTubOnline(SmartTubSensorBase, BinarySensorEntity): """A binary sensor indicating whether the spa is currently online (connected to the cloud).""" @@ -98,3 +114,9 @@ def extra_state_attributes(self): def device_class(self) -> str: """Return the device class for this entity.""" return DEVICE_CLASS_PROBLEM + + async def async_snooze(self, **kwargs): + """Snooze this reminder for the specified number of days.""" + days = kwargs[ATTR_SNOOZE_DAYS] + await self.reminder.snooze(days) + await self.coordinator.async_request_refresh() diff --git a/homeassistant/components/smarttub/services.yaml b/homeassistant/components/smarttub/services.yaml index 30bd225113e2d5..f3da5b7be519d4 100644 --- a/homeassistant/components/smarttub/services.yaml +++ b/homeassistant/components/smarttub/services.yaml @@ -45,3 +45,21 @@ set_secondary_filtration: - "away" required: true example: "frequent" + +snooze_reminder: + name: Snooze a reminder + description: Delay a reminder, so that it won't trigger again for a period of time + target: + entity: + integration: smarttub + domain: binary_sensor + fields: + days: + description: The number of days to delay the reminder + required: true + example: 7 + selector: + number: + min: 0 + max: 120 + unit_of_measurement: days diff --git a/tests/components/smarttub/test_binary_sensor.py b/tests/components/smarttub/test_binary_sensor.py index b5a7c516a0e4a8..b39986ef3943a1 100644 --- a/tests/components/smarttub/test_binary_sensor.py +++ b/tests/components/smarttub/test_binary_sensor.py @@ -19,3 +19,23 @@ async def test_reminders(spa, setup_entry, hass): assert state is not None assert state.state == STATE_OFF assert state.attributes["snoozed"] is False + + +async def test_snooze(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 = 30 + + await hass.services.async_call( + "smarttub", + "snooze_reminder", + { + "entity_id": entity_id, + "days": 30, + }, + blocking=True, + ) + + reminder.snooze.assert_called_with(days) From 019d6b2006285bc29027f99e55cab14df41f90c3 Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Sun, 23 May 2021 15:12:42 -0700 Subject: [PATCH 2/5] minimum is 10 days --- homeassistant/components/smarttub/binary_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/smarttub/binary_sensor.py b/homeassistant/components/smarttub/binary_sensor.py index c710cb9d6b4442..2ca2e10245cbda 100644 --- a/homeassistant/components/smarttub/binary_sensor.py +++ b/homeassistant/components/smarttub/binary_sensor.py @@ -22,7 +22,7 @@ # how many days to snooze the reminder for ATTR_SNOOZE_DAYS = "days" SNOOZE_REMINDER_SCHEMA = { - vol.Required(ATTR_SNOOZE_DAYS): vol.All(vol.Coerce(int), vol.Range(min=1, max=120)) + vol.Required(ATTR_SNOOZE_DAYS): vol.All(vol.Coerce(int), vol.Range(min=10, max=120)) } From 3d26190471dc91991a5163632b2417521f4eef85 Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Sun, 23 May 2021 18:53:30 -0700 Subject: [PATCH 3/5] 0->10 in services.yaml as well --- homeassistant/components/smarttub/services.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/smarttub/services.yaml b/homeassistant/components/smarttub/services.yaml index f3da5b7be519d4..5403a72fea6728 100644 --- a/homeassistant/components/smarttub/services.yaml +++ b/homeassistant/components/smarttub/services.yaml @@ -60,6 +60,6 @@ snooze_reminder: example: 7 selector: number: - min: 0 + min: 10 max: 120 unit_of_measurement: days From df1a9bfe00daa94db5d715eaa7d417f3dc48c907 Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Tue, 25 May 2021 08:38:39 -0700 Subject: [PATCH 4/5] Update homeassistant/components/smarttub/services.yaml Co-authored-by: Franck Nijhof --- homeassistant/components/smarttub/services.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/smarttub/services.yaml b/homeassistant/components/smarttub/services.yaml index 5403a72fea6728..e4d4f5c0944ade 100644 --- a/homeassistant/components/smarttub/services.yaml +++ b/homeassistant/components/smarttub/services.yaml @@ -55,7 +55,8 @@ snooze_reminder: domain: binary_sensor fields: days: - description: The number of days to delay the reminder + name: Days + description: The number of days to delay the reminder. required: true example: 7 selector: From 7caea5ebdd9b6159fceb8ff924ceeabf73d63351 Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Tue, 25 May 2021 08:38:47 -0700 Subject: [PATCH 5/5] Update homeassistant/components/smarttub/services.yaml Co-authored-by: Franck Nijhof --- homeassistant/components/smarttub/services.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/smarttub/services.yaml b/homeassistant/components/smarttub/services.yaml index e4d4f5c0944ade..bb5ee66f1d076f 100644 --- a/homeassistant/components/smarttub/services.yaml +++ b/homeassistant/components/smarttub/services.yaml @@ -48,7 +48,7 @@ set_secondary_filtration: snooze_reminder: name: Snooze a reminder - description: Delay a reminder, so that it won't trigger again for a period of time + description: Delay a reminder, so that it won't trigger again for a period of time. target: entity: integration: smarttub