[External Converter] TS0601 from _TZE200_gnw1rril #87
This file contains hidden or 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: External Converter Issue | |
on: | |
issues: | |
types: [opened, edited] | |
permissions: | |
issues: write | |
contents: read | |
jobs: | |
comment: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Comment on external converter | |
uses: actions/github-script@v8 | |
with: | |
script: | | |
const issue = context.payload.issue; | |
// Only proceed if the issue title starts with "[External Converter]" | |
if (!issue.title || !issue.title.startsWith("[External Converter]")) { | |
return; | |
} | |
// Hide previous bot comments | |
const comments = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number, | |
}); | |
for (const comment of comments.data) { | |
if (comment.user.type === 'Bot' && comment.user.login === 'github-actions[bot]') { | |
await github.graphql(` | |
mutation { | |
minimizeComment(input: {subjectId: "${comment.node_id}", classifier: OUTDATED}) { | |
clientMutationId | |
} | |
} | |
`); | |
} | |
} | |
// Check if Tuya manufacturer name is already supported. | |
const tuyaManufacturerNameRe = /['"](_T\w+_(\w+))['"]/g; | |
const tuyaManufacturerNames = Array.from(issue.body.matchAll(tuyaManufacturerNameRe), m => [m[1], m[2]]); | |
if (tuyaManufacturerNames.length > 0) { | |
// Checkout the repo and grep for the manufacturer name | |
const execSync = require('child_process').execSync; | |
execSync(`git clone https://github.com/Koenkk/zigbee-herdsman-converters.git zhc`, {stdio: 'ignore'}); | |
for (const [fullName, partialName] of tuyaManufacturerNames) { | |
const fullMatch = (() => { try { return execSync(`grep -r --include="*.ts" "${fullName}" zhc`, {encoding: 'utf8'}); } catch { return undefined; } })(); | |
if (fullMatch) { | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number, | |
body: [ | |
`👋 Hi there! The Tuya device with manufacturer name \`${fullName}\` is already supported in the latest dev branch.`, | |
"See this [guide](https://www.zigbee2mqtt.io/advanced/more/switch-to-dev-branch.html) on how to update, after updating you can remove your external converter.", | |
"", | |
"In case you created the external converter with the goal to extend or fix an issue with the out-of-the-box support, please submit a pull request.", | |
"For instructions on how to create a pull request see the [docs](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html#_4-create-a-pull-request).", | |
"If you need help with the process, feel free to ask here and we'll be happy to assist." | |
].join('\n') | |
}); | |
await github.rest.issues.update({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number, | |
state: "closed", | |
}); | |
return; | |
} | |
const partialMatch = (() => { try { return execSync(`grep -r --include="*.ts" "${partialName}" zhc`, {encoding: 'utf8'}); } catch { return undefined; } })(); | |
if (partialMatch) { | |
const candidates = Array.from(partialMatch.matchAll(tuyaManufacturerNameRe), m => m[1]); | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number, | |
body: [ | |
`👋 Hi there! A similar Tuya device of which the manufacturer name also ends with \`_${partialName}\` is already supported.`, | |
"This means the device can probably be easily be supported by re-using the existing converter.", | |
"", | |
"I found the following matches: " + candidates.map((c) => `\`${c}\``).join(', '), | |
`Try to stop Z2M, change all occurrences of \`${fullName}\` in the \`data/database.db\` to one of the matches above and start Z2M.`, | |
"Let us know if it works so we can support this device out-of-the-box!", | |
].join('\n') | |
}); | |
return; | |
} | |
} | |
} | |
// Check for placeholder text | |
const placeholderText = '<!-- REPLACE THIS LINE WITH YOUR EXTERNAL CONVERTER\'S CODE -->'; | |
if (issue.body && issue.body.includes(placeholderText)) { | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number, | |
body: [ | |
"👋 Hi there! It looks like you haven't replaced the placeholder text with your external converter code.", | |
"", | |
"Please replace this line:", | |
"```", | |
"<!-- REPLACE THIS LINE WITH YOUR EXTERNAL CONVERTER'S CODE -->", | |
"```", | |
"", | |
"With your actual external converter code.", | |
"Read the [docs](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html#_2-creating-the-external-definition) on how to retrieve the external converter." | |
].join('\n') | |
}); | |
return; | |
} | |
// Create a request to pull request comment | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number, | |
body: [ | |
"🙏 Thank you for creating this issue and sharing your external converter!", | |
"", | |
"In case all features work, please submit a pull request on this repository so the device can be supported out-of-the-box with the next release.", | |
"For instructions on how to create a pull request see the [docs](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html#_4-create-a-pull-request).", | |
"", | |
"If **NOT** all features work, please follow the [How To Support new devices](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html)", | |
tuyaManufacturerNames.length > 0 ? "Since this is a Tuya also consider [How To Support new Tuya devices](https://www.zigbee2mqtt.io/advanced/support-new-devices/02_support_new_tuya_devices.html)" : "", | |
"", | |
"If you need help with the process, feel free to ask here and we'll be happy to assist." | |
].join('\n') | |
}); |