예제 #1
0
async def get_casambi_controller(*, email, user_password, network_password,
                                 api_key, session, sslcontext, callback,
                                 wire_id):
    """Setup Casambi controller and verify credentials."""
    controller = aiocasambi.Controller(
        email=email,
        user_password=user_password,
        network_password=network_password,
        api_key=api_key,
        websession=session,
        sslcontext=sslcontext,
        wire_id=wire_id,
        callback=callback,
    )

    try:
        with async_timeout.timeout(10):
            await controller.create_user_session()
            await controller.create_network_session()
        return controller

    except aiocasambi.LoginRequired:
        LOGGER.warning(f"Connected to casambi but couldn't log in")

    except aiocasambi.Unauthorized:
        LOGGER.warning(f"Connected to casambi but not registered")

    except (asyncio.TimeoutError, aiocasambi.RequestError):
        LOGGER.exception('Error connecting to the Casambi')

    except aiocasambi.AiocasambiException:
        LOGGER.exception('Unknown Casambi communication error occurred')
예제 #2
0
async def async_setup_entry(
    hass: core.HomeAssistant,
    config_entry: config_entries.ConfigEntry,
    async_add_entities,
):
    """Setup sensors from a config entry created in the integrations UI."""
    config = hass.data[DOMAIN][config_entry.entry_id]
    user_password = config[CONF_USER_PASSWORD]
    network_password = config[CONF_NETWORK_PASSWORD]
    email = config[CONF_EMAIL]
    api_key = config[CONF_API_KEY]

    network_timeout = DEFAULT_NETWORK_TIMEOUT
    scan_interval = DEFAULT_POLLING_TIME

    if CONF_NETWORK_TIMEOUT in config:
        network_timeout = config[CONF_NETWORK_TIMEOUT]

    if CONF_SCAN_INTERVAL in config:
        scan_interval = config[CONF_SCAN_INTERVAL]

    sslcontext = ssl.create_default_context()
    session = aiohttp_client.async_get_clientsession(hass)

    global CASAMBI_CONTROLLER

    if CASAMBI_CONTROLLER:
        return

    CASAMBI_CONTROLLER = CasambiController(hass)

    controller = aiocasambi.Controller(
        email=email,
        user_password=user_password,
        network_password=network_password,
        api_key=api_key,
        websession=session,
        sslcontext=sslcontext,
        wire_id=WIRE_ID,
        callback=CASAMBI_CONTROLLER.signalling_callback,
        network_timeout=network_timeout,
    )

    CASAMBI_CONTROLLER.controller = controller

    try:
        with async_timeout.timeout(10):
            await controller.create_user_session()
            await controller.create_network_session()
            await controller.start_websocket()

    except aiocasambi.LoginRequired:
        _LOGGER.error("Connected to casambi but couldn't log in")
        return False

    except aiocasambi.Unauthorized:
        _LOGGER.error("Connected to casambi but not registered")
        return False

    except (asyncio.TimeoutError, aiocasambi.RequestError):
        _LOGGER.error('Error connecting to the Casambi')
        return False

    except aiocasambi.AiocasambiException:
        _LOGGER.error('Unknown Casambi communication error occurred')
        return False

    await controller.initialize()

    units = controller.get_units()

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        # Name of the data. For logging purposes.
        name="light",
        update_method=CASAMBI_CONTROLLER.async_update_data,
        # Polling interval. Will only be polled if there are subscribers.
        update_interval=timedelta(seconds=scan_interval),
    )

    await coordinator.async_refresh()

    for unit in units:
        casambi_light = CasambiLight(coordinator, unit.unique_id, unit,
                                     controller, hass)
        async_add_entities([casambi_light], True)

        CASAMBI_CONTROLLER.units[casambi_light.unique_id] = casambi_light

    return True
예제 #3
0
async def async_setup_platform(hass: HomeAssistant,
                               config: dict,
                               async_add_entities,
                               discovery_info=None):

    user_password = config[CONF_USER_PASSWORD]
    network_password = config[CONF_NETWORK_PASSWORD]
    email = config[CONF_EMAIL]
    api_key = config[CONF_API_KEY]

    sslcontext = ssl.create_default_context()
    session = aiohttp_client.async_get_clientsession(hass)

    controller = aiocasambi.Controller(
        email=email,
        user_password=user_password,
        network_password=network_password,
        api_key=api_key,
        websession=session,
        sslcontext=sslcontext,
        wire_id=WIRE_ID,
        callback=signalling_callback,
    )

    try:
        with async_timeout.timeout(10):
            await controller.create_user_session()
            await controller.create_network_session()
            await controller.start_websocket()

    except aiocasambi.LoginRequired:
        _LOGGER.error(f"Connected to casambi but couldn't log in")
        return False

    except aiocasambi.Unauthorized:
        _LOGGER.error(f"Connected to casambi but not registered")
        return False

    except (asyncio.TimeoutError, aiocasambi.RequestError):
        #_LOGGER.exception('Error connecting to the Casambi')
        _LOGGER.error('Error connecting to the Casambi')
        return False

    except aiocasambi.AiocasambiException:
        _LOGGER.error('Unknown Casambi communication error occurred')
        return False

    await controller.initialize()

    units = controller.get_units()

    #_LOGGER.debug(f"Casambi unit: f{units}")

    for unit in units:
        #_LOGGER.debug(f"Casambi unit: f{unit}")
        casambi_light = CasambiLight(unit, controller, hass)
        async_add_entities([casambi_light], True)

        UNITS[casambi_light.unique_id] = casambi_light

    return True
예제 #4
0
async def async_setup_entry(
    hass: core.HomeAssistant,
    config_entry: config_entries.ConfigEntry,
    async_add_entities,
):
    """Setup sensors from a config entry created in the integrations UI."""
    config = hass.data[DOMAIN][config_entry.entry_id]
    user_password = config[CONF_USER_PASSWORD]
    network_password = config[CONF_NETWORK_PASSWORD]
    email = config[CONF_EMAIL]
    api_key = config[CONF_API_KEY]

    network_timeout = DEFAULT_NETWORK_TIMEOUT
    scan_interval = DEFAULT_POLLING_TIME

    if CONF_NETWORK_TIMEOUT in config:
        network_timeout = config[CONF_NETWORK_TIMEOUT]

    if CONF_SCAN_INTERVAL in config:
        scan_interval = config[CONF_SCAN_INTERVAL]

    sslcontext = ssl.create_default_context()
    session = aiohttp_client.async_get_clientsession(hass)

    if CONF_CONTROLLER in hass.data[DOMAIN]:
        dbg_msg = "async_setup_platform CasambiController already created!"
        _LOGGER.debug(dbg_msg)
        return

    hass.data[DOMAIN][CONF_CONTROLLER] = CasambiController(hass)
    casambi_controller = hass.data[DOMAIN][CONF_CONTROLLER]

    controller = aiocasambi.Controller(
        email=email,
        user_password=user_password,
        network_password=network_password,
        api_key=api_key,
        websession=session,
        sslcontext=sslcontext,
        wire_id=WIRE_ID,
        callback=casambi_controller.signalling_callback,
        network_timeout=network_timeout,
    )

    casambi_controller.controller = controller

    try:
        with async_timeout.timeout(10):
            await controller.create_user_session()
            await controller.create_network_session()
            await controller.start_websocket()

    except aiocasambi.LoginRequired:
        _LOGGER.error("Connected to casambi but couldn't log in")
        return False

    except aiocasambi.Unauthorized:
        _LOGGER.error("Connected to casambi but not registered")
        return False

    except (asyncio.TimeoutError, aiocasambi.RequestError):
        _LOGGER.error("Error connecting to the Casambi")
        return False

    except aiocasambi.AiocasambiException:
        _LOGGER.error("Unknown Casambi communication error occurred")
        return False

    await controller.initialize()

    units = controller.get_units()

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        # Name of the data. For logging purposes.
        name="light",
        update_method=casambi_controller.async_update_data,
        # Polling interval. Will only be polled if there are subscribers.
        update_interval=timedelta(seconds=scan_interval),
    )

    await coordinator.async_refresh()

    for unit in units:
        casambi_light = CasambiLight(coordinator, unit.unique_id, unit,
                                     controller, hass)
        async_add_entities([casambi_light], True)

        casambi_controller.units[casambi_light.unique_id] = casambi_light

    # add entity service to turn on Casambi light
    platform = entity_platform.async_get_current_platform()
    platform.async_register_entity_service(
        SERVICE_CASAMBI_LIGHT_TURN_ON,
        {
            vol.Required(ATTR_BRIGHTNESS):
            vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
            vol.Optional(ATTR_DISTRIBUTION):
            vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
        },
        "async_handle_entity_service_light_turn_on",
    )

    return True
예제 #5
0
async def async_setup_platform(hass: HomeAssistant,
                               config: dict,
                               async_add_entities,
                               discovery_info=None):
    """
    Setup Casambi platform
    """
    user_password = config[CONF_USER_PASSWORD]
    network_password = config[CONF_NETWORK_PASSWORD]
    email = config[CONF_EMAIL]
    api_key = config[CONF_API_KEY]

    network_timeout = DEFAULT_NETWORK_TIMEOUT
    scan_interval = DEFAULT_POLLING_TIME

    if CONF_NETWORK_TIMEOUT in config:
        network_timeout = config[CONF_NETWORK_TIMEOUT]

    if CONF_SCAN_INTERVAL in config:
        scan_interval = config[CONF_SCAN_INTERVAL]

    sslcontext = ssl.create_default_context()
    session = aiohttp_client.async_get_clientsession(hass)

    if CONF_CONTROLLER in hass.data[DOMAIN]:
        dbg_msg = "async_setup_platform CasambiController already created!"
        _LOGGER.debug(dbg_msg)
        return

    hass.data[DOMAIN][CONF_CONTROLLER] = CasambiController(hass)
    casambi_controller = hass.data[DOMAIN][CONF_CONTROLLER]

    controller = aiocasambi.Controller(
        email=email,
        user_password=user_password,
        network_password=network_password,
        api_key=api_key,
        websession=session,
        sslcontext=sslcontext,
        wire_id=WIRE_ID,
        callback=casambi_controller.signalling_callback,
        network_timeout=network_timeout,
    )

    casambi_controller.controller = controller

    try:
        with async_timeout.timeout(10):
            await controller.create_user_session()
            await controller.create_network_session()
            await controller.start_websocket()

    except aiocasambi.LoginRequired:
        _LOGGER.error("Connected to casambi but couldn't log in")
        return False

    except aiocasambi.Unauthorized:
        _LOGGER.error("Connected to casambi but not registered")
        return False

    except (asyncio.TimeoutError, aiocasambi.RequestError):
        _LOGGER.error("Error connecting to the Casambi")
        return False

    except aiocasambi.AiocasambiException:
        _LOGGER.error("Unknown Casambi communication error occurred")
        return False

    await controller.initialize()

    units = controller.get_units()

    coordinator = DataUpdateCoordinator(
        hass,
        _LOGGER,
        # Name of the data. For logging purposes.
        name="light",
        update_method=casambi_controller.async_update_data,
        # Polling interval. Will only be polled if there are subscribers.
        update_interval=timedelta(seconds=scan_interval),
    )

    await coordinator.async_refresh()

    for unit in units:
        casambi_light = CasambiLight(coordinator, unit.unique_id, unit,
                                     controller, hass)
        async_add_entities([casambi_light], True)

        casambi_controller.units[casambi_light.unique_id] = casambi_light

    @callback
    async def async_handle_platform_service_light_turn_on(
            call: ServiceCall) -> None:
        """Handle turn on of Casambi light when setup from yaml."""
        dbg_msg = f"ServiceCall {call.domain}.{call.service}, data: {call.data}"
        _LOGGER.debug(dbg_msg)

        # Check if entities were selected
        if ATTR_SERV_ENTITY_ID not in call.data:
            # service handle currently only supports selection of entities
            dbg_msg = f"ServiceCall {call.domain}.{call.service}: No entity was specified. Please specify entities instead of areas or devices."
            _LOGGER.error(dbg_msg)

            return

        # Get parameters
        _distribution = None
        _brightness = call.data[ATTR_SERV_BRIGHTNESS]
        _entity_ids = call.data[ATTR_SERV_ENTITY_ID]

        dbg_msg = f"ServiceCall {call.domain}.{call.service}: data: {call.data}, brightness: {_brightness}, entity_ids: {_entity_ids}"

        # Check if optional attribute distribution has been set
        if ATTR_SERV_DISTRIBUTION in call.data:
            _distribution = call.data[ATTR_SERV_DISTRIBUTION]
            dbg_msg += f", distribution: {_distribution}"

        _LOGGER.debug(dbg_msg)

        _units = hass.data[DOMAIN]["controller"].units

        for _entity_id in _entity_ids:
            for casambi_light in _units.values():
                if casambi_light.entity_id == _entity_id:
                    # entity found
                    params = {}
                    params["brightness"] = _brightness
                    if _distribution is not None:
                        params["distribution"] = _distribution

                    dbg_msg = f"async_handle_platform_service_light_turn_on: entity found: {_entity_id}, setting params: {params}"
                    _LOGGER.debug(dbg_msg)

                    await casambi_light.async_turn_on(**params)

    # add platform service to turn on Casambi light
    hass.services.async_register(
        DOMAIN,
        SERVICE_CASAMBI_LIGHT_TURN_ON,
        async_handle_platform_service_light_turn_on,
    )

    return True