Skip to content

Commit ac9d634

Browse files
Refactoring and made all string translatable
1 parent 66df7db commit ac9d634

File tree

8 files changed

+115
-53
lines changed

8 files changed

+115
-53
lines changed

custom_components/battery_notes/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
from homeassistant.const import Platform
1010
from homeassistant.core import HomeAssistant, callback
1111

12-
PLATFORMS = [Platform.SENSOR]
12+
from .const import DOMAIN, LOGGER, PLATFORMS
1313

1414
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
1515
"""Set up a config entry."""
1616

17-
await hass.config_entries.async_forward_entry_setups(
18-
entry, (Platform.SENSOR,)
19-
)
17+
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
2018

2119
entry.async_on_unload(entry.add_update_listener(async_update_options))
2220

custom_components/battery_notes/const.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import json
33
from logging import Logger, getLogger
44
from pathlib import Path
5+
from typing import Final
6+
7+
from homeassistant.const import Platform
58

69
LOGGER: Logger = getLogger(__package__)
710

@@ -22,3 +25,7 @@
2225
CONF_SENSORS = "sensors"
2326

2427
DATA_CONFIGURED_ENTITIES = "configured_entities"
28+
29+
PLATFORMS: Final = [
30+
Platform.SENSOR,
31+
]

custom_components/battery_notes/diagnostics.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""Diagnostic helpers."""
22

33
from homeassistant.config_entries import ConfigEntry
4-
from homeassistant.core import HomeAssistant
54

65
async def async_get_config_entry_diagnostics(
7-
hass: HomeAssistant,
86
entry: ConfigEntry,
97
) -> dict:
108
"""Return diagnostics for a config entry."""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Entities for battery_notes."""
2+
from __future__ import annotations
3+
4+
from dataclasses import dataclass
5+
6+
from homeassistant.helpers.entity import EntityDescription
7+
8+
@dataclass
9+
class BatteryNotesRequiredKeysMixin:
10+
"""Mixin for required keys."""
11+
12+
unique_id_suffix: str
13+
14+
15+
@dataclass
16+
class BatteryNotesEntityDescription(EntityDescription, BatteryNotesRequiredKeysMixin):
17+
"""Generic Battery Notes entity description."""

custom_components/battery_notes/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"integration_type": "device",
1010
"iot_class": "calculated",
1111
"issue_tracker": "https://github.com/andrew-codechimp/ha-battery-notes/issues",
12-
"version": "1.0.0"
12+
"version": "1.0.1"
1313
}

custom_components/battery_notes/sensor.py

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Sensor platform for battery_notes."""
22
from __future__ import annotations
33

4+
from dataclasses import dataclass
45
import voluptuous as vol
56

67
from homeassistant.components.sensor import (
78
PLATFORM_SCHEMA,
89
SensorEntity,
10+
SensorEntityDescription,
11+
RestoreSensor,
912
)
1013
from homeassistant.config_entries import ConfigEntry
1114
from homeassistant.const import CONF_ENTITY_ID
@@ -23,24 +26,38 @@
2326
)
2427

2528
from homeassistant.helpers.reload import async_setup_reload_service
26-
from homeassistant.helpers.typing import (
27-
ConfigType,
28-
)
2929

3030
from homeassistant.const import (
3131
CONF_NAME,
32-
CONF_UNIQUE_ID,
3332
)
3433

35-
from . import PLATFORMS
36-
3734
from .const import (
3835
DOMAIN,
36+
PLATFORMS,
3937
CONF_BATTERY_TYPE,
4038
CONF_DEVICE_ID,
4139
)
4240

43-
ICON = "mdi:battery-unknown"
41+
from .entity import (
42+
BatteryNotesEntityDescription,
43+
)
44+
45+
46+
@dataclass
47+
class BatteryNotesSensorEntityDescription(
48+
BatteryNotesEntityDescription,
49+
SensorEntityDescription,
50+
):
51+
"""Describes Battery Notes sensor entity."""
52+
unique_id_suffix: str
53+
54+
typeSensorEntityDescription = BatteryNotesSensorEntityDescription(
55+
unique_id_suffix="", # battery_type has uniqueId set to entityId in V1, never add a suffix
56+
key="battery_type",
57+
translation_key="battery_type",
58+
icon="mdi:battery-unknown",
59+
entity_category=EntityCategory.DIAGNOSTIC,
60+
)
4461

4562
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
4663
{
@@ -110,76 +127,68 @@ async def async_registry_updated(event: Event) -> None:
110127

111128
device_id = async_add_to_device(hass, config_entry)
112129

113-
async_add_entities(
114-
[
115-
BatteryTypeSensor(
130+
entities = [
131+
BatteryNotesTypeSensor(
116132
hass,
117-
config_entry.title,
118-
config_entry.entry_id,
119-
device_id=device_id,
120-
battery_type=battery_type,
121-
)
122-
]
123-
)
133+
typeSensorEntityDescription,
134+
device_id,
135+
f"{config_entry.entry_id}{typeSensorEntityDescription.unique_id_suffix}",
136+
battery_type
137+
),
138+
139+
]
140+
141+
async_add_entities(entities)
142+
124143

125144
async def async_setup_platform(
126145
hass: HomeAssistant,
127-
config: ConfigType,
128-
async_add_entities: AddEntitiesCallback,
129146
) -> None:
130-
"""Set up the battery type sensor."""
131-
name: str | None = config.get(CONF_NAME)
132-
unique_id = config.get(CONF_UNIQUE_ID)
133-
device_id: str = config[CONF_DEVICE_ID]
134-
battery_type: str = config[CONF_BATTERY_TYPE]
135-
147+
"""Set up the battery note sensor."""
136148
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
137149

138-
async_add_entities(
139-
[BatteryTypeSensor(hass, name, unique_id, device_id, battery_type)]
140-
)
141-
142-
class BatteryTypeSensor(SensorEntity):
143-
"""Represents a battery type sensor."""
150+
class BatteryNotesSensor(RestoreSensor, SensorEntity):
151+
"""Represents a battery note sensor."""
144152

145-
_attr_icon = ICON
146153
_attr_should_poll = False
154+
entity_description: BatteryNotesSensorEntityDescription
147155

148156
def __init__(
149157
self,
150-
hass: HomeAssistant,
151-
name: str,
152-
unique_id: str,
158+
hass,
159+
description: BatteryNotesSensorEntityDescription,
153160
device_id: str,
154-
battery_type: str,
161+
unique_id: str,
155162
) -> None:
156-
"""Create a battery type sensor."""
163+
"""Initialize the sensor."""
157164
device_registry = dr.async_get(hass)
158165

166+
self.entity_description = description
167+
self._attr_has_entity_name = True
159168
self._attr_unique_id = unique_id
160-
self._attr_name = name + " Battery type"
161169
self._device_id = device_id
162170

163-
self._device_id = device_id
164171
if device_id and (device := device_registry.async_get(device_id)):
165172
self._attr_device_info = DeviceInfo(
166173
connections=device.connections,
167174
identifiers=device.identifiers,
168175
)
169-
self._attr_entity_category = EntityCategory.DIAGNOSTIC
170-
self._battery_type = battery_type
171-
172176

173177
async def async_added_to_hass(self) -> None:
174178
"""Handle added to Hass."""
179+
await super().async_added_to_hass()
180+
state = await self.async_get_last_sensor_data()
181+
if state:
182+
self._attr_native_value = state.native_value
183+
175184
self.async_on_remove(
176185
async_track_state_change_event(
177-
self.hass, [self._attr_unique_id], self._async_battery_type_state_changed_listener
186+
self.hass, [self._attr_unique_id], self._async_battery_note_state_changed_listener
178187
)
179188
)
180189

181190
# Call once on adding
182-
self._async_battery_type_state_changed_listener()
191+
self._async_battery_note_state_changed_listener()
183192

184193
# Update entity options
185194
registry = er.async_get(self.hass)
@@ -190,10 +199,36 @@ async def async_added_to_hass(self) -> None:
190199
{"entity_id": self._attr_unique_id},
191200
)
192201

202+
@callback
203+
def _async_battery_note_state_changed_listener(self) -> None:
204+
"""Handle the sensor state changes."""
205+
206+
self.async_write_ha_state()
207+
self.async_schedule_update_ha_state(True)
208+
209+
210+
class BatteryNotesTypeSensor(BatteryNotesSensor):
211+
"""Represents a battery note sensor."""
212+
213+
entity_description: BatteryNotesSensorEntityDescription
214+
215+
def __init__(
216+
self,
217+
hass,
218+
description: BatteryNotesSensorEntityDescription,
219+
device_id: str,
220+
unique_id: str,
221+
battery_type: str | None = None,
222+
) -> None:
223+
"""Initialize the sensor."""
224+
super().__init__(hass, description, device_id, unique_id)
225+
226+
self._battery_type = battery_type
227+
193228
@property
194229
def native_value(self) -> str:
195230
"""Return the native value of the sensor."""
196-
# return self.battery_type
231+
197232
return self._battery_type
198233

199234
@callback

custom_components/battery_notes/translations/en.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,12 @@
3636
"error": {
3737
"unknown": "Unknown error occurred."
3838
}
39+
},
40+
"entity": {
41+
"sensor": {
42+
"battery_type": {
43+
"name": "Battery type"
44+
}
45+
}
3946
}
4047
}

hacs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "Battery Notes",
33
"filename": "battery-notes.zip",
44
"hide_default_branch": true,
5-
"homeassistant": "2023.3.0",
5+
"homeassistant": "2023.7.0",
66
"render_readme": true,
77
"zip_release": true
88
}

0 commit comments

Comments
 (0)