mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Add binary_sensor entities for SmartTub reminders (#47583)
This commit is contained in:
parent
6a66ccef1b
commit
73c6728e98
@ -1,23 +1,38 @@
|
|||||||
"""Platform for binary sensor integration."""
|
"""Platform for binary sensor integration."""
|
||||||
|
from datetime import datetime, timedelta
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from smarttub import SpaReminder
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
DEVICE_CLASS_CONNECTIVITY,
|
DEVICE_CLASS_CONNECTIVITY,
|
||||||
|
DEVICE_CLASS_PROBLEM,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import DOMAIN, SMARTTUB_CONTROLLER
|
from .const import ATTR_REMINDERS, DOMAIN, SMARTTUB_CONTROLLER
|
||||||
from .entity import SmartTubSensorBase
|
from .entity import SmartTubEntity, SmartTubSensorBase
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# whether the reminder has been snoozed (bool)
|
||||||
|
ATTR_REMINDER_SNOOZED = "snoozed"
|
||||||
|
# the date at which the reminder will be activated
|
||||||
|
ATTR_REMINDER_DATE = "date"
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry, async_add_entities):
|
async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
"""Set up binary sensor entities for the binary sensors in the tub."""
|
"""Set up binary sensor entities for the binary sensors in the tub."""
|
||||||
|
|
||||||
controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
|
controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
|
||||||
|
|
||||||
entities = [SmartTubOnline(controller.coordinator, spa) for spa in controller.spas]
|
entities = []
|
||||||
|
for spa in controller.spas:
|
||||||
|
entities.append(SmartTubOnline(controller.coordinator, spa))
|
||||||
|
entities.extend(
|
||||||
|
SmartTubReminder(controller.coordinator, spa, reminder)
|
||||||
|
for reminder in controller.coordinator.data[spa.id][ATTR_REMINDERS].values()
|
||||||
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
@ -38,3 +53,45 @@ class SmartTubOnline(SmartTubSensorBase, BinarySensorEntity):
|
|||||||
def device_class(self) -> str:
|
def device_class(self) -> str:
|
||||||
"""Return the device class for this entity."""
|
"""Return the device class for this entity."""
|
||||||
return DEVICE_CLASS_CONNECTIVITY
|
return DEVICE_CLASS_CONNECTIVITY
|
||||||
|
|
||||||
|
|
||||||
|
class SmartTubReminder(SmartTubEntity, BinarySensorEntity):
|
||||||
|
"""Reminders for maintenance actions."""
|
||||||
|
|
||||||
|
def __init__(self, coordinator, spa, reminder):
|
||||||
|
"""Initialize the entity."""
|
||||||
|
super().__init__(
|
||||||
|
coordinator,
|
||||||
|
spa,
|
||||||
|
f"{reminder.name.title()} Reminder",
|
||||||
|
)
|
||||||
|
self.reminder_id = reminder.id
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return a unique id for this sensor."""
|
||||||
|
return f"{self.spa.id}-reminder-{self.reminder_id}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def reminder(self) -> SpaReminder:
|
||||||
|
"""Return the underlying SpaReminder object for this entity."""
|
||||||
|
return self.coordinator.data[self.spa.id]["reminders"][self.reminder_id]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return whether the specified maintenance action needs to be taken."""
|
||||||
|
return self.reminder.remaining_days == 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_state_attributes(self):
|
||||||
|
"""Return the state attributes."""
|
||||||
|
when = datetime.now() + timedelta(days=self.reminder.remaining_days)
|
||||||
|
return {
|
||||||
|
ATTR_REMINDER_SNOOZED: self.reminder.snoozed,
|
||||||
|
ATTR_REMINDER_DATE: when.date().isoformat(),
|
||||||
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self) -> str:
|
||||||
|
"""Return the device class for this entity."""
|
||||||
|
return DEVICE_CLASS_PROBLEM
|
||||||
|
@ -54,7 +54,7 @@ class SmartTubThermostat(SmartTubEntity, ClimateEntity):
|
|||||||
|
|
||||||
def __init__(self, coordinator, spa):
|
def __init__(self, coordinator, spa):
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
super().__init__(coordinator, spa, "thermostat")
|
super().__init__(coordinator, spa, "Thermostat")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def temperature_unit(self):
|
def temperature_unit(self):
|
||||||
|
@ -21,6 +21,7 @@ DEFAULT_LIGHT_EFFECT = "purple"
|
|||||||
# default to 50% brightness
|
# default to 50% brightness
|
||||||
DEFAULT_LIGHT_BRIGHTNESS = 128
|
DEFAULT_LIGHT_BRIGHTNESS = 128
|
||||||
|
|
||||||
ATTR_STATUS = "status"
|
|
||||||
ATTR_PUMPS = "pumps"
|
|
||||||
ATTR_LIGHTS = "lights"
|
ATTR_LIGHTS = "lights"
|
||||||
|
ATTR_PUMPS = "pumps"
|
||||||
|
ATTR_REMINDERS = "reminders"
|
||||||
|
ATTR_STATUS = "status"
|
||||||
|
@ -18,6 +18,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||||||
from .const import (
|
from .const import (
|
||||||
ATTR_LIGHTS,
|
ATTR_LIGHTS,
|
||||||
ATTR_PUMPS,
|
ATTR_PUMPS,
|
||||||
|
ATTR_REMINDERS,
|
||||||
ATTR_STATUS,
|
ATTR_STATUS,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
POLLING_TIMEOUT,
|
POLLING_TIMEOUT,
|
||||||
@ -93,15 +94,17 @@ class SmartTubController:
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
async def _get_spa_data(self, spa):
|
async def _get_spa_data(self, spa):
|
||||||
status, pumps, lights = await asyncio.gather(
|
status, pumps, lights, reminders = await asyncio.gather(
|
||||||
spa.get_status(),
|
spa.get_status(),
|
||||||
spa.get_pumps(),
|
spa.get_pumps(),
|
||||||
spa.get_lights(),
|
spa.get_lights(),
|
||||||
|
spa.get_reminders(),
|
||||||
)
|
)
|
||||||
return {
|
return {
|
||||||
ATTR_STATUS: status,
|
ATTR_STATUS: status,
|
||||||
ATTR_PUMPS: {pump.id: pump for pump in pumps},
|
ATTR_PUMPS: {pump.id: pump for pump in pumps},
|
||||||
ATTR_LIGHTS: {light.zone: light for light in lights},
|
ATTR_LIGHTS: {light.zone: light for light in lights},
|
||||||
|
ATTR_REMINDERS: {reminder.id: reminder for reminder in reminders},
|
||||||
}
|
}
|
||||||
|
|
||||||
async def async_register_devices(self, entry):
|
async def async_register_devices(self, entry):
|
||||||
|
@ -105,6 +105,14 @@ def mock_spa():
|
|||||||
|
|
||||||
mock_spa.get_lights.return_value = [mock_light_off, mock_light_on]
|
mock_spa.get_lights.return_value = [mock_light_off, mock_light_on]
|
||||||
|
|
||||||
|
mock_filter_reminder = create_autospec(smarttub.SpaReminder, instance=True)
|
||||||
|
mock_filter_reminder.id = "FILTER01"
|
||||||
|
mock_filter_reminder.name = "MyFilter"
|
||||||
|
mock_filter_reminder.remaining_days = 2
|
||||||
|
mock_filter_reminder.snoozed = False
|
||||||
|
|
||||||
|
mock_spa.get_reminders.return_value = [mock_filter_reminder]
|
||||||
|
|
||||||
return mock_spa
|
return mock_spa
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,31 @@
|
|||||||
"""Test the SmartTub binary sensor platform."""
|
"""Test the SmartTub binary sensor platform."""
|
||||||
|
from datetime import date, timedelta
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import DEVICE_CLASS_CONNECTIVITY, STATE_ON
|
from homeassistant.components.binary_sensor import (
|
||||||
|
DEVICE_CLASS_CONNECTIVITY,
|
||||||
|
STATE_OFF,
|
||||||
|
STATE_ON,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_binary_sensors(spa, setup_entry, hass):
|
async def test_binary_sensors(spa, setup_entry, hass):
|
||||||
"""Test the binary sensors."""
|
"""Test simple binary sensors."""
|
||||||
|
|
||||||
entity_id = f"binary_sensor.{spa.brand}_{spa.model}_online"
|
entity_id = f"binary_sensor.{spa.brand}_{spa.model}_online"
|
||||||
state = hass.states.get(entity_id)
|
state = hass.states.get(entity_id)
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state == STATE_ON
|
assert state.state == STATE_ON
|
||||||
assert state.attributes.get("device_class") == DEVICE_CLASS_CONNECTIVITY
|
assert state.attributes.get("device_class") == DEVICE_CLASS_CONNECTIVITY
|
||||||
|
|
||||||
|
|
||||||
|
async def test_reminders(spa, setup_entry, hass):
|
||||||
|
"""Test the reminder sensor."""
|
||||||
|
|
||||||
|
entity_id = f"binary_sensor.{spa.brand}_{spa.model}_myfilter_reminder"
|
||||||
|
state = hass.states.get(entity_id)
|
||||||
|
assert state is not None
|
||||||
|
assert state.state == STATE_OFF
|
||||||
|
assert date.fromisoformat(state.attributes["date"]) <= date.today() + timedelta(
|
||||||
|
days=2
|
||||||
|
)
|
||||||
|
assert state.attributes["snoozed"] is False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user