Where is notify component? Used it on devcontainer but it's not there on my rpi HA install

Hi all.
I have a rpi running HA with these SW versions:

  • Core 2024.1.3
  • Supervisor 2024.01.1
  • Operating System 11.4
  • Frontend 20240104.0

I’m developing a firebase cloud messaging service custom component, and have successfully tested it with the devcontainer and core repository, as described here:

The core repository has a /components folder which includes notify.

On my rpi install that /components folder isn’t present. I have /custom_components folder for my HAC installed integrations.

My custom component is trying to import nofify like this:

from homeassistant.components.notify import (
    ATTR_DATA,
    ATTR_TITLE,
    ATTR_TITLE_DEFAULT,
    BaseNotificationService,
)

but because I don’t have the components path on my rpi it fails, where is worked on the devcontainer. I don’t understand why the 2 installs are different. Can anyone advise please, and is there a way to fix it?

You will have a components folder otherwise HA would not start. Many of the core functions load from there.

How are you running HA?

Your custom component should be able to import from homeassistant components. What error are you getting?

Its in the homeassistant container. Which you can’t see without some effort

docker exec -it homeassistant bash

Thanks @msp1974 and @nickrout for your help and comments. I’ve actually determined that the reason for the error was misleading and actually because I was trying to access a JSON file from one of my python files within the custom component. I know I’m in danger of changing the thread here but, how do you access a file within a custom component? I was doing this:

credentials = service_account.Credentials.from_service_account_file(
        "homeassistant/custom_components/push_to_fcm/service-account.json",
        scopes=SCOPES,
    )

It’s a temporary file to get me up and running but would appreciate input to how files are accessed by python within a custom component.

1 Like

You’re above my pay grade sorry. I assume you have been here https://developers.home-assistant.io/

Also you’re more likely to get a devs attention on discord.

I can only suggest that hard coding home assistant directory may be an error, as the place is different in different install methods.

There are a number of ways to do this. As HA uses async, your are best using something like aiofiles. Example below from an api for the Wiser integration i help maintain. However, depending on the purpose of this file, you maybe better storing in config.yaml (as part of tour integrarion config) or store in config flow. If you are creating the file as a place to hold info as your integration runs, put it in config/.storage with all the others.

async def async_load_config(self):
        if exists(self._config_file):
            async with aiofiles.open(self._config_file, mode="r") as config_file:
                try:
                    contents = await config_file.read()
                    if contents:
                        self._config = json.loads(contents)
                except (
                    OSError,
                    EOFError,
                    TypeError,
                    AttributeError,
                    json.JSONDecodeError,
                ) as ex:
                    raise WiserExtraConfigError("Error loading extra config file") from ex
        else:
            await self.async_update_config("Info", "Version", "1.0.0")
        return

Edit:

To get the config dir donsomething like this

config_file=hass.config.config_dir + "/.storage/myfile.json"

EDIT: looking some more at this there is actually a built in utility to load json (orig thought just yaml ones). Anyway you can use

load_json_object from homeassistant.utils.json.

It is a non async function so use something like this.

integration = await async_get_integration(hass, DOMAIN)
my_file = integration.file_path / "my_file.json"
self.my_data = await hass.async_add_executor_job(
            load_json_object, str(my_file)
        )