1 """Platform for binary sensor integration."""
3 from __future__
import annotations
5 from smarttub
import SpaError, SpaReminder
6 import voluptuous
as vol
9 BinarySensorDeviceClass,
18 from .const
import ATTR_ERRORS, ATTR_REMINDERS, DOMAIN, SMARTTUB_CONTROLLER
19 from .entity
import SmartTubEntity, SmartTubSensorBase
22 ATTR_REMINDER_SNOOZED =
"snoozed"
24 ATTR_ERROR_CODE =
"error_code"
25 ATTR_ERROR_TITLE =
"error_title"
26 ATTR_ERROR_DESCRIPTION =
"error_description"
27 ATTR_ERROR_TYPE =
"error_type"
28 ATTR_CREATED_AT =
"created_at"
29 ATTR_UPDATED_AT =
"updated_at"
32 ATTR_REMINDER_DAYS =
"days"
33 RESET_REMINDER_SCHEMA: VolDictType = {
34 vol.Required(ATTR_REMINDER_DAYS): vol.All(
35 vol.Coerce(int), vol.Range(min=30, max=365)
38 SNOOZE_REMINDER_SCHEMA: VolDictType = {
39 vol.Required(ATTR_REMINDER_DAYS): vol.All(
40 vol.Coerce(int), vol.Range(min=10, max=120)
46 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
48 """Set up binary sensor entities for the binary sensors in the tub."""
50 controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
52 entities: list[BinarySensorEntity] = []
53 for spa
in controller.spas:
58 for reminder
in controller.coordinator.data[spa.id][ATTR_REMINDERS].values()
63 platform = entity_platform.async_get_current_platform()
65 platform.async_register_entity_service(
67 SNOOZE_REMINDER_SCHEMA,
70 platform.async_register_entity_service(
72 RESET_REMINDER_SCHEMA,
78 """A binary sensor indicating whether the spa is currently online (connected to the cloud)."""
80 _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
82 _attr_entity_registry_enabled_default =
False
85 """Initialize the entity."""
86 super().
__init__(coordinator, spa,
"Online",
"online")
90 """Return true if the binary sensor is on."""
91 return self.
_state_state
is True
95 """Reminders for maintenance actions."""
97 _attr_device_class = BinarySensorDeviceClass.PROBLEM
99 def __init__(self, coordinator, spa, reminder):
100 """Initialize the entity."""
104 f
"{reminder.name.title()} Reminder",
111 """Return the underlying SpaReminder object for this entity."""
112 return self.coordinator.data[self.
spaspa.id][ATTR_REMINDERS][self.
reminder_idreminder_id]
116 """Return whether the specified maintenance action needs to be taken."""
117 return self.
reminderreminder.remaining_days == 0
121 """Return the state attributes."""
123 ATTR_REMINDER_SNOOZED: self.
reminderreminder.snoozed,
124 ATTR_REMINDER_DAYS: self.
reminderreminder.remaining_days,
128 """Snooze this reminder for the specified number of days."""
129 await self.
reminderreminder.snooze(days)
130 await self.coordinator.async_request_refresh()
133 """Dismiss this reminder, and reset it to the specified number of days."""
134 await self.
reminderreminder.reset(days)
135 await self.coordinator.async_request_refresh()
139 """Indicates whether an error code is present.
141 There may be 0 or more errors. If there are >0, we show the first one.
144 _attr_device_class = BinarySensorDeviceClass.PROBLEM
147 """Initialize the entity."""
156 """Return the underlying SpaError object for this entity."""
157 errors = self.coordinator.data[self.
spaspa.id][ATTR_ERRORS]
164 """Return true if an error is signaled."""
165 return self.
errorerror
is not None
169 """Return the state attributes."""
170 if (error := self.
errorerror)
is None:
174 ATTR_ERROR_CODE: error.code,
175 ATTR_ERROR_TITLE: error.title,
176 ATTR_ERROR_DESCRIPTION: error.description,
177 ATTR_ERROR_TYPE: error.error_type,
178 ATTR_CREATED_AT: error.created_at.isoformat(),
179 ATTR_UPDATED_AT: error.updated_at.isoformat(),
def __init__(self, coordinator, spa)
def extra_state_attributes(self)
SpaError|None error(self)
def __init__(self, coordinator, spa)
def extra_state_attributes(self)
def __init__(self, coordinator, spa, reminder)
SpaReminder reminder(self)
def async_reset(self, days)
def async_snooze(self, days)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)