Changing the Battery Notes battery sensor name unexpectedly sets it to not-visible #429
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: New Device | |
on: | |
issues: | |
types: [opened, edited] | |
jobs: | |
create-device-pull-request: | |
if: ${{ contains(github.event.issue.labels.*.name, 'new-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) | |
# 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.manufacturer }} - ${{ steps.update-json.outputs.model }}" | |
}' | |
- name: Validate JSON | |
uses: docker://orrosenblatt/validate-json-action:latest | |
env: | |
INPUT_SCHEMA: custom_components/battery_notes/schema.json | |
INPUT_JSONS: custom_components/battery_notes/data/library.json | |
- name: Create pull request | |
uses: peter-evans/create-pull-request@v6 | |
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 }}" | |
- name: Close Issue | |
run: gh issue close --comment "Thanks for the contribution. We're auto-closing this issue. If it's a new device, a pull request will be created that will be reviewed and merged." ${{github.event.issue.number}} | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |