Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
modrisb committed Feb 8, 2023
0 parents commit 1f29388
Show file tree
Hide file tree
Showing 27 changed files with 7,382 additions and 0 deletions.
128 changes: 128 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, religion, or sexual identity
and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our
community include:

* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the
overall community

Examples of unacceptable behavior include:

* The use of sexualized language or imagery, and sexual attention or
advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email
address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.

Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
GitHub.
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series
of actions.

**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or
permanent ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.

**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within
the community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.0, available at
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.

Community Impact Guidelines were inspired by [Mozilla's code of conduct
enforcement ladder](https://github.com/mozilla/diversity).

[homepage]: https://www.contributor-covenant.org

For answers to common questions about this code of conduct, see the FAQ at
https://www.contributor-covenant.org/faq. Translations are available at
https://www.contributor-covenant.org/translations.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# PiJups - Home Assistant PiJuice Integration

[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg)](https://github.com/custom-components/hacs) (https://github.com/modrisb/pijups/releases)


PiJups exposes [PiJuice](https://github.com/PiSupply/PiJuice) sensor values in [Home Assistant](https://home-assistant.io) and allows to use this HAT in HA automations to handle battery low cases and other.

## Sensors supported
* Battery status
* Power input status
* Power input I/O status
* Charge in %
* Temperature (native format)
* Battery voltage
* Battery current
* I/O voltage
* I/O current
* External Power

## Prerequisite
Enable I2C bus on the host system.<br>
Like described in : https://www.home-assistant.io/common-tasks/os/#enable-i2c<br>

## Manual installation
1. Inside the `custom_components` directory, create a new folder called `pijups`.
2. Download all files from the `custom_components/pijuice/` repository to this directory `custom_components/pijups`.
3. Install integration from Home Assistant Settings/Devices & Services/Add Integration. HAT should be detected automatically within ~ 20s.

## Configuration
Parameters for hardware configuration, default values reflect current HAT's settings:
1. Battery temperature sense source selection - impacts HAT temperature sensor.
2. Battery battery profile. This impacts HAT funstionaly - battery charge options, by default set to smallest by capacity battery.

Parameters for HAT circular log configuration - select events to store in log. Default selections corresponf to HAT's current settings.

Parameters to control integration behaviour on shutdown/restart and sensor polling rate:
1. Power Off Delay, specifies time HAT will delay switch off power, this gives time to HA to perform software shutdown actions. Default - 120s might be too much for most cases, need to measure time needed. Noticed ~ 1 minute run time uses ~ 1% of battery charge, but this may vary per hardware.
2. Wake On Delta specifies HAT action after power is resumed. -1 forces reboot right after power is resumed, any positive value is added to charge % and reboot should happen when battery reaches this level after power resume. Idea to always have capacity to do shutdown without data loss.
3. Sensor refresh interval in seconds. This time period applies to Battery status, Power input status, Power input I/O status and External Power, others are updated every 6th cycle. Integration need to be reloaded to start using new scan interval value, HA restart works too.


## Example automation
Automation example below is triggered by battery status change and in case of no external power and battery capacity below specified limit initiates HA shutdown and then HAT switch off:
```
alias: UPS
description: ""
trigger:
- platform: state
entity_id:
- sensor.pijups_charge
condition:
- condition: and
conditions:
- condition: state
entity_id: sensor.pijups_power_input_io_status
state: NOT_PRESENT
- condition: state
entity_id: sensor.ppijups_power_input_status
state: NOT_PRESENT
- condition: numeric_state
entity_id: sensor.pijups_charge
below: 25
action:
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- service: hassio.host_shutdown
data: {}
mode: single
```

## Credits
[PiJuice](https://pi-supply.com/) : PiJuice Pi supply hardware/software platform to support Raspberry Pi, Arduino. PiJups uses PiJuice API with very little changes, see published pull request on github<br>
[Home Assistant](https://github.com/home-assistant) : Home Assistant open-source powerful domotic plateform.<br>
[HACS](https://hacs.xyz/) : Home Assistant Community Store gives you a powerful UI to handle downloads of all your custom needs.<br>
[smbus2 library](https://pypi.org/project/smbus2) : PyPI library for I2C access
1 change: 1 addition & 0 deletions custom_components/pijups/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
55 changes: 55 additions & 0 deletions custom_components/pijups/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""The PiJuPS HAT integration - setup."""
from __future__ import annotations

from datetime import timedelta
import importlib
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_SCAN_INTERVAL, Platform
from homeassistant.core import HomeAssistant

from .const import BASE, DOMAIN
from .sensor import PiJups

_LOGGER = logging.getLogger(__name__)

# List of platforms to support. There should be a matching .py file for each,
# eg <cover.py> and <sensor.py>
PLATFORMS: list[Platform] = [Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up PiJups from a config entry."""
# Store an instance of the "connecting" class that does the work of speaking
# with your actual devices.
hass.data.setdefault(DOMAIN, {})

pijups: PiJups = PiJups(hass, entry)
hass.data[DOMAIN][entry.entry_id] = {BASE: pijups}
await hass.async_add_executor_job(pijups.configure_device, hass, entry)
# set scan interval to integration configuration for all integrated platforms
for platform in PLATFORMS:
module = importlib.import_module("." + platform, __name__)
if "SCAN_INTERVAL" in dir(module):
module.SCAN_INTERVAL = timedelta(
seconds=entry.options.get(CONF_SCAN_INTERVAL)
)

# This creates each HA object for each platform your device requires.
# It's done by calling the `async_setup_entry` function in each platform module.
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
_LOGGER.debug("async_setup_entry completed")
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
# This is called when an entry/configured device is to be removed. The class
# needs to unload itself, and remove callbacks. See the classes for further
# details
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
_LOGGER.debug("async_unload_entry completed")
return unload_ok
Loading

0 comments on commit 1f29388

Please sign in to comment.