Skip to content

Commit

Permalink
refactor: Add common method for entity_key
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 18, 2024
1 parent 03bd7ba commit 5e9147c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion custom_components/solarman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def async_update_listener(hass: HomeAssistant, config_entry: ConfigEntry)
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry({config_entry.as_dict()})")

# Forward setup
# Forward unload
#
_LOGGER.debug(f"async_setup: hass.config_entries.async_unload_platforms: {PLATFORMS}")

Expand Down
4 changes: 3 additions & 1 deletion custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,13 @@ async def try_read_write(self, code, start, arg, message: str, incremental_wait:
except Exception as e:
_LOGGER.debug(f"[{self.config.serial}] {message} failed, attempts left: {attempts_left}{'' if attempts_left > 0 else ', aborting.'} [{format_exception(e)}]")

await self.endpoint.discover()

if not self.modbus.auto_reconnect:
await self.modbus.disconnect()
if not attempts_left > 0:
raise

await asyncio.sleep(((ACTION_ATTEMPTS - attempts_left) * TIMINGS_WAIT_SLEEP) if incremental_wait else TIMINGS_WAIT_SLEEP)

return response
Expand Down Expand Up @@ -235,7 +238,6 @@ async def get(self, runtime = 0, requests = None):
self.state.update()

except Exception as e:
await self.endpoint.discover()
if self.state.reevaluate():
await self.modbus.disconnect()
raise
Expand Down
5 changes: 4 additions & 1 deletion custom_components/solarman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ def unwrap(source: dict, key: Any, mod: int = 0):
source[key] = c[mod]
return source

def entity_key(object: dict):
return slugify('_'.join(filter(None, (object["name"], object["platform"]))))

def process_descriptions(item, group, table, code, mod):
def modify(source: dict):
for i in source:
Expand All @@ -161,7 +164,7 @@ def modify(source: dict):

if not "platform" in item:
item["platform"] = "sensor" if not "configurable" in item else "number"
item["key"] = slugify('_'.join(filter(None, (item["name"], item["platform"]))))
item["key"] = entity_key(item)
bulk_inherit(item, group, *(REQUEST_UPDATE_INTERVAL, REQUEST_CODE) if "registers" in item else REQUEST_UPDATE_INTERVAL)
if not REQUEST_CODE in item and (r := item.get("registers")) is not None and (addr := min(r)) is not None:
item[REQUEST_CODE] = table.get(addr, code)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/solarman/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def create_entity(creator, description):

if description is not None and (nlookup := description.get("name_lookup")) is not None and (prefix := entity.coordinator.data.get(nlookup)) is not None:
description["name"] = replace_first(description["name"], get_tuple(prefix))
description["key"] = slugify('_'.join(filter(None, (description["name"], description["platform"]))))
description["key"] = entity_key(description)
entity = creator(description)

entity.update()
Expand Down

0 comments on commit 5e9147c

Please sign in to comment.