-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
321 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
"""HACS Base entities.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.core import callback | ||
from homeassistant.helpers.entity import Entity | ||
|
||
from custom_components.hacs.enums import HacsGitHubRepo | ||
|
||
from .base import HacsBase | ||
from .const import DOMAIN, HACS_SYSTEM_ID, NAME_SHORT | ||
from .repositories.base import HacsRepository | ||
|
||
|
||
def system_info(hacs: HacsBase) -> dict: | ||
"""Return system info.""" | ||
info = { | ||
"identifiers": {(DOMAIN, HACS_SYSTEM_ID)}, | ||
"name": NAME_SHORT, | ||
"manufacturer": "hacs.xyz", | ||
"model": "", | ||
"sw_version": str(hacs.version), | ||
"configuration_url": "homeassistant://hacs", | ||
} | ||
# LEGACY can be removed when min HA version is 2021.12 | ||
if hacs.core.ha_version >= "2021.12.0b0": | ||
# pylint: disable=import-outside-toplevel | ||
from homeassistant.helpers.device_registry import DeviceEntryType | ||
|
||
info["entry_type"] = DeviceEntryType.SERVICE | ||
else: | ||
info["entry_type"] = "service" | ||
return info | ||
|
||
|
||
class HacsBaseEntity(Entity): | ||
"""Base HACS entity.""" | ||
|
||
_attr_should_poll = False | ||
|
||
def __init__(self, hacs: HacsBase) -> None: | ||
"""Initialize.""" | ||
self.hacs = hacs | ||
|
||
async def async_added_to_hass(self) -> None: | ||
"""Register for status events.""" | ||
self.async_on_remove( | ||
self.hass.bus.async_listen("hacs/repository", self._update_and_write_state) | ||
) | ||
|
||
@callback | ||
def _update(self) -> None: | ||
"""Update the sensor.""" | ||
|
||
async def async_update(self) -> None: | ||
"""Manual updates of the sensor.""" | ||
self._update() | ||
|
||
@callback | ||
def _update_and_write_state(self, *_) -> None: | ||
"""Update the sensor and write state.""" | ||
self._update() | ||
self.async_write_ha_state() | ||
|
||
|
||
class HacsSystemEntity(HacsBaseEntity): | ||
"""Base system entity.""" | ||
|
||
_attr_icon = "hacs:hacs" | ||
_attr_unique_id = HACS_SYSTEM_ID | ||
|
||
@property | ||
def device_info(self) -> dict[str, any]: | ||
"""Return device information about HACS.""" | ||
return system_info(self.hacs) | ||
|
||
|
||
class HacsRepositoryEntity(HacsBaseEntity): | ||
"""Base repository entity.""" | ||
|
||
def __init__(self, hacs: HacsBase, repository: HacsRepository) -> None: | ||
"""Initialize.""" | ||
super().__init__(hacs=hacs) | ||
self.repository = repository | ||
self._attr_unique_id = str(repository.data.id) | ||
|
||
@property | ||
def available(self) -> bool: | ||
return self.hacs.repositories.is_downloaded(repository_id=str(self.repository.data.id)) | ||
|
||
@property | ||
def device_info(self) -> dict[str, any]: | ||
"""Return device information about HACS.""" | ||
if self.repository.data.full_name == HacsGitHubRepo.INTEGRATION: | ||
return system_info(self.hacs) | ||
|
||
info = { | ||
"identifiers": {(DOMAIN, str(self.repository.data.id))}, | ||
"name": self.repository.display_name, | ||
"model": self.repository.data.category, | ||
"manufacturer": ", ".join( | ||
author.replace("@", "") for author in self.repository.data.authors | ||
), | ||
"configuration_url": "homeassistant://hacs", | ||
} | ||
# LEGACY can be removed when min HA version is 2021.12 | ||
if self.hacs.core.ha_version >= "2021.12.0b0": | ||
# pylint: disable=import-outside-toplevel | ||
from homeassistant.helpers.device_registry import DeviceEntryType | ||
|
||
info["entry_type"] = DeviceEntryType.SERVICE | ||
else: | ||
info["entry_type"] = "service" | ||
return info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""""Starting setup task: Update".""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.core import HomeAssistant | ||
|
||
from ..base import HacsBase | ||
from ..enums import ConfigurationType, HacsStage | ||
from .base import HacsTask | ||
|
||
UPDATE_DOMAIN = "update" | ||
|
||
|
||
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task: | ||
"""Set up this task.""" | ||
return Task(hacs=hacs, hass=hass) | ||
|
||
|
||
class Task(HacsTask): | ||
"""Setup the HACS update platform.""" | ||
|
||
stages = [HacsStage.RUNNING] | ||
|
||
async def async_execute(self) -> None: | ||
"""Execute the task.""" | ||
if self.hacs.configuration.config_type == ConfigurationType.YAML: | ||
self.task_logger( | ||
self.hacs.log.info, "Update entities are only supported when using UI configuration" | ||
) | ||
elif self.hacs.core.ha_version >= "2022.4.0.dev0" and self.hacs.configuration.experimental: | ||
self.hass.config_entries.async_setup_platforms( | ||
self.hacs.configuration.config_entry, [UPDATE_DOMAIN] | ||
) |
Oops, something went wrong.