Skip to content

Commit 0e9e292

Browse files
Use is/is not for same-enum identity comparisons (homeassistant/)
Mechanical codemod that replaces `==` with `is` (and `!=` with `is not`) on every line where both operands statically resolve to the same enum class — as identified by `mypy_plugins.enum_identity_compare` from home-assistant#171551. This commit covers `homeassistant/` only (156 files, 289 sites). The companion tests-side change is in home-assistant#171689. Scope is intentionally narrow: - Plain `enum.Enum` subclasses: `Enum.__eq__` is identity-based, so `==` and `is` are mathematically equivalent. - `homeassistant.data_entry_flow.FlowResultType`: explicitly allowlisted StrEnum where HA's framework normalizes every consumer-facing result to the enum instance. NOT changed: other StrEnum/IntEnum/ReprEnum subclasses (their `__eq__` delegates to str/int equality and accepts raw primitives at runtime), and Flag/IntFlag (bitwise `==` is idiomatic). `homeassistant/data_entry_flow.py:497` is the one exception left as `!=` (with a noqa) because it runs before the legacy-string normalization shim a few lines below. Verified by re-running the mypy plugin against the codemodded source: zero remaining violations on this side.
1 parent 298f4f8 commit 0e9e292

156 files changed

Lines changed: 295 additions & 290 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

homeassistant/auth/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ async def async_finish_flow(
134134
"""
135135
flow = cast(LoginFlow, flow)
136136

137-
if result["type"] != FlowResultType.CREATE_ENTRY:
137+
if result["type"] is not FlowResultType.CREATE_ENTRY:
138138
return result
139139

140140
# we got final result

homeassistant/components/anthropic/config_flow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ async def async_step_init(
226226
) -> SubentryFlowResult:
227227
"""Set initial options."""
228228
# abort if entry is not loaded
229-
if self._get_entry().state != ConfigEntryState.LOADED:
229+
if self._get_entry().state is not ConfigEntryState.LOADED:
230230
return self.async_abort(reason="entry_not_loaded")
231231

232232
hass_apis: list[SelectOptionDict] = [

homeassistant/components/aosmith/water_heater.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def operation_list(self) -> list[str]:
8989
def supported_features(self) -> WaterHeaterEntityFeature:
9090
"""Return the list of supported features."""
9191
supports_vacation_mode = any(
92-
supported_mode.mode == AOSmithOperationMode.VACATION
92+
supported_mode.mode is AOSmithOperationMode.VACATION
9393
for supported_mode in self.device.supported_modes
9494
)
9595

@@ -122,7 +122,7 @@ def current_operation(self) -> str:
122122
@property
123123
def is_away_mode_on(self) -> bool:
124124
"""Return True if away mode is on."""
125-
return self.device.status.current_mode == AOSmithOperationMode.VACATION
125+
return self.device.status.current_mode is AOSmithOperationMode.VACATION
126126

127127
async def async_set_operation_mode(self, operation_mode: str) -> None:
128128
"""Set new target operation mode."""

homeassistant/components/apple_tv/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def _async_setup_device_registry(self) -> None:
369369

370370
attrs[ATTR_MODEL] = (
371371
dev_info.raw_model
372-
if dev_info.model == DeviceModel.Unknown and dev_info.raw_model
372+
if dev_info.model is DeviceModel.Unknown and dev_info.raw_model
373373
else model_str(dev_info.model)
374374
)
375375
attrs[ATTR_SW_VERSION] = dev_info.version

homeassistant/components/apple_tv/binary_sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def async_device_connected(self, atv: AppleTV) -> None:
6363
# Listen to keyboard updates
6464
atv.keyboard.listener = self
6565
# Set initial state based on current focus state
66-
self._update_state(atv.keyboard.text_focus_state == KeyboardFocusState.Focused)
66+
self._update_state(atv.keyboard.text_focus_state is KeyboardFocusState.Focused)
6767

6868
@callback
6969
def async_device_disconnected(self) -> None:
@@ -78,7 +78,7 @@ def focusstate_update(
7878
7979
This is a callback function from pyatv.interface.KeyboardListener.
8080
"""
81-
self._update_state(new_state == KeyboardFocusState.Focused)
81+
self._update_state(new_state is KeyboardFocusState.Focused)
8282

8383
def _update_state(self, new_state: bool) -> None:
8484
"""Update and report."""

homeassistant/components/apple_tv/config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ async def async_find_device(self, allow_exist: bool = False) -> None:
354354
"name": self.atv.name,
355355
"type": (
356356
dev_info.raw_model
357-
if dev_info.model == DeviceModel.Unknown and dev_info.raw_model
357+
if dev_info.model is DeviceModel.Unknown and dev_info.raw_model
358358
else model_str(dev_info.model)
359359
),
360360
}
@@ -441,7 +441,7 @@ async def async_pair_next_protocol(self) -> ConfigFlowResult:
441441
return await self.async_step_password()
442442

443443
# Figure out, depending on protocol, what kind of pairing is needed
444-
if service.pairing == PairingRequirement.Unsupported:
444+
if service.pairing is PairingRequirement.Unsupported:
445445
_LOGGER.debug("%s does not support pairing", self.protocol)
446446
return await self.async_pair_next_protocol()
447447
if service.pairing == PairingRequirement.Disabled:
@@ -457,7 +457,7 @@ async def async_pair_next_protocol(self) -> ConfigFlowResult:
457457
pair_args: dict[str, Any] = {}
458458
if self.protocol in {Protocol.AirPlay, Protocol.Companion, Protocol.DMAP}:
459459
pair_args["name"] = "Home Assistant"
460-
if self.protocol == Protocol.DMAP:
460+
if self.protocol is Protocol.DMAP:
461461
pair_args["zeroconf"] = await zeroconf.async_get_instance(self.hass)
462462

463463
# Initiate the pairing process

homeassistant/components/apple_tv/media_player.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def async_device_connected(self, atv: AppleTV) -> None:
139139
all_features = atv.features.all_features()
140140
for feature_name, support_flag in SUPPORT_FEATURE_MAPPING.items():
141141
feature_info = all_features.get(feature_name)
142-
if feature_info and feature_info.state != FeatureState.Unsupported:
142+
if feature_info and feature_info.state is not FeatureState.Unsupported:
143143
self._attr_supported_features |= support_flag
144144

145145
# No need to schedule state update here as that will happen when the first
@@ -188,14 +188,14 @@ def state(self) -> MediaPlayerState | None:
188188
return MediaPlayerState.OFF
189189
if (
190190
self._is_feature_available(FeatureName.PowerState)
191-
and self.atv.power.power_state == PowerState.Off
191+
and self.atv.power.power_state is PowerState.Off
192192
):
193193
return MediaPlayerState.OFF
194194
if self._playing:
195195
state = self._playing.device_state
196196
if state in (DeviceState.Idle, DeviceState.Loading):
197197
return MediaPlayerState.IDLE
198-
if state == DeviceState.Playing:
198+
if state is DeviceState.Playing:
199199
return MediaPlayerState.PLAYING
200200
if state in (DeviceState.Paused, DeviceState.Seeking, DeviceState.Stopped):
201201
return MediaPlayerState.PAUSED
@@ -446,7 +446,7 @@ def repeat(self) -> RepeatMode | None:
446446
def shuffle(self) -> bool | None:
447447
"""Boolean if shuffle is enabled."""
448448
if self._playing and self._is_feature_available(FeatureName.Shuffle):
449-
return self._playing.shuffle != ShuffleState.Off
449+
return self._playing.shuffle is not ShuffleState.Off
450450
return None
451451

452452
def _is_feature_available(self, feature: FeatureName) -> bool:
@@ -506,7 +506,7 @@ async def async_turn_off(self) -> None:
506506
and (self._is_feature_available(FeatureName.TurnOff))
507507
and (
508508
not self._is_feature_available(FeatureName.PowerState)
509-
or self.atv.power.power_state == PowerState.On
509+
or self.atv.power.power_state is PowerState.On
510510
)
511511
):
512512
await self.atv.power.turn_off()

homeassistant/components/apple_tv/services.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def _check_keyboard_focus(atv: AppleTVInterface) -> None:
5959
translation_domain=DOMAIN,
6060
translation_key="keyboard_not_available",
6161
) from err
62-
if focus_state != KeyboardFocusState.Focused:
62+
if focus_state is not KeyboardFocusState.Focused:
6363
raise ServiceValidationError(
6464
translation_domain=DOMAIN,
6565
translation_key="keyboard_not_focused",

homeassistant/components/arcam_fmj/media_player.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def media_content_id(self) -> str | None:
263263
def media_channel(self) -> str | None:
264264
"""Channel currently playing."""
265265
source = self._state.get_source()
266-
if source == SourceCodes.DAB:
266+
if source is SourceCodes.DAB:
267267
value = self._state.get_dab_station()
268268
elif source == SourceCodes.FM:
269269
value = self._state.get_rds_information()
@@ -274,7 +274,7 @@ def media_channel(self) -> str | None:
274274
@property
275275
def media_artist(self) -> str | None:
276276
"""Artist of current playing media, music track only."""
277-
if self._state.get_source() == SourceCodes.DAB:
277+
if self._state.get_source() is SourceCodes.DAB:
278278
value = self._state.get_dls_pdt()
279279
else:
280280
value = None

homeassistant/components/assist_pipeline/pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1355,7 +1355,7 @@ def _get_all_targets_in_satellite_area(
13551355
) -> bool:
13561356
"""Return true if all targeted entities were in the same area as the device."""
13571357
if (
1358-
intent_response.response_type != intent.IntentResponseType.ACTION_DONE
1358+
intent_response.response_type is not intent.IntentResponseType.ACTION_DONE
13591359
or not intent_response.matched_states
13601360
):
13611361
return False

0 commit comments

Comments
 (0)