Add smarttub sensor platform and state sensor (#46775)

This commit is contained in:
Matt Zimmerman 2021-02-20 14:25:02 -08:00 committed by GitHub
parent 2b6619f815
commit 115fe26642
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 84 additions and 40 deletions

View file

@ -7,7 +7,7 @@ from .controller import SmartTubController
_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["climate"]
PLATFORMS = ["climate", "sensor"]
async def async_setup(hass, config):

View file

@ -36,11 +36,6 @@ class SmartTubThermostat(SmartTubEntity, ClimateEntity):
"""Initialize the entity."""
super().__init__(coordinator, spa, "thermostat")
@property
def unique_id(self) -> str:
"""Return a unique id for the entity."""
return f"{self.spa.id}-{self._entity_type}"
@property
def temperature_unit(self):
"""Return the unit of measurement used by the platform."""

View file

@ -32,6 +32,11 @@ class SmartTubEntity(CoordinatorEntity):
self.spa = spa
self._entity_type = entity_type
@property
def unique_id(self) -> str:
"""Return a unique id for the entity."""
return f"{self.spa.id}-{self._entity_type}"
@property
def device_info(self) -> str:
"""Return device info."""

View file

@ -0,0 +1,30 @@
"""Platform for sensor integration."""
import logging
from .const import DOMAIN, SMARTTUB_CONTROLLER
from .entity import SmartTubEntity
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up climate entity for the thermostat in the tub."""
controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
entities = [SmartTubState(controller.coordinator, spa) for spa in controller.spas]
async_add_entities(entities)
class SmartTubState(SmartTubEntity):
"""The state of the spa."""
def __init__(self, coordinator, spa):
"""Initialize the entity."""
super().__init__(coordinator, spa, "state")
@property
def state(self) -> str:
"""Return the current state of the sensor."""
return self.get_spa_status("state").lower()

View file

@ -1 +1,15 @@
"""Tests for the smarttub integration."""
from datetime import timedelta
from homeassistant.components.smarttub.const import SCAN_INTERVAL
from homeassistant.util import dt
from tests.common import async_fire_time_changed
async def trigger_update(hass):
"""Trigger a polling update by moving time forward."""
new_time = dt.utcnow() + timedelta(seconds=SCAN_INTERVAL + 1)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()

View file

@ -46,6 +46,7 @@ def mock_spa():
"setTemperature": 39,
"water": {"temperature": 38},
"heater": "ON",
"state": "NORMAL",
}
return mock_spa
@ -60,7 +61,7 @@ def mock_account(spa):
return mock_account
@pytest.fixture(name="smarttub_api")
@pytest.fixture(name="smarttub_api", autouse=True)
def mock_api(account, spa):
"""Mock the SmartTub API."""
@ -71,3 +72,11 @@ def mock_api(account, spa):
api_mock = api_class_mock.return_value
api_mock.get_account.return_value = account
yield api_mock
@pytest.fixture
async def setup_entry(hass, config_entry):
"""Initialize the config entry."""
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

View file

@ -1,7 +1,5 @@
"""Test the SmartTub climate platform."""
from datetime import timedelta
import smarttub
from homeassistant.components.climate.const import (
@ -19,35 +17,19 @@ from homeassistant.components.climate.const import (
SERVICE_SET_TEMPERATURE,
SUPPORT_TARGET_TEMPERATURE,
)
from homeassistant.components.smarttub.const import (
DEFAULT_MAX_TEMP,
DEFAULT_MIN_TEMP,
SCAN_INTERVAL,
)
from homeassistant.components.smarttub.const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_SUPPORTED_FEATURES,
ATTR_TEMPERATURE,
)
from homeassistant.util import dt
from tests.common import async_fire_time_changed
from . import trigger_update
async def test_thermostat_update(spa, hass, config_entry, smarttub_api):
async def test_thermostat_update(spa, setup_entry, hass):
"""Test the thermostat entity."""
spa.get_status.return_value = {
"heater": "ON",
"water": {
"temperature": 38,
},
"setTemperature": 39,
}
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
entity_id = f"climate.{spa.brand}_{spa.model}_thermostat"
state = hass.states.get(entity_id)
assert state
@ -87,10 +69,3 @@ async def test_thermostat_update(spa, hass, config_entry, smarttub_api):
spa.get_status.side_effect = smarttub.APIError
await trigger_update(hass)
# should not fail
async def trigger_update(hass):
"""Trigger a polling update by moving time forward."""
new_time = dt.utcnow() + timedelta(seconds=SCAN_INTERVAL + 1)
async_fire_time_changed(hass, new_time)
await hass.async_block_till_done()

View file

@ -7,7 +7,7 @@ from homeassistant import config_entries
from homeassistant.components.smarttub.const import DOMAIN
async def test_form(hass, smarttub_api):
async def test_form(hass):
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}

View file

@ -39,15 +39,13 @@ async def test_setup_auth_failed(setup_component, hass, config_entry, smarttub_a
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
async def test_config_passed_to_config_entry(
hass, config_entry, config_data, smarttub_api
):
async def test_config_passed_to_config_entry(hass, config_entry, config_data):
"""Test that configured options are loaded via config entry."""
config_entry.add_to_hass(hass)
assert await async_setup_component(hass, smarttub.DOMAIN, config_data)
async def test_unload_entry(hass, config_entry, smarttub_api):
async def test_unload_entry(hass, config_entry):
"""Test being able to unload an entry."""
config_entry.add_to_hass(hass)

View file

@ -0,0 +1,18 @@
"""Test the SmartTub sensor platform."""
from . import trigger_update
async def test_state_update(spa, setup_entry, hass, smarttub_api):
"""Test the state entity."""
entity_id = f"sensor.{spa.brand}_{spa.model}_state"
state = hass.states.get(entity_id)
assert state is not None
assert state.state == "normal"
spa.get_status.return_value["state"] = "BAD"
await trigger_update(hass)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == "bad"