Giter VIP home page Giter VIP logo

homeassistant's Introduction

AirtelTracker

AirtelTracker is shell based script for accessing data bundle balances for your data plan from the Airtel mobile carrier.

Home Assistant calls the script periodically to retrieve the data values.

The data bundle balance is then exposed for use in dashboard and graphing in Home Assistant via a sensor entity🍾

Data balance Home Assistant data gauge Home Assistant entity card

QuickStart

Open your fav *nix terminal

  1. Run the bootstrap script
wget -q https://raw.githubusercontent.com/Protocol789/homeassistant/main/bootstrap/bootstrap.sh -O- | sh
  1. Run the Get balance command below with your Airtel web credentials in place of the $variables below
AirtelTracker/Airtel_GetBalance.sh $username '$password' -V
  1. Data balances returned in JSON packet
    image

Overview

Requirements

  • Home Assistant
  • SSH access
  • Access to edit Home Assistant configuration.yaml file
  • Internet access from Home Assistant

Prerequisites

Usage

In these installation steps, the optional prerequisites are used however you can use whatever you are most comfortable with.

  1. Jump into a terminal (click open web gui) session on the Home Assistant instance and move into the /config directory via
cd config/
  1. Run the bootstrap command which will download the latest release for you
wget -q https://raw.githubusercontent.com/Protocol789/homeassistant/main/bootstrap/bootstrap.sh -O- | sh
  1. Time to test!
    Get the Airtel credentials and plug them into the script below where $username and '$password' are your Airtel credentials respectively

Command to run
AirtelTracker/Airtel_GetBalance.sh $username '$password' -V

Response

2024-01-12 01:12:33 - INFO ---- Running airtel login call
2024-01-12 01:12:33 - INFO ---- -------------------------
2024-01-12 01:12:35 - INFO ---- Login: HTTP response of 200 OK!
2024-01-12 01:12:35 - INFO ---- Extracted login token
2024-01-12 01:12:35 - INFO ---- -------------------------
2024-01-12 01:12:35 - INFO ---- Starting Airtel Balance call
2024-01-12 01:12:36 - INFO ---- Get Balance HTTP response of 200 OK!
2024-01-12 01:12:36 - INFO ---- Here's your balances per bundle
{"balance":"8.45","unit":"GB","message":"success","status":"SUCCESS","statusCode":200}
  1. Now its time to add the sensor entity into Home Assistant so that the JSON packet can be consumed and eventually visualized.

    Open up the configuration.yaml in File editor (click open web gui) or open the file in your favourite text editor

  2. Copy the following yaml snippet into Home Assistant's configuration.yaml file

Important

Ensure your update the $username and '$password' values and note that the password must be encpasulated in single quotes '' to avoid special charectors being read by bash

  • The scan_interval is set to 30 minutes but can be set to any value you need (in seconds)

  • The entity name can be whatever you like Save the file

    command_line:
      - sensor:
          name: Airtel
          scan_interval: 1800      
          command: sh /config/AirtelTracker/Airtel_GetBalance.sh $username '$password'
          value_template: "{{ value_json.status }}"
          json_attributes:
            - message
            - statusCode
            - balance
            - unit
Example config Example config.yaml snippet
  1. Now do a FULL restart Home Assistant instance so the new configuration file and entity is loaded up

    This can be done directly from File Editor Addon

<img src="https://raw.githubusercontent.com/Protocol789/homeassistant/readme-images/.github/images/ha-fileeditor-restart.png" alt="File Editor restart Home Assistant" width="300"/>
  1. Once restarted head over to Developer Tools States and filter for your new entity.

    Note the json attributes now show the total data balance available on your Airtel bundle! 👏 image

  2. It's now time to expose the data bundle balance via a helper entity which will reference the attribute and allow you to add it to a dashboard
    Click Create Helper in bottom right corner

  3. Click Template
    image

  4. Select Template a sensor
    image

  5. New helper template sensor details

Important

Ensure you reference the name of the sensor you created in step 6 by replacing the sensor.SensorName

The `state template` will be referencing the new entity we created in the configuration.yml and extracting the `balance` attribute out  

` {{ state_attr( 'sensor.SensorName' , 'balance' ) }} `

Select the unit of measurement, device class and state class and click `SUBMIT` buttom  

<img src="https://raw.githubusercontent.com/Protocol789/homeassistant/readme-images/.github/images/ha-sensor-details.png"  width="200" alt="template sensor details"/> 
  1. Add the new sensor to a dashboard of your choosing!

  2. Example of entity history

HA entity card popp with history

Architecture

High level flow

flowchart LR;
    ha["Home Assistant"]
    at["AirtelTracker"]
    ai["Airtel API"]
    ha <-- `command line` --> at <-- REST API --> ai 
    
Loading

API message flow

sequenceDiagram
    participant A as Home Assistant
    participant B as Airtel Tracker
    participant C as Airtel API
    
    A->>+B: Command_Line processor 
    B->>+C: Login credentials
    C->>-B: Login token
    B->>+C: Get data balance
    C->>-B: Data balances
    B->>-A: Return balance

Loading

Debugging

The base script can be run with a few arguments for logging verbosity.

  • -s Silent (Default mode)

    The default mode which the script runs in. Only returns the json response success or failure information.
    This response is parsed by the command_line processor in Home Assistant and exposed into the entity

    • Replace $WebUsername and $WebPassword with your Airtel credentials used to login to the website

Run command

  ./Airtel_GetBalance.sh $WebUsername '$WebPassword' 

Response

root@zorab-surface:~/Airtel# ./Airtel_GetBalance.sh $WebUsername '$WebPassword'
{"balance":"8.46", "unit":"GB", "message":"success", "status":"SUCCESS", "statusCode":200}
  • -V Informational

    Show informational example
      ./Airtel_GetBalance.sh $WebUsername '$WebPassword' -V
    root@pc:~/Airtel# ./Airtel_GetBalance.sh $WebUsername '$WebPassword' -V
    2024-01-12 00:37:54 - INFO ---- Running airtel login call
    2024-01-12 00:37:54 - INFO ---- -------------------------
    2024-01-12 00:37:56 - INFO ---- Login: HTTP response of 200 OK!
    2024-01-12 00:37:56 - INFO ---- Extracted login token
    2024-01-12 00:37:56 - INFO ---- -------------------------
    2024-01-12 00:37:56 - INFO ---- Starting Airtel Balance call
    2024-01-12 00:37:57 - INFO ---- Get Balance HTTP response of 200 OK!
    2024-01-12 00:37:57 - INFO ---- Here's your balances per bundle
    {"balance":"8.46", "unit":"GB", "message":"success", "status":"SUCCESS", "statusCode":200}
  • -G Debug

    Show debug example

    Replace $WebUsername and $WebPassword with your Airtel credentials used to login to the website

      ./Airtel_GetBalance.sh $WebUsername '$WebPassword' -G
    root@pc:~/Airtel# ./Airtel_GetBalance.sh $WebUsername '$WebPassword' -G
    2024-01-12 00:35:12 - DEBUG --- -G specified: Debug mode
    2024-01-12 00:35:12 - DEBUG --- Variable passed in postion 1: $username
    2024-01-12 00:35:12 - DEBUG --- Variable passed in postion 2: $password
    2024-01-12 00:35:12 - DEBUG --- Running payload creator
    2024-01-12 00:35:12 - DEBUG --- This is login payload:
    {"username":"$username","password":"$password"}
    2024-01-12 00:35:13 - INFO ---- Running airtel login call
    2024-01-12 00:35:13 - INFO ---- -------------------------
    2024-01-12 00:35:15 - DEBUG --- Login: HTTP code response 200
    2024-01-12 00:35:15 - INFO ---- Login: HTTP response of 200 OK!
    2024-01-12 00:35:15 - DEBUG --- Login: Body reponse is:

Compatibility

The scripts were tested on following systems however any *nix system should work

  • Ubuntu 22.04.2 LTS (WSL)

  • Home Assistant Operating System (2024)
    Home Assistant version screen

homeassistant's People

Contributors

protocol789 avatar

Watchers

 avatar

homeassistant's Issues

Complex password failing

When a complex password is used it fails to retrieve the values.

The & symbol looks to be the issue as it's being read directly and should be escaped.

image

Command failed (with return code 2)

Home assistant unable to run shell command
image

2024-01-11 18:00:50.388 ERROR (SyncWorker_7) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:01:20.388 ERROR (SyncWorker_7) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:01:50.389 ERROR (SyncWorker_10) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:02:20.390 ERROR (SyncWorker_4) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:02:50.390 ERROR (SyncWorker_2) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:03:20.393 ERROR (SyncWorker_8) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:03:50.393 ERROR (SyncWorker_5) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:04:20.394 ERROR (SyncWorker_8) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh
2024-01-11 18:04:50.395 ERROR (SyncWorker_3) [homeassistant.components.command_line.utils] Command failed (with return code 2): sh /config/airtel/Airtel_GetBalance.sh

MB conversion when value drops below 1GB

  • Need to do a check on the value coming back during the call to see if it's MB.
  • If it is, need to divide the value by 1000 or it breaks the graph like below

image

Preceding days with values in GB
image

Convert HA project to python

Instead of using bash, move to python so it's more flexible in terms of implementation.

This is so that it could be run as a custom component in HACS repo or as a docker container

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.