Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No-Code Device Submission #56

Merged
merged 7 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions .github/ISSUE_TEMPLATE/new_device_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: New Device
description: Submit a new device to the library so that others don't have to configure it themselves.
title: "[Device]: "
labels: ["new-device"]
body:
- type: markdown
attributes:
value: |
The battery library is a JSON document at [custom_components/battery_notes/data/library.json](custom_components/battery_notes/data/library.json)
To contribute, submit your device details via this form and the relevant code changes will be proposed on your behalf.
Note: The manufacturer and model should be exactly what is displayed on the Device screen within Home Assistant.
To see your devices, click here:

[![Open your Home Assistant instance and show your devices.](https://my.home-assistant.io/badges/devices.svg)](https://my.home-assistant.io/redirect/devices/)

- type: input
id: manufacturer
attributes:
label: Manufacturer
description: The manufacturer should be exactly what is displayed on the Devices screen within Home Assistant.
placeholder: ex. eWeLink
validations:
required: true

- type: input
id: model
attributes:
label: Model
description: The model should be exactly what is displayed on the Devices screen within Home Assistant.
placeholder: ex. DS01
validations:
required: true

- type: input
id: battery-type
attributes:
label: Battery Type
description: When specifying battery types please use the Most Common naming for general batteries and the IEC naming for battery cells according to [Wikipedia](https://en.wikipedia.org/wiki/List_of_battery_sizes).
placeholder: ex. CR2032
validations:
required: true

- type: input
id: battery-quantity
attributes:
label: Battery Quantity
description: The battery_quantity attribute is numeric (no letters or special characters).
placeholder: ex. 1
validations:
required: true
4 changes: 2 additions & 2 deletions .github/workflows/json_validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
verify-json-validation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- name: Validate JSON
uses: docker://orrosenblatt/validate-json-action:latest
uses: ScratchAddons/validate-json-action@master
env:
INPUT_SCHEMA: ./schema.json
INPUT_JSONS: custom_components/battery_notes/data/library.json
44 changes: 26 additions & 18 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
name: "Lint"
name: Lint

on:
push:
branches:
- "main"
paths-ignore:
- 'custom_components/battery_notes/data/**'
paths:
- '**.py' # Run if pushed commits include a change to a Python (.py) file.
- '.github/workflows/*.yml' # Run if pushed commits include a change to a github actions workflow file.
- 'requirements.txt' # Run if pushed commits include a change to the Python requirements.txt file.
pull_request:
branches:
- "main"
paths-ignore:
- 'custom_components/battery_notes/data/**'
paths:
- '**.py' # Run if pushed commits include a change to a Python (.py) file.
- '.github/workflows/*.yml' # Run if pushed commits include a change to a github actions workflow file.
- 'requirements.txt' # Run if pushed commits include a change to the Python requirements.txt file.
workflow_dispatch:

jobs:
ruff:
name: "Ruff"
build:
runs-on: "ubuntu-latest"
strategy:
matrix:
python-version: ["3.11"]
steps:
- name: "Checkout the repository"
uses: "actions/checkout@v4"
- name: Checkout repo
uses: actions/checkout@v4

- name: "Set up Python"
uses: actions/setup-python@v5.0.0
with:
python-version: "3.11"
cache: "pip"
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: "Install requirements"
run: python3 -m pip install -r requirements.txt
- name: Install dependencies from requirements.txt
run: |
if [ -f requirements.txt ]; then pip3 install -r requirements.txt; fi

- name: "Run"
run: python3 -m ruff check .
- name: Analyse the code with ruff
run: |
python3 -m ruff check .
91 changes: 91 additions & 0 deletions .github/workflows/new_device.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: New Device

on:
issues:
types: [opened, edited]

jobs:
create-device-pull-request:
if: ${{ contains(github.event.issue.title , '[Device]')}}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Parse device data
id: device-data
uses: issue-ops/parser@v0
with:
body: ${{ github.event.issue.body }}
issue-form-template: new_device_request.yml

- name: Install jq
run: sudo apt install jq

- name: Set up python 3.12
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Update JSON file
id: update-json
uses: jannekem/run-python-script-action@v1
with:
script: |
import re
import json

# Load the existing JSON library file
with open("custom_components/battery_notes/data/library.json",'r') as f:
devices_json = json.loads(f.read())
devices = devices_json.get('devices')

# Remove the "battery_quantity" key from the device dictionary if it's 1
new_device = ${{ steps.device-data.outputs.json }}
# Convert battery_quantity field to a numeric
numeric_quantity = int(new_device["battery_quantity"])
del new_device["battery_quantity"]
# Add numeric "battery_quantity" key if it's more than 1
if numeric_quantity > 1:
new_device["battery_quantity"] = numeric_quantity

# Check for duplicates and replace old entry with new one
duplicate_found = False
for i, device in enumerate(devices):
if device["manufacturer"] == new_device["manufacturer"] and device["model"] == new_device["model"]:
devices[i] = new_device
duplicate_found = True
break

# If no duplicate found, add the new device to the JSON library file
if not duplicate_found:
devices.append(new_device)

# Sort the devices by manufacturer and model
devices.sort(key=lambda k: (k['manufacturer'], k['model']))

# Save manufacturer and model for later use
set_output("mm", "_".join(re.findall(r"\w+",f"{new_device['manufacturer']}{new_device['model']})".lower())))
set_output("manufacturer", "_".join(re.findall(r"\w+",f"{new_device['manufacturer']})")))
set_output("model", "_".join(re.findall(r"\w+",f"{new_device['model']})")))

with open("custom_components/battery_notes/data/library.json", "w") as f:
f.write(json.dumps(devices_json, indent=4))

- name: Rename Issue
run: |
curl --request PATCH \
--url https://api.github.com/repos/${{github.repository}}/issues/${{github.event.issue.number}} \
--header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'Content-Type: application/json' \
--data '{
"title": "Device: ${{ steps.update-json.outputs.mm }}"
}'

- name: Create pull request
uses: peter-evans/create-pull-request@v5
with:
commit-message: "Update device: ${{ steps.update-json.outputs.model }} by ${{ steps.update-json.outputs.manufacturer }}"
title: "DEVICE: ${{ steps.update-json.outputs.manufacturer }} - ${{ steps.update-json.outputs.model }}"
body: "This pull request adds or updates the device information for ${{ steps.update-json.outputs.model }} by ${{ steps.update-json.outputs.manufacturer }}\nIt closes issue #${{ github.event.issue.number }}"
branch: "device-${{ steps.update-json.outputs.mm }}"
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ battery_notes:
## Contributing to the Battery Library

The battery library is a JSON document at ```custom_components/battery_notes/data/library.json```
To contribute, fork the repository, add your device details to the JSON document and submit a pull request.
You can either raise an issue using a form or submit a pull request.

[Raise new issue for a device to be added (BETA)](https://github.com/andrew-codechimp/HA-Battery-Notes/issues/new?assignees=&labels=new-device&projects=&template=new_device_request.yml&title=%5BDevice%5D%3A+)

To contribute, via a pull request.
Fork the repository, add your device details to the JSON document and submit a pull request.
Please keep devices in alphabetical order by manufacturer/model.
The manufacturer and model should be exactly what is displayed on the Device screen within Home Assistant. The make & model names may be different between integrations such as Zigbee2MQTT and ZHA, if you see a similar device please duplicate the entry rather than changing it.

Expand Down Expand Up @@ -102,6 +107,10 @@ If you want to contribute to this please read the [Contribution guidelines](CONT

A lot of the inspiration for this integration came from the excellent [PowerCalc by bramstroker](https://github.com/bramstroker/homeassistant-powercalc), without adapting code from PowerCalc I'd never have worked out how to add additional sensors to a device.

Huge thanks to @bmos for creating the issue form & automations for adding new devices.

Thanks to everyone who has submitted devices to the library.

<!---->
[battery_notes]: https://github.com/andrew-codechimp/HA-Battery-Notes
[commits-shield]: https://img.shields.io/github/commit-activity/y/andrew-codechimp/HA-Battery-Notes.svg?style=for-the-badge
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
colorlog==6.8.0
homeassistant==2023.7.0
pip>=21.0,<23.4
ruff==0.1.7
ruff==0.1.8