core/tests/components/image/conftest.py

194 lines
5.2 KiB
Python
Raw Normal View History

2023-06-19 15:03:48 +00:00
"""Test helpers for image."""
from collections.abc import Generator
2023-06-19 15:03:48 +00:00
import pytest
from homeassistant.components import image
from homeassistant.config_entries import ConfigEntry, ConfigFlow
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from tests.common import (
MockConfigEntry,
MockModule,
mock_config_flow,
mock_integration,
mock_platform,
)
TEST_DOMAIN = "test"
class MockImageEntity(image.ImageEntity):
"""Mock image entity."""
_attr_name = "Test"
async def async_added_to_hass(self):
"""Set the update time."""
self._attr_image_last_updated = dt_util.utcnow()
async def async_image(self) -> bytes | None:
"""Return bytes of image."""
return b"Test"
class MockImageEntityInvalidContentType(image.ImageEntity):
"""Mock image entity."""
_attr_name = "Test"
async def async_added_to_hass(self):
"""Set the update time and assign and incorrect content type."""
self._attr_content_type = "text/json"
self._attr_image_last_updated = dt_util.utcnow()
async def async_image(self) -> bytes | None:
"""Return bytes of image."""
return b"Test"
class MockURLImageEntity(image.ImageEntity):
"""Mock image entity."""
_attr_image_url = "https://example.com/myimage.jpg"
_attr_name = "Test"
async def async_added_to_hass(self):
"""Set the update time."""
self._attr_image_last_updated = dt_util.utcnow()
2023-06-19 15:03:48 +00:00
class MockImageNoStateEntity(image.ImageEntity):
"""Mock image entity."""
_attr_name = "Test"
async def async_image(self) -> bytes | None:
"""Return bytes of image."""
return b"Test"
class MockImageSyncEntity(image.ImageEntity):
"""Mock image entity."""
_attr_name = "Test"
async def async_added_to_hass(self):
"""Set the update time."""
self._attr_image_last_updated = dt_util.utcnow()
def image(self) -> bytes | None:
"""Return bytes of image."""
return b"Test"
class MockImageConfigEntry:
"""A mock image config entry."""
def __init__(self, entities: list[image.ImageEntity]) -> None:
"""Initialize."""
self._entities = entities
async def async_setup_entry(
self,
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up test image platform via config entry."""
async_add_entities([self._entities])
class MockImagePlatform:
"""A mock image platform."""
PLATFORM_SCHEMA = image.PLATFORM_SCHEMA
def __init__(self, entities: list[image.ImageEntity]) -> None:
"""Initialize."""
self._entities = entities
async def async_setup_platform(
self,
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the mock image platform."""
async_add_entities(self._entities)
@pytest.fixture(name="config_flow")
def config_flow_fixture(hass: HomeAssistant) -> Generator[None]:
2023-06-19 15:03:48 +00:00
"""Mock config flow."""
class MockFlow(ConfigFlow):
"""Test flow."""
mock_platform(hass, f"{TEST_DOMAIN}.config_flow")
with mock_config_flow(TEST_DOMAIN, MockFlow):
yield
@pytest.fixture(name="mock_image_config_entry")
async def mock_image_config_entry_fixture(
hass: HomeAssistant, config_flow: None
) -> ConfigEntry:
2023-06-19 15:03:48 +00:00
"""Initialize a mock image config_entry."""
async def async_setup_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Set up test config entry."""
Ensure config entries are not unloaded while their platforms are setting up (#118767) * Report non-awaited/non-locked config entry platform forwards Its currently possible for config entries to be reloaded while their platforms are being forwarded if platform forwards are not awaited or done after the config entry is setup since the lock will not be held in this case. In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards we advised to await platform forwards to ensure this does not happen, however for sleeping devices and late discovered devices, platform forwards may happen later. If config platform forwards are happening during setup, they should be awaited If config entry platform forwards are not happening during setup, instead async_late_forward_entry_setups should be used which will hold the lock to prevent the config entry from being unloaded while its platforms are being setup * Report non-awaited/non-locked config entry platform forwards Its currently possible for config entries to be reloaded while their platforms are being forwarded if platform forwards are not awaited or done after the config entry is setup since the lock will not be held in this case. In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards we advised to await platform forwards to ensure this does not happen, however for sleeping devices and late discovered devices, platform forwards may happen later. If config platform forwards are happening during setup, they should be awaited If config entry platform forwards are not happening during setup, instead async_late_forward_entry_setups should be used which will hold the lock to prevent the config entry from being unloaded while its platforms are being setup * run with error on to find them * cert_exp, hold lock * cert_exp, hold lock * shelly async_late_forward_entry_setups * compact * compact * found another * patch up mobileapp * patch up hue tests * patch up smartthings * fix mqtt * fix esphome * zwave_js * mqtt * rework * fixes * fix mocking * fix mocking * do not call async_forward_entry_setup directly * docstrings * docstrings * docstrings * add comments * doc strings * fixed all in core, turn off strict * coverage * coverage * missing * coverage
2024-06-05 01:34:39 +00:00
await hass.config_entries.async_forward_entry_setups(
config_entry, [image.DOMAIN]
)
2023-06-19 15:03:48 +00:00
return True
async def async_unload_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Unload test config entry."""
Ensure config entries are not unloaded while their platforms are setting up (#118767) * Report non-awaited/non-locked config entry platform forwards Its currently possible for config entries to be reloaded while their platforms are being forwarded if platform forwards are not awaited or done after the config entry is setup since the lock will not be held in this case. In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards we advised to await platform forwards to ensure this does not happen, however for sleeping devices and late discovered devices, platform forwards may happen later. If config platform forwards are happening during setup, they should be awaited If config entry platform forwards are not happening during setup, instead async_late_forward_entry_setups should be used which will hold the lock to prevent the config entry from being unloaded while its platforms are being setup * Report non-awaited/non-locked config entry platform forwards Its currently possible for config entries to be reloaded while their platforms are being forwarded if platform forwards are not awaited or done after the config entry is setup since the lock will not be held in this case. In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards we advised to await platform forwards to ensure this does not happen, however for sleeping devices and late discovered devices, platform forwards may happen later. If config platform forwards are happening during setup, they should be awaited If config entry platform forwards are not happening during setup, instead async_late_forward_entry_setups should be used which will hold the lock to prevent the config entry from being unloaded while its platforms are being setup * run with error on to find them * cert_exp, hold lock * cert_exp, hold lock * shelly async_late_forward_entry_setups * compact * compact * found another * patch up mobileapp * patch up hue tests * patch up smartthings * fix mqtt * fix esphome * zwave_js * mqtt * rework * fixes * fix mocking * fix mocking * do not call async_forward_entry_setup directly * docstrings * docstrings * docstrings * add comments * doc strings * fixed all in core, turn off strict * coverage * coverage * missing * coverage
2024-06-05 01:34:39 +00:00
await hass.config_entries.async_unload_platforms(config_entry, [image.DOMAIN])
2023-06-19 15:03:48 +00:00
return True
mock_integration(
hass,
MockModule(
TEST_DOMAIN,
async_setup_entry=async_setup_entry_init,
async_unload_entry=async_unload_entry_init,
),
)
mock_platform(
hass,
f"{TEST_DOMAIN}.{image.DOMAIN}",
MockImageConfigEntry(MockImageEntity(hass)),
2023-06-19 15:03:48 +00:00
)
config_entry = MockConfigEntry(domain=TEST_DOMAIN)
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry
@pytest.fixture(name="mock_image_platform")
async def mock_image_platform_fixture(hass: HomeAssistant) -> None:
2023-06-19 15:03:48 +00:00
"""Initialize a mock image platform."""
mock_integration(hass, MockModule(domain="test"))
mock_platform(hass, "test.image", MockImagePlatform([MockImageEntity(hass)]))
2023-06-19 15:03:48 +00:00
assert await async_setup_component(
hass, image.DOMAIN, {"image": {"platform": "test"}}
)
await hass.async_block_till_done()