diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index efad44b15ef..05f732565e8 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -721,7 +721,7 @@ class AutomationEntity(BaseAutomationEntity, RestoreEntity): self._is_enabled = True # HomeAssistant is starting up - if self.hass.state != CoreState.not_running: + if self.hass.state is not CoreState.not_running: self._async_detach_triggers = await self._async_attach_triggers(False) self.async_write_ha_state() return diff --git a/homeassistant/components/cloud/google_config.py b/homeassistant/components/cloud/google_config.py index c11ec47b2e5..b64ec558389 100644 --- a/homeassistant/components/cloud/google_config.py +++ b/homeassistant/components/cloud/google_config.py @@ -412,7 +412,7 @@ class CloudGoogleConfig(AbstractConfig): if ( not self.enabled or not self._cloud.is_logged_in - or self.hass.state != CoreState.running + or self.hass.state is not CoreState.running ): return @@ -435,7 +435,7 @@ class CloudGoogleConfig(AbstractConfig): if ( not self.enabled or not self._cloud.is_logged_in - or self.hass.state != CoreState.running + or self.hass.state is not CoreState.running ): return diff --git a/homeassistant/components/dsmr/sensor.py b/homeassistant/components/dsmr/sensor.py index 3e26ee1ea62..79136a27f16 100644 --- a/homeassistant/components/dsmr/sensor.py +++ b/homeassistant/components/dsmr/sensor.py @@ -614,7 +614,7 @@ async def async_setup_entry( transport = None protocol = None - while hass.state == CoreState.not_running or hass.is_running: + while hass.state is CoreState.not_running or hass.is_running: # Start DSMR asyncio.Protocol reader # Reflect connected state in devices state by setting an @@ -641,7 +641,7 @@ async def async_setup_entry( await protocol.wait_closed() # Unexpected disconnect - if hass.state == CoreState.not_running or hass.is_running: + if hass.state is CoreState.not_running or hass.is_running: stop_listener() transport = None @@ -673,7 +673,7 @@ async def async_setup_entry( update_entities_telegram(None) if stop_listener and ( - hass.state == CoreState.not_running or hass.is_running + hass.state is CoreState.not_running or hass.is_running ): stop_listener() diff --git a/homeassistant/components/emulated_roku/binding.py b/homeassistant/components/emulated_roku/binding.py index 1d233c9ed81..3559c0da99b 100644 --- a/homeassistant/components/emulated_roku/binding.py +++ b/homeassistant/components/emulated_roku/binding.py @@ -155,7 +155,7 @@ class EmulatedRoku: ) # start immediately if already running - if self.hass.state == CoreState.running: + if self.hass.state is CoreState.running: await emulated_roku_start(None) else: self._unsub_start_listener = self.hass.bus.async_listen_once( diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py index c9fcde87162..03a98401668 100644 --- a/homeassistant/components/generic_thermostat/climate.py +++ b/homeassistant/components/generic_thermostat/climate.py @@ -270,7 +270,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity): ): self.hass.create_task(self._check_switch_initial_state()) - if self.hass.state == CoreState.running: + if self.hass.state is CoreState.running: _async_startup() else: self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_startup) diff --git a/homeassistant/components/google_travel_time/sensor.py b/homeassistant/components/google_travel_time/sensor.py index 06a50dab854..95eb965a4ff 100644 --- a/homeassistant/components/google_travel_time/sensor.py +++ b/homeassistant/components/google_travel_time/sensor.py @@ -93,7 +93,7 @@ class GoogleTravelTimeSensor(SensorEntity): async def async_added_to_hass(self) -> None: """Handle when entity is added.""" - if self.hass.state != CoreState.running: + if self.hass.state is not CoreState.running: self.hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STARTED, self.first_update ) diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index cd90c4acf60..5812bc122c7 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -353,7 +353,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ) hass.data[DOMAIN][entry.entry_id] = entry_data - if hass.state == CoreState.running: + if hass.state is CoreState.running: await homekit.async_start() else: hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, homekit.async_start) diff --git a/homeassistant/components/homekit_controller/connection.py b/homeassistant/components/homekit_controller/connection.py index 4a5a4953c4b..c127c6dd95e 100644 --- a/homeassistant/components/homekit_controller/connection.py +++ b/homeassistant/components/homekit_controller/connection.py @@ -261,7 +261,7 @@ class HKDevice: # Ideally we would know which entities we are about to add # so we only poll those chars but that is not possible # yet. - attempts = None if self.hass.state == CoreState.running else 1 + attempts = None if self.hass.state is CoreState.running else 1 if ( transport == Transport.BLE and pairing.accessories diff --git a/homeassistant/components/kodi/media_player.py b/homeassistant/components/kodi/media_player.py index 89f0a992ff1..bca1c7f6f0e 100644 --- a/homeassistant/components/kodi/media_player.py +++ b/homeassistant/components/kodi/media_player.py @@ -404,7 +404,7 @@ class KodiEntity(MediaPlayerEntity): # If Home Assistant is already in a running state, start the watchdog # immediately, else trigger it after Home Assistant has finished starting. - if self.hass.state == CoreState.running: + if self.hass.state is CoreState.running: await start_watchdog() else: self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, start_watchdog) diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index 7d102e3a32f..14a18354b01 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -413,7 +413,7 @@ class MQTT: ) self._pending_unsubscribes: set[str] = set() # topic - if self.hass.state == CoreState.running: + if self.hass.state is CoreState.running: self._ha_started.set() else: diff --git a/homeassistant/components/nmap_tracker/__init__.py b/homeassistant/components/nmap_tracker/__init__.py index 0dafff996d0..726b3fa3db8 100644 --- a/homeassistant/components/nmap_tracker/__init__.py +++ b/homeassistant/components/nmap_tracker/__init__.py @@ -179,7 +179,7 @@ class NmapDeviceScanner: seconds=cv.positive_float(config[CONF_CONSIDER_HOME]) ) self._scan_lock = asyncio.Lock() - if self._hass.state == CoreState.running: + if self._hass.state is CoreState.running: await self._async_start_scanner() return diff --git a/homeassistant/components/oralb/__init__.py b/homeassistant/components/oralb/__init__.py index 4a4d06cabbb..23a022effef 100644 --- a/homeassistant/components/oralb/__init__.py +++ b/homeassistant/components/oralb/__init__.py @@ -36,7 +36,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Only poll if hass is running, we need to poll, # and we actually have a way to connect to the device return ( - hass.state == CoreState.running + hass.state is CoreState.running and data.poll_needed(service_info, last_poll) and bool( async_ble_device_from_address( diff --git a/homeassistant/components/rflink/__init__.py b/homeassistant/components/rflink/__init__.py index 60e2b0fef58..42b6d9a3ecf 100644 --- a/homeassistant/components/rflink/__init__.py +++ b/homeassistant/components/rflink/__init__.py @@ -254,7 +254,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async_dispatcher_send(hass, SIGNAL_AVAILABILITY, False) # If HA is not stopping, initiate new connection - if hass.state != CoreState.stopping: + if hass.state is not CoreState.stopping: _LOGGER.warning("Disconnected from Rflink, reconnecting") hass.async_create_task(connect()) diff --git a/homeassistant/components/simplisafe/__init__.py b/homeassistant/components/simplisafe/__init__.py index 772b6f9cbf6..1e558356ea3 100644 --- a/homeassistant/components/simplisafe/__init__.py +++ b/homeassistant/components/simplisafe/__init__.py @@ -456,7 +456,7 @@ class SimpliSafe: @callback def _async_process_new_notifications(self, system: SystemType) -> None: """Act on any new system notifications.""" - if self._hass.state != CoreState.running: + if self._hass.state is not CoreState.running: # If HASS isn't fully running yet, it may cause the SIMPLISAFE_NOTIFICATION # event to fire before dependent components (like automation) are fully # ready. If that's the case, skip: diff --git a/homeassistant/components/speedtestdotnet/__init__.py b/homeassistant/components/speedtestdotnet/__init__.py index 67abecdf5d0..1fb368b13c7 100644 --- a/homeassistant/components/speedtestdotnet/__init__.py +++ b/homeassistant/components/speedtestdotnet/__init__.py @@ -31,7 +31,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b """Request a refresh.""" await coordinator.async_request_refresh() - if hass.state == CoreState.running: + if hass.state is CoreState.running: await coordinator.async_config_entry_first_refresh() else: # Running a speed test during startup can prevent diff --git a/homeassistant/components/switchbot/coordinator.py b/homeassistant/components/switchbot/coordinator.py index 39f2a4aa6da..1965867887c 100644 --- a/homeassistant/components/switchbot/coordinator.py +++ b/homeassistant/components/switchbot/coordinator.py @@ -65,7 +65,7 @@ class SwitchbotDataUpdateCoordinator(ActiveBluetoothDataUpdateCoordinator[None]) # Only poll if hass is running, we need to poll, # and we actually have a way to connect to the device return ( - self.hass.state == CoreState.running + self.hass.state is CoreState.running and self.device.poll_needed(seconds_since_last_poll) and bool( bluetooth.async_ble_device_from_address( diff --git a/homeassistant/components/template/coordinator.py b/homeassistant/components/template/coordinator.py index 7f24fe731cc..5ac2b7efa67 100644 --- a/homeassistant/components/template/coordinator.py +++ b/homeassistant/components/template/coordinator.py @@ -42,7 +42,7 @@ class TriggerUpdateCoordinator(DataUpdateCoordinator): async def async_setup(self, hass_config: ConfigType) -> None: """Set up the trigger and create entities.""" - if self.hass.state == CoreState.running: + if self.hass.state is CoreState.running: await self._attach_triggers() else: self._unsub_start = self.hass.bus.async_listen_once( diff --git a/homeassistant/components/toon/__init__.py b/homeassistant/components/toon/__init__.py index 59174cff260..36f7ca12b84 100644 --- a/homeassistant/components/toon/__init__.py +++ b/homeassistant/components/toon/__init__.py @@ -118,7 +118,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # If Home Assistant is already in a running state, register the webhook # immediately, else trigger it after Home Assistant has finished starting. - if hass.state == CoreState.running: + if hass.state is CoreState.running: await coordinator.register_webhook() else: hass.bus.async_listen_once( diff --git a/homeassistant/components/waze_travel_time/sensor.py b/homeassistant/components/waze_travel_time/sensor.py index b54d723f95d..ef372e5fd33 100644 --- a/homeassistant/components/waze_travel_time/sensor.py +++ b/homeassistant/components/waze_travel_time/sensor.py @@ -110,7 +110,7 @@ class WazeTravelTime(SensorEntity): async def async_added_to_hass(self) -> None: """Handle when entity is added.""" - if self.hass.state != CoreState.running: + if self.hass.state is not CoreState.running: self.hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STARTED, self.first_update ) diff --git a/homeassistant/components/xiaomi_ble/__init__.py b/homeassistant/components/xiaomi_ble/__init__.py index ced8c3cc471..228a72cb8a5 100644 --- a/homeassistant/components/xiaomi_ble/__init__.py +++ b/homeassistant/components/xiaomi_ble/__init__.py @@ -119,7 +119,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: # Only poll if hass is running, we need to poll, # and we actually have a way to connect to the device return ( - hass.state == CoreState.running + hass.state is CoreState.running and data.poll_needed(service_info, last_poll) and bool( async_ble_device_from_address( diff --git a/homeassistant/components/zwave_js/update.py b/homeassistant/components/zwave_js/update.py index cf743a3e85a..f3e60f925e6 100644 --- a/homeassistant/components/zwave_js/update.py +++ b/homeassistant/components/zwave_js/update.py @@ -191,7 +191,7 @@ class ZWaveNodeFirmwareUpdate(UpdateEntity): # If hass hasn't started yet, push the next update to the next day so that we # can preserve the offsets we've created between each node - if self.hass.state != CoreState.running: + if self.hass.state is not CoreState.running: self._poll_unsub = async_call_later( self.hass, timedelta(days=1), self._async_update ) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index d8738e67a04..9e4791fdef6 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -455,7 +455,7 @@ class ConfigEntry: wait_time, ) - if hass.state == CoreState.running: + if hass.state is CoreState.running: self._async_cancel_retry_setup = async_call_later( hass, wait_time, self._async_get_setup_again_job(hass) ) diff --git a/homeassistant/core.py b/homeassistant/core.py index 3ad358b0b4a..bb84e7597b6 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -425,7 +425,7 @@ class HomeAssistant: This method is a coroutine. """ - if self.state != CoreState.not_running: + if self.state is not CoreState.not_running: raise RuntimeError("Home Assistant is already running") # _async_stop will set this instead of stopping the loop @@ -474,7 +474,7 @@ class HomeAssistant: # Allow automations to set up the start triggers before changing state await asyncio.sleep(0) - if self.state != CoreState.starting: + if self.state is not CoreState.starting: _LOGGER.warning( "Home Assistant startup has been interrupted. " "Its state may be inconsistent" @@ -824,7 +824,7 @@ class HomeAssistant: def stop(self) -> None: """Stop Home Assistant and shuts down all threads.""" - if self.state == CoreState.not_running: # just ignore + if self.state is CoreState.not_running: # just ignore return # The future is never retrieved, and we only hold a reference # to it to prevent it from being garbage collected. @@ -844,12 +844,12 @@ class HomeAssistant: if not force: # Some tests require async_stop to run, # regardless of the state of the loop. - if self.state == CoreState.not_running: # just ignore + if self.state is CoreState.not_running: # just ignore return if self.state in [CoreState.stopping, CoreState.final_write]: _LOGGER.info("Additional call to async_stop was ignored") return - if self.state == CoreState.starting: + if self.state is CoreState.starting: # This may not work _LOGGER.warning( "Stopping Home Assistant before startup has completed may fail" diff --git a/homeassistant/helpers/discovery_flow.py b/homeassistant/helpers/discovery_flow.py index c2c9a04b7c3..7ad9caa5a93 100644 --- a/homeassistant/helpers/discovery_flow.py +++ b/homeassistant/helpers/discovery_flow.py @@ -23,7 +23,7 @@ def async_create_flow( dispatcher: FlowDispatcher | None = None if DISCOVERY_FLOW_DISPATCHER in hass.data: dispatcher = hass.data[DISCOVERY_FLOW_DISPATCHER] - elif hass.state != CoreState.running: + elif hass.state is not CoreState.running: dispatcher = hass.data[DISCOVERY_FLOW_DISPATCHER] = FlowDispatcher(hass) dispatcher.async_setup() diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index 89eb44a0459..e1c05c21828 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -401,7 +401,7 @@ class EntityPlatform: self._async_cancel_retry_setup = None await self._async_setup_platform(async_create_setup_task, tries) - if hass.state == CoreState.running: + if hass.state is CoreState.running: self._async_cancel_retry_setup = async_call_later( hass, wait_time, setup_again ) diff --git a/homeassistant/helpers/start.py b/homeassistant/helpers/start.py index fe3bd2b0987..30e8466070e 100644 --- a/homeassistant/helpers/start.py +++ b/homeassistant/helpers/start.py @@ -81,7 +81,7 @@ def async_at_started( """ def _is_started(hass: HomeAssistant) -> bool: - return hass.state == CoreState.running + return hass.state is CoreState.running return _async_at_core_state( hass, at_start_cb, EVENT_HOMEASSISTANT_STARTED, _is_started diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 0e92cc6ff01..f789aeb37e4 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -260,7 +260,7 @@ class Store(Generic[_T]): "data": data, } - if self.hass.state == CoreState.stopping: + if self.hass.state is CoreState.stopping: self._async_ensure_final_write_listener() return @@ -286,7 +286,7 @@ class Store(Generic[_T]): self._async_cleanup_delay_listener() self._async_ensure_final_write_listener() - if self.hass.state == CoreState.stopping: + if self.hass.state is CoreState.stopping: return self._unsub_delay_listener = async_call_later( @@ -318,7 +318,7 @@ class Store(Generic[_T]): async def _async_callback_delayed_write(self, _now): """Handle a delayed write callback.""" # catch the case where a call is scheduled and then we stop Home Assistant - if self.hass.state == CoreState.stopping: + if self.hass.state is CoreState.stopping: self._async_ensure_final_write_listener() return await self._async_handle_write_data() diff --git a/tests/components/template/test_sensor.py b/tests/components/template/test_sensor.py index 0ca666d22f1..8aef5947d56 100644 --- a/tests/components/template/test_sensor.py +++ b/tests/components/template/test_sensor.py @@ -818,7 +818,7 @@ async def test_this_variable_early_hass_running( """ # Start hass - assert hass.state == CoreState.running + assert hass.state is CoreState.running await hass.async_start() await hass.async_block_till_done() diff --git a/tests/conftest.py b/tests/conftest.py index ea4ddd23d28..856213fa60a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1509,7 +1509,7 @@ async def async_setup_recorder_instance( await hass.async_block_till_done() instance = hass.data[recorder.DATA_INSTANCE] # The recorder's worker is not started until Home Assistant is running - if hass.state == CoreState.running: + if hass.state is CoreState.running: await async_recorder_block_till_done(hass) return instance diff --git a/tests/helpers/test_start.py b/tests/helpers/test_start.py index ec7ffbc9afc..f5204a2ec64 100644 --- a/tests/helpers/test_start.py +++ b/tests/helpers/test_start.py @@ -8,7 +8,7 @@ from homeassistant.helpers import start async def test_at_start_when_running_awaitable(hass: HomeAssistant) -> None: """Test at start when already running.""" - assert hass.state == CoreState.running + assert hass.state is CoreState.running assert hass.is_running calls = [] @@ -33,7 +33,7 @@ async def test_at_start_when_running_callback( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test at start when already running.""" - assert hass.state == CoreState.running + assert hass.state is CoreState.running assert hass.is_running calls = [] @@ -110,7 +110,7 @@ async def test_cancelling_at_start_when_running( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test cancelling at start when already running.""" - assert hass.state == CoreState.running + assert hass.state is CoreState.running assert hass.is_running calls = [] @@ -151,7 +151,7 @@ async def test_cancelling_at_start_when_starting(hass: HomeAssistant) -> None: async def test_at_started_when_running_awaitable(hass: HomeAssistant) -> None: """Test at started when already started.""" - assert hass.state == CoreState.running + assert hass.state is CoreState.running calls = [] @@ -175,7 +175,7 @@ async def test_at_started_when_running_callback( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test at started when already running.""" - assert hass.state == CoreState.running + assert hass.state is CoreState.running calls = [] @@ -257,7 +257,7 @@ async def test_cancelling_at_started_when_running( hass: HomeAssistant, caplog: pytest.LogCaptureFixture ) -> None: """Test cancelling at start when already running.""" - assert hass.state == CoreState.running + assert hass.state is CoreState.running assert hass.is_running calls = [] diff --git a/tests/test_core.py b/tests/test_core.py index 1210b110601..918f098eab7 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -413,7 +413,7 @@ async def test_stage_shutdown_timeouts(hass: HomeAssistant) -> None: with patch.object(hass.timeout, "async_timeout", side_effect=asyncio.TimeoutError): await hass.async_stop() - assert hass.state == CoreState.stopped + assert hass.state is CoreState.stopped async def test_stage_shutdown_generic_error(hass: HomeAssistant, caplog) -> None: