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.""" """Determine is a name or port is already used."""
name = user_input[CONF_NAME] name = user_input[CONF_NAME]
port = user_input[CONF_PORT] port = user_input[CONF_PORT]
for entry in self._async_current_entries(): return not any(
if entry.data[CONF_NAME] == name or entry.data[CONF_PORT] == port: entry.data[CONF_NAME] == name or entry.data[CONF_PORT] == port
return False for entry in self._async_current_entries()
return True )
@staticmethod @staticmethod
@callback @callback

View File

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

View File

@ -240,6 +240,8 @@ class HumidifierDehumidifier(HomeAccessory):
# Update target humidity # Update target humidity
target_humidity = new_state.attributes.get(ATTR_HUMIDITY) target_humidity = new_state.attributes.get(ATTR_HUMIDITY)
if isinstance(target_humidity, (int, float)): if (
if self.char_target_humidity.value != target_humidity: isinstance(target_humidity, (int, float))
self.char_target_humidity.set_value(target_humidity) 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 # LockTargetState only supports locked and unlocked
# Must set lock target state before current state # Must set lock target state before current state
# or there will be no notification # or there will be no notification
if hass_state in (STATE_LOCKED, STATE_UNLOCKED): if (
if self.char_target_state.value != current_lock_state: hass_state in (STATE_LOCKED, STATE_UNLOCKED)
self.char_target_state.set_value(current_lock_state) 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 # Set lock current state ONLY after ensuring that
# target state is correct or there will be no # target state is correct or there will be no

View File

@ -253,42 +253,44 @@ class Thermostat(HomeAccessory):
hvac_mode = state.state hvac_mode = state.state
homekit_hvac_mode = HC_HASS_TO_HOMEKIT[hvac_mode] 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
# Homekit will reset the mode when VIEWING the temp # Ignore it if its the same mode
# Ignore it if its the same mode if (
if char_values[CHAR_TARGET_HEATING_COOLING] != homekit_hvac_mode: CHAR_TARGET_HEATING_COOLING in char_values
target_hc = char_values[CHAR_TARGET_HEATING_COOLING] and char_values[CHAR_TARGET_HEATING_COOLING] != homekit_hvac_mode
if target_hc not in self.hc_homekit_to_hass: ):
# If the target heating cooling state we want does not target_hc = char_values[CHAR_TARGET_HEATING_COOLING]
# exist on the device, we have to sort it out if target_hc not in self.hc_homekit_to_hass:
# based on the the current and target temperature since # If the target heating cooling state we want does not
# siri will always send HC_HEAT_COOL_AUTO in this case # exist on the device, we have to sort it out
# and hope for the best. # based on the the current and target temperature since
hc_target_temp = char_values.get(CHAR_TARGET_TEMPERATURE) # siri will always send HC_HEAT_COOL_AUTO in this case
hc_current_temp = _get_current_temperature(state, self._unit) # and hope for the best.
hc_fallback_order = HC_HEAT_COOL_PREFER_HEAT hc_target_temp = char_values.get(CHAR_TARGET_TEMPERATURE)
if ( hc_current_temp = _get_current_temperature(state, self._unit)
hc_target_temp is not None hc_fallback_order = HC_HEAT_COOL_PREFER_HEAT
and hc_current_temp is not None if (
and hc_target_temp < hc_current_temp hc_target_temp is not None
): and hc_current_temp is not None
hc_fallback_order = HC_HEAT_COOL_PREFER_COOL and hc_target_temp < hc_current_temp
for hc_fallback in hc_fallback_order: ):
if hc_fallback in self.hc_homekit_to_hass: hc_fallback_order = HC_HEAT_COOL_PREFER_COOL
_LOGGER.debug( for hc_fallback in hc_fallback_order:
"Siri requested target mode: %s and the device does not support, falling back to %s", if hc_fallback in self.hc_homekit_to_hass:
target_hc, _LOGGER.debug(
hc_fallback, "Siri requested target mode: %s and the device does not support, falling back to %s",
) target_hc,
target_hc = hc_fallback hc_fallback,
break )
target_hc = hc_fallback
break
service = SERVICE_SET_HVAC_MODE_THERMOSTAT service = SERVICE_SET_HVAC_MODE_THERMOSTAT
hass_value = self.hc_homekit_to_hass[target_hc] hass_value = self.hc_homekit_to_hass[target_hc]
params = {ATTR_HVAC_MODE: hass_value} params = {ATTR_HVAC_MODE: hass_value}
events.append( events.append(
f"{CHAR_TARGET_HEATING_COOLING} to {char_values[CHAR_TARGET_HEATING_COOLING]}" f"{CHAR_TARGET_HEATING_COOLING} to {char_values[CHAR_TARGET_HEATING_COOLING]}"
) )
if CHAR_TARGET_TEMPERATURE in char_values: if CHAR_TARGET_TEMPERATURE in char_values:
hc_target_temp = char_values[CHAR_TARGET_TEMPERATURE] hc_target_temp = char_values[CHAR_TARGET_TEMPERATURE]
@ -462,23 +464,26 @@ class Thermostat(HomeAccessory):
# Update current temperature # Update current temperature
current_temp = _get_current_temperature(new_state, self._unit) current_temp = _get_current_temperature(new_state, self._unit)
if current_temp is not None: if current_temp is not None and self.char_current_temp.value != current_temp:
if self.char_current_temp.value != current_temp: self.char_current_temp.set_value(current_temp)
self.char_current_temp.set_value(current_temp)
# Update current humidity # Update current humidity
if CHAR_CURRENT_HUMIDITY in self.chars: if CHAR_CURRENT_HUMIDITY in self.chars:
current_humdity = new_state.attributes.get(ATTR_CURRENT_HUMIDITY) current_humdity = new_state.attributes.get(ATTR_CURRENT_HUMIDITY)
if isinstance(current_humdity, (int, float)): if (
if self.char_current_humidity.value != current_humdity: isinstance(current_humdity, (int, float))
self.char_current_humidity.set_value(current_humdity) and self.char_current_humidity.value != current_humdity
):
self.char_current_humidity.set_value(current_humdity)
# Update target humidity # Update target humidity
if CHAR_TARGET_HUMIDITY in self.chars: if CHAR_TARGET_HUMIDITY in self.chars:
target_humdity = new_state.attributes.get(ATTR_HUMIDITY) target_humdity = new_state.attributes.get(ATTR_HUMIDITY)
if isinstance(target_humdity, (int, float)): if (
if self.char_target_humidity.value != target_humdity: isinstance(target_humdity, (int, float))
self.char_target_humidity.set_value(target_humdity) and self.char_target_humidity.value != target_humdity
):
self.char_target_humidity.set_value(target_humdity)
# Update cooling threshold temperature if characteristic exists # Update cooling threshold temperature if characteristic exists
if self.char_cooling_thresh_temp: if self.char_cooling_thresh_temp:
@ -575,9 +580,8 @@ class WaterHeater(HomeAccessory):
"""Change operation mode to value if call came from HomeKit.""" """Change operation mode to value if call came from HomeKit."""
_LOGGER.debug("%s: Set heat-cool to %d", self.entity_id, value) _LOGGER.debug("%s: Set heat-cool to %d", self.entity_id, value)
hass_value = HC_HOMEKIT_TO_HASS[value] hass_value = HC_HOMEKIT_TO_HASS[value]
if hass_value != HVAC_MODE_HEAT: if hass_value != HVAC_MODE_HEAT and self.char_target_heat_cool.value != 1:
if self.char_target_heat_cool.value != 1: self.char_target_heat_cool.set_value(1) # Heat
self.char_target_heat_cool.set_value(1) # Heat
def set_target_temperature(self, value): def set_target_temperature(self, value):
"""Set target temperature to value if call came from HomeKit.""" """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 water_heater state after state change."""
# Update current and target temperature # Update current and target temperature
target_temperature = _get_target_temperature(new_state, self._unit) target_temperature = _get_target_temperature(new_state, self._unit)
if target_temperature is not None: if (
if target_temperature != self.char_target_temp.value: target_temperature is not None
self.char_target_temp.set_value(target_temperature) 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) current_temperature = _get_current_temperature(new_state, self._unit)
if current_temperature is not None: if (
if current_temperature != self.char_current_temp.value: current_temperature is not None
self.char_current_temp.set_value(current_temperature) and current_temperature != self.char_current_temp.value
):
self.char_current_temp.set_value(current_temperature)
# Update display units # Update display units
if self._unit and self._unit in UNIT_HASS_TO_HOMEKIT: 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: async def async_find_next_available_port(hass: HomeAssistant, start_port: int) -> int:
"""Find the next available port not assigned to a config entry.""" """Find the next available port not assigned to a config entry."""
exclude_ports = set() exclude_ports = {
for entry in hass.config_entries.async_entries(DOMAIN): entry.data[CONF_PORT]
if CONF_PORT in entry.data: for entry in hass.config_entries.async_entries(DOMAIN)
exclude_ports.add(entry.data[CONF_PORT]) if CONF_PORT in entry.data
}
return await hass.async_add_executor_job( return await hass.async_add_executor_job(
_find_next_available_port, start_port, exclude_ports _find_next_available_port, start_port, exclude_ports
@ -499,10 +500,7 @@ def state_needs_accessory_mode(state):
if state.domain == CAMERA_DOMAIN: if state.domain == CAMERA_DOMAIN:
return True return True
if ( return (
state.domain == MEDIA_PLAYER_DOMAIN state.domain == MEDIA_PLAYER_DOMAIN
and state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TV and state.attributes.get(ATTR_DEVICE_CLASS) == DEVICE_CLASS_TV
): )
return True
return False