From bf1cacf4b27ce44af040fb12f9c4533c2eb5f3b1 Mon Sep 17 00:00:00 2001 From: Tim Rightnour <6556271+garbled1@users.noreply.github.com> Date: Sun, 5 Dec 2021 09:22:13 -0700 Subject: [PATCH] Address late review of Balboa (#61004) * Initial fixes from review of balboa climate * Minor fixes from review --- homeassistant/components/balboa/__init__.py | 8 ++++---- homeassistant/components/balboa/binary_sensor.py | 6 ++---- homeassistant/components/balboa/climate.py | 10 ++++------ homeassistant/components/balboa/config_flow.py | 8 +++++--- homeassistant/components/balboa/entity.py | 2 +- tests/components/balboa/test_climate.py | 5 ++--- 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/balboa/__init__.py b/homeassistant/components/balboa/__init__.py index 0922218aa5c..731f7b2c2d1 100644 --- a/homeassistant/components/balboa/__init__.py +++ b/homeassistant/components/balboa/__init__.py @@ -1,6 +1,6 @@ """The Balboa Spa Client integration.""" import asyncio -from datetime import timedelta +from datetime import datetime, timedelta import time from pybalboa import BalboaSpaWifi @@ -52,7 +52,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: spa.new_data_cb = _async_balboa_update_cb _LOGGER.debug("Starting listener and monitor tasks") - hass.loop.create_task(spa.listen()) + asyncio.create_task(spa.listen()) await spa.spa_configured() asyncio.create_task(spa.check_connection_status()) @@ -92,11 +92,11 @@ async def async_setup_time_sync(hass: HomeAssistant, entry: ConfigEntry) -> None _LOGGER.debug("Setting up daily time sync") spa = hass.data[DOMAIN][entry.entry_id] - async def sync_time(): + async def sync_time(now: datetime): _LOGGER.debug("Syncing time with Home Assistant") await spa.set_time(time.strptime(str(dt_util.now()), "%Y-%m-%d %H:%M:%S.%f%z")) - await sync_time() + await sync_time(dt_util.utcnow()) entry.async_on_unload( async_track_time_interval(hass, sync_time, SYNC_TIME_INTERVAL) ) diff --git a/homeassistant/components/balboa/binary_sensor.py b/homeassistant/components/balboa/binary_sensor.py index 133ed2da9f4..e00537439a1 100644 --- a/homeassistant/components/balboa/binary_sensor.py +++ b/homeassistant/components/balboa/binary_sensor.py @@ -18,11 +18,9 @@ FILTER_STATES = [ async def async_setup_entry(hass, entry, async_add_entities): """Set up the spa's binary sensors.""" spa = hass.data[DOMAIN][entry.entry_id] - entities = [ - BalboaSpaFilter(hass, entry, spa, FILTER, index) for index in range(1, 3) - ] + entities = [BalboaSpaFilter(entry, spa, FILTER, index) for index in range(1, 3)] if spa.have_circ_pump(): - entities.append(BalboaSpaCircPump(hass, entry, spa, CIRC_PUMP)) + entities.append(BalboaSpaCircPump(entry, spa, CIRC_PUMP)) async_add_entities(entities) diff --git a/homeassistant/components/balboa/climate.py b/homeassistant/components/balboa/climate.py index 567c65d6388..c99448a77de 100644 --- a/homeassistant/components/balboa/climate.py +++ b/homeassistant/components/balboa/climate.py @@ -25,7 +25,6 @@ from homeassistant.const import ( TEMP_CELSIUS, TEMP_FAHRENHEIT, ) -from homeassistant.exceptions import HomeAssistantError from .const import CLIMATE, CLIMATE_SUPPORTED_FANSTATES, CLIMATE_SUPPORTED_MODES, DOMAIN from .entity import BalboaEntity @@ -36,7 +35,6 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities( [ BalboaSpaClimate( - hass, entry, hass.data[DOMAIN][entry.entry_id], CLIMATE, @@ -52,9 +50,9 @@ class BalboaSpaClimate(BalboaEntity, ClimateEntity): _attr_fan_modes = CLIMATE_SUPPORTED_FANSTATES _attr_hvac_modes = CLIMATE_SUPPORTED_MODES - def __init__(self, hass, entry, client, devtype, num=None): + def __init__(self, entry, client, devtype, num=None): """Initialize the climate entity.""" - super().__init__(hass, entry, client, devtype, num) + super().__init__(entry, client, devtype, num) self._balboa_to_ha_blower_map = { self._client.BLOWER_OFF: FAN_OFF, self._client.BLOWER_LOW: FAN_LOW, @@ -137,7 +135,7 @@ class BalboaSpaClimate(BalboaEntity, ClimateEntity): modelist = self._client.get_heatmode_stringlist() self._async_validate_mode_or_raise(preset_mode) if preset_mode not in modelist: - raise HomeAssistantError(f"{preset_mode} is not a valid preset mode") + raise ValueError(f"{preset_mode} is not a valid preset mode") await self._client.change_heatmode(modelist.index(preset_mode)) async def async_set_fan_mode(self, fan_mode): @@ -147,7 +145,7 @@ class BalboaSpaClimate(BalboaEntity, ClimateEntity): def _async_validate_mode_or_raise(self, mode): """Check that the mode can be set.""" if mode == self._client.HEATMODE_RNR: - raise HomeAssistantError(f"{mode} can only be reported but not set") + raise ValueError(f"{mode} can only be reported but not set") async def async_set_hvac_mode(self, hvac_mode): """Set new target hvac mode. diff --git a/homeassistant/components/balboa/config_flow.py b/homeassistant/components/balboa/config_flow.py index 1c91376d76e..42895e5ccd6 100644 --- a/homeassistant/components/balboa/config_flow.py +++ b/homeassistant/components/balboa/config_flow.py @@ -1,4 +1,6 @@ """Config flow for Balboa Spa Client integration.""" +import asyncio + from pybalboa import BalboaSpaWifi import voluptuous as vol @@ -26,15 +28,15 @@ async def validate_input(hass: core.HomeAssistant, data): await spa.send_mod_ident_req() await spa.send_panel_req(0, 1) - hass.loop.create_task(spa.listen()) + asyncio.create_task(spa.listen()) await spa.spa_configured() - macaddr = format_mac(spa.get_macaddr()) + mac_addr = format_mac(spa.get_macaddr()) model = spa.get_model_name() await spa.disconnect() - return {"title": model, "formatted_mac": macaddr} + return {"title": model, "formatted_mac": mac_addr} class BalboaSpaClientFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): diff --git a/homeassistant/components/balboa/entity.py b/homeassistant/components/balboa/entity.py index 016beadac5c..44f06350243 100644 --- a/homeassistant/components/balboa/entity.py +++ b/homeassistant/components/balboa/entity.py @@ -19,7 +19,7 @@ class BalboaEntity(Entity): _attr_should_poll = False - def __init__(self, hass, entry, client, devtype, num=None): + def __init__(self, entry, client, devtype, num=None): """Initialize the spa entity.""" self._client = client self._device_name = self._client.get_model_name() diff --git a/tests/components/balboa/test_climate.py b/tests/components/balboa/test_climate.py index 53eb0307beb..2363c35efaa 100644 --- a/tests/components/balboa/test_climate.py +++ b/tests/components/balboa/test_climate.py @@ -28,7 +28,6 @@ from homeassistant.components.climate.const import ( ) from homeassistant.const import ATTR_TEMPERATURE, TEMP_FAHRENHEIT from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.setup import async_setup_component @@ -154,7 +153,7 @@ async def test_spa_hvac_modes(hass: HomeAssistant): assert [HVAC_MODE_AUTO, HVAC_MODE_HEAT, HVAC_MODE_OFF] == modes assert state.state == HVAC_SETTINGS[heat_mode] - with pytest.raises(HomeAssistantError): + with pytest.raises(ValueError): await _patch_spa_heatmode(hass, config_entry, 2) @@ -198,7 +197,7 @@ async def test_spa_preset_modes(hass: HomeAssistant): with patch( "homeassistant.components.balboa.BalboaSpaWifi.get_heatmode", return_value=2, - ), pytest.raises(HomeAssistantError): + ), pytest.raises(ValueError): await common.async_set_preset_mode(hass, 2, ENTITY_CLIMATE)