Migrate issue registry to use singleton helper (#117848)

* Migrate issue registry to use singleton helper

The other registries were already migrated, but since this
one had a read only flag, it required a slightly different
solution since it uses the same hass.data key

* refactor
This commit is contained in:
J. Nick Koston 2024-05-22 08:13:19 -10:00 committed by GitHub
parent 7a6b107248
commit 0d5c8e30cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 7 deletions

View File

@ -18,6 +18,7 @@ from homeassistant.util.event_type import EventType
from homeassistant.util.hass_dict import HassKey from homeassistant.util.hass_dict import HassKey
from .registry import BaseRegistry from .registry import BaseRegistry
from .singleton import singleton
from .storage import Store from .storage import Store
DATA_REGISTRY: HassKey[IssueRegistry] = HassKey("issue_registry") DATA_REGISTRY: HassKey[IssueRegistry] = HassKey("issue_registry")
@ -108,18 +109,16 @@ class IssueRegistryStore(Store[dict[str, list[dict[str, Any]]]]):
class IssueRegistry(BaseRegistry): class IssueRegistry(BaseRegistry):
"""Class to hold a registry of issues.""" """Class to hold a registry of issues."""
def __init__(self, hass: HomeAssistant, *, read_only: bool = False) -> None: def __init__(self, hass: HomeAssistant) -> None:
"""Initialize the issue registry.""" """Initialize the issue registry."""
self.hass = hass self.hass = hass
self.issues: dict[tuple[str, str], IssueEntry] = {} self.issues: dict[tuple[str, str], IssueEntry] = {}
self._read_only = read_only
self._store = IssueRegistryStore( self._store = IssueRegistryStore(
hass, hass,
STORAGE_VERSION_MAJOR, STORAGE_VERSION_MAJOR,
STORAGE_KEY, STORAGE_KEY,
atomic_writes=True, atomic_writes=True,
minor_version=STORAGE_VERSION_MINOR, minor_version=STORAGE_VERSION_MINOR,
read_only=read_only,
) )
@callback @callback
@ -244,6 +243,14 @@ class IssueRegistry(BaseRegistry):
return issue return issue
@callback
def make_read_only(self) -> None:
"""Make the registry read-only.
This method is irreversible.
"""
self._store.make_read_only()
async def async_load(self) -> None: async def async_load(self) -> None:
"""Load the issue registry.""" """Load the issue registry."""
data = await self._store.async_load() data = await self._store.async_load()
@ -301,16 +308,18 @@ class IssueRegistry(BaseRegistry):
@callback @callback
@singleton(DATA_REGISTRY)
def async_get(hass: HomeAssistant) -> IssueRegistry: def async_get(hass: HomeAssistant) -> IssueRegistry:
"""Get issue registry.""" """Get issue registry."""
return hass.data[DATA_REGISTRY] return IssueRegistry(hass)
async def async_load(hass: HomeAssistant, *, read_only: bool = False) -> None: async def async_load(hass: HomeAssistant, *, read_only: bool = False) -> None:
"""Load issue registry.""" """Load issue registry."""
assert DATA_REGISTRY not in hass.data ir = async_get(hass)
hass.data[DATA_REGISTRY] = IssueRegistry(hass, read_only=read_only) if read_only: # only used in for check config script
await hass.data[DATA_REGISTRY].async_load() ir.make_read_only()
return await ir.async_load()
@callback @callback

View File

@ -264,6 +264,13 @@ class Store[_T: Mapping[str, Any] | Sequence[Any]]:
"""Return the config path.""" """Return the config path."""
return self.hass.config.path(STORAGE_DIR, self.key) return self.hass.config.path(STORAGE_DIR, self.key)
def make_read_only(self) -> None:
"""Make the store read-only.
This method is irreversible.
"""
self._read_only = True
async def async_load(self) -> _T | None: async def async_load(self) -> _T | None:
"""Load data. """Load data.