Share configuration between __init__ and sensor

Hi, I have a question for those who know how I can share configuration between my init.py and my sensor.py file.

I developed a custom plugin for home assistant, I only used sensor.py file.
This morning, I decided to add to my custom component various services.

So I used the init.py file to initialize my services. And the sensor.py file for my custom sensor.
But now, I want to share my CONFIG_SCHEMA (define in the init) to my sensor.py file.

CONFIG_SCHEMA = vol.Schema(
    {
        DOMAIN: vol.Schema(
            {
                vol.Required(CONF_TOKEN): cv.string,
                vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.string,
                vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
                vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTO): cv.string
            }
        )
    },
    extra=vol.ALLOW_EXTRA,
)

I want to use those variables wich are get from init.py in my sensor.py file.
But curently, I don’t find any post wich deals with my problem.

I’m just wondering, does anybody have any idea how to share those variables ?

Please, someone can help me ? Or can give me some advise ?

In your sensor.py you can do an absolute import to import that schema.

from . import CONFIG_SCHEMA

Ok, thank you for your answer. It’s not easy to find documentation about this.

Now I imported that in my sensor.py, but how can I get my vars ?
Do I redeclarate another CONFIG_SCHEMA in my sensor.py ?

I’m a little lost

What exactly are you trying to reuse or accomplish? I can’t think of a reason to use the CONFIG_SCHEMA in your sensor.py, but I might not be understanding what problem you are trying to solve.

Which vars are you trying to access in sensor.py?

The CONFIG_SCHEMA is used only for validating and formatting your configuration parameters. The configuration parameters are then passed as the second argument to your component´s async_setup(hass, config) coroutine from where you have access to them (config is a dictionary).
Usually you can store arbitrary information (and also your parameters) in the hass.data[DOMAIN] dictionary where DOMAIN is usually your component´s name.
The hass object is then available in your sensor.py (passed to async_setup_platform(hass, ...)) from where you again have access to the data attribute and your parameters.

1 Like

I define CONFIG_SCHEMA in my init.py file to get var from the configuration.yaml.
And I want to reuse those var in my sensor.py.

@alengwenus If I understood, I have to do this in my init.py :

    hass.data[DOMAIN] = {
        'token': 'XXXX',
        'port': 'XXXX',
        'host': 'XXXX',
        'protocol': 'XXXX',
    }

    hass.helpers.discovery.load_platform('sensor', DOMAIN, {}, config)

And in my sensor file, I have to do this to retreive my vars :

token = hass.data[DOMAIN]["token]

Thank you for all :slight_smile:

The better way to do this is to use the discovery_info in the async_setup_platform and pass it that way.

Sorry hit send before I was done. If you want to configure multiple entries for this interegration, you won’t be able to distinguish them using the hass.data approach.