async def patched_close() -> None: """Mock close to avoid integrations closing our session.""" try: found_frame, integration, path = get_integration_frame() except MissingIntegrationFrame: # Did not source from an integration? Hard error. raise RuntimeError( "Detected closing of the Home Assistant aiohttp session in the Home Assistant core. " "Please report this issue.") index = found_frame.filename.index(path) if path == "custom_components/": extra = " to the custom component author" else: extra = "" _LOGGER.warning( "Detected integration that closes the Home Assistant aiohttp session. " "Please report issue%s for %s using this method at %s, line %s: %s", extra, integration, found_frame.filename[index:], found_frame.lineno, found_frame.line.strip(), )
async def test_extract_frame_integration(caplog, mock_integration_frame): """Test extracting the current frame from integration context.""" found_frame, integration, path = frame.get_integration_frame() assert integration == "hue" assert path == "homeassistant/components/" assert found_frame == mock_integration_frame
async def test_extract_frame_integration_with_excluded_integration(caplog): """Test extracting the current frame from integration context.""" correct_frame = Mock( filename="/home/dev/homeassistant/components/mdns/light.py", lineno="23", line="self.light.is_on", ) with patch( "homeassistant.helpers.frame.extract_stack", return_value=[ Mock( filename="/home/dev/homeassistant/core.py", lineno="23", line="do_something()", ), correct_frame, Mock( filename= "/home/dev/homeassistant/components/zeroconf/usage.py", lineno="23", line="self.light.is_on", ), Mock( filename="/home/dev/mdns/lights.py", lineno="2", line="something()", ), ], ): found_frame, integration, path = frame.get_integration_frame( exclude_integrations={"zeroconf"}) assert integration == "mdns" assert path == "homeassistant/components/" assert found_frame == correct_frame
async def test_extract_frame_integration(caplog): """Test extracting the current frame from integration context.""" correct_frame = Mock( filename="/home/paulus/homeassistant/components/hue/light.py", lineno="23", line="self.light.is_on", ) with patch( "homeassistant.helpers.frame.extract_stack", return_value=[ Mock( filename="/home/paulus/homeassistant/core.py", lineno="23", line="do_something()", ), correct_frame, Mock( filename="/home/paulus/aiohue/lights.py", lineno="2", line="something()", ), ], ): found_frame, integration, path = frame.get_integration_frame() assert integration == "hue" assert path == "homeassistant/components/" assert found_frame == correct_frame
async def test_extract_frame_no_integration(caplog): """Test extracting the current frame without integration context.""" with patch( "homeassistant.helpers.frame.extract_stack", return_value=[ Mock( filename="/home/paulus/homeassistant/core.py", lineno="23", line="do_something()", ), Mock( filename="/home/paulus/aiohue/lights.py", lineno="2", line="something()", ), ], ), pytest.raises(frame.MissingIntegrationFrame): frame.get_integration_frame()
def _report(what: str) -> None: """Report incorrect usage. Async friendly. """ integration_frame = None with suppress(MissingIntegrationFrame): integration_frame = get_integration_frame( exclude_integrations={"zeroconf"}) if not integration_frame: _LOGGER.warning("Detected code that %s; Please report this issue", what, stack_info=True) return report_integration(what, integration_frame)