1
mirror of https://github.com/home-assistant/core synced 2024-08-31 05:57:13 +02:00

Small code styling tweaks for HomeKit (#48163)

This commit is contained in:
Franck Nijhof 2021-03-21 04:08:49 +01:00 committed by GitHub
parent 99d2d72d13
commit 87499989a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 92 additions and 83 deletions

View File

@ -247,10 +247,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Determine is a name or port is already used."""
name = user_input[CONF_NAME]
port = user_input[CONF_PORT]
for entry in self._async_current_entries():
if entry.data[CONF_NAME] == name or entry.data[CONF_PORT] == port:
return False
return True
return not any(
entry.data[CONF_NAME] == name or entry.data[CONF_PORT] == port
for entry in self._async_current_entries()
)
@staticmethod
@callback

View File

@ -364,18 +364,17 @@ class WindowCoveringBasic(OpeningDeviceBase, HomeAccessory):
"""Move cover to value if call came from HomeKit."""
_LOGGER.debug("%s: Set position to %d", self.entity_id, value)
if self._supports_stop:
if value > 70:
service, position = (SERVICE_OPEN_COVER, 100)
elif value < 30:
service, position = (SERVICE_CLOSE_COVER, 0)
else:
service, position = (SERVICE_STOP_COVER, 50)
if (
self._supports_stop
and value > 70
or not self._supports_stop
and value >= 50
):
service, position = (SERVICE_OPEN_COVER, 100)
elif value < 30 or not self._supports_stop:
service, position = (SERVICE_CLOSE_COVER, 0)
else:
if value >= 50:
service, position = (SERVICE_OPEN_COVER, 100)
else:
service, position = (SERVICE_CLOSE_COVER, 0)
service, position = (SERVICE_STOP_COVER, 50)
params = {ATTR_ENTITY_ID: self.entity_id}
self.async_call_service(DOMAIN, service, params)

View File

@ -240,6 +240,8 @@ class HumidifierDehumidifier(HomeAccessory):
# Update target humidity
target_humidity = new_state.attributes.get(ATTR_HUMIDITY)
if isinstance(target_humidity, (int, float)):
if self.char_target_humidity.value != target_humidity:
self.char_target_humidity.set_value(target_humidity)
if (
isinstance(target_humidity, (int, float))
and self.char_target_humidity.value != target_humidity
):
self.char_target_humidity.set_value(target_humidity)

View File

@ -78,9 +78,11 @@ class Lock(HomeAccessory):
# LockTargetState only supports locked and unlocked
# Must set lock target state before current state
# or there will be no notification
if hass_state in (STATE_LOCKED, STATE_UNLOCKED):
if self.char_target_state.value != current_lock_state:
self.char_target_state.set_value(current_lock_state)
if (
hass_state in (STATE_LOCKED, STATE_UNLOCKED)
and self.char_target_state.value != current_lock_state
):
self.char_target_state.set_value(current_lock_state)
# Set lock current state ONLY after ensuring that
# target state is correct or there will be no

View File

@ -253,42 +253,44 @@ class Thermostat(HomeAccessory):
hvac_mode = state.state
homekit_hvac_mode = HC_HASS_TO_HOMEKIT[hvac_mode]
if CHAR_TARGET_HEATING_COOLING in char_values:
# Homekit will reset the mode when VIEWING the temp
# Ignore it if its the same mode
if char_values[CHAR_TARGET_HEATING_COOLING] != homekit_hvac_mode:
target_hc = char_values[CHAR_TARGET_HEATING_COOLING]
if target_hc not in self.hc_homekit_to_hass:
# If the target heating cooling state we want does not
# exist on the device, we have to sort it out
# based on the the current and target temperature since
# siri will always send HC_HEAT_COOL_AUTO in this case
# and hope for the best.
hc_target_temp = char_values.get(CHAR_TARGET_TEMPERATURE)
hc_current_temp = _get_current_temperature(state, self._unit)
hc_fallback_order = HC_HEAT_COOL_PREFER_HEAT
if (
hc_target_temp is not None
and hc_current_temp is not None
and hc_target_temp < hc_current_temp
):
hc_fallback_order = HC_HEAT_COOL_PREFER_COOL
for hc_fallback in hc_fallback_order:
if hc_fallback in self.hc_homekit_to_hass:
_LOGGER.debug(
"Siri requested target mode: %s and the device does not support, falling back to %s",
target_hc,
hc_fallback,
)
target_hc = hc_fallback
break
# Homekit will reset the mode when VIEWING the temp
# Ignore it if its the same mode
if (
CHAR_TARGET_HEATING_COOLING in char_values
and char_values[CHAR_TARGET_HEATING_COOLING] != homekit_hvac_mode
):
target_hc = char_values[CHAR_TARGET_HEATING_COOLING]
if target_hc not in self.hc_homekit_to_hass:
# If the target heating cooling state we want does not
# exist on the device, we have to sort it out
# based on the the current and target temperature since
# siri will always send HC_HEAT_COOL_AUTO in this case
# and hope for the best.
hc_target_temp = char_values.get(CHAR_TARGET_TEMPERATURE)
hc_current_temp = _get_current_temperature(state, self._unit)
hc_fallback_order = HC_HEAT_COOL_PREFER_HEAT
if (
hc_target_temp is not None
and hc_current_temp is not None
and hc_target_temp < hc_current_temp
):
hc_fallback_order = HC_HEAT_COOL_PREFER_COOL
for hc_fallback in hc_fallback_order:
if hc_fallback in self.hc_homekit_to_hass:
_LOGGER.debug(
"Siri requested target mode: %s and the device does not support, falling back to %s",
target_hc,
hc_fallback,
)
target_hc = hc_fallback
break
service = SERVICE_SET_HVAC_MODE_THERMOSTAT
hass_value = self.hc_homekit_to_hass[target_hc]
params = {ATTR_HVAC_MODE: hass_value}
events.append(
f"{CHAR_TARGET_HEATING_COOLING} to {char_values[CHAR_TARGET_HEATING_COOLING]}"
)
service = SERVICE_SET_HVAC_MODE_THERMOSTAT
hass_value = self.hc_homekit_to_hass[target_hc]
params = {ATTR_HVAC_MODE: hass_value}
events.append(
f"{CHAR_TARGET_HEATING_COOLING} to {char_values[CHAR_TARGET_HEATING_COOLING]}"
)
if CHAR_TARGET_TEMPERATURE in char_values:
hc_target_temp = char_values[CHAR_TARGET_TEMPERATURE]
@ -462,23 +464,26 @@ class Thermostat(HomeAccessory):
# Update current temperature
current_temp = _get_current_temperature(new_state, self._unit)
if current_temp is not None:
if self.char_current_temp.value != current_temp:
self.char_current_temp.set_value(current_temp)
if current_temp is not None and self.char_current_temp.value != current_temp:
self.char_current_temp.set_value(current_temp)
# Update current humidity
if CHAR_CURRENT_HUMIDITY in self.chars:
current_humdity = new_state.attributes.get(ATTR_CURRENT_HUMIDITY)
if isinstance(current_humdity, (int, float)):
if self.char_current_humidity.value != current_humdity:
self.char_current_humidity.set_value(current_humdity)
if (
isinstance(current_humdity, (int, float))
and self.char_current_humidity.value != current_humdity
):
self.char_current_humidity.set_value(current_humdity)
# Update target humidity
if CHAR_TARGET_HUMIDITY in self.chars:
target_humdity = new_state.attributes.get(ATTR_HUMIDITY)
if isinstance(target_humdity, (int, float)):
if self.char_target_humidity.value != target_humdity:
self.char_target_humidity.set_value(target_humdity)
if (
isinstance(target_humdity, (int, float))
and self.char_target_humidity.value != target_humdity
):
self.char_target_humidity.set_value(target_humdity)
# Update cooling threshold temperature if characteristic exists
if self.char_cooling_thresh_temp:
@ -575,9 +580,8 @@ class WaterHeater(HomeAccessory):
"""Change operation mode to value if call came from HomeKit."""
_LOGGER.debug("%s: Set heat-cool to %d", self.entity_id, value)
hass_value = HC_HOMEKIT_TO_HASS[value]
if hass_value != HVAC_MODE_HEAT:
if self.char_target_heat_cool.value != 1:
self.char_target_heat_cool.set_value(1) # Heat
if hass_value != HVAC_MODE_HEAT and self.char_target_heat_cool.value != 1:
self.char_target_heat_cool.set_value(1) # Heat
def set_target_temperature(self, value):
"""Set target temperature to value if call came from HomeKit."""
@ -596,14 +600,18 @@ class WaterHeater(HomeAccessory):
"""Update water_heater state after state change."""
# Update current and target temperature
target_temperature = _get_target_temperature(new_state, self._unit)
if target_temperature is not None:
if target_temperature != self.char_target_temp.value:
self.char_target_temp.set_value(target_temperature)
if (
target_temperature is not None
and target_temperature != self.char_target_temp.value
):
self.char_target_temp.set_value(target_temperature)
current_temperature = _get_current_temperature(new_state, self._unit)
if current_temperature is not None:
if current_temperature != self.char_current_temp.value:
self.char_current_temp.set_value(current_temperature)
if (
current_temperature is not None
and current_temperature != self.char_current_temp.value
):
self.char_current_temp.set_value(current_temperature)
# Update display units
if self._unit and self._unit in UNIT_HASS_TO_HOMEKIT:

View File

@ -444,10 +444,11 @@ def port_is_available(port: int) -> bool:
async def async_find_next_available_port(hass: HomeAssistant, start_port: int) -> int:
"""Find the next available port not assigned to a config entry."""
exclude_ports = set()
for entry in hass.config_entries.async_entries(DOMAIN):
if CONF_PORT in entry.data:
exclude_ports.add(entry.data[CONF_PORT])
exclude_ports = {
entry.data[CONF_PORT]
for entry in hass.config_entries.async_entries(DOMAIN)
if CONF_PORT in entry.data
}
return await hass.async_add_executor_job(
_find_next_available_port, start_port, exclude_ports
@ -499,10 +500,7 @@ def state_needs_accessory_mode(state):
if state.domain == CAMERA_DOMAIN:
return True
if (
return (
state.domain == MEDIA_PLAYER_DOMAIN
and state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TV
):
return True
return False
)