Skip to content

Commit

Permalink
feat: add option to disable downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Apr 12, 2024
1 parent b81982a commit 532432b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 6 deletions.
5 changes: 5 additions & 0 deletions custom_components/powercalc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
CONF_CREATE_ENERGY_SENSORS,
CONF_CREATE_UTILITY_METERS,
CONF_DISABLE_EXTENDED_ATTRIBUTES,
CONF_DISABLE_LIBRARY_DOWNLOAD,
CONF_ENABLE_AUTODISCOVERY,
CONF_ENERGY_INTEGRATION_METHOD,
CONF_ENERGY_SENSOR_CATEGORY,
Expand Down Expand Up @@ -131,6 +132,10 @@
CONF_DISABLE_EXTENDED_ATTRIBUTES,
default=False,
): cv.boolean,
vol.Optional(
CONF_DISABLE_LIBRARY_DOWNLOAD,
default=False,
): cv.boolean,
vol.Optional(CONF_ENABLE_AUTODISCOVERY, default=True): cv.boolean,
vol.Optional(CONF_CREATE_ENERGY_SENSORS, default=True): cv.boolean,
vol.Optional(CONF_CREATE_UTILITY_METERS, default=False): cv.boolean,
Expand Down
1 change: 1 addition & 0 deletions custom_components/powercalc/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
CONF_CREATE_UTILITY_METERS = "create_utility_meters"
CONF_DAILY_FIXED_ENERGY = "daily_fixed_energy"
CONF_DELAY = "delay"
CONF_DISABLE_LIBRARY_DOWNLOAD = "disable_library_download"
CONF_DISABLE_EXTENDED_ATTRIBUTES = "disable_extended_attributes"
CONF_ENABLE_AUTODISCOVERY = "enable_autodiscovery"
CONF_ENERGY_INTEGRATION_METHOD = "energy_integration_method"
Expand Down
12 changes: 9 additions & 3 deletions custom_components/powercalc/power_profile/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.core import HomeAssistant

from custom_components.powercalc.aliases import MANUFACTURER_DIRECTORY_MAPPING
from custom_components.powercalc.const import DATA_PROFILE_LIBRARY, DOMAIN
from custom_components.powercalc.const import CONF_DISABLE_LIBRARY_DOWNLOAD, DATA_PROFILE_LIBRARY, DOMAIN, DOMAIN_CONFIG

from .error import LibraryError
from .loader.composite import CompositeLoader
Expand Down Expand Up @@ -50,12 +50,15 @@ async def factory(hass: HomeAssistant) -> ProfileLibrary:
os.path.join(hass.config.config_dir, LEGACY_CUSTOM_DATA_DIRECTORY),
os.path.join(hass.config.config_dir, CUSTOM_DATA_DIRECTORY),
os.path.join(os.path.dirname(__file__), "../custom_data"),
#get_library_path(),
]:
if os.path.exists(data_dir):
loaders.append(LocalLoader(hass, data_dir))

loaders.append(RemoteLoader(hass))
global_config = hass.data[DOMAIN].get(DOMAIN_CONFIG, {})
disable_library_download: bool = bool(global_config.get(CONF_DISABLE_LIBRARY_DOWNLOAD, False))
if not disable_library_download:
loaders.append(RemoteLoader(hass))

loader = CompositeLoader(loaders)
library = ProfileLibrary(hass, loader)
await library.initialize()
Expand Down Expand Up @@ -163,6 +166,9 @@ async def find_model(self, manufacturer: str, model: str) -> str | None:

return await self._loader.find_model(manufacturer, search)

def get_loader(self) -> Loader:
return self._loader


class ModelInfo(NamedTuple):
manufacturer: str
Expand Down
2 changes: 2 additions & 0 deletions docs/source/configuration/global-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ All the possible options are listed below.
+-------------------------------+----------+--------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| disable_extended_attributes | boolean | **Optional** | false | Set to `true` to disable all extra attributes powercalc adds to the power, energy and group entity states. This will help keep the database size small especially when you have a lot of powercalc sensors and frequent update ratio |
+-------------------------------+----------+--------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| disable_library_download | boolean | **Optional** | false | Set to `true` to disable the Powercalc library download feature, see :doc:'/library/library` |
+-------------------------------+----------+--------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| enable_autodiscovery | boolean | **Optional** | true | Whether you want powercalc to automatically setup power sensors for `supported models`_ in your HA instance. |
+-------------------------------+----------+--------------+-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| energy_sensor_naming | string | **Optional** | {} energy | Change the name of the sensors. Use the `{}` placeholder for the entity name of your appliance. This will also change the entity_id of your sensor |
Expand Down
20 changes: 18 additions & 2 deletions docs/source/library/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Starting from version 1.12.0 all the power profiles are moved out of the compone
This way we can roll out updates to the library without the need to update the component.
Also you only need to download the profiles you actually use, saving bandwidth and storage.

#TODO: maybe add option to disable remote loading
.. note::
The library is hosted on GitHub, so you need an internet connection to download the profiles.
When you prefer to have full local control, you can disable the library in the configuration. See `Disable download feature`_.

For more information about the library structure, See :doc:`structure`.

Expand Down Expand Up @@ -41,4 +43,18 @@ For example:
│ │ └─...
.. note::
Custom models are loaded before the built-in library models, so you can override the library models by creating a custom model with the same manufacturer and model id.
Custom models are loaded before the built-in library models, so you can override the library models by creating a custom model with the same manufacturer and model id.

Disable download feature
------------------------

If you want to disable the download feature, you can set the use the ``disable_library_download`` option in the configuration.

.. code-block:: yaml
powercalc:
disable_library_download: true
This will prevent the component from downloading the library profiles from the internet.

You will have to manage the library profiles yourself, by downloading them from the GitHub repository and placing them in the ``config/powercalc/profiles`` directory.
20 changes: 19 additions & 1 deletion tests/power_profile/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import pytest
from homeassistant.core import HomeAssistant

from custom_components.powercalc import CONF_DISABLE_LIBRARY_DOWNLOAD
from custom_components.powercalc.aliases import MANUFACTURER_IKEA, MANUFACTURER_SIGNIFY
from custom_components.powercalc.power_profile.library import ModelInfo, ProfileLibrary
from custom_components.powercalc.power_profile.loader.composite import CompositeLoader
from custom_components.powercalc.power_profile.loader.local import LocalLoader
from tests.common import get_test_config_dir, get_test_profile_dir
from custom_components.powercalc.power_profile.loader.remote import RemoteLoader
from tests.common import get_test_config_dir, get_test_profile_dir, run_powercalc_setup


async def test_manufacturer_listing(hass: HomeAssistant) -> None:
Expand Down Expand Up @@ -130,3 +133,18 @@ async def test_create_power_profile_raises_library_error(hass: HomeAssistant, ca
await library.create_power_profile(ModelInfo("signify", "LCT010"))

assert "Problem loading model" in caplog.text


async def test_download_feature_can_be_disabled(hass: HomeAssistant) -> None:
await run_powercalc_setup(
hass,
{},
{
CONF_DISABLE_LIBRARY_DOWNLOAD: True,
},
)

library = await ProfileLibrary.factory(hass)
composite_loader: CompositeLoader = library.get_loader()
has_remote_loader = any(isinstance(loader, RemoteLoader) for loader in composite_loader.loaders)
assert not has_remote_loader

0 comments on commit 532432b

Please sign in to comment.