Skip to content

Commit

Permalink
refactor: Remove code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Dec 13, 2024
1 parent 3054e20 commit 592b84b
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 54 deletions.
10 changes: 5 additions & 5 deletions custom_components/solarman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_setup(hass: HomeAssistant, _: ConfigType) -> bool:
_LOGGER.debug(f"async_setup")

async_register(hass)
Expand Down Expand Up @@ -76,15 +76,15 @@ async def async_update_listener(hass: HomeAssistant, config_entry: ConfigEntry)

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry({config.as_dict()})")
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry({config_entry.as_dict()})")

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

if unload_ok := await hass.config_entries.async_unload_platforms(config, PLATFORMS):
_ = hass.data[DOMAIN].pop(config.entry_id)
if unload_ok := await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS):
_ = hass.data[DOMAIN].pop(config_entry.entry_id)

return unload_ok

Expand Down
11 changes: 5 additions & 6 deletions custom_components/solarman/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ def _create_entity(coordinator, description):

return SolarmanBinarySensorEntity(coordinator, description)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: _create_entity(coordinator, x), d) for d in descriptions if is_platform(d, _PLATFORM))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down
11 changes: 5 additions & 6 deletions custom_components/solarman/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@

_PLATFORM = get_current_file_name(__name__)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: SolarmanButtonEntity(coordinator, x), d) for d in descriptions if is_platform(d, _PLATFORM))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down
5 changes: 5 additions & 0 deletions custom_components/solarman/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from typing import Any

from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo, format_mac

from .const import *
Expand All @@ -36,6 +37,10 @@ def execute_async(x):
loop = asyncio.get_event_loop()
return loop.run_until_complete(x)

def get_coordinator(hass: HomeAssistant, entry_id: str):
coordinator = hass.data[DOMAIN][entry_id]
return coordinator, coordinator.inverter.get_entity_descriptions()

def to_dict(*keys: list):
return {k: k for k in keys}

Expand Down
11 changes: 5 additions & 6 deletions custom_components/solarman/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@

_PLATFORM = get_current_file_name(__name__)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: SolarmanDateTimeEntity(coordinator, x), d) for d in descriptions if is_platform(d, _PLATFORM))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down
11 changes: 5 additions & 6 deletions custom_components/solarman/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@

_PLATFORM = get_current_file_name(__name__)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: SolarmanNumberEntity(coordinator, x), d) for d in descriptions if is_platform(d, _PLATFORM) or "configurable" in d)

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down
11 changes: 5 additions & 6 deletions custom_components/solarman/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@

_PLATFORM = get_current_file_name(__name__)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: SolarmanSelectEntity(coordinator, x), d) for d in descriptions if is_platform(d, _PLATFORM))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down
13 changes: 6 additions & 7 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,19 @@ def _create_entity(coordinator, description, options):

return SolarmanSensor(coordinator, description)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: _create_entity(coordinator, x, config.options), d) for d in descriptions if (is_platform(d, _PLATFORM) and not "configurable" in d))
async_add_entities(create_entity(lambda x: _create_entity(coordinator, x, config_entry.options), d) for d in descriptions if (is_platform(d, _PLATFORM) and not "configurable" in d))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down
11 changes: 5 additions & 6 deletions custom_components/solarman/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@

_PLATFORM = get_current_file_name(__name__)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: SolarmanSwitchEntity(coordinator, x), d) for d in descriptions if is_platform(d, _PLATFORM))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down
11 changes: 5 additions & 6 deletions custom_components/solarman/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@

_PLATFORM = get_current_file_name(__name__)

async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config.options}")
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> bool:
_LOGGER.debug(f"async_setup_entry: {config_entry.options}")

coordinator = hass.data[DOMAIN][config.entry_id]
descriptions = coordinator.inverter.get_entity_descriptions()
coordinator, descriptions = get_coordinator(hass, config_entry.entry_id)

_LOGGER.debug(f"async_setup_entry: async_add_entities")

async_add_entities(create_entity(lambda x: SolarmanTimeEntity(coordinator, x), d) for d in descriptions if is_platform(d, _PLATFORM))

return True

async def async_unload_entry(hass: HomeAssistant, config: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config.options}")
async def async_unload_entry(_: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug(f"async_unload_entry: {config_entry.options}")

return True

Expand Down

0 comments on commit 592b84b

Please sign in to comment.