From 253ae3f4236e208195379471f80adb9a32c9ce13 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Tue, 2 Feb 2021 00:09:25 +0000 Subject: [PATCH] Lyric Code Improvements (#45819) Co-authored-by: J. Nick Koston --- homeassistant/components/lyric/__init__.py | 4 +-- homeassistant/components/lyric/climate.py | 35 ++++++++++------------ tests/components/lyric/test_config_flow.py | 14 ++++++--- 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/lyric/__init__.py b/homeassistant/components/lyric/__init__.py index d29fca09166..0697dfbfd35 100644 --- a/homeassistant/components/lyric/__init__.py +++ b/homeassistant/components/lyric/__init__.py @@ -85,7 +85,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: client = ConfigEntryLyricClient(session, oauth_session) client_id = hass.data[DOMAIN][CONF_CLIENT_ID] - lyric: Lyric = Lyric(client, client_id) + lyric = Lyric(client, client_id) async def async_update_data() -> Lyric: """Fetch data from Lyric.""" @@ -93,7 +93,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async with async_timeout.timeout(60): await lyric.get_locations() return lyric - except (*LYRIC_EXCEPTIONS, TimeoutError) as exception: + except LYRIC_EXCEPTIONS as exception: raise UpdateFailed(exception) from exception coordinator = DataUpdateCoordinator( diff --git a/homeassistant/components/lyric/climate.py b/homeassistant/components/lyric/climate.py index bcfefef9c93..41e8fa90b67 100644 --- a/homeassistant/components/lyric/climate.py +++ b/homeassistant/components/lyric/climate.py @@ -82,7 +82,11 @@ async def async_setup_entry( for location in coordinator.data.locations: for device in location.devices: - entities.append(LyricClimate(hass, coordinator, location, device)) + entities.append( + LyricClimate( + coordinator, location, device, hass.config.units.temperature_unit + ) + ) async_add_entities(entities, True) @@ -100,13 +104,13 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity): def __init__( self, - hass: HomeAssistantType, coordinator: DataUpdateCoordinator, location: LyricLocation, device: LyricDevice, + temperature_unit: str, ) -> None: """Initialize Honeywell Lyric climate entity.""" - self._temperature_unit = hass.config.units.temperature_unit + self._temperature_unit = temperature_unit # Setup supported hvac modes self._hvac_modes = [HVAC_MODE_OFF] @@ -161,23 +165,26 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity): @property def target_temperature(self) -> Optional[float]: """Return the temperature we try to reach.""" - device: LyricDevice = self.device + device = self.device if not device.hasDualSetpointStatus: return device.changeableValues.heatSetpoint + return None @property def target_temperature_low(self) -> Optional[float]: """Return the upper bound temperature we try to reach.""" - device: LyricDevice = self.device + device = self.device if device.hasDualSetpointStatus: return device.changeableValues.coolSetpoint + return None @property def target_temperature_high(self) -> Optional[float]: """Return the upper bound temperature we try to reach.""" - device: LyricDevice = self.device + device = self.device if device.hasDualSetpointStatus: return device.changeableValues.heatSetpoint + return None @property def preset_mode(self) -> Optional[str]: @@ -198,7 +205,7 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity): @property def min_temp(self) -> float: """Identify min_temp in Lyric API or defaults if not available.""" - device: LyricDevice = self.device + device = self.device if LYRIC_HVAC_MODE_COOL in device.allowedModes: return device.minCoolSetpoint return device.minHeatSetpoint @@ -206,7 +213,7 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity): @property def max_temp(self) -> float: """Identify max_temp in Lyric API or defaults if not available.""" - device: LyricDevice = self.device + device = self.device if LYRIC_HVAC_MODE_HEAT in device.allowedModes: return device.maxHeatSetpoint return device.maxCoolSetpoint @@ -216,7 +223,7 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity): target_temp_low = kwargs.get(ATTR_TARGET_TEMP_LOW) target_temp_high = kwargs.get(ATTR_TARGET_TEMP_HIGH) - device: LyricDevice = self.device + device = self.device if device.hasDualSetpointStatus: if target_temp_low is not None and target_temp_high is not None: temp = (target_temp_low, target_temp_high) @@ -255,16 +262,6 @@ class LyricClimate(LyricDeviceEntity, ClimateEntity): _LOGGER.error(exception) await self.coordinator.async_refresh() - async def async_set_preset_period(self, period: str) -> None: - """Set preset period (time).""" - try: - await self._update_thermostat( - self.location, self.device, nextPeriodTime=period - ) - except LYRIC_EXCEPTIONS as exception: - _LOGGER.error(exception) - await self.coordinator.async_refresh() - async def async_set_hold_time(self, time_period: str) -> None: """Set the time to hold until.""" _LOGGER.debug("set_hold_time: %s", time_period) diff --git a/tests/components/lyric/test_config_flow.py b/tests/components/lyric/test_config_flow.py index 24b6b68d731..78fd9013466 100644 --- a/tests/components/lyric/test_config_flow.py +++ b/tests/components/lyric/test_config_flow.py @@ -34,9 +34,9 @@ async def mock_impl(hass): async def test_abort_if_no_configuration(hass): """Check flow abort when no configuration.""" - flow = config_flow.OAuth2FlowHandler() - flow.hass = hass - result = await flow.async_step_user() + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "missing_configuration" @@ -114,8 +114,14 @@ async def test_full_flow( assert len(mock_setup.mock_calls) == 1 -async def test_abort_if_authorization_timeout(hass, mock_impl): +async def test_abort_if_authorization_timeout( + hass, mock_impl, current_request_with_host +): """Check Somfy authorization timeout.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + flow = config_flow.OAuth2FlowHandler() flow.hass = hass