Lyric Code Improvements (#45819)

Co-authored-by: J. Nick Koston <nick@koston.org>
pull/45852/head
Aidan Timson 2021-02-02 00:09:25 +00:00 committed by GitHub
parent f2286d4811
commit 253ae3f423
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 25 deletions

View File

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

View File

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

View File

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