My first custom_component is doing nothing

Hello,

I am writing my first custom component.
The init.py

from homeassistant import core
from .uvr import config_flow

async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
    """Set up the UVR TA-Designer component."""
    hass.async_create_task(
        hass.config_entries.flow.async_init(
            'uvr', context={'source': 'import'}
        )
    )
    return True

uvr.py:

from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import xml.etree.ElementTree as ET
from requests.auth import HTTPBasicAuth
import requests
from datetime import timedelta
from homeassistant import config_entries
from cmi-reader import read_data, extract_entity_data
import logging

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'uvr'
CONFIG_SCHEMA = cv.deprecated(DOMAIN)

class config_flow(config_entries.ConfigFlow, domain=DOMAIN):
    async def async_step_user(self, user_input=None):
        errors = {}

        if user_input is not None:
            # Validate the provided configuration
            if not user_input.get("ip"):
                errors["base"] = "missing_ip"
            # Add more validation as needed

            if not errors:
                return self.async_create_entry(title="Custom Integration", data=user_input)

        # Show the configuration form
        return self.async_show_form(
            step_id="user",
            data_schema=config_entries.CONFIG_SCHEMA,
            errors=errors,
        )

async def async_setup_entry(hass, entry):
    _LOGGER.debug("UVR -- starting")
    ip = entry.data['ip']
    username = entry.data['username']
    password = entry.data['password']
    interval = entry.data['interval']
    xml_file = entry.data['xml_file']

    data = CustomData(hass, ip, username, password, interval, xml_file)

    async def update_entities(now):
        data.update()
        for name, value in data.get_data().items():
            if name not in hass.states.entity_ids():
                hass.states.set(f"{DOMAIN}.{name}", value)

    update_entities(None)

    # Schedule the first update
    hass.helpers.event.async_track_utc_time_change(update_entities, second=0)

    # Schedule subsequent updates
    hass.helpers.event.async_track_time_interval(update_entities, timedelta(seconds=interval))

    hass.data[DOMAIN][entry.entry_id] = data

    return True

async def async_unload_entry(hass, entry):
    hass.data[DOMAIN].pop(entry.entry_id)
    return True

class CustomData:
    def __init__(self, hass, ip, username, password, interval, xml_file):
        self.hass = hass
        self.ip = ip
        self.username = username
        self.password = password
        self.interval = interval
        self.xml_file = xml_file
        self.raw_data = {}
        self.data = {}

    @Throttle(timedelta(seconds=interval))
    def update(self):
        new_data = self.read()
        self.raw_data = new_data
        self.data=extract_entity_data(self.raw_data)
        self.create_entities()

    def read(self):
        page_values=read_data(self.xml_file, self.ip, self.username, self.password)
        _LOGGER.debug("UVR read data: %s", page_values)
        return page_values

    def create_entities(self):
        for name, value in self.data.items():
            entity_id = f"{DOMAIN}.{name}"
            if entity_id not in self.hass.states.entity_ids():
                unique_id = f"{DOMAIN}_{name.lower()}"
                _LOGGER.debug("UVR create entity: %s", entity_id)
                self.hass.states.set(entity_id, value, attributes={'unique_id': unique_id})

    def get_data(self):
        return self.data

Now the custom component is loaded according to the logs.
But “UVR – starting” never appears in the logs.

Where is my mistake?

Greetings,
Hendrik

Did you actually create the config_flow.py for your integration?
If so, show it, please.

Hello,

no, the class config_flow is in my uvr.py.
Is that wrong?

Greetings,
Hendrik

Mmm… All integrations use a separate config_flow.py, I suspect there might be a reason for that.
For one, it’s not your integration that instantiate the config flow, so I suspect HA needs that hardcoded filename to be able to find your flow.

Hm, maybe I am missing some basics here.
My custom component does not really need any user-configuration, apart from what is specified in configuration.yml.

Can you advise me of any minimum example, that I could use as “template”?

It basically only needs to run regularly, poll some information via http and create+update entities.

Greetings,
Hendrik

Hmm, i wrote my own integration, it consists of 3 files:
_ _ init _ _.py
climate.py
manifest.json

With a config flow?
I’d be surprised, but happy to see an example :slight_smile:

@koying
I think I misunderstood , my intergration does have a config flow :wink:

I understood that all integrations should have one :thinking:

For the record, all integrations configured through the UI must have a config flow.

You can still create integrations without, configured through YAML, but those new ones are not permitted in HA core anymore, only through custom_components (HACS). Even worse, existing core integrations without config_flow are not allowed to be enhanced, so there is a strong incentive to implement it :wink: