Skip to content

Commit

Permalink
Attribs (#389)
Browse files Browse the repository at this point in the history
* Add attributes to battery_type sensor

* Got some debugging working
  • Loading branch information
andrew-codechimp authored Jan 6, 2024
1 parent b5e0242 commit ebeaa44
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ __pycache__

# misc
.coverage
.vscode
coverage.xml


Expand Down
34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Home Assistant",
"type": "python",
"request": "launch",
"module": "homeassistant",
"justMyCode": false,
"args": [
"--debug",
"-c",
"config"
]
},
{
// Example of attaching to my production server
"name": "Python: Attach Remote",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/src/homeassistant"
}
],
}
]
}
16 changes: 16 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,21 @@
"command": "scripts/develop",
"problemMatcher": []
}
],
"configurations": [
{
// Example of attaching to local debug server
"name": "Python: Attach Local",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
}
]
}
3 changes: 3 additions & 0 deletions custom_components/battery_notes/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
ATTR_DEVICE_ID = "device_id"
ATTR_DATE_TIME_REPLACED = "datetime_replaced"
ATTR_REMOVE = "remove"
ATTR_BATTERY_QUANTITY = "battery_quantity"
ATTR_BATTERY_TYPE = "battery_type"


SERVICE_BATTERY_REPLACED_SCHEMA = vol.Schema(
{
Expand Down
29 changes: 29 additions & 0 deletions custom_components/battery_notes/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime
from dataclasses import dataclass
import voluptuous as vol
import re

from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
Expand Down Expand Up @@ -43,6 +44,8 @@
LAST_REPLACED,
DOMAIN_CONFIG,
CONF_ENABLE_REPLACED,
ATTR_BATTERY_QUANTITY,
ATTR_BATTERY_TYPE,
)

from .coordinator import BatteryNotesCoordinator
Expand Down Expand Up @@ -243,6 +246,32 @@ def native_value(self) -> str:

return self._battery_type

@property
def extra_state_attributes(self) -> dict[str, str] | None:
"""Return the state attributes of the battery type."""

matches: re.Match = re.search(
"^(\d+)(?=x)(?:x\s)(\w+$)|([\s\S]+)", self._battery_type
)
if matches:
_qty = matches.group(1) if matches.group(1) is not None else "1"
_type = (
matches.group(2) if matches.group(2) is not None else matches.group(3)
)
else:
_qty = 1
_type = self._battery_type

attrs = {
ATTR_BATTERY_QUANTITY: _qty,
ATTR_BATTERY_TYPE: _type,
}

super_attrs = super().extra_state_attributes
if super_attrs:
attrs.update(super_attrs)
return attrs


class BatteryNotesLastReplacedSensor(SensorEntity, CoordinatorEntity):
"""Represents a battery note sensor."""
Expand Down

0 comments on commit ebeaa44

Please sign in to comment.