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
and value > 70
or not self._supports_stop
and value >= 50
):
service, position = (SERVICE_OPEN_COVER, 100) service, position = (SERVICE_OPEN_COVER, 100)
elif value < 30: elif value < 30 or not self._supports_stop:
service, position = (SERVICE_CLOSE_COVER, 0) service, position = (SERVICE_CLOSE_COVER, 0)
else: else:
service, position = (SERVICE_STOP_COVER, 50) service, position = (SERVICE_STOP_COVER, 50)
else:
if value >= 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))
and self.char_target_humidity.value != target_humidity
):
self.char_target_humidity.set_value(target_humidity) self.char_target_humidity.set_value(target_humidity)

View File

@ -78,8 +78,10 @@ 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)
and self.char_target_state.value != current_lock_state
):
self.char_target_state.set_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

View File

@ -253,10 +253,12 @@ 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 char_values[CHAR_TARGET_HEATING_COOLING] != homekit_hvac_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] target_hc = char_values[CHAR_TARGET_HEATING_COOLING]
if target_hc not in self.hc_homekit_to_hass: if target_hc not in self.hc_homekit_to_hass:
# If the target heating cooling state we want does not # If the target heating cooling state we want does not
@ -462,22 +464,25 @@ 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))
and self.char_current_humidity.value != current_humdity
):
self.char_current_humidity.set_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))
and self.char_target_humidity.value != target_humdity
):
self.char_target_humidity.set_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
@ -575,8 +580,7 @@ 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):
@ -596,13 +600,17 @@ 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
and target_temperature != self.char_target_temp.value
):
self.char_target_temp.set_value(target_temperature) 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
and current_temperature != self.char_current_temp.value
):
self.char_current_temp.set_value(current_temperature) self.char_current_temp.set_value(current_temperature)
# Update display units # Update display units

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