Git Product home page Git Product logo

pfsense-fauxapi-ha's Introduction

pfSense FauxAPI for Home Assistant - DEPRECIATED

DEPRECIATED

⚠️ ⚠️ This has been depreciated and is no longer developed: Check out https://github.com/travisghansen/hass-pfsense which is excatly what I always wanted - Shoutout to the great work by travisghansen!

pfSense

hacs_badge GitHub Release Date GitHub release (latest by date) GitHub All Releases

A custom component for Home Assistant that interfaces with FauxAPI installed on a pfSense firewall.

Attention: This isn't in any way intended to be an official or supported release, it is intended mainly for my own personal use. Continue at your own risk.
If anyone with a better understanding of Python would like to help improve this, that would be cool.

I owe credit mainly to the incredibly intellegent folks in this Home Assistant community post: pfSense stat monitor
and @dgshue for the pfSense Rule Switch Component

Prerequisites

  • A device with pfSense installed - tested with v2.5 CE
    • Web access should be configured with ssl/https encryption and a certificate
  • FauxAPI installed on your pfSense instance, configured with an API key and access token - tested using the latest version as of 3/7/21
  • Home Assistant - tested with core version 2021.3.3 on Home Assistant OS 5.12
  • The ability to run Python3 commands in your Home Assistant environmet - see #8
  • Patience and understanding

Screenshot

Screenshot 1

# Install instructions

1. HACS installation - recommended

  1. Open HACS
  2. Select "Frontend"
  3. Select the ... menu button at the top right
  4. Select "Custom repositories"
  5. At the bottom left of the menu where it says "Add custom repository URL" add this repository: https://github.com/JOHLC/pfSense-FauxAPI-ha
  6. Select the "Integration" category
  7. Select "ADD"
  8. Close this menu
  9. This should now show up as a new repository
  10. Click "INSTALL" then install again on the pop-up
  11. Restart Home Assistant

1.1 Manual Installation - not recommended

  1. Download the files in custom components folder
  2. Upload the downloadeded files to your custom components directory
  3. Restart Home Assistant

2. Configure your sensors and commands

Configure your secrets.yaml with the appropriate python commands

Important: You need to change a few things below with your setup information

# Example secrets.yaml entries - you will need to change your host IP, apikey, and accesstoken in each command below
pf_api_restart: "python3 /config/custom_components/pfsense_fauxapi/function-reboot.py 192.168.1.1 PFFAyourapikey youraccesstoken"
pf_api_command: "python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.1.1 PFFAyourapikey youraccesstoken"
# Note: In order to fetch wan information you may need to do at least the following:
#   - Add `interface_*` to the `permit` line in `/etc/fauxapi/credentials.ini` in your pfSense instance
#   - Change the interface name in `config/custom_components/pfsense_fauxapi/function-iterate.py` if your pfSense wan interface is not `igb0` #e.g. interface = 'igb1' 

pf_key: PFFAyourpfsensekey
pf_token: yourpfsensetoken

Configure your rule switches

For more information on rule switches, see the creator's repo

# Example configuration.yaml entries
switch:
  - platform: pfsense_fauxapi
    host: 192.168.1.1
    api_key: !secret pf_key
    access_token: !secret pf_token
    rule_filter: ha

Configure your reboot command

# Example configuration.yaml entries
shell_command:
  pfsense_restart: !secret pf_api_restart

Configure your sensors

Note: You will need to change your.gw.ip.address under WAN section below to the IP address of your upstream gateway.

# Example configuration.yaml entry
sensor:
#######################################
#              FauxAPI                #
#######################################
###Version
  - platform: command_line
    command: !secret pf_api_command
    name: pfSense version
    value_template: '{{ value_json["info"]["data"]["info"]["pfsense_remote_version"]["installed_version"] }}'
    scan_interval: 43200

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense latest
    value_template: '{{ value_json["info"]["data"]["info"]["pfsense_remote_version"]["version"] }}'
    scan_interval: 43200
    
###Hardware
  - platform: command_line
    command: !secret pf_api_command
    name: pfSense CPU temp
    value_template: '{{ value_json["stat"]["data"]["stats"]["temp"] }}'
    unit_of_measurement : '°C'
    scan_interval: 60

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense uptime
    value_template: '{{ value_json["stat"]["data"]["stats"]["uptime"] | regex_replace(find=" days ",replace=":",ignorecase=True) | regex_replace(find=" day ",replace=":",ignorecase=True) | regex_replace(find=" hours ",replace=":",ignorecase=True) | regex_replace(find=" hour ",replace=":",ignorecase=True)| regex_replace(find=" Minutes ",replace=":",ignorecase=True) | regex_replace(find=" Minute ",replace=":",ignorecase=True) | regex_replace(find=" Seconds",replace="",ignorecase=True) | regex_replace(find=" Second",replace="",ignorecase=True) }}'
    scan_interval: 300

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense memory use
    value_template: '{{ value_json["stat"]["data"]["stats"]["mem"] }}'
    unit_of_measurement : '%'
    scan_interval: 30

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense CPU load avg
    value_template: '{{ ((value_json["stat"]["data"]["stats"]["load_average"][0] | float) * 100.0 / 2.0 ) | round(0) }}'
    unit_of_measurement : '%'
    scan_interval: 30

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense CPU usage
    value_template: '{{ ( ( ((value_json["stat"]["data"]["stats"]["cpu"].split("|")[0] | float) / (value_json["stat"]["data"]["stats"]["cpu"].split("|")[1] | float)) - 1.0 ) * 100.0 ) | round(1) }}'
    unit_of_measurement : '%'
    scan_interval: 30

###WAN stats - change your.gw.ip.address to the correct IP address
  - platform: command_line
    command: !secret pf_api_command
    name: pfSense WAN IP
    value_template: '{{ value_json["gw_status"]["data"]["gateway_status"]["your.gw.ip.address"]["srcip"] }}'
    scan_interval: 300
    
  - platform: command_line
    command: !secret pf_api_command
    name: pfSense WAN packetloss
    value_template: '{{ (value_json["gw_status"]["data"]["gateway_status"]["your.gw.ip.address"]["loss"]) | regex_replace(find="%",replace="",ignorecase=True) }}'
    unit_of_measurement: "%"
    scan_interval: 30

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense WAN status
    value_template: '{{ (value_json["gw_status"]["data"]["gateway_status"]["your.gw.ip.address"]["status"]) }}'
    scan_interval: 30

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense WAN latency
    value_template: '{{ (value_json["gw_status"]["data"]["gateway_status"]["your.gw.ip.address"]["delay"]) | regex_replace(find="ms",replace="",ignorecase=True) }}' 
    unit_of_measurement: "ms"
    scan_interval: 60
    
  - platform: command_line
    command: !secret pf_api_command
    name: pfSense WAN GB in
    value_template: '{{ (value_json["int_status"]["data"]["stats"]["inbytes"] | float / 1000 / 1000 / 1000) | round(2)}}'
    scan_interval: 60
    unit_of_measurement: GB

  - platform: command_line
    command: !secret pf_api_command
    name: pfSense WAN GB out
    value_template: '{{ (value_json["int_status"]["data"]["stats"]["outbytes"] | float / 1000 / 1000 / 1000) | round(2)}}'
    scan_interval: 60
    unit_of_measurement: GB

pfsense-fauxapi-ha's People

Contributors

johlc avatar meichthys avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pfsense-fauxapi-ha's Issues

#Question: Can I get hostnames of clients connected to each interface?

I want to setup a presence detection that uses hostnames instead of IP. How would i accomplish that. I already have the basic sensors working

**Is your feature request related to a problem?no]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Port Other than 443, Can It Be Configurable?

I have my firewall configured to use 4343 instead of 443 for security purposes (plus it conflict with web services behind it on that port. Try as I might I could not get this going with port 4343.

I tried:

switch:

  • platform: pfsense_fauxapi
    host: 192.168.2.1:4343
    api_key: !secret pf_key
    access_token: !secret pf_token
    rule_filter: ha

switch:

  • platform: pfsense_fauxapi
    host: 192.168.2.1
    port: 4343
    api_key: !secret pf_key
    access_token: !secret pf_token
    rule_filter: ha

switch:

  • platform: pfsense_fauxapi
    host: 192.168.2.1
    server_port: 4343
    api_key: !secret pf_key
    access_token: !secret pf_token
    rule_filter: ha

I also tried all combinations with:

pf_api_restart: "python3 /config/custom_components/pfsense_fauxapi/function-reboot.py 192.168.2.1:4343 PFFAXXX
pf_api_command: "python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.2.1:4343 PFFAXXX

These were the errors I got:

`
2021-06-24 20:11:53 ERROR (SyncWorker_3) [custom_components.pfsense_fauxapi.switch] Problem getting rule set from pfSense host: 192.168.2.1. Likely due to API key or secret. More Info:HTTPSConnectionPool(host='192.168.2.1', port=443): Max retries exceeded with url: /fauxapi/v1/?action=config_get&__debug=true (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f864280ee20>: Failed to establish a new connection: [Errno 110] Operation timed out'))

2021-06-24 20:18:03 ERROR (SyncWorker_22) [homeassistant.components.command_line] Command failed: python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.2.1:4343 PFFAcredential1 3jmtdkbfvp3kndq5wswavbq4spakj6pmc82dno4sfc
Traceback (most recent call last):
File "/config/custom_components/pfsense_fauxapi/function-iterate.py", line 26, in
gw_status = FauxapiLib.gateway_status()
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 95, in gateway_status
return self._api_request('GET', 'gateway_status')
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 141, in _api_request
raise PfsenseFauxapiException('Unable to complete {}() request'.format(action), json.loads(res.text))
PfsenseFauxapi.PfsenseFauxapiException: ('Unable to complete gateway_status() request', {'callid': '60d520bc375a2', 'message': 'action authorization failed'})
2021-06-24 20:18:04 ERROR (SyncWorker_24) [homeassistant.components.command_line] Command failed: python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.2.1:4343 PFFAcredential1 3jmtdkbfvp3kndq5wswavbq4spakj6pmc82dno4sfc
Traceback (most recent call last):
File "/config/custom_components/pfsense_fauxapi/function-iterate.py", line 26, in
gw_status = FauxapiLib.gateway_status()
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 95, in gateway_status
return self._api_request('GET', 'gateway_status')
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 141, in _api_request
raise PfsenseFauxapiException('Unable to complete {}() request'.format(action), json.loads(res.text))
PfsenseFauxapi.PfsenseFauxapiException: ('Unable to complete gateway_status() request', {'callid': '60d520bc3d358', 'message': 'action authorization failed'})
2021-06-24 20:18:04 ERROR (SyncWorker_16) [homeassistant.components.command_line] Command failed: python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.2.1:4343 PFFAcredential1 3jmtdkbfvp3kndq5wswavbq4spakj6pmc82dno4sfc
Traceback (most recent call last):
File "/config/custom_components/pfsense_fauxapi/function-iterate.py", line 26, in
gw_status = FauxapiLib.gateway_status()
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 95, in gateway_status
return self._api_request('GET', 'gateway_status')
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 141, in _api_request
raise PfsenseFauxapiException('Unable to complete {}() request'.format(action), json.loads(res.text))
PfsenseFauxapi.PfsenseFauxapiException: ('Unable to complete gateway_status() request', {'callid': '60d520bcf0495', 'message': 'action authorization failed'})
2021-06-24 20:18:05 ERROR (SyncWorker_24) [homeassistant.components.command_line] Command failed: python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.2.1:4343 PFFAcredential1 3jmtdkbfvp3kndq5wswavbq4spakj6pmc82dno4sfc
Traceback (most recent call last):
File "/config/custom_components/pfsense_fauxapi/function-iterate.py", line 26, in
gw_status = FauxapiLib.gateway_status()
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 95, in gateway_status
return self._api_request('GET', 'gateway_status')
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 141, in _api_request
raise PfsenseFauxapiException('Unable to complete {}() request'.format(action), json.loads(res.text))
PfsenseFauxapi.PfsenseFauxapiException: ('Unable to complete gateway_status() request', {'callid': '60d520bd02736', 'message': 'action authorization failed'})
2021-06-24 20:18:05 ERROR (SyncWorker_13) [homeassistant.components.command_line] Command failed: python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.2.1:4343 PFFAcredential1 3jmtdkbfvp3kndq5wswavbq4spakj6pmc82dno4sfc
Traceback (most recent call last):
File "/config/custom_components/pfsense_fauxapi/function-iterate.py", line 26, in
gw_status = FauxapiLib.gateway_status()
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 95, in gateway_status
return self._api_request('GET', 'gateway_status')
File "/config/custom_components/pfsense_fauxapi/PfsenseFauxapi.py", line 141, in _api_request
raise PfsenseFauxapiException('Unable to complete {}() request'.format(action), json.loads(res.text))
PfsenseFauxapi.PfsenseFauxapiException: ('Unable to complete gateway_status() request', {'callid': '60d520bdae6bd', 'message': 'action authorization failed'})
2021-06-24 20:18:05 ERROR (SyncWorker_4) [homeassistant.components.command_line] Command failed: python3 /config/custom_components/pfsense_fauxapi/function-iterate.py 192.168.2.1:4343 PFFAcredential1 3jmtdkbfvp3kndq5wswavbq4spakj6pmc82dno4sfc

2021-06-24 20:19:11 ERROR (SyncWorker_6) [custom_components.pfsense_fauxapi.switch] Problem getting rule set from pfSense host: 192.168.2.1. Likely due to API key or secret. More Info:HTTPSConnectionPool(host='192.168.2.1', port=443): Max retries exceeded with url: /fauxapi/v1/?action=config_get&__debug=true (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fb73b382460>: Failed to establish a new connection: [Errno 110] Operation timed out'))
`

No function-info.py

Looks great, thanks for your work.

The intructions refer to function-info.py, but it's not in the code

Instruction Update

I spent multiple days trying to figure out why I couldn't get this working. Only to find out that I had PFSense setup as HTTP access, when FauxAPI requires HTTPS. Could you add a note to the installation instructions to prevent some other n00b from making my same mistake?

How to use python3 command in HomeAssistant

Thanks for this great repo.
My Problem is that i dont get it how to activate in HomeAssistant to use python3 from the command line.
There are a bunch of intergrations but they seem not to work the way it should with this addon here.

Can you tell how you have done this.

Thanks and have a nice day

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.