Add binary sensor to SmartTub for online status (#46889)

This commit is contained in:
Matt Zimmerman 2021-02-22 06:10:00 -08:00 committed by GitHub
parent 603191702f
commit 81d011efc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 72 additions and 16 deletions

View File

@ -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):

View File

@ -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

View File

@ -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)

View File

@ -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:

View File

@ -48,6 +48,7 @@ def mock_spa():
"setTemperature": 39,
"water": {"temperature": 38},
"heater": "ON",
"online": True,
"heatMode": "AUTO",
"state": "NORMAL",
"primaryFiltration": {

View File

@ -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