diff --git a/homeassistant/components/smarttub/__init__.py b/homeassistant/components/smarttub/__init__.py index 8467c208076..457af4b7bc0 100644 --- a/homeassistant/components/smarttub/__init__.py +++ b/homeassistant/components/smarttub/__init__.py @@ -7,7 +7,7 @@ from .controller import SmartTubController _LOGGER = logging.getLogger(__name__) -PLATFORMS = ["climate", "light", "sensor", "switch"] +PLATFORMS = ["binary_sensor", "climate", "light", "sensor", "switch"] async def async_setup(hass, config): diff --git a/homeassistant/components/smarttub/binary_sensor.py b/homeassistant/components/smarttub/binary_sensor.py new file mode 100644 index 00000000000..52dbfd71a37 --- /dev/null +++ b/homeassistant/components/smarttub/binary_sensor.py @@ -0,0 +1,40 @@ +"""Platform for binary sensor integration.""" +import logging + +from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_CONNECTIVITY, + BinarySensorEntity, +) + +from .const import DOMAIN, SMARTTUB_CONTROLLER +from .entity import SmartTubSensorBase + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass, entry, async_add_entities): + """Set up binary sensor entities for the binary sensors in the tub.""" + + controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER] + + entities = [SmartTubOnline(controller.coordinator, spa) for spa in controller.spas] + + async_add_entities(entities) + + +class SmartTubOnline(SmartTubSensorBase, BinarySensorEntity): + """A binary sensor indicating whether the spa is currently online (connected to the cloud).""" + + def __init__(self, coordinator, spa): + """Initialize the entity.""" + super().__init__(coordinator, spa, "Online", "online") + + @property + def is_on(self) -> bool: + """Return true if the binary sensor is on.""" + return self._state is True + + @property + def device_class(self) -> str: + """Return the device class for this entity.""" + return DEVICE_CLASS_CONNECTIVITY diff --git a/homeassistant/components/smarttub/entity.py b/homeassistant/components/smarttub/entity.py index eab60b4162c..8be956a2b70 100644 --- a/homeassistant/components/smarttub/entity.py +++ b/homeassistant/components/smarttub/entity.py @@ -13,8 +13,6 @@ from .helpers import get_spa_name _LOGGER = logging.getLogger(__name__) -PLATFORMS = ["climate"] - class SmartTubEntity(CoordinatorEntity): """Base class for SmartTub entities.""" @@ -57,3 +55,17 @@ class SmartTubEntity(CoordinatorEntity): """Retrieve the result of Spa.get_status().""" return self.coordinator.data[self.spa.id].get("status") + + +class SmartTubSensorBase(SmartTubEntity): + """Base class for SmartTub sensors.""" + + def __init__(self, coordinator, spa, sensor_name, attr_name): + """Initialize the entity.""" + super().__init__(coordinator, spa, sensor_name) + self._attr_name = attr_name + + @property + def _state(self): + """Retrieve the underlying state from the spa.""" + return getattr(self.spa_status, self._attr_name) diff --git a/homeassistant/components/smarttub/sensor.py b/homeassistant/components/smarttub/sensor.py index 4619d07dbe0..be3d60c0241 100644 --- a/homeassistant/components/smarttub/sensor.py +++ b/homeassistant/components/smarttub/sensor.py @@ -3,7 +3,7 @@ from enum import Enum import logging from .const import DOMAIN, SMARTTUB_CONTROLLER -from .entity import SmartTubEntity +from .entity import SmartTubSensorBase _LOGGER = logging.getLogger(__name__) @@ -42,18 +42,8 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(entities) -class SmartTubSensor(SmartTubEntity): - """Generic and base class for SmartTub sensors.""" - - def __init__(self, coordinator, spa, sensor_name, attr_name): - """Initialize the entity.""" - super().__init__(coordinator, spa, sensor_name) - self._attr_name = attr_name - - @property - def _state(self): - """Retrieve the underlying state from the spa.""" - return getattr(self.spa_status, self._attr_name) +class SmartTubSensor(SmartTubSensorBase): + """Generic class for SmartTub status sensors.""" @property def state(self) -> str: diff --git a/tests/components/smarttub/conftest.py b/tests/components/smarttub/conftest.py index 79e5d06d3b3..b7c90b5ad3e 100644 --- a/tests/components/smarttub/conftest.py +++ b/tests/components/smarttub/conftest.py @@ -48,6 +48,7 @@ def mock_spa(): "setTemperature": 39, "water": {"temperature": 38}, "heater": "ON", + "online": True, "heatMode": "AUTO", "state": "NORMAL", "primaryFiltration": { diff --git a/tests/components/smarttub/test_binary_sensor.py b/tests/components/smarttub/test_binary_sensor.py new file mode 100644 index 00000000000..b2624369e96 --- /dev/null +++ b/tests/components/smarttub/test_binary_sensor.py @@ -0,0 +1,13 @@ +"""Test the SmartTub binary sensor platform.""" + +from homeassistant.components.binary_sensor import DEVICE_CLASS_CONNECTIVITY, STATE_ON + + +async def test_binary_sensors(spa, setup_entry, hass): + """Test the binary sensors.""" + + entity_id = f"binary_sensor.{spa.brand}_{spa.model}_online" + state = hass.states.get(entity_id) + assert state is not None + assert state.state == STATE_ON + assert state.attributes.get("device_class") == DEVICE_CLASS_CONNECTIVITY