### Install CoreAPI CLI Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/langs/shell-intro.html Use this command to install the command line client via pip. ```bash # Install the command line client $ pip install coreapi-cli ``` -------------------------------- ### Install Python Client Library Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/langs/python-intro.html Install the coreapi Python client library using pip. This is required to interact with the API from Python. ```bash # Install the Python client library $ pip install coreapi ``` -------------------------------- ### Run Docker Compose Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/README.md Command to start the Docker containers defined in the docker-compose.yaml file in detached mode. ```bash docker compose up -d ``` -------------------------------- ### GET /timer/{device}/ Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Applies a timer to a specific bulb. ```APIDOC ## GET /timer/{device}/ ### Description Applies a timer to a bulb to turn it off after a specified duration. ### Method GET ### Endpoint http://localhost:8000/timer/{device}/?{minutes} ### Parameters #### Path Parameters - **device** (string) - Required - The device identifier #### Query Parameters - **minutes** (integer) - Required - Duration in minutes ``` -------------------------------- ### Get Weather Data Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Retrieve current weather or forecasts for a saved location in a format optimized for Garmin watch displays. ```bash curl -X GET "http://localhost:8000/current/home/" \ -H "Authorization: Token abc123def456..." ``` ```bash curl -X GET "http://localhost:8000/forecast/home/" \ -H "Authorization: Token abc123def456..." ``` -------------------------------- ### Get Garmin Endpoint Definitions Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Retrieve endpoint definitions formatted specifically for the Garmin HttpClient app. ```bash curl -X GET "http://localhost:8000/endpoints/" \ -H "Authorization: Token abc123def456..." ``` -------------------------------- ### Manage Timer Presets Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Create timer presets and apply them to bulbs using admin or user tokens. ```bash curl -X POST "http://localhost:8000/timers/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{"minutes": 15}' ``` ```bash curl -X GET "http://localhost:8000/timer/living_room/?15" \ -H "Authorization: Token abc123def456..." ``` -------------------------------- ### POST /timers/ Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Creates a new timer preset. ```APIDOC ## POST /timers/ ### Description Creates a new timer preset. Admin access required. ### Method POST ### Endpoint http://localhost:8000/timers/ ### Request Body - **minutes** (integer) - Required - Duration in minutes ### Request Example { "minutes": 15 } ``` -------------------------------- ### Manage Temperature Presets Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt List and create Kelvin temperature presets within the 1700K-6500K range. ```bash curl -X GET "http://localhost:8000/temperatures/" \ -H "Authorization: Token admin_token..." ``` ```bash curl -X POST "http://localhost:8000/temperatures/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{"name": "candlelight", "kelvins": 2700, "brightness": 40}' ``` -------------------------------- ### Set Brightness Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Create a brightness preset and apply it to a bulb. ```bash # Create a brightness preset (admin only) curl -X POST "http://localhost:8000/brightnesses/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{ "name": "dim", "brightness": 25 }' # Apply brightness preset to bulb curl -X GET "http://localhost:8000/brightness/living_room/?dim" \ -H "Authorization: Token abc123def456..." # Response (plain text): # Ok ``` -------------------------------- ### Set Color Temperature Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Create a temperature preset and apply it to a bulb. ```bash # Create a temperature preset (admin only) curl -X POST "http://localhost:8000/temperatures/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{ "name": "daylight", "kelvins": 5500, "brightness": 100 }' # Apply temperature preset to bulb curl -X GET "http://localhost:8000/temperature/living_room/?daylight" \ -H "Authorization: Token abc123def456..." # Response (plain text): # Ok ``` -------------------------------- ### Interact with API using CoreAPI Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/langs/python.html Initializes a client to fetch the schema and perform actions on specific endpoints. Requires the coreapi library. ```python import coreapi # Initialize a client & load the schema document client = coreapi.Client() schema = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %}) # Interact with the API endpoint action = [{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"] {% if link.fields %}params = { {% for field in link.fields %} "{{ field.name }}": ...{% if not loop.last %},{% endif %} {% endfor %}} {% endif %}result = client.action(schema, action{% if link.fields %}, params=params{% endif %}) ``` -------------------------------- ### Load API Schema Document with coreapi Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/langs/shell.html Use this command to load the API schema document. Optionally specify a format for the schema. ```bash # Load the schema document $ coreapi get {{ document.url }}{% if schema_format %} --format {{ schema_format }}{% endif %} ``` -------------------------------- ### Docker Compose Configuration Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Defines the backend service for the smart home API, including build context, environment variables, and volume mounts for the database. ```yaml services: backend: container_name: garlight build: . environment: PORT: 8000 CSRF_ORIGIN: http://localhost:8000 SECRET_KEY: your-secure-secret-key-here WEATHER_API_KEY: your-weatherapi-com-key DEVELOPER: "False" DJANGO_SUPERUSER_EMAIL: admin@example.com DJANGO_SUPERUSER_USERNAME: admin DJANGO_SUPERUSER_PASSWORD: securepassword network_mode: host volumes: - /path/to/database:/app/home_api/database ``` -------------------------------- ### Docker Compose Configuration for Smart Home API Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/README.md This configuration sets up the backend service for the Smart Home API using Docker. Adjust volume paths and environment variables as needed for your deployment. Ensure all required environment variables are set correctly. ```yaml services: backend: container_name: garlight build: . environment: PORT: 8000 CSRF_ORIGIN: http://localhost:${PORT} SECRET_KEY: T0pS3cReT WEATHER_API_KEY: 112mn12sda3mfd DEVELOPER: "False" DJANGO_SUPERUSER_EMAIL: admin@example.com DJANGO_SUPERUSER_USERNAME: admin DJANGO_SUPERUSER_PASSWORD: admin network_mode: host volumes: - /path/database:/app/home_api/database ``` -------------------------------- ### Set Bulb Color Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Create a color preset and apply it to a bulb using the preset name. ```bash # Create a color preset (admin only) curl -X POST "http://localhost:8000/colors/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{ "name": "warm", "r": 255, "g": 180, "b": 100, "brightness": 80 }' # Apply color preset to bulb (query parameter is the preset name) curl -X GET "http://localhost:8000/color/living_room/?warm" \ -H "Authorization: Token abc123def456..." # Response (plain text): # Ok ``` -------------------------------- ### Authentication Methods Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/sidebar.html Overview of the supported authentication mechanisms for the Smart Home API. ```APIDOC ## Authentication ### Description The API supports multiple authentication methods to secure endpoints. Users must authenticate to access protected resources. ### Supported Methods - **None**: Publicly accessible endpoints. - **Token**: Bearer token authentication. - **Basic**: Standard HTTP Basic authentication. - **Session**: Session-based authentication for logged-in users. ``` -------------------------------- ### Create Admin User in Docker Source: https://github.com/mateuszbaranczyk/smart-home-api/wiki/Prepare Execute this command within the Docker container to initialize a superuser account for administrative access. ```bash docker exec -it python manage.py createsuperuser ``` -------------------------------- ### Authenticate and Retrieve API Token Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Perform login to obtain a session, retrieve an API token, and use it for subsequent authenticated requests. ```bash # Step 1: Login via browser or POST request curl -X POST "http://localhost:8000/login/" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=deviceuser&password=yourpassword&remember_me=false" # Step 2: Get API token (requires session authentication) curl -X GET "http://localhost:8000/token/" \ -H "Cookie: sessionid=your_session_id" # Response: # {"token": "Token abc123def456..."} # Step 3: Use token for subsequent API calls curl -X GET "http://localhost:8000/on-off/living_room/" \ -H "Authorization: Token abc123def456..." ``` -------------------------------- ### Manage Custom Endpoints Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Create and list endpoints that map devices to specific actions for the Garmin watch app. ```bash curl -X POST "http://localhost:8000/endpoint/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{ "name": "Warm Light", "action": "color", "device": 1, "preset": "warm" }' ``` ```bash curl -X GET "http://localhost:8000/endpoint/" \ -H "Authorization: Token admin_token..." ``` -------------------------------- ### Manage Weather Locations Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Create, list, and retrieve weather locations using geographic coordinates. ```bash curl -X POST "http://localhost:8000/location/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{"name": "home", "lat": 51.5074, "lon": -0.1278}' ``` ```bash curl -X GET "http://localhost:8000/location/" \ -H "Authorization: Token admin_token..." ``` ```bash curl -X GET "http://localhost:8000/location/home/" \ -H "Authorization: Token admin_token..." ``` -------------------------------- ### Authentication API Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Handles user login and API token generation for authenticated access to the Smart Home API. ```APIDOC ## Authentication API ### Description Handles user login and API token generation for authenticated access to the Smart Home API. ### Method POST ### Endpoint /login/ ### Parameters #### Request Body - **username** (string) - Required - The username for login. - **password** (string) - Required - The password for login. - **remember_me** (boolean) - Optional - Whether to remember the user's session. ### Method GET ### Endpoint /token/ ### Description Retrieves an API token after successful session authentication. ### Parameters #### Headers - **Cookie** (string) - Required - The session ID obtained from the login process. ### Response #### Success Response (200) - **token** (string) - The generated API token. ### Request Example ```bash # Step 1: Login via browser or POST request curl -X POST "http://localhost:8000/login/" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=deviceuser&password=yourpassword&remember_me=false" # Step 2: Get API token (requires session authentication) curl -X GET "http://localhost:8000/token/" \ -H "Cookie: sessionid=your_session_id" ``` ### Response Example ```json { "token": "Token abc123def456..." } ``` ``` -------------------------------- ### Initialize and call API endpoint with CoreAPI Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/langs/javascript.html Uses the global coreapi and schema objects to perform an action on a specific API endpoint. Ensure coreapi.js and schema.js are loaded before execution. ```javascript var coreapi = window.coreapi // Loaded by `coreapi.js` var schema = window.schema // Loaded by `schema.js` // Initialize a client var client = new coreapi.Client() // Interact with the API endpoint var action = [{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"] {% if link.fields %}var params = { {% for field in link.fields %} {{ field.name }}: ...{% if not loop.last %},{% endif %} {% endfor %}} {% endif %}client.action(schema, action{% if link.fields %}, params{% endif %}).then(function(result) { // Return value is in 'result' }) ``` -------------------------------- ### List and Update Bulbs Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Retrieve registered bulbs or update a bulb's name for identification. ```bash # List all registered bulbs curl -X GET "http://localhost:8000/bulbs/" \ -H "Authorization: Token abc123def456..." # Update bulb name for easier identification curl -X PATCH "http://localhost:8000/bulbs/0x00000000abcd1234/" \ -H "Authorization: Token abc123def456..." \ -H "Content-Type: application/json" \ -d '{"name": "living_room"}' # Response: # { # "id": 1, # "bulb_id": "0x00000000abcd1234", # "ip": "192.168.1.100", # "name": "living_room", # "urls": [] # } ``` -------------------------------- ### Interact with API Endpoint using coreapi Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/langs/shell.html Use this command to interact with API endpoints. Provide the section key, link key, and any required fields as parameters. ```bash # Interact with the API endpoint $ coreapi action {% if section_key %}{{ section_key }} {% endif %}{{ link_key|cut:"> " }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %} ``` -------------------------------- ### Manage Color Presets Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Perform CRUD operations on RGB color presets with validation for color values and brightness. ```bash curl -X GET "http://localhost:8000/colors/" \ -H "Authorization: Token admin_token..." ``` ```bash curl -X POST "http://localhost:8000/colors/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{"name": "sunset", "r": 255, "g": 100, "b": 50, "brightness": 70}' ``` ```bash curl -X PUT "http://localhost:8000/colors/1/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{"name": "warm", "r": 255, "g": 200, "b": 120, "brightness": 85}' ``` ```bash curl -X DELETE "http://localhost:8000/colors/1/" \ -H "Authorization: Token admin_token..." ``` -------------------------------- ### Device Management API Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Endpoints for discovering, listing, and updating Yeelight bulbs registered with the Smart Home API. ```APIDOC ## Device Management API ### Discover Yeelight Bulbs #### Description Scans the local network for Yeelight devices with LAN mode enabled and automatically registers them in the database. ### Method GET ### Endpoint /bulbs/discover/ ### Parameters #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Response #### Success Response (200) - **Redirects** to /bulbs/ with a list of discovered devices. #### Response Example ```json [ { "id": 1, "bulb_id": "0x00000000abcd1234", "ip": "192.168.1.100", "name": "0x00000000abcd1234", "urls": ["/on-off/living_room/?power", "/color/living_room/?warm"] } ] ``` ### List and Update Bulbs #### Description Retrieve all registered bulbs or update a specific bulb's name for easier identification. ### Method GET ### Endpoint /bulbs/ ### Parameters #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Method PATCH ### Endpoint /bulbs/{bulb_id}/ ### Parameters #### Path Parameters - **bulb_id** (string) - Required - The unique identifier of the bulb. #### Headers - **Authorization** (string) - Required - API Token for authentication. #### Request Body - **name** (string) - Required - The new name for the bulb. ### Response #### Success Response (200) - **id** (integer) - The internal ID of the bulb. - **bulb_id** (string) - The unique identifier of the bulb. - **ip** (string) - The IP address of the bulb. - **name** (string) - The name of the bulb. - **urls** (array) - List of available control URLs for the bulb. ### Request Example ```bash # List all registered bulbs curl -X GET "http://localhost:8000/bulbs/" \ -H "Authorization: Token abc123def456..." # Update bulb name for easier identification curl -X PATCH "http://localhost:8000/bulbs/0x00000000abcd1234/" \ -H "Authorization: Token abc123def456..." \ -H "Content-Type: application/json" \ -d '{"name": "living_room"}' ``` ### Response Example ```json { "id": 1, "bulb_id": "0x00000000abcd1234", "ip": "192.168.1.100", "name": "living_room", "urls": [] } ``` ``` -------------------------------- ### Initialize Authentication State Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/index.html Sets the global authentication type and updates the UI to reflect the active session state. ```javascript window.auth = { 'type': 'session', }; $('#selected-authentication').text('session'); $('#auth-control').children().removeClass('active'); $('#auth-control').find("[data-auth='session']").closest('li').addClass('active'); ``` -------------------------------- ### SmartBulb Python Class - Direct Control Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Provides methods to interact with Yeelight bulbs directly using the SmartBulb Python class. Ensure the YeelightBulb model is retrieved from the database before instantiation. ```python from garlight.bulbs import SmartBulb from garlight.models import YeelightBulb, Color, Temperature # Get bulb from database db_bulb = YeelightBulb.objects.get(name="living_room") bulb = SmartBulb(db_bulb) # Check current state state = bulb.check_state() # Returns: "on", "off", or "offline" print(f"Bulb state: {state}") # Toggle power result = bulb.on_off() # Returns: "Power on", "Power off", or "Offline" # Set RGB color with brightness color = Color(name="custom", r=255, g=100, b=50, brightness=80) bulb.set_color(color) # Returns: "Ok" or "Failed" # Set color temperature (1700K-6500K) temp = Temperature(name="custom", kelvins=4000, brightness=100) bulb.set_temperature(temp) # Returns: "Ok" or "Failed" # Set brightness directly (1-100) bulb.set_brightness(50) # Returns: "Ok" or "Failed" # Set auto-off timer bulb.set_timer(minutes=30) # Returns: "Timer to 30 min." or "Failed" ``` -------------------------------- ### Dynamic Link Request Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/interact.html Handles the submission of data for a specific link within the Smart Home API. ```APIDOC ## POST /api/links/{link_key} ### Description Submits data to a specific link endpoint defined in the Smart Home API. ### Method POST ### Endpoint /api/links/{link_key} ### Parameters #### Path Parameters - **link_key** (string) - Required - The unique identifier for the link. ### Request Example { "data": "raw_data_payload" } ### Response #### Success Response (200) - **status** (string) - Indicates the request was processed successfully. ``` -------------------------------- ### Temperature Presets Management Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt CRUD operations for Kelvin temperature presets. ```APIDOC ## GET /temperatures/ ### Description List all temperature presets. ## POST /temperatures/ ### Description Create a new temperature preset. - **name** (string) - Required - **kelvins** (integer) - Required - 1700-6500 - **brightness** (integer) - Required - 1-100 ``` -------------------------------- ### Basic Authentication Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/auth/basic.html Provides credentials for basic authentication. ```APIDOC ## Basic Authentication ### Description Use Basic Authentication by providing your username and password. ### Method POST (typically, though not explicitly stated, authentication is often done via POST) ### Endpoint /api/auth/token (inferred, as no specific endpoint is provided) ### Parameters #### Request Body - **username** (string) - Required - Your username for authentication. - **password** (string) - Required - Your password for authentication. ### Request Example ```json { "username": "your_username", "password": "your_password" } ``` ### Response #### Success Response (200) - **token** (string) - The authentication token. #### Response Example ```json { "token": "your_auth_token" } ``` ``` -------------------------------- ### Weather Integration Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Manage weather locations and retrieve conditions. ```APIDOC ## POST /location/ ### Description Create a weather location. ## GET /current/{location}/ ### Description Get current weather for a location. ## GET /forecast/{location}/ ### Description Get weather forecast for a location. ``` -------------------------------- ### Color Presets Management Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt CRUD operations for RGB color presets. ```APIDOC ## GET /colors/ ### Description List all color presets. ## POST /colors/ ### Description Create a new color preset. - **name** (string) - Required - **r** (integer) - Required - 0-255 - **g** (integer) - Required - 0-255 - **b** (integer) - Required - 0-255 - **brightness** (integer) - Required - 1-100 ## PUT /colors/{id}/ ### Description Update an existing color preset. ## DELETE /colors/{id}/ ### Description Delete a color preset. ``` -------------------------------- ### Discover Yeelight Bulbs Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Scan the local network for Yeelight devices with LAN mode enabled and register them. ```bash # Discover and register all Yeelight bulbs on the network curl -X GET "http://localhost:8000/bulbs/discover/" \ -H "Authorization: Token abc123def456..." # Response: Redirects to /bulbs/ with list of discovered devices # [ # { # "id": 1, # "bulb_id": "0x00000000abcd1234", # "ip": "192.168.1.100", # "name": "0x00000000abcd1234", # "urls": ["/on-off/living_room/?power", "/color/living_room/?warm"] # } # ] ``` -------------------------------- ### Toggle Bulb Power Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Toggle the power state of a bulb, returning the new state as plain text. ```bash # Toggle bulb power (on -> off, off -> on) curl -X GET "http://localhost:8000/on-off/living_room/" \ -H "Authorization: Token abc123def456..." # Response (plain text): # Power on # -- or -- # Power off # -- or -- # Offline ``` -------------------------------- ### Interact API Endpoint Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/link.html This section details how to interact with various API endpoints, including path, query, header, and body parameters. ```APIDOC ## INTERACT /mateuszbaranczyk/smart-home-api ### Description Interact with the Smart Home API. ### Method POST ### Endpoint /mateuszbaranczyk/smart-home-api ### Parameters #### Path Parameters - **param1** (type) - Required/Optional - Description #### Query Parameters - **param1** (type) - Required/Optional - Description #### Header Parameters - **param1** (type) - Required/Optional - Description #### Request Body - **field1** (type) - Required/Optional - Description ### Request Example ```json { "example": "request body" } ``` ### Response #### Success Response (200) - **field1** (type) - Description #### Response Example ```json { "example": "response body" } ``` ``` -------------------------------- ### Endpoint Configuration Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Manage custom endpoints for the Garmin watch app. ```APIDOC ## POST /endpoint/ ### Description Create an endpoint mapping a device to an action and preset. ## GET /endpoints/ ### Description Returns endpoint definitions formatted for the Garmin HttpClient app. ``` -------------------------------- ### Disable authentication and permissions for docs Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/error.html Use this configuration in your urls.py to bypass authentication and permission checks when serving API documentation. ```python path('docs/', include_docs_urls(title='Your API', authentication_classes=[], permission_classes=[])) ``` -------------------------------- ### Bulb Control Actions API Source: https://context7.com/mateuszbaranczyk/smart-home-api/llms.txt Endpoints for controlling various aspects of Yeelight bulbs, including power, color, temperature, brightness, and timers. ```APIDOC ## Bulb Control Actions API ### Power On/Off Toggle #### Description Toggles the power state of a Yeelight bulb. Returns the new state as plain text for Garmin watch compatibility. ### Method GET ### Endpoint /on-off/{bulb_name}/ ### Parameters #### Path Parameters - **bulb_name** (string) - Required - The name of the bulb to control. #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Response #### Success Response (200) - Returns plain text indicating the new power state ('Power on', 'Power off', or 'Offline'). ### Request Example ```bash # Toggle bulb power (on -> off, off -> on) curl -X GET "http://localhost:8000/on-off/living_room/" \ -H "Authorization: Token abc123def456..." ``` ### Power On/Off Toggle #### Description Toggles the power state of a Yeelight bulb. Returns the new state as plain text for Garmin watch compatibility. ### Method GET ### Endpoint /on-off/{bulb_name}/ ### Parameters #### Path Parameters - **bulb_name** (string) - Required - The name of the bulb to control. #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Response #### Success Response (200) - Returns plain text indicating the new power state ('Power on', 'Power off', or 'Offline'). ### Request Example ```bash # Toggle bulb power (on -> off, off -> on) curl -X GET "http://localhost:8000/on-off/living_room/" \ -H "Authorization: Token abc123def456..." ``` ### Set Bulb Color #### Description Sets the bulb to a predefined RGB color with brightness. Requires a color preset to be created first. ### Method POST ### Endpoint /colors/ ### Parameters #### Headers - **Authorization** (string) - Required - API Token for authentication (admin token required). #### Request Body - **name** (string) - Required - The name of the color preset. - **r** (integer) - Required - Red component (0-255). - **g** (integer) - Required - Green component (0-255). - **b** (integer) - Required - Blue component (0-255). - **brightness** (integer) - Required - Brightness level (1-100). ### Method GET ### Endpoint /color/{bulb_name}/ ### Parameters #### Path Parameters - **bulb_name** (string) - Required - The name of the bulb to control. #### Query Parameters - **color_preset_name** (string) - Required - The name of the color preset to apply. #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Response #### Success Response (200) - Returns 'Ok' on success. ### Request Example ```bash # Create a color preset (admin only) curl -X POST "http://localhost:8000/colors/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{ "name": "warm", "r": 255, "g": 180, "b": 100, "brightness": 80 }' # Apply color preset to bulb (query parameter is the preset name) curl -X GET "http://localhost:8000/color/living_room/?warm" \ -H "Authorization: Token abc123def456..." ``` ### Set Color Temperature #### Description Sets the bulb to a specific Kelvin temperature (1700-6500K) with brightness level. ### Method POST ### Endpoint /temperatures/ ### Parameters #### Headers - **Authorization** (string) - Required - API Token for authentication (admin token required). #### Request Body - **name** (string) - Required - The name of the temperature preset. - **kelvins** (integer) - Required - Kelvin temperature (1700-6500). - **brightness** (integer) - Required - Brightness level (1-100). ### Method GET ### Endpoint /temperature/{bulb_name}/ ### Parameters #### Path Parameters - **bulb_name** (string) - Required - The name of the bulb to control. #### Query Parameters - **temperature_preset_name** (string) - Required - The name of the temperature preset to apply. #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Response #### Success Response (200) - Returns 'Ok' on success. ### Request Example ```bash # Create a temperature preset (admin only) curl -X POST "http://localhost:8000/temperatures/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{ "name": "daylight", "kelvins": 5500, "brightness": 100 }' # Apply temperature preset to bulb curl -X GET "http://localhost:8000/temperature/living_room/?daylight" \ -H "Authorization: Token abc123def456..." ``` ### Set Brightness #### Description Adjusts the bulb brightness to a predefined level (1-100%). ### Method POST ### Endpoint /brightnesses/ ### Parameters #### Headers - **Authorization** (string) - Required - API Token for authentication (admin token required). #### Request Body - **name** (string) - Required - The name of the brightness preset. - **brightness** (integer) - Required - Brightness level (1-100). ### Method GET ### Endpoint /brightness/{bulb_name}/ ### Parameters #### Path Parameters - **bulb_name** (string) - Required - The name of the bulb to control. #### Query Parameters - **brightness_preset_name** (string) - Required - The name of the brightness preset to apply. #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Response #### Success Response (200) - Returns 'Ok' on success. ### Request Example ```bash # Create a brightness preset (admin only) curl -X POST "http://localhost:8000/brightnesses/" \ -H "Authorization: Token admin_token..." \ -H "Content-Type: application/json" \ -d '{ "name": "dim", "brightness": 25 }' # Apply brightness preset to bulb curl -X GET "http://localhost:8000/brightness/living_room/?dim" \ -H "Authorization: Token abc123def456..." ``` ### Set Sleep Timer #### Description Sets an auto-off timer for the bulb in minutes. ### Method GET ### Endpoint /timer/{bulb_name}/ ### Parameters #### Path Parameters - **bulb_name** (string) - Required - The name of the bulb to control. #### Query Parameters - **minutes** (integer) - Required - The number of minutes until the bulb turns off. #### Headers - **Authorization** (string) - Required - API Token for authentication. ### Response #### Success Response (200) - Returns 'Ok' on success. ### Request Example ```bash # Set a 30-minute sleep timer for the living room bulb curl -X GET "http://localhost:8000/timer/living_room/?minutes=30" \ -H "Authorization: Token abc123def456..." ``` ``` -------------------------------- ### Django Template Tags for Iteration and Formatting Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/admin/dict_value.html This snippet demonstrates the use of Django template tags for iterating over dictionary items and formatting their keys and values. Ensure the 'rest_framework' app is loaded. ```django {% load rest_framework %} {% for k, v in value|items %} {% endfor %} {{ k|format_value }} {{ v|format_value }} ``` -------------------------------- ### Load Django REST Framework Tags Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/admin/list_value.html Load the rest_framework template tags to enable their use in Django templates. ```django {% load rest_framework %} ``` -------------------------------- ### Toggle Code Block Visibility Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/docs/index.html Filters and displays code blocks based on the selected language. ```javascript $('pre.highlight').filter('[data-language="{{ langs | first }}"]').removeClass('hide'); ``` -------------------------------- ### Render Django Form Field Components Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/vertical/checkbox.html Use this template logic to display field labels, error messages, and help text. Ensure the field object is passed from the view context. ```django {% if field.label %}{{ field.label }}{% endif %} {% if field.errors %} {% for error in field.errors %} {{ error }} {% endfor %} {% endif %} {% if field.help_text %} {{ field.help_text|safe }} {% endif %} ``` -------------------------------- ### Render Form Fields in Django Template Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/raw_data_form.html Use this template code to iterate over form fields and render their labels, input elements, and help text within a Django template. Ensure the `rest_framework` template tags are loaded. ```django {% load rest_framework %} {{ form.non_field_errors }} {% for field in form %} {{ field.label_tag|add_class:"col-sm-2 control-label" }} {{ field|add_class:"form-control" }} {{ field.help_text|safe }} {% endfor %} ``` -------------------------------- ### Render Form Fields in Django Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/inline/form.html This snippet iterates over form fields and renders only the non-read-only fields using Django's rest_framework template tags. Ensure 'rest_framework' is loaded. ```django {% load rest_framework %} {% for field in form %} {% if not field.read_only %} {% render_field field style=style %} {% endif %} {% endfor %} ``` -------------------------------- ### Render Form Field with Label Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/horizontal/fieldset.html Loads Django REST framework template tags. Renders a form field, including its label if present. Iterates over nested fields and renders them if they are not read-only. ```django {% load rest_framework %} {% if field.label %} {{ field.label }} {% endif %} {% for nested_field in field %} {% if not nested_field.read_only %} {% render_field nested_field style=style %} {% endif %} {% endfor %} ``` -------------------------------- ### Iterate Over a List in Django Template Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/admin/list_value.html Use the 'for' tag to loop through a list of items in a Django template. Access the current item's index using forloop.counter0. ```django {% for item in value %}{% endfor %} ``` ```django {{ forloop.counter0 }} ``` -------------------------------- ### Format Value in Django Template Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/admin/list_value.html Apply a custom filter 'format_value' to an item within a Django template for specific data presentation. ```django {{ item|format_value }} ``` -------------------------------- ### Render nested fields in Django template Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/inline/fieldset.html Iterates through nested fields and renders them using the render_field tag if they are not read-only. ```html {% load rest_framework %} {% for nested_field in field %} {% if not nested_field.read_only %} {% render_field nested_field style=style %} {% endif %} {% endfor %} ``` -------------------------------- ### Render Form Fields with Django Template Tags Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/vertical/fieldset.html Use the rest_framework template tags to load and render form fields within a Django template. This is useful for dynamically displaying form elements based on field properties like labels and read-only status. ```django {% load rest_framework %} {% if field.label %} {{ field.label }} {% endif %} {% for nested_field in field %} {% if not nested_field.read_only %} {% render_field nested_field style=style %} {% endif %} {% endfor %} ``` -------------------------------- ### Django Template Tag for Form Field Rendering Source: https://github.com/mateuszbaranczyk/smart-home-api/blob/master/home_api/authentication/templates/rest_framework/horizontal/checkbox_multiple.html This Django template tag is used to render form fields, including their labels, choices, errors, and help text. It handles both inline and block rendering of choices. ```Django Template {% load rest_framework %} {% if field.label %} {{ field.label }} {% endif %} {% if style.inline %} {% for key, text in field.choices|items %} {{ text }} {% endfor %} {% else %} {% for key, text in field.choices|items %} {{ text }} {% endfor %} {% endif %} {% if field.errors %} {% for error in field.errors %} {{ error }} {% endfor %} {% endif %} {% if field.help_text %} {{ field.help_text|safe }} {% endif %} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.