Skip to content

Commit

Permalink
Service date (#411)
Browse files Browse the repository at this point in the history
* Add an optional date for the service

* Lint
  • Loading branch information
andrew-codechimp authored Jan 8, 2024
1 parent 0d74a65 commit eb54d97
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 16 deletions.
17 changes: 11 additions & 6 deletions custom_components/battery_notes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
SERVICE_BATTERY_REPLACED_SCHEMA,
DATA_COORDINATOR,
ATTR_REMOVE,
ATTR_DEVICE_ID,
ATTR_DATE_TIME_REPLACED
)

MIN_HA_VERSION = "2023.7"
Expand All @@ -63,8 +65,6 @@
extra=vol.ALLOW_EXTRA,
)

ATTR_SERVICE_DEVICE_ID = "device_id"

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

Expand Down Expand Up @@ -154,7 +154,13 @@ def register_services(hass):

async def handle_battery_replaced(call):
"""Handle the service call."""
device_id = call.data.get(ATTR_SERVICE_DEVICE_ID, "")
device_id = call.data.get(ATTR_DEVICE_ID, "")
datetime_replaced_entry = call.data.get(ATTR_DATE_TIME_REPLACED)

if datetime_replaced_entry:
datetime_replaced = dt_util.as_utc(datetime_replaced_entry).replace(tzinfo=None)
else:
datetime_replaced = datetime.utcnow()

device_registry = dr.async_get(hass)

Expand All @@ -166,10 +172,9 @@ async def handle_battery_replaced(call):
if (
entry := hass.config_entries.async_get_entry(entry_id)
) and entry.domain == DOMAIN:
date_replaced = datetime.utcnow()

coordinator: BatteryNotesCoordinator = hass.data[DOMAIN][DATA_COORDINATOR]
device_entry = {"battery_last_replaced": date_replaced}
device_entry = {"battery_last_replaced": datetime_replaced}

coordinator.async_update_device_config(
device_id=device_id, data=device_entry
Expand All @@ -178,7 +183,7 @@ async def handle_battery_replaced(call):
await coordinator.async_request_refresh()

_LOGGER.debug(
"Device %s battery replaced on %s", device_id, str(date_replaced)
"Device %s battery replaced on %s", device_id, str(datetime_replaced)
)

hass.services.async_register(
Expand Down
1 change: 1 addition & 0 deletions custom_components/battery_notes/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
SERVICE_BATTERY_REPLACED_SCHEMA = vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Optional(ATTR_DATE_TIME_REPLACED): cv.datetime
}
)

Expand Down
8 changes: 7 additions & 1 deletion custom_components/battery_notes/services.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

set_battery_replaced:
name: Set battery replaced
description: "Set the battery last replaced to now."
description: "Set the battery last replaced."
fields:
device_id:
name: Device
Expand All @@ -11,3 +11,9 @@ set_battery_replaced:
device:
filter:
- integration: battery_notes
datetime_replaced:
name: Date
description: Date replaced.
required: false
selector:
datetime:
8 changes: 6 additions & 2 deletions custom_components/battery_notes/translations/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@
},
"services": {
"set_battery_replaced": {
"description": "Indstil det sidst udskiftede batteri til nu.",
"description": "Indstil det sidst udskiftede batteri.",
"fields": {
"device_id": {
"description": "Enhed, der har fået skiftet batteri.",
"name": "Enhed"
},
"datetime_replaced": {
"description": "Dato udskiftet.",
"name": "Dato"
}
},
"name": "Sæt batteri udskiftet"
}
}
}
}
8 changes: 6 additions & 2 deletions custom_components/battery_notes/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@
},
"services": {
"set_battery_replaced": {
"description": "ktualisiert den Zeitpunkt des letzten Batteriewechsels auf den jetzigen Zeitpunkt.",
"description": "Stellen Sie die zuletzt ausgetauschte Batterie ein.",
"fields": {
"device_id": {
"description": "Gerät, bei dem die Batterie ersetzt wurde.",
"name": "Gerät"
},
"datetime_replaced": {
"description": "Datum ersetzt.",
"name": "Datum"
}
},
"name": "ktualisiert den Zeitpunkt des letzten Batteriewechsels"
}
}
}
}
6 changes: 5 additions & 1 deletion custom_components/battery_notes/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@
},
"services": {
"set_battery_replaced": {
"description": "Set the battery last replaced to now.",
"description": "Set the battery last replaced.",
"fields": {
"device_id": {
"description": "Device that has had it's battery replaced.",
"name": "Device"
},
"datetime_replaced": {
"description": "Date replaced.",
"name": "Date"
}
},
"name": "Set battery replaced"
Expand Down
8 changes: 6 additions & 2 deletions custom_components/battery_notes/translations/hu.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@
},
"services": {
"set_battery_replaced": {
"description": "Utolsó elemcsere időpont beállítása mostra.",
"description": "Állítsa be az utoljára cserélt akkumulátort.",
"fields": {
"device_id": {
"description": "Az eszköz eleme cserélve lett.",
"name": "Eszköz"
},
"datetime_replaced": {
"description": "Csere dátuma.",
"name": "Dátum"
}
},
"name": "Elemcsere beállítása"
}
}
}
}
8 changes: 6 additions & 2 deletions custom_components/battery_notes/translations/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@
},
"services": {
"set_battery_replaced": {
"description": "Nastavte poslednú vymenenú batériu na teraz.",
"description": "Nastavte batériu ako poslednú vymenenú.",
"fields": {
"device_id": {
"description": "Zariadenie, v ktorom bola vymenená batéria.",
"name": "Zariadenie"
},
"datetime_replaced": {
"description": "Dátum nahradený.",
"name": "Dátum"
}
},
"name": "Sada vymenená batéria"
}
}
}
}

0 comments on commit eb54d97

Please sign in to comment.