2023-06-19 15:03:48 +00:00
|
|
|
"""Test helpers for image."""
|
2024-03-08 13:50:04 +00:00
|
|
|
|
2024-07-01 10:09:11 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
|
2023-06-27 09:46:31 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
|
2023-06-27 06:36:12 +00:00
|
|
|
class MockURLImageEntity(image.ImageEntity):
|
|
|
|
"""Mock image entity."""
|
|
|
|
|
2023-06-27 09:59:24 +00:00
|
|
|
_attr_image_url = "https://example.com/myimage.jpg"
|
2023-06-27 06:36:12 +00:00
|
|
|
_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")
|
2024-06-06 15:28:59 +00:00
|
|
|
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")
|
2023-06-27 09:46:31 +00:00
|
|
|
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(
|
2023-06-27 06:36:12 +00:00
|
|
|
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")
|
2023-06-27 09:46:31 +00:00
|
|
|
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"))
|
2023-06-27 06:36:12 +00:00
|
|
|
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()
|