Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Battery Replaced #147

Merged
merged 83 commits into from
Dec 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
39fd848
An initial button
andrew-codechimp Sep 10, 2023
b2b9c70
Bump ruff from 0.0.287 to 0.0.288
dependabot[bot] Sep 11, 2023
5763cf7
Update README.md
andrew-codechimp Sep 13, 2023
b62e1f6
Made the button appear
andrew-codechimp Sep 15, 2023
50846f6
Start of multiple sensors
andrew-codechimp Sep 15, 2023
ac851f4
Remove unused const
andrew-codechimp Sep 15, 2023
f3af4dd
Improving sensors
andrew-codechimp Sep 16, 2023
60c9115
Add entity translations
andrew-codechimp Sep 16, 2023
8f8f9bd
Tidy up entity naming
andrew-codechimp Sep 17, 2023
01a599a
Formatting
andrew-codechimp Sep 17, 2023
d48651b
Update minimum requirements
andrew-codechimp Sep 18, 2023
39b11a6
Work on battery changed
andrew-codechimp Sep 20, 2023
98b9cfa
Got a datetime for last changed
andrew-codechimp Sep 21, 2023
e955be8
Work on last changed sensor
andrew-codechimp Oct 9, 2023
130a1fd
Trying things
andrew-codechimp Oct 9, 2023
979af01
Work on last changed time
andrew-codechimp Oct 10, 2023
afd52e8
Work on last changed
andrew-codechimp Nov 22, 2023
0ae41b6
Merge branch 'main' into lastchanged
andrew-codechimp Nov 22, 2023
45ca049
Manual merge from main refactor
andrew-codechimp Nov 22, 2023
d4e9029
Merge branch 'main' into lastchanged
andrew-codechimp Nov 22, 2023
966f988
WIP
andrew-codechimp Dec 7, 2023
b482446
Merge branch 'main' into lastchanged
andrew-codechimp Dec 13, 2023
53442ba
Merge branch 'main' into lastchanged
andrew-codechimp Dec 13, 2023
56e5555
WIP
andrew-codechimp Dec 13, 2023
f446b67
Merge branch 'main' into lastchanged
andrew-codechimp Dec 14, 2023
e2acf12
Update library.json (#48)
P-v-D Dec 15, 2023
196a280
Add 2 Devices (#50)
askpatrickw Dec 15, 2023
e303a9e
Update manifest.json
andrew-codechimp Dec 15, 2023
ea776e0
Add discovery screenshot
andrew-codechimp Dec 15, 2023
4b3b950
Update README.md
andrew-codechimp Dec 15, 2023
8156346
Update discovery screenshot
andrew-codechimp Dec 15, 2023
cf1b16e
Merge branch 'main' into lastchanged
andrew-codechimp Dec 18, 2023
9444318
WIP
andrew-codechimp Dec 18, 2023
07eeeff
WIP
andrew-codechimp Dec 20, 2023
0a9d3d3
Merge branch 'main' into lastchanged
andrew-codechimp Dec 20, 2023
3727c44
Merge branch 'main' into lastchanged
andrew-codechimp Dec 21, 2023
4088299
remove duplicate entries for LUMI devices (#121)
bmos Dec 21, 2023
2418f8c
Update library.json (#122)
edouardkleinhans Dec 21, 2023
aa51407
Update library.json (#123)
cermakjn Dec 21, 2023
05798c3
Create translation to Hungarian (#125)
bekesizl Dec 22, 2023
5ea8c8a
Added device details (Xiaomi SRTS-A01, Sonoff TRVZB) (#127)
jkrgr0 Dec 22, 2023
85f9735
Update library.json (#128)
cgallred Dec 22, 2023
73948d3
Add Drayton Wiser iTRV & RoomStat (#129)
edouardkleinhans Dec 22, 2023
40d27b5
Updated readme and additions to library
andrew-codechimp Dec 22, 2023
7655ee6
Update README.md
andrew-codechimp Dec 22, 2023
25aa699
Add multiple devices to library (#130)
ThomDietrich Dec 22, 2023
820af93
Update README.md
andrew-codechimp Dec 22, 2023
b8866ed
Update validate.yml
andrew-codechimp Dec 22, 2023
fdbd653
WIP
andrew-codechimp Dec 22, 2023
60b3051
WIP
andrew-codechimp Dec 22, 2023
d851db8
Create battery_changed service
andrew-codechimp Dec 22, 2023
2cd2bc0
Change the service to use the last changed entity
andrew-codechimp Dec 22, 2023
31f3487
WIP
andrew-codechimp Dec 23, 2023
c933ebe
Merge branch 'main' into lastchanged
andrew-codechimp Dec 23, 2023
97e8a70
WIP
andrew-codechimp Dec 23, 2023
87a53f7
WIP
andrew-codechimp Dec 23, 2023
4c852ea
Change back to device id
andrew-codechimp Dec 23, 2023
8aa13ca
WIP
andrew-codechimp Dec 23, 2023
9935c28
WIP
andrew-codechimp Dec 23, 2023
c862607
WIP
andrew-codechimp Dec 23, 2023
570429e
WIP
andrew-codechimp Dec 23, 2023
cba06d9
WIP
andrew-codechimp Dec 23, 2023
713d96a
Merge branch 'main' into lastchanged
andrew-codechimp Dec 23, 2023
7aa2386
WIP
andrew-codechimp Dec 23, 2023
4f41701
Button press update store
andrew-codechimp Dec 24, 2023
b653056
Docs
andrew-codechimp Dec 24, 2023
5da9674
Sensor update on button & service
andrew-codechimp Dec 24, 2023
6a91716
WIP
andrew-codechimp Dec 24, 2023
aac3ce6
Tidy up storage on device remove
andrew-codechimp Dec 24, 2023
9fce171
Check device exists on service call
andrew-codechimp Dec 24, 2023
3aab1ef
Lint fixes
andrew-codechimp Dec 24, 2023
b67f9ef
Update HA version to test service translation
andrew-codechimp Dec 24, 2023
dc2bcae
Add placeholders for new translation requirements
andrew-codechimp Dec 24, 2023
0aa089c
Tidy up code
andrew-codechimp Dec 24, 2023
4236b73
Merge branch 'main' into lastchanged
andrew-codechimp Dec 26, 2023
81ab8d2
Add new fields to Danish translation
andrew-codechimp Dec 26, 2023
355dc75
Remove date from service
andrew-codechimp Dec 26, 2023
ba752ee
Rename to battery replaced
andrew-codechimp Dec 26, 2023
f4e836a
Update service description
andrew-codechimp Dec 26, 2023
f12547e
Update library
andrew-codechimp Dec 26, 2023
635b321
Merge branch 'main' into lastchanged
andrew-codechimp Dec 26, 2023
cee4dd6
Merge branch 'main' into lastchanged
andrew-codechimp Dec 26, 2023
b7116db
Fix lint issues & version bump
andrew-codechimp Dec 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
WIP
andrew-codechimp committed Dec 23, 2023

Verified

This commit was signed with the committer’s verified signature.
andrew-codechimp Andrew Jackson
commit 8aa13cad26d29bb73fb8ed9f76e833738ec6ec2a
31 changes: 26 additions & 5 deletions custom_components/battery_notes/__init__.py
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@

import logging
from datetime import date
from typing import cast

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
@@ -26,6 +27,12 @@
from .library_updater import (
LibraryUpdaterClient,
)
from .coordinator import BatteryNotesCoordinator
from .store import (
BatteryNotesStorage,
DeviceEntry,
async_get_registry,
)

from .const import (
DOMAIN,
@@ -36,6 +43,8 @@
DATA_UPDATE_COORDINATOR,
SERVICE_BATTERY_CHANGED,
SERVICE_BATTERY_CHANGED_SCHEMA,
DATA_STORE,
DATA_COORDINATOR,
)

MIN_HA_VERSION = "2023.7"
@@ -56,7 +65,7 @@
extra=vol.ALLOW_EXTRA,
)

ATTR_SERVICE_ENTITY_ID = "entity_id"
ATTR_SERVICE_DEVICE_ID = "device_id"
ATTR_SERVICE_DATE_CHANGED = "date_changed"

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
@@ -79,12 +88,17 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
DOMAIN_CONFIG: domain_config,
}

coordinator = BatteryNotesLibraryUpdateCoordinator(
store = await async_get_registry(hass)

coordinator = BatteryNotesCoordinator(hass, store)
hass.data[DOMAIN][DATA_COORDINATOR] = coordinator

library_coordinator = BatteryNotesLibraryUpdateCoordinator(
hass=hass,
client=LibraryUpdaterClient(session=async_get_clientsession(hass)),
)

hass.data[DOMAIN][DATA_UPDATE_COORDINATOR] = coordinator
hass.data[DOMAIN][DATA_UPDATE_COORDINATOR] = library_coordinator

await coordinator.async_refresh()

@@ -126,10 +140,17 @@ def register_services(hass):

async def handle_battery_changed(call):
"""Handle the service call."""
entity_id = call.data.get(ATTR_SERVICE_ENTITY_ID, "")
device_id = call.data.get(ATTR_SERVICE_DEVICE_ID, "")
date_changed = call.data.get(ATTR_SERVICE_DATE_CHANGED, date.today())

coordinator = hass.data[DOMAIN][DATA_COORDINATOR]
device_entry = {
"last_changed" : date_changed
}

coordinator.store.async_create_device(device_id = device_id, data = device_entry)

# coordinator.store.async_update_user(user[const.ATTR_USER_ID], {const.ATTR_ENABLED: enable})
_LOGGER.debug("Entity {} battery changed on {}".format(entity_id,str(date_changed)))
_LOGGER.debug("Device {} battery changed on {}".format(device_id,str(date_changed)))

hass.services.async_register(DOMAIN, SERVICE_BATTERY_CHANGED, handle_battery_changed, schema=SERVICE_BATTERY_CHANGED_SCHEMA)
7 changes: 5 additions & 2 deletions custom_components/battery_notes/const.py
Original file line number Diff line number Diff line change
@@ -40,15 +40,18 @@
DATA_LIBRARY = "library"
DATA_UPDATE_COORDINATOR = "update_coordinator"
DATA_LIBRARY_LAST_UPDATE = "library_last_update"
DATA_COORDINATOR = "coordinator"
DATA_STORE = "store"

SERVICE_BATTERY_CHANGED = "set_battery_changed"

ATTR_ENTITY_ID = "entity_id"
ATTR_DEVICE_ID = "device_id"
ATTR_DATE_CHANGED = "date_changed"
ATTR_REMOVE = "remove"

SERVICE_BATTERY_CHANGED_SCHEMA = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): cv.entity_id,
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Optional(ATTR_DATE_CHANGED): cv.date,
}
)
57 changes: 57 additions & 0 deletions custom_components/battery_notes/coordinator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""DataUpdateCoordinator for battery notes."""
from __future__ import annotations

from datetime import datetime, timedelta
import logging
import json
import os

from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import (
DataUpdateCoordinator,
UpdateFailed,
)

from .library_updater import (
LibraryUpdaterClient,
LibraryUpdaterClientError,
)

from .const import (
DOMAIN,
LOGGER,
DATA_LIBRARY_LAST_UPDATE,
ATTR_REMOVE,
)

_LOGGER = logging.getLogger(__name__)


class BatteryNotesCoordinator(DataUpdateCoordinator):
"""Define an object to hold Battery Notes device."""

def __init__(self, hass, store):
"""Initialize."""
self.hass = hass
self.store = store

super().__init__(hass, _LOGGER, name=DOMAIN)

async def _async_update_data(self):
"""Update data."""

_LOGGER.debug("Update coordinator")

def async_update_device_config(self, device_id: str, data: dict):
if ATTR_REMOVE in data:
self.store.async_delete_device(device_id)
elif self.store.async_get_device(device_id):
self.store.async_update_device(device_id, data)
else:
self.store.async_create_device(device_id, data)

async def async_delete_config(self):
"""wipe battery notes storage"""
await self.store.async_delete()

10 changes: 4 additions & 6 deletions custom_components/battery_notes/services.yaml
Original file line number Diff line number Diff line change
@@ -3,16 +3,14 @@ set_battery_changed:
name: Set battery changed
description: "Set the battery last changed date."
fields:
entity_id:
name: Entity
description: Entity that has had it's battery changed.
device_id:
name: Device
description: Device that has had it's battery changed.
required: true
selector:
entity:
device:
filter:
- integration: battery_notes
domain: sensor
device_class: date
date_changed:
name: Date Changed
description: Date battery changed.
2 changes: 1 addition & 1 deletion custom_components/battery_notes/store.py
Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ def _data_to_save(self) -> dict:
store_data = {}

store_data["devices"] = [
attr.asdict(entry) for entry in self.sensors.values()
attr.asdict(entry) for entry in self.devices.values()
]

return store_data
6 changes: 3 additions & 3 deletions custom_components/battery_notes/translations/en.json
Original file line number Diff line number Diff line change
@@ -63,9 +63,9 @@
"set_battery_changed": {
"description": "Set the battery last changed date.",
"fields": {
"entity_id": {
"description": "Entity that has had it's battery changed.",
"name": "Entity"
"device_id": {
"description": "Device that has had it's battery changed.",
"name": "Device"
}
},
"name": "Set battery changed"