Skip to content

Commit 81e8473

Browse files
MyPy
1 parent 26025f8 commit 81e8473

File tree

9 files changed

+34
-24
lines changed

9 files changed

+34
-24
lines changed

custom_components/battery_notes/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: BatteryNotesConfi
147147
"""Set up a config entry."""
148148

149149
data = hass.data[MY_KEY]
150+
assert(data.store)
150151
config_entry.runtime_data = BatteryNotesData(
151152
domain_config=data,
152153
store=data.store,
@@ -174,9 +175,7 @@ async def async_remove_entry(hass: HomeAssistant, config_entry: BatteryNotesConf
174175
# Remove any issues raised
175176
ir.async_delete_issue(hass, DOMAIN, f"missing_device_{config_entry.entry_id}")
176177

177-
config_entry.runtime_data.coordinator.async_update_device_config
178-
179-
if not config_entry.runtime_data.coordinator.device_id:
178+
if not config_entry.runtime_data.coordinator or not config_entry.runtime_data.coordinator.device_id:
180179
return
181180

182181
data = {ATTR_REMOVE: True}
@@ -201,7 +200,7 @@ async def async_remove_entry(hass: HomeAssistant, config_entry: BatteryNotesConf
201200

202201
if wrapped_battery_entity_entry.hidden_by == er.RegistryEntryHider.INTEGRATION:
203202
entity_registry.async_update_entity(
204-
config_entry.runtime_data.wrapped_battery.entity_id, hidden_by=None
203+
config_entry.runtime_data.coordinator.wrapped_battery.entity_id, hidden_by=None
205204
)
206205
_LOGGER.debug(
207206
"Unhidden Original Battery for device%s", config_entry.runtime_data.coordinator.device_id

custom_components/battery_notes/binary_sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ async def async_registry_updated(
162162
)
163163

164164
coordinator = config_entry.runtime_data.coordinator
165+
assert(coordinator)
165166

166167
config_entry.async_on_unload(
167168
async_track_entity_registry_updated_event(

custom_components/battery_notes/button.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ async def async_registry_updated(event: Event[er.EventEntityRegistryUpdatedData]
128128
)
129129

130130
coordinator = config_entry.runtime_data.coordinator
131+
assert(coordinator)
131132

132133
config_entry.async_on_unload(
133134
async_track_entity_registry_updated_event(

custom_components/battery_notes/config_flow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ async def async_step_device(
185185
device_entry.hw_version,
186186
)
187187

188-
library = await Library.factory(self.hass)
188+
library = Library(self.hass)
189+
await library.load_libraries()
189190

190191
# Set defaults if not found in library
191192
self.data[CONF_BATTERY_QUANTITY] = 1
@@ -284,7 +285,8 @@ async def async_step_entity(
284285
device_entry.hw_version,
285286
)
286287

287-
library = await Library.factory(self.hass)
288+
library = Library(self.hass)
289+
await library.load_libraries()
288290

289291
device_battery_details = (
290292
await library.get_device_battery_details(self.model_info)

custom_components/battery_notes/coordinator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class BatteryNotesData:
9696
class BatteryNotesCoordinator(DataUpdateCoordinator[None]):
9797
"""Define an object to hold Battery Notes device."""
9898

99+
config_entry: BatteryNotesConfigEntry
99100
device_id: str | None = None
100101
source_entity_id: str | None = None
101102
device_name: str
@@ -176,7 +177,7 @@ def __init__(
176177
last_replaced = device_entry.created_at.strftime(
177178
"%Y-%m-%dT%H:%M:%S:%f"
178179
)
179-
else:
180+
elif self.source_entity_id:
180181
entity = entity_registry.async_get(self.source_entity_id)
181182
if entity and entity.created_at.year > 1970:
182183
last_replaced = entity.created_at.strftime("%Y-%m-%dT%H:%M:%S:%f")
@@ -295,7 +296,9 @@ def _link_device(self) -> bool:
295296
if self.wrapped_battery:
296297
break
297298

298-
device_entry = device_registry.async_get(self.device_id)
299+
device_entry = None
300+
if self.device_id:
301+
device_entry = device_registry.async_get(self.device_id)
299302
if device_entry:
300303
self.device_name = (
301304
device_entry.name_by_user or device_entry.name or self.config_entry.title

custom_components/battery_notes/discovery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from homeassistant.const import CONF_DEVICE_ID
1111
from homeassistant.core import HomeAssistant, callback
1212
from homeassistant.helpers import discovery_flow
13-
from homeassistant.helpers.typing import ConfigType
1413

1514
from .common import get_device_model_id
1615
from .const import (
@@ -22,6 +21,7 @@
2221
CONF_MODEL_ID,
2322
DOMAIN,
2423
)
24+
from .coordinator import BatteryNotesDomainConfig
2525
from .library import DeviceBatteryDetails, Library, ModelInfo
2626

2727
_LOGGER = logging.getLogger(__name__)
@@ -77,7 +77,7 @@ class DiscoveryManager:
7777
so the user can add them to their HA instance.
7878
"""
7979

80-
def __init__(self, hass: HomeAssistant, ha_config: ConfigType) -> None:
80+
def __init__(self, hass: HomeAssistant, ha_config: BatteryNotesDomainConfig) -> None:
8181
"""Init."""
8282
self.hass = hass
8383
self.ha_config = ha_config

custom_components/battery_notes/library.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,18 @@ def _load_library_json(library_file: str) -> dict[str, Any]:
101101
json_default_path,
102102
)
103103

104-
@staticmethod
105-
async def factory(hass: HomeAssistant) -> Library:
106-
"""Return the library or create."""
107-
108-
domain_config = hass.data[MY_KEY]
109-
if domain_config.library:
110-
return domain_config.library
111-
112-
library = Library(hass)
113-
await library.load_libraries()
114-
domain_config.library = library
115-
return library
104+
# @staticmethod
105+
# async def factory(hass: HomeAssistant) -> Library:
106+
# """Return the library or create."""
107+
108+
# domain_config = hass.data[MY_KEY]
109+
# if domain_config.library:
110+
# return domain_config.library
111+
112+
# library = Library(hass)
113+
# await library.load_libraries()
114+
# domain_config.library = library
115+
# return library
116116

117117
async def get_device_battery_details(
118118
self,

custom_components/battery_notes/sensor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ async def async_registry_updated(event: Event[er.EventEntityRegistryUpdatedData]
164164
)
165165

166166
coordinator = config_entry.runtime_data.coordinator
167+
assert(coordinator)
167168

168169
if not coordinator.fake_device:
169170
device_id = async_add_to_device(hass, config_entry)

custom_components/battery_notes/services.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ async def handle_battery_replaced(call: ServiceCall) -> ServiceResponse:
7777
config_entry: BatteryNotesConfigEntry
7878
for config_entry in hass.config_entries.async_loaded_entries(DOMAIN):
7979
coordinator = config_entry.runtime_data.coordinator
80-
if coordinator.source_entity_id == source_entity_id:
80+
assert(coordinator)
81+
if coordinator.source_entity_id and coordinator.source_entity_id == source_entity_id:
8182

8283
coordinator.last_replaced =datetime_replaced
8384
await coordinator.async_request_refresh()
@@ -120,9 +121,9 @@ async def handle_battery_replaced(call: ServiceCall) -> ServiceResponse:
120121
)
121122
return None
122123

123-
config_entry: BatteryNotesConfigEntry
124124
for config_entry in hass.config_entries.async_loaded_entries(DOMAIN):
125125
coordinator = config_entry.runtime_data.coordinator
126+
assert(coordinator)
126127
if coordinator.device_id == device_id:
127128
coordinator.last_replaced = datetime_replaced
128129
await coordinator.async_request_refresh()
@@ -168,6 +169,7 @@ async def handle_battery_last_reported(call: ServiceCall) -> ServiceResponse:
168169
config_entry: BatteryNotesConfigEntry
169170
for config_entry in hass.config_entries.async_loaded_entries(DOMAIN):
170171
coordinator = config_entry.runtime_data.coordinator
172+
assert(coordinator)
171173

172174
if coordinator.wrapped_battery and coordinator.last_reported:
173175
time_since_lastreported = (
@@ -206,6 +208,7 @@ async def handle_battery_low(call: ServiceCall) -> ServiceResponse:
206208
config_entry: BatteryNotesConfigEntry
207209
for config_entry in hass.config_entries.async_loaded_entries(DOMAIN):
208210
coordinator = config_entry.runtime_data.coordinator
211+
assert(coordinator)
209212

210213
if coordinator.battery_low is True:
211214
hass.bus.async_fire(

0 commit comments

Comments
 (0)