Skip to content

Commit

Permalink
Merge branch 'main' into lastchanged
Browse files Browse the repository at this point in the history
# Conflicts:
#	custom_components/battery_notes/manifest.json
#	custom_components/battery_notes/sensor.py
  • Loading branch information
andrew-codechimp committed Dec 13, 2023
2 parents 966f988 + 4ef936d commit b482446
Show file tree
Hide file tree
Showing 20 changed files with 940 additions and 98 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/json_validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Validate JSONs

on:
workflow_dispatch:
push:
paths:
- 'custom_components/battery_notes/data/**'
- '.github/workflows/**'
pull_request:
paths:
- 'custom_components/battery_notes/data/**'
- '.github/workflows/**'

jobs:
verify-json-validation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Validate JSON
uses: docker://orrosenblatt/validate-json-action:latest
env:
INPUT_SCHEMA: ./schema.json
INPUT_JSONS: custom_components/battery_notes/data/library.json
10 changes: 7 additions & 3 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ on:
push:
branches:
- "main"
paths-ignore:
- 'custom_components/battery_notes/data/**'
pull_request:
branches:
- "main"
paths-ignore:
- 'custom_components/battery_notes/data/**'

jobs:
ruff:
name: "Ruff"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout the repository"
uses: "actions/checkout@v4.1.1"
uses: "actions/checkout@v4"

- name: "Set up Python"
uses: actions/setup-python@v4.7.1
uses: actions/setup-python@v5.0.0
with:
python-version: "3.10"
python-version: "3.11"
cache: "pip"

- name: "Install requirements"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
contents: write
steps:
- name: "Checkout the repository"
uses: "actions/checkout@v4.1.1"
uses: "actions/checkout@v4"

- name: "Adjust version number"
shell: "bash"
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ on:
push:
branches:
- "main"
paths-ignore:
- 'custom_components/battery_notes/data/**'
pull_request:
branches:
- "main"
paths-ignore:
- 'custom_components/battery_notes/data/**'

jobs:
hassfest: # https://developers.home-assistant.io/blog/2020/04/16/hassfest
name: "Hassfest Validation"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout the repository"
uses: "actions/checkout@v4.1.1"
uses: "actions/checkout@v4"

- name: "Run hassfest validation"
uses: "home-assistant/actions/hassfest@master"
Expand All @@ -27,7 +31,7 @@ jobs:
runs-on: "ubuntu-latest"
steps:
- name: "Checkout the repository"
uses: "actions/checkout@v4.1.1"
uses: "actions/checkout@v4"

- name: "Run HACS validation"
uses: "hacs/action@main"
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ coverage.xml
config/*
!config/configuration.yaml
.DS_Store
custom_components/battery_notes/data/library-dev.json
config/configuration.yaml

88 changes: 69 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

[![Community Forum][forum-shield]][forum]

_Integration to add battery notes to a device._
_Integration to add battery notes to a device, with automatic discovery via a growing battery library for devices_

**Discovery and the Battery Library are currently in beta**
If you wish to try this version please go into HACS, select Battery Notes and in the top right menu choose Redownload, switch on Show Beta Versions and select the latest version (starts with 1.1)

![Battery Notes](https://github.com/andrew-codechimp/HA-Battery-Notes/blob/main/images/screenshot-device.png "Battery Notes")

Expand All @@ -24,43 +27,90 @@ Platform | Description

[![Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=andrew-codechimp&repository=HA-Battery-Notes&category=Integration)

1. Make sure the [HACS](https://github.com/custom-components/hacs) component is installed and working.
1. Add the project repository `https://github.com/andrew-codechimp/HA-Battery-Notes` as a custom repository to HACS, see: https://hacs.xyz/docs/faq/custom_repositories
1. Search for `Battery Notes` in HACS and install it under the "Integrations" category.
1. Restart Home Assistant
1. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Battery Notes"
Or
Search for `Battery Notes` in HACS and install it under the "Integrations" category.


Restart Home Assistant

**Important**
Add the following entry to your ```configuration.yaml```
```
battery_notes:
enable_autodiscovery: true
```
Restart Home Assistant a final time
In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Battery Notes"

### Manual Installation

<details>
<summary>Show detailed instructions</summary>
1. Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
1. If you do not have a `custom_components` directory (folder) there, you need to create it.
1. In the `custom_components` directory (folder) create a new folder called `battery_notes`.
1. Download _all_ the files from the `custom_components/battery_notes/` directory (folder) in this repository.
1. Place the files you downloaded in the new directory (folder) you created.
1. Restart Home Assistant
1. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Battery Notes"
</details>
Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
If you do not have a `custom_components` directory (folder) there, you need to create it.
In the `custom_components` directory (folder) create a new folder called `battery_notes`.
Download _all_ the files from the `custom_components/battery_notes/` directory (folder) in this repository.
Place the files you downloaded in the new directory (folder) you created.
Restart Home Assistant
Add the following entry to your ```configuration.yaml```
```
battery_notes:
enable_autodiscovery: true
```
Restart Home Assistant a final time
In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Battery Notes"


## Configuration is done in the UI

On the "Configuration" -> "Integrations" -> "Battery Notes" screen add a new device, pick your device with a battery and add the battery type.
The battery type will then be displayed as a diagnotic sensor on the device.
The battery type will then be displayed as a diagnostic sensor on the device.

## Automatic discovery

Battery Notes will automatically discover devices (as long as you have followed the installation instructions above) that it has in its library and create a notification to add a battery note.
If you wish to disable this functionality then change your ```configuration.yaml``` to set enable_autodiscovery to false
```
battery_notes:
enable_autodiscovery: false
```

## Contributing to the Battery Library

The battery library is a JSON document at ```custom_components/battery_notes/data/library.json```
To contribute, fork the repository, add your device details to the JSON document and submit a pull request.
Please keep devices in alphabetical order by manufacturer/model.
The manufacturer and model should be exactly what is displayed on the Device screen within Home Assistant. The make & model names may be different between integrations such as Zigbee2MQTT and ZHA, if you see a similar device please duplicate the entry rather than changing it.

For the example image below your JSON entry will look like this

```
{
"manufacturer": "Philips",
"model": "Hue motion sensor (9290012607)",
"battery_type": "AAA",
"battery_quantity": 2
},
```

Note that the ```battery_quantity``` is numeric (no quotes) and optional, if a device only has one battery it should be omitted.
When specifying battery types please use the Most Common naming for general batteries and the IEC naming for battery cells according to [Wikipedia](https://en.wikipedia.org/wiki/List_of_battery_sizes)

![Device Details](https://github.com/andrew-codechimp/HA-Battery-Notes/blob/main/images/screenshot-device-info.png "Device Details")
<!---->

## Contributions are welcome!

If you want to contribute to this please read the [Contribution guidelines](CONTRIBUTING.md)

***
## Acknowledgements

A lot of the inspiration for this integration came from the excellent [PowerCalc by bramstroker](https://github.com/bramstroker/homeassistant-powercalc), without adapting code from PowerCalc I'd never have worked out how to add additional sensors to a device.

<!---->
[battery_notes]: https://github.com/andrew-codechimp/HA-Battery-Notes
[commits-shield]: https://img.shields.io/github/commit-activity/y/andrew-codechimp/HA-Battery-Notes.svg?style=for-the-badge
[commits]: https://github.com/andrew-codechimp/HA-Battery-Notes/commits/main
[hacs]: https://github.com/hacs/integration
[hacsbadge]: https://img.shields.io/badge/HACS-Custom-orange.svg?style=for-the-badge
[hacsbadge]: https://img.shields.io/badge/HACS-Default-41BDF5.svg?style=for-the-badge
[discord]: https://discord.gg/Qa5fW2R
[discord-shield]: https://img.shields.io/discord/330944238910963714.svg?style=for-the-badge
[exampleimg]: example.png
Expand Down
8 changes: 0 additions & 8 deletions config/configuration.yaml

This file was deleted.

52 changes: 51 additions & 1 deletion custom_components/battery_notes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,57 @@
"""
from __future__ import annotations

import logging

from awesomeversion.awesomeversion import AwesomeVersion
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.const import __version__ as HA_VERSION # noqa: N812

from homeassistant.helpers.typing import ConfigType

from .discovery import DiscoveryManager

from .const import (
DOMAIN,
DOMAIN_CONFIG,
PLATFORMS,
CONF_ENABLE_AUTODISCOVERY,
)

MIN_HA_VERSION = "2023.7"

_LOGGER = logging.getLogger(__name__)


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Integration setup."""

if AwesomeVersion(HA_VERSION) < AwesomeVersion(MIN_HA_VERSION): # pragma: no cover
msg = (
"This integration requires at least HomeAssistant version "
f" {MIN_HA_VERSION}, you are running version {HA_VERSION}."
" Please upgrade HomeAssistant to continue use this integration."
)
_LOGGER.critical(msg)
return False

domain_config: ConfigType = config.get(DOMAIN) or {
CONF_ENABLE_AUTODISCOVERY: True,
}

hass.data[DOMAIN] = {
DOMAIN_CONFIG: domain_config,
}

if domain_config.get(CONF_ENABLE_AUTODISCOVERY):
discovery_manager = DiscoveryManager(hass, config)
await discovery_manager.start_discovery()
else:
_LOGGER.debug("Auto discovery disabled")

return True

from .const import PLATFORMS

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a config entry."""
Expand All @@ -19,15 +66,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

return True


@callback
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update options."""
await hass.config_entries.async_reload(entry.entry_id)


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener, called when the config entry options are changed."""
await hass.config_entries.async_reload(entry.entry_id)
Loading

0 comments on commit b482446

Please sign in to comment.