Ejemplo n.º 1
0
    async def post(self, request) -> web.Response:
        data = await request.json()

        device_config = data.get("config")
        if device_config is None:
            response = {
                "status": "failed",
                "reason": 'Required attribute "config" was not provided',
            }
            return web.json_response(data=response, status=500)

        device_type = data.get("type")
        if device_type is None:
            response = {
                "status": "failed",
                "reason": 'Required attribute "type" was not provided',
            }
            return web.json_response(data=response, status=500)

        device_id = generate_id(device_config.get("name"))
        # Remove the device it if already exist?

        # Create the device
        _LOGGER.info(
            "Adding device of type {} with config {}".format(
                device_type, device_config
            )
        )
        device = self._ledfx.devices.create(
            id=device_id,
            type=device_type,
            config=device_config,
            ledfx=self._ledfx,
        )

        # Update and save the configuration
        self._ledfx.config["devices"].append(
            {
                "id": device.id,
                "type": device.type,
                "config": device.config,
            }
        )
        save_config(
            config=self._ledfx.config,
            config_dir=self._ledfx.config_dir,
        )

        response = {
            "status": "success",
            "device": {
                "type": device.type,
                "config": device.config,
                "id": device.id,
            },
        }
        return web.json_response(data=response, status=200)
Ejemplo n.º 2
0
    def add_service(self, zeroconf_obj, type, name):
        # DMX universe_size
        c = 510
        d = 512
        info = zeroconf_obj.get_service_info(type, name)

        if info:
            address = socket.inet_ntoa(info.addresses[0])
            url = f"http://{address}/json/info"
            # For each WLED device found, based on the WLED IPv4 address, do a GET requests
            response = requests.get(url)
            b = response.json()
            # For each WLED json response, format from WLED payload to LedFx payload.
            # Note, set universe_size to 510 if LED 170 or less, If you have more than 170 LED, set universe_size to 510
            wledled = b["leds"]
            wledname = b["name"]
            wledcount = wledled["count"]
            
            if wledcount > 170:
                unisize = c
            else:
                unisize = d

            device_id = generate_id(wledname)
            device_type = "e131"
            device_config = {
                "max_brightness": 1,
                "refresh_rate": 60,
                "universe": 1,
                "universe_size": unisize,
                "name": wledname,
                "pixel_count": wledcount,
                "ip_address": address
            }

            # Check this device doesn't share IP with any other device
            for device in self._ledfx.devices.values():
                if device.config["ip_address"] == address:
                    return

            # Create the device
            _LOGGER.info("Adding device of type {} with config {}".format(device_type, device_config))
            device = self._ledfx.devices.create(
                id = device_id,
                type = device_type,
                config = device_config,
                ledfx = self._ledfx)

            # Update and save the configuration
            self._ledfx.config['devices'].append({'id': device.id, 'type': device.type, 'config': device.config })
            save_config(
                config = self._ledfx.config, 
                config_dir = self._ledfx.config_dir)
Ejemplo n.º 3
0
    async def post(self, request) -> web.Response:
        data = await request.json()

        device_config = data.get('config')
        if device_config is None:
            response = {
                'status': 'failed',
                'reason': 'Required attribute "config" was not provided'
            }
            return web.Response(text=json.dumps(response), status=500)

        device_type = data.get('type')
        if device_type is None:
            response = {
                'status': 'failed',
                'reason': 'Required attribute "type" was not provided'
            }
            return web.Response(text=json.dumps(response), status=500)

        device_id = generate_id(device_config.get('name'))
        # Remove the device it if already exist?

        # Create the device
        _LOGGER.info("Adding device of type {} with config {}".format(
            device_type, device_config))
        device = self._ledfx.devices.create(id=device_id,
                                            type=device_type,
                                            config=device_config,
                                            ledfx=self._ledfx)

        # Update and save the configuration
        self._ledfx._config['devices'].append({
            'id': device.id,
            'type': device.type,
            'config': device.config
        })
        save_config(config=self._ledfx._config,
                    config_dir=self._ledfx._config_dir)

        response = {
            'status': 'success',
            'device': {
                'type': device.type,
                'config': device.config,
                'id': device.id
            }
        }
        return web.Response(text=json.dumps(response), status=200)
Ejemplo n.º 4
0
    async def post(self, request) -> web.Response:
        """Save current effects of devices as a scene"""
        data = await request.json()

        scene_name = data.get("name")
        if scene_name is None:
            response = {
                "status": "failed",
                "reason": 'Required attribute "scene_name" was not provided',
            }
            return web.json_response(data=response, status=500)

        scene_id = generate_id(scene_name)

        scene_config = {}
        scene_config["name"] = scene_name
        scene_config["devices"] = {}
        for device in self._ledfx.devices.values():
            effect = {}
            if device.active_effect:
                effect["type"] = device.active_effect.type
                effect["config"] = device.active_effect.config
                # effect['name'] = device.active_effect.name
            scene_config["devices"][device.id] = effect

        # Update the scene if it already exists, else create it
        self._ledfx.config["scenes"][scene_id] = scene_config

        save_config(
            config=self._ledfx.config,
            config_dir=self._ledfx.config_dir,
        )

        response = {
            "status": "success",
            "scene": {
                "id": scene_id,
                "config": scene_config
            },
        }
        return web.json_response(data=response, status=200)
Ejemplo n.º 5
0
    async def post(self, request) -> web.Response:
        """Save current effects of devices as a preset"""
        data = await request.json()

        preset_name = data.get('name')
        if preset_name is None:
            response = {
                'status': 'failed',
                'reason': 'Required attribute "preset_name" was not provided'
            }
            return web.Response(text=json.dumps(response), status=500)

        preset_id = generate_id(preset_name)

        preset_config = {}
        preset_config['name'] = preset_name
        preset_config['devices'] = {}
        for device in self._ledfx.devices.values():
            effect = {}
            if device.active_effect:
                effect['type'] = device.active_effect.type
                effect['config'] = device.active_effect.config
                #effect['name'] = device.active_effect.name
            preset_config['devices'][device.id] = effect

        # Update the preset if it already exists, else create it
        self._ledfx.config['presets'][preset_id] = preset_config

        save_config(config=self._ledfx.config,
                    config_dir=self._ledfx.config_dir)

        response = {
            'status': 'success',
            'preset': {
                'id': preset_id,
                'config': preset_config
            }
        }
        return web.Response(text=json.dumps(response), status=200)
Ejemplo n.º 6
0
    async def post(self, request) -> web.Response:
        """Save current effects of devices as a scene"""
        data = await request.json()

        scene_name = data.get('name')
        if scene_name is None:
            response = {
                'status': 'failed',
                'reason': 'Required attribute "scene_name" was not provided'
            }
            return web.json_response(data=response, status=500)

        scene_id = generate_id(scene_name)

        scene_config = {}
        scene_config['name'] = scene_name
        scene_config['devices'] = {}
        for device in self._ledfx.devices.values():
            effect = {}
            if device.active_effect:
                effect['type'] = device.active_effect.type
                effect['config'] = device.active_effect.config
                #effect['name'] = device.active_effect.name
            scene_config['devices'][device.id] = effect

        # Update the scene if it already exists, else create it
        self._ledfx.config['scenes'][scene_id] = scene_config

        save_config(config=self._ledfx.config,
                    config_dir=self._ledfx.config_dir)

        response = {
            'status': 'success',
            'scene': {
                'id': scene_id,
                'config': scene_config
            }
        }
        return web.json_response(data=response, status=200)
Ejemplo n.º 7
0
    async def post(self, request) -> web.Response:
        """Create a new integration, or update an existing one"""
        data = await request.json()

        integration_config = data.get("config")
        if integration_config is None:
            response = {
                "status": "failed",
                "reason": 'Required attribute "config" was not provided',
            }
            return web.json_response(data=response, status=500)

        integration_type = data.get("type")
        if integration_type is None:
            response = {
                "status": "failed",
                "reason": 'Required attribute "type" was not provided',
            }
            return web.json_response(data=response, status=500)

        integration_id = data.get("id")
        new = not bool(integration_id)
        if integration_id is None:
            # Create new integration if no id is given
            integration_id = generate_id(integration_config.get("name"))
            _LOGGER.info(("Creating {} integration with config {}").format(
                integration_type, integration_config))
        else:
            # Update existing integration if valid id is given
            existing_integration = self._ledfx.integrations.get(integration_id)

            if existing_integration is None:
                response = {
                    "status": "failed",
                    "reason":
                    f"Integration with id {integration_id} not found",
                }
                return web.json_response(data=response, status=500)

            _LOGGER.info(
                ("Updating {} integration '{}' with config {}").format(
                    integration_type, integration_id, integration_config))

            self._ledfx.integrations.destroy(integration_id)

        integration = self._ledfx.integrations.create(
            id=integration_id,
            type=integration_type,
            active=True,
            config=integration_config,
            data=None,
            ledfx=self._ledfx,
        )

        # Update and save the configuration
        if new:
            self._ledfx.config["integrations"].append({
                "id":
                integration.id,
                "type":
                integration.type,
                "active":
                integration.active,
                "data":
                integration.data,
                "config":
                integration.config,
            })
        else:
            for integration in self._ledfx.config["integrations"]:
                if integration["id"] == integration_id:
                    integration["config"] = integration_config
                    break
                    # Update and save the configuration

        save_config(
            config=self._ledfx.config,
            config_dir=self._ledfx.config_dir,
        )

        response = {"status": "success"}
        return web.json_response(data=response, status=200)
Ejemplo n.º 8
0
    def add_service(self, zeroconf_obj, type, name):

        _LOGGER.info("Found Device!")

        info = zeroconf_obj.get_service_info(type, name)

        if info:
            address = socket.inet_ntoa(info.addresses[0])
            hostname = str(info.server)
            url = f"http://{address}/json/info"
            # For each WLED device found, based on the WLED IPv4 address, do a
            # GET requests
            response = requests.get(url)
            b = response.json()
            # For each WLED json response, format from WLED payload to LedFx payload.
            # Note, set universe_size to 510 if LED 170 or less, If you have
            # more than 170 LED, set universe_size to 510
            wledled = b["leds"]
            wledname = b["name"]
            wledcount = wledled["count"]

            # We need to use a universe size of 510 if there are more than 170
            # pixels to prevent spanning pixel data across sequential universes
            if wledcount > 170:
                unisize = 510
            else:
                unisize = 512

            device_id = generate_id(wledname)
            device_type = "e131"
            device_config = {
                "max_brightness": 1,
                "refresh_rate": 60,
                "universe": 1,
                "universe_size": unisize,
                "name": wledname,
                "pixel_count": wledcount,
                "ip_address": hostname.rstrip("."),
            }

            # Check this device doesn't share IP, name or hostname with any current saved device
            for device in self._ledfx.devices.values():
                if (device.config["ip_address"] == hostname.rstrip(".")
                        or device.config["ip_address"] == hostname
                        or device.config["name"] == wledname
                        or device.config["ip_address"] == address):
                    return

            # Create the device
            _LOGGER.info("Adding device of type {} with config {}".format(
                device_type, device_config))
            device = self._ledfx.devices.create(
                id=device_id,
                type=device_type,
                config=device_config,
                ledfx=self._ledfx,
            )

            # Update and save the configuration
            self._ledfx.config["devices"].append({
                "id": device.id,
                "type": device.type,
                "config": device.config,
            })
            save_config(
                config=self._ledfx.config,
                config_dir=self._ledfx.config_dir,
            )