Python Forum

Full Version: Trying to understand method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to understand the following code in home assisant integration. Is setup_platform method is ending on where it is returning None or everything until add_entties method. Any pointers would help me too. Thanks.
def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Connect to Sony projector using network."""

    host = config[CONF_HOST]
    name = config[CONF_NAME]
    sdcp_connection = pysdcp.Projector(host)

    # Sanity check the connection
    try:
        sdcp_connection.get_power()
    except ConnectionError:
        _LOGGER.error("Failed to connect to projector '%s'", host)
        return
    _LOGGER.debug("Validated projector '%s' OK", host)
    add_entities([SonyProjector(sdcp_connection, name)], True)
It depends. If there is a ConnectionError calling sdcp_connection.get_power(), an error is logged and the function returns. If there is not a ConnectionError the function logs the debug message and calls add_entities. Read about python exception handling here:

https://docs.python.org/3/tutorial/errors.html