1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00

Remove HomeKit event guards (#54343)

This commit is contained in:
J. Nick Koston 2021-08-09 16:49:51 -05:00 committed by GitHub
parent c07b1423ee
commit dcf4eb5e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 84 additions and 162 deletions

View File

@ -178,18 +178,11 @@ class GarageDoorOpener(HomeAccessory):
obstruction_detected = (
new_state.attributes[ATTR_OBSTRUCTION_DETECTED] is True
)
if self.char_obstruction_detected.value != obstruction_detected:
self.char_obstruction_detected.set_value(obstruction_detected)
self.char_obstruction_detected.set_value(obstruction_detected)
if (
target_door_state is not None
and self.char_target_state.value != target_door_state
):
if target_door_state is not None:
self.char_target_state.set_value(target_door_state)
if (
current_door_state is not None
and self.char_current_state.value != current_door_state
):
if current_door_state is not None:
self.char_current_state.set_value(current_door_state)
@ -260,10 +253,8 @@ class OpeningDeviceBase(HomeAccessory):
# We'll have to normalize to [0,100]
current_tilt = (current_tilt / 100.0 * 180.0) - 90.0
current_tilt = int(current_tilt)
if self.char_current_tilt.value != current_tilt:
self.char_current_tilt.set_value(current_tilt)
if self.char_target_tilt.value != current_tilt:
self.char_target_tilt.set_value(current_tilt)
self.char_current_tilt.set_value(current_tilt)
self.char_target_tilt.set_value(current_tilt)
class OpeningDevice(OpeningDeviceBase, HomeAccessory):
@ -312,14 +303,11 @@ class OpeningDevice(OpeningDeviceBase, HomeAccessory):
current_position = new_state.attributes.get(ATTR_CURRENT_POSITION)
if isinstance(current_position, (float, int)):
current_position = int(current_position)
if self.char_current_position.value != current_position:
self.char_current_position.set_value(current_position)
if self.char_target_position.value != current_position:
self.char_target_position.set_value(current_position)
self.char_current_position.set_value(current_position)
self.char_target_position.set_value(current_position)
position_state = _hass_state_to_position_start(new_state.state)
if self.char_position_state.value != position_state:
self.char_position_state.set_value(position_state)
self.char_position_state.set_value(position_state)
super().async_update_state(new_state)

View File

@ -193,16 +193,14 @@ class Fan(HomeAccessory):
state = new_state.state
if state in (STATE_ON, STATE_OFF):
self._state = 1 if state == STATE_ON else 0
if self.char_active.value != self._state:
self.char_active.set_value(self._state)
self.char_active.set_value(self._state)
# Handle Direction
if self.char_direction is not None:
direction = new_state.attributes.get(ATTR_DIRECTION)
if direction in (DIRECTION_FORWARD, DIRECTION_REVERSE):
hk_direction = 1 if direction == DIRECTION_REVERSE else 0
if self.char_direction.value != hk_direction:
self.char_direction.set_value(hk_direction)
self.char_direction.set_value(hk_direction)
# Handle Speed
if self.char_speed is not None and state != STATE_OFF:
@ -222,7 +220,7 @@ class Fan(HomeAccessory):
# in order to avoid this incorrect behavior.
if percentage == 0 and state == STATE_ON:
percentage = 1
if percentage is not None and self.char_speed.value != percentage:
if percentage is not None:
self.char_speed.set_value(percentage)
# Handle Oscillating
@ -230,11 +228,9 @@ class Fan(HomeAccessory):
oscillating = new_state.attributes.get(ATTR_OSCILLATING)
if isinstance(oscillating, bool):
hk_oscillating = 1 if oscillating else 0
if self.char_swing.value != hk_oscillating:
self.char_swing.set_value(hk_oscillating)
self.char_swing.set_value(hk_oscillating)
current_preset_mode = new_state.attributes.get(ATTR_PRESET_MODE)
for preset_mode, char in self.preset_mode_chars.items():
hk_value = 1 if preset_mode == current_preset_mode else 0
if char.value != hk_value:
char.set_value(hk_value)
char.set_value(hk_value)

View File

@ -224,8 +224,7 @@ class HumidifierDehumidifier(HomeAccessory):
is_active = new_state.state == STATE_ON
# Update active state
if self.char_active.value != is_active:
self.char_active.set_value(is_active)
self.char_active.set_value(is_active)
# Set current state
if is_active:
@ -235,13 +234,9 @@ class HumidifierDehumidifier(HomeAccessory):
current_state = HC_STATE_DEHUMIDIFYING
else:
current_state = HC_STATE_INACTIVE
if self.char_current_humidifier_dehumidifier.value != current_state:
self.char_current_humidifier_dehumidifier.set_value(current_state)
self.char_current_humidifier_dehumidifier.set_value(current_state)
# Update target humidity
target_humidity = new_state.attributes.get(ATTR_HUMIDITY)
if (
isinstance(target_humidity, (int, float))
and self.char_target_humidity.value != target_humidity
):
if isinstance(target_humidity, (int, float)):
self.char_target_humidity.set_value(target_humidity)

View File

@ -205,13 +205,10 @@ class Light(HomeAccessory):
color_temp_mode = color_mode == COLOR_MODE_COLOR_TEMP
primary_on_value = char_on_value if not color_temp_mode else 0
secondary_on_value = char_on_value if color_temp_mode else 0
if self.char_on_primary.value != primary_on_value:
self.char_on_primary.set_value(primary_on_value)
if self.char_on_secondary.value != secondary_on_value:
self.char_on_secondary.set_value(secondary_on_value)
self.char_on_primary.set_value(primary_on_value)
self.char_on_secondary.set_value(secondary_on_value)
else:
if self.char_on_primary.value != char_on_value:
self.char_on_primary.set_value(char_on_value)
self.char_on_primary.set_value(char_on_value)
# Handle Brightness
if self.is_brightness_supported:
@ -230,12 +227,8 @@ class Light(HomeAccessory):
# order to avoid this incorrect behavior.
if brightness == 0 and state == STATE_ON:
brightness = 1
if self.char_brightness_primary.value != brightness:
self.char_brightness_primary.set_value(brightness)
if (
self.color_and_temp_supported
and self.char_brightness_secondary.value != brightness
):
self.char_brightness_primary.set_value(brightness)
if self.color_and_temp_supported:
self.char_brightness_secondary.set_value(brightness)
# Handle color temperature
@ -243,8 +236,7 @@ class Light(HomeAccessory):
color_temperature = attributes.get(ATTR_COLOR_TEMP)
if isinstance(color_temperature, (int, float)):
color_temperature = round(color_temperature, 0)
if self.char_color_temperature.value != color_temperature:
self.char_color_temperature.set_value(color_temperature)
self.char_color_temperature.set_value(color_temperature)
# Handle Color
if self.is_color_supported:
@ -252,7 +244,5 @@ class Light(HomeAccessory):
if isinstance(hue, (int, float)) and isinstance(saturation, (int, float)):
hue = round(hue, 0)
saturation = round(saturation, 0)
if hue != self.char_hue.value:
self.char_hue.set_value(hue)
if saturation != self.char_saturation.value:
self.char_saturation.set_value(saturation)
self.char_hue.set_value(hue)
self.char_saturation.set_value(saturation)

View File

@ -106,14 +106,10 @@ class Lock(HomeAccessory):
# LockTargetState only supports locked and unlocked
# Must set lock target state before current state
# or there will be no notification
if (
target_lock_state is not None
and self.char_target_state.value != target_lock_state
):
if target_lock_state is not None:
self.char_target_state.set_value(target_lock_state)
# Set lock current state ONLY after ensuring that
# target state is correct or there will be no
# notification
if self.char_current_state.value != current_lock_state:
self.char_current_state.set_value(current_lock_state)
self.char_current_state.set_value(current_lock_state)

View File

@ -180,8 +180,7 @@ class MediaPlayer(HomeAccessory):
_LOGGER.debug(
'%s: Set current state for "on_off" to %s', self.entity_id, hk_state
)
if self.chars[FEATURE_ON_OFF].value != hk_state:
self.chars[FEATURE_ON_OFF].set_value(hk_state)
self.chars[FEATURE_ON_OFF].set_value(hk_state)
if self.chars[FEATURE_PLAY_PAUSE]:
hk_state = current_state == STATE_PLAYING
@ -190,8 +189,7 @@ class MediaPlayer(HomeAccessory):
self.entity_id,
hk_state,
)
if self.chars[FEATURE_PLAY_PAUSE].value != hk_state:
self.chars[FEATURE_PLAY_PAUSE].set_value(hk_state)
self.chars[FEATURE_PLAY_PAUSE].set_value(hk_state)
if self.chars[FEATURE_PLAY_STOP]:
hk_state = current_state == STATE_PLAYING
@ -200,8 +198,7 @@ class MediaPlayer(HomeAccessory):
self.entity_id,
hk_state,
)
if self.chars[FEATURE_PLAY_STOP].value != hk_state:
self.chars[FEATURE_PLAY_STOP].set_value(hk_state)
self.chars[FEATURE_PLAY_STOP].set_value(hk_state)
if self.chars[FEATURE_TOGGLE_MUTE]:
current_state = bool(new_state.attributes.get(ATTR_MEDIA_VOLUME_MUTED))
@ -210,8 +207,7 @@ class MediaPlayer(HomeAccessory):
self.entity_id,
current_state,
)
if self.chars[FEATURE_TOGGLE_MUTE].value != current_state:
self.chars[FEATURE_TOGGLE_MUTE].set_value(current_state)
self.chars[FEATURE_TOGGLE_MUTE].set_value(current_state)
@TYPES.register("TelevisionMediaPlayer")
@ -341,8 +337,7 @@ class TelevisionMediaPlayer(RemoteInputSelectAccessory):
if current_state not in MEDIA_PLAYER_OFF_STATES:
hk_state = 1
_LOGGER.debug("%s: Set current active state to %s", self.entity_id, hk_state)
if self.char_active.value != hk_state:
self.char_active.set_value(hk_state)
self.char_active.set_value(hk_state)
# Set mute state
if CHAR_VOLUME_SELECTOR in self.chars_speaker:
@ -352,7 +347,6 @@ class TelevisionMediaPlayer(RemoteInputSelectAccessory):
self.entity_id,
current_mute_state,
)
if self.char_mute.value != current_mute_state:
self.char_mute.set_value(current_mute_state)
self.char_mute.set_value(current_mute_state)
self._async_update_input_state(hk_state, new_state)

View File

@ -154,8 +154,7 @@ class RemoteInputSelectAccessory(HomeAccessory):
_LOGGER.debug("%s: Set current input to %s", self.entity_id, source_name)
if source_name in self.sources:
index = self.sources.index(source_name)
if self.char_input_source.value != index:
self.char_input_source.set_value(index)
self.char_input_source.set_value(index)
return
possible_sources = new_state.attributes.get(self.source_list_key, [])
@ -174,8 +173,7 @@ class RemoteInputSelectAccessory(HomeAccessory):
source_name,
possible_sources,
)
if self.char_input_source.value != 0:
self.char_input_source.set_value(0)
self.char_input_source.set_value(0)
@TYPES.register("ActivityRemote")
@ -225,7 +223,6 @@ class ActivityRemote(RemoteInputSelectAccessory):
# Power state remote
hk_state = 1 if current_state == STATE_ON else 0
_LOGGER.debug("%s: Set current active state to %s", self.entity_id, hk_state)
if self.char_active.value != hk_state:
self.char_active.set_value(hk_state)
self.char_active.set_value(hk_state)
self._async_update_input_state(hk_state, new_state)

View File

@ -158,15 +158,12 @@ class SecuritySystem(HomeAccessory):
"""Update security state after state changed."""
hass_state = new_state.state
if (current_state := HASS_TO_HOMEKIT_CURRENT.get(hass_state)) is not None:
if self.char_current_state.value != current_state:
self.char_current_state.set_value(current_state)
_LOGGER.debug(
"%s: Updated current state to %s (%d)",
self.entity_id,
hass_state,
current_state,
)
self.char_current_state.set_value(current_state)
_LOGGER.debug(
"%s: Updated current state to %s (%d)",
self.entity_id,
hass_state,
current_state,
)
if (target_state := HASS_TO_HOMEKIT_TARGET.get(hass_state)) is not None:
if self.char_target_state.value != target_state:
self.char_target_state.set_value(target_state)
self.char_target_state.set_value(target_state)

View File

@ -101,11 +101,10 @@ class TemperatureSensor(HomeAccessory):
temperature = convert_to_float(new_state.state)
if temperature:
temperature = temperature_to_homekit(temperature, unit)
if self.char_temp.value != temperature:
self.char_temp.set_value(temperature)
_LOGGER.debug(
"%s: Current temperature set to %.1f°C", self.entity_id, temperature
)
self.char_temp.set_value(temperature)
_LOGGER.debug(
"%s: Current temperature set to %.1f°C", self.entity_id, temperature
)
@TYPES.register("HumiditySensor")
@ -128,7 +127,7 @@ class HumiditySensor(HomeAccessory):
def async_update_state(self, new_state):
"""Update accessory after state change."""
humidity = convert_to_float(new_state.state)
if humidity and self.char_humidity.value != humidity:
if humidity:
self.char_humidity.set_value(humidity)
_LOGGER.debug("%s: Percent set to %d%%", self.entity_id, humidity)
@ -161,9 +160,8 @@ class AirQualitySensor(HomeAccessory):
self.char_density.set_value(density)
_LOGGER.debug("%s: Set density to %d", self.entity_id, density)
air_quality = density_to_air_quality(density)
if self.char_quality.value != air_quality:
self.char_quality.set_value(air_quality)
_LOGGER.debug("%s: Set air_quality to %d", self.entity_id, air_quality)
self.char_quality.set_value(air_quality)
_LOGGER.debug("%s: Set air_quality to %d", self.entity_id, air_quality)
@TYPES.register("CarbonMonoxideSensor")
@ -194,14 +192,12 @@ class CarbonMonoxideSensor(HomeAccessory):
"""Update accessory after state change."""
value = convert_to_float(new_state.state)
if value:
if self.char_level.value != value:
self.char_level.set_value(value)
self.char_level.set_value(value)
if value > self.char_peak.value:
self.char_peak.set_value(value)
co_detected = value > THRESHOLD_CO
if self.char_detected.value is not co_detected:
self.char_detected.set_value(co_detected)
_LOGGER.debug("%s: Set to %d", self.entity_id, value)
self.char_detected.set_value(co_detected)
_LOGGER.debug("%s: Set to %d", self.entity_id, value)
@TYPES.register("CarbonDioxideSensor")
@ -232,14 +228,12 @@ class CarbonDioxideSensor(HomeAccessory):
"""Update accessory after state change."""
value = convert_to_float(new_state.state)
if value:
if self.char_level.value != value:
self.char_level.set_value(value)
self.char_level.set_value(value)
if value > self.char_peak.value:
self.char_peak.set_value(value)
co2_detected = value > THRESHOLD_CO2
if self.char_detected.value is not co2_detected:
self.char_detected.set_value(co2_detected)
_LOGGER.debug("%s: Set to %d", self.entity_id, value)
self.char_detected.set_value(co2_detected)
_LOGGER.debug("%s: Set to %d", self.entity_id, value)
@TYPES.register("LightSensor")
@ -262,7 +256,7 @@ class LightSensor(HomeAccessory):
def async_update_state(self, new_state):
"""Update accessory after state change."""
luminance = convert_to_float(new_state.state)
if luminance and self.char_light.value != luminance:
if luminance:
self.char_light.set_value(luminance)
_LOGGER.debug("%s: Set to %d", self.entity_id, luminance)
@ -297,6 +291,5 @@ class BinarySensor(HomeAccessory):
"""Update accessory after state change."""
state = new_state.state
detected = self.format(state in (STATE_ON, STATE_HOME))
if self.char_detected.value != detected:
self.char_detected.set_value(detected)
_LOGGER.debug("%s: Set to %d", self.entity_id, detected)
self.char_detected.set_value(detected)
_LOGGER.debug("%s: Set to %d", self.entity_id, detected)

View File

@ -91,9 +91,8 @@ class Outlet(HomeAccessory):
def async_update_state(self, new_state):
"""Update switch state after state changed."""
current_state = new_state.state == STATE_ON
if self.char_on.value is not current_state:
_LOGGER.debug("%s: Set current state to %s", self.entity_id, current_state)
self.char_on.set_value(current_state)
_LOGGER.debug("%s: Set current state to %s", self.entity_id, current_state)
self.char_on.set_value(current_state)
@TYPES.register("Switch")
@ -123,8 +122,7 @@ class Switch(HomeAccessory):
def reset_switch(self, *args):
"""Reset switch to emulate activate click."""
_LOGGER.debug("%s: Reset switch to off", self.entity_id)
if self.char_on.value is not False:
self.char_on.set_value(False)
self.char_on.set_value(False)
def set_state(self, value):
"""Move switch state to value if call came from HomeKit."""
@ -156,9 +154,8 @@ class Switch(HomeAccessory):
return
current_state = new_state.state == STATE_ON
if self.char_on.value is not current_state:
_LOGGER.debug("%s: Set current state to %s", self.entity_id, current_state)
self.char_on.set_value(current_state)
_LOGGER.debug("%s: Set current state to %s", self.entity_id, current_state)
self.char_on.set_value(current_state)
@TYPES.register("Vacuum")
@ -186,9 +183,8 @@ class Vacuum(Switch):
def async_update_state(self, new_state):
"""Update switch state after state changed."""
current_state = new_state.state in (STATE_CLEANING, STATE_ON)
if self.char_on.value is not current_state:
_LOGGER.debug("%s: Set current state to %s", self.entity_id, current_state)
self.char_on.set_value(current_state)
_LOGGER.debug("%s: Set current state to %s", self.entity_id, current_state)
self.char_on.set_value(current_state)
@TYPES.register("Valve")
@ -226,9 +222,7 @@ class Valve(HomeAccessory):
def async_update_state(self, new_state):
"""Update switch state after state changed."""
current_state = 1 if new_state.state == STATE_ON else 0
if self.char_active.value != current_state:
_LOGGER.debug("%s: Set active state to %s", self.entity_id, current_state)
self.char_active.set_value(current_state)
if self.char_in_use.value != current_state:
_LOGGER.debug("%s: Set in_use state to %s", self.entity_id, current_state)
self.char_in_use.set_value(current_state)
_LOGGER.debug("%s: Set active state to %s", self.entity_id, current_state)
self.char_active.set_value(current_state)
_LOGGER.debug("%s: Set in_use state to %s", self.entity_id, current_state)
self.char_in_use.set_value(current_state)

View File

@ -446,8 +446,7 @@ class Thermostat(HomeAccessory):
if hvac_mode and hvac_mode in HC_HASS_TO_HOMEKIT:
homekit_hvac_mode = HC_HASS_TO_HOMEKIT[hvac_mode]
if homekit_hvac_mode in self.hc_homekit_to_hass:
if self.char_target_heat_cool.value != homekit_hvac_mode:
self.char_target_heat_cool.set_value(homekit_hvac_mode)
self.char_target_heat_cool.set_value(homekit_hvac_mode)
else:
_LOGGER.error(
"Cannot map hvac target mode: %s to homekit as only %s modes are supported",
@ -459,30 +458,23 @@ class Thermostat(HomeAccessory):
hvac_action = new_state.attributes.get(ATTR_HVAC_ACTION)
if hvac_action:
homekit_hvac_action = HC_HASS_TO_HOMEKIT_ACTION[hvac_action]
if self.char_current_heat_cool.value != homekit_hvac_action:
self.char_current_heat_cool.set_value(homekit_hvac_action)
self.char_current_heat_cool.set_value(homekit_hvac_action)
# Update current temperature
current_temp = _get_current_temperature(new_state, self._unit)
if current_temp is not None and self.char_current_temp.value != current_temp:
if current_temp is not None:
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))
and self.char_current_humidity.value != current_humdity
):
if isinstance(current_humdity, (int, float)):
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))
and self.char_target_humidity.value != target_humdity
):
if isinstance(target_humdity, (int, float)):
self.char_target_humidity.set_value(target_humdity)
# Update cooling threshold temperature if characteristic exists
@ -490,16 +482,14 @@ class Thermostat(HomeAccessory):
cooling_thresh = new_state.attributes.get(ATTR_TARGET_TEMP_HIGH)
if isinstance(cooling_thresh, (int, float)):
cooling_thresh = self._temperature_to_homekit(cooling_thresh)
if self.char_heating_thresh_temp.value != cooling_thresh:
self.char_cooling_thresh_temp.set_value(cooling_thresh)
self.char_cooling_thresh_temp.set_value(cooling_thresh)
# Update heating threshold temperature if characteristic exists
if self.char_heating_thresh_temp:
heating_thresh = new_state.attributes.get(ATTR_TARGET_TEMP_LOW)
if isinstance(heating_thresh, (int, float)):
heating_thresh = self._temperature_to_homekit(heating_thresh)
if self.char_heating_thresh_temp.value != heating_thresh:
self.char_heating_thresh_temp.set_value(heating_thresh)
self.char_heating_thresh_temp.set_value(heating_thresh)
# Update target temperature
target_temp = _get_target_temperature(new_state, self._unit)
@ -515,14 +505,13 @@ class Thermostat(HomeAccessory):
temp_high = new_state.attributes.get(ATTR_TARGET_TEMP_HIGH)
if isinstance(temp_high, (int, float)):
target_temp = self._temperature_to_homekit(temp_high)
if target_temp and self.char_target_temp.value != target_temp:
if target_temp:
self.char_target_temp.set_value(target_temp)
# Update display units
if self._unit and self._unit in UNIT_HASS_TO_HOMEKIT:
unit = UNIT_HASS_TO_HOMEKIT[self._unit]
if self.char_display_units.value != unit:
self.char_display_units.set_value(unit)
self.char_display_units.set_value(unit)
@TYPES.register("WaterHeater")
@ -580,7 +569,7 @@ 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 and self.char_target_heat_cool.value != 1:
if hass_value != HVAC_MODE_HEAT:
self.char_target_heat_cool.set_value(1) # Heat
def set_target_temperature(self, value):
@ -600,28 +589,21 @@ 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
and target_temperature != self.char_target_temp.value
):
if target_temperature is not None:
self.char_target_temp.set_value(target_temperature)
current_temperature = _get_current_temperature(new_state, self._unit)
if (
current_temperature is not None
and current_temperature != self.char_current_temp.value
):
if current_temperature is not None:
self.char_current_temp.set_value(current_temperature)
# Update display units
if self._unit and self._unit in UNIT_HASS_TO_HOMEKIT:
unit = UNIT_HASS_TO_HOMEKIT[self._unit]
if self.char_display_units.value != unit:
self.char_display_units.set_value(unit)
self.char_display_units.set_value(unit)
# Update target operation mode
operation_mode = new_state.state
if operation_mode and self.char_target_heat_cool.value != 1:
if operation_mode:
self.char_target_heat_cool.set_value(1) # Heat