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