コード例 #1
0
async def async_discover_devices(hass, config_entry):
    """
    Run periodically to discover new devices.

    Currently it's only run at startup.
    """

    server = SuplaAPI(
        config_entry.data[CONF_SERVER], config_entry.data[CONF_ACCESS_TOKEN]
    )

    hass.data[DOMAIN][CONF_SERVER][config_entry.entry_id] = server
    component_configs = {}

    for channel in server.get_channels(include=["iodevice"]):
        channel_function = channel["function"]["name"]
        component_name = SUPLA_FUNCTION_HA_CMP_MAP.get(channel_function)

        if component_name is None:
            _LOGGER.info(
                "Unsupported function: %s, channel id: %s",
                channel_function,
                channel["id"],
            )
            continue

        component_configs.setdefault(component_name, []).append(channel)

    # Load discovered devices
    for component_name, channel in component_configs.items():
        hass.data[DOMAIN][CONF_CHANNELS][component_name] = channel
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(config_entry, component_name)
        )
コード例 #2
0
ファイル: __init__.py プロジェクト: 19125064/core
def setup(hass, base_config):
    """Set up the Supla component."""

    server_confs = base_config[DOMAIN][CONF_SERVERS]

    hass.data[SUPLA_SERVERS] = {}
    hass.data[SUPLA_CHANNELS] = {}

    for server_conf in server_confs:

        server_address = server_conf[CONF_SERVER]

        server = SuplaAPI(server_address, server_conf[CONF_ACCESS_TOKEN])

        # Test connection
        try:
            srv_info = server.get_server_info()
            if srv_info.get("authenticated"):
                hass.data[SUPLA_SERVERS][server_conf[CONF_SERVER]] = server
            else:
                _LOGGER.error(
                    "Server: %s not configured. API call returned: %s",
                    server_address,
                    srv_info,
                )
                return False
        except OSError:
            _LOGGER.exception(
                "Server: %s not configured. Error on Supla API access: ",
                server_address)
            return False

    discover_devices(hass, base_config)

    return True
コード例 #3
0
    async def async_step_init(self, user_input=None):
        """Handle a flow start."""
        errors = {}
        description_placeholders = {"error_info": ""}
        if user_input is not None:
            # Test connection
            server = SuplaAPI(user_input[CONF_SERVER], user_input[CONF_ACCESS_TOKEN])
            srv_info = server.get_server_info()
            if srv_info.get("authenticated"):
                """Finish config flow"""
                return self.async_create_entry(
                    title="AIS " + user_input[CONF_SERVER], data=user_input
                )
            else:
                _LOGGER.error(
                    "Server: %s not configured. API call returned: %s",
                    user_input[CONF_SERVER],
                    srv_info,
                )
                errors = {CONF_SERVER: "supla_no_connection"}
                description_placeholders = {"error_info": str(srv_info)}

        return self.async_show_form(
            step_id="init",
            data_schema=vol.Schema(
                {vol.Required(CONF_SERVER): str, vol.Required(CONF_ACCESS_TOKEN): str}
            ),
            errors=errors,
            description_placeholders=description_placeholders,
        )
コード例 #4
0
async def async_setup_entry(hass, config_entry):
    """Set up supla as config entry."""
    _LOGGER.info("supla async_setup_entry")
    if DOMAIN not in hass.data:
        hass.data[DOMAIN] = {}

    server_address = config_entry.data[CONF_SERVER]
    supla_server = SuplaAPI(server_address,
                            config_entry.data[CONF_ACCESS_TOKEN])
    hass.data[SUPLA_SERVERS][config_entry.data[CONF_SERVER]] = supla_server
    hass.async_create_task(async_discover_devices(hass, config_entry))

    return True
コード例 #5
0
def api(SERVER, PERSONAL_ACCESS_TOKEN):
    from pysupla import SuplaAPI

    return SuplaAPI(server=SERVER, personal_access_token=PERSONAL_ACCESS_TOKEN)