Skip to content

Commit

Permalink
Setup CI/CD
Browse files Browse the repository at this point in the history
  • Loading branch information
bokub committed Aug 30, 2023
1 parent 9d1e899 commit 3974851
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 21 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: CI/CD

on:
- push
- pull_request
- workflow_dispatch

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npm run lint

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm ci
- run: npm run build

build:
name: Build Docker image
runs-on: ubuntu-latest
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
needs:
- lint
- test
steps:
- uses: actions/checkout@v3
- uses: home-assistant/builder@master
with:
args: |
--test \
--all \
--target . \
--docker-hub ghcr.io/bokub
publish:
name: Build and publish Docker image
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/') # Invert
needs:
- lint
- test
steps:
- uses: actions/checkout@v3
- uses: docker/login-action@v2
with:
registry: ghcr.io
username: bokub
password: ${{ secrets.CR_PAT }}
- uses: home-assistant/builder@master
with:
args: |
--all \
--target . \
--docker-hub ghcr.io/bokub
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
docker run --rm --privileged \
-v ~/.docker:/root/.docker \
-v ./:/data \
homeassistant/amd64-builder --all -t /data
homeassistant/amd64-builder --all -t /data --test --docker-hub ghcr.io/bokub
```
10 changes: 6 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
ARG BUILD_FROM
FROM $BUILD_FROM

RUN \
apk add --no-cache \
nodejs npm
LABEL org.opencontainers.image.source=https://github.com/bokub/ha-linky
LABEL org.opencontainers.image.description="HA Linky Add-on"
LABEL org.opencontainers.image.licenses=MIT

RUN apk add --no-cache nodejs npm

WORKDIR /linky

# Install dependencies
COPY package.json .
COPY package-lock.json .
RUN npm install
RUN npm ci --ignore-scripts

# Copy add-on code
COPY . .
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# HA Linky
# Home Assistant Linky Add-on

> A **Home Assistant** add-on to sync Energy dashboards with your **Linky** smart meter
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'Linky'
description: 'TODO'
version: '1.0.0'
description: 'Home Assistant add-on to sync Energy dashboards with Linky smart meters'
version: '0.1.0'
slug: 'linky'
init: false
url: 'https://github.com/bokub/ha-linky'
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "ha-linky",
"private": true,
"version": "1.0.0",
"description": "Home Assistant add-on for Linky smart meters",
"version": "0.1.0",
"description": "Home Assistant add-on to sync Energy dashboards with Linky smart meters",
"author": "First Last <[email protected]>",
"type": "module",
"scripts": {
"build": "tsc",
"prepare": "husky install"
"prepare": "husky install",
"lint": "npx prettier --check --ignore-path .gitignore ."
},
"prettier": "@bokub/prettier-config",
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/ha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class HomeAssistantClient {
metadata: {
has_mean: false,
has_sum: true,
name: 'Consommation Linky ' + prm,
name: `Linky ${prm}`, // TODO make it configurable
source: SOURCE,
statistic_id: statisticId,
unit_of_measurement: 'Wh',
Expand All @@ -122,7 +122,7 @@ export class HomeAssistantClient {
}> {
const isNew = await this.isNewPRM(prm);
if (isNew) {
debug(`No statistics found for PRM ${prm} in Home Assistant`);
warn(`PRM ${prm} not found in Home Assistant statistics`);
return null;
}

Expand Down
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ async function main() {

async function init() {
info(`New PRM detected, importing as much historical data as possible`);
const historicalData = await consumptionClient.getEnergyData(null);
await haClient.saveStatistics(userConfig.consumption.prm, historicalData);
const energyData = await consumptionClient.getEnergyData(null);
await haClient.saveStatistics(userConfig.consumption.prm, energyData);
}
async function sync() {
debug('Data synchronization started');
Expand All @@ -50,9 +50,9 @@ async function main() {
return;
}
const firstDay = dayjs(lastStatistic.start).add(1, 'day');
const historicalData = await consumptionClient.getEnergyData(firstDay);
incrementSums(historicalData, lastStatistic.sum);
await haClient.saveStatistics(userConfig.consumption.prm, historicalData);
const energyData = await consumptionClient.getEnergyData(firstDay);
incrementSums(energyData, lastStatistic.sum);
await haClient.saveStatistics(userConfig.consumption.prm, energyData);
}

const isNew = await haClient.isNewPRM(userConfig.consumption.prm);
Expand All @@ -73,7 +73,9 @@ async function main() {
);

cron.schedule(`${randomSecond} ${randomMinute} 6,9 * * *`, async () => {
await haClient.connect();
await sync();
haClient.disconnect();
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/linky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class LinkyClient {
constructor(token: string, prm: string) {
this.prm = prm;
this.session = new Session(token, prm);
this.session.userAgent = 'ha-linky/1.0.0';
this.session.userAgent = 'ha-linky/0.1.0';
}

public async getEnergyData(firstDay: null | Dayjs): Promise<EnergyDataPoint[]> {
Expand Down

0 comments on commit 3974851

Please sign in to comment.