Skip to content

Commit

Permalink
Remove duplicate code (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
iMicknl authored Jan 31, 2021
1 parent 89ce9a4 commit f92c19d
Showing 1 changed file with 26 additions and 56 deletions.
82 changes: 26 additions & 56 deletions custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,32 +179,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.config_entries.async_forward_entry_setup(entry, platform)
)

async def handle_execute_command(call):
"""Handle execute command service."""
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entity = entity_registry.entities.get(call.data.get("entity_id"))
await tahoma_coordinator.client.execute_command(
entity.unique_id,
Command(call.data.get("command"), call.data.get("args")),
"Home Assistant Service",
)

service.async_register_admin_service(
hass,
DOMAIN,
SERVICE_EXECUTE_COMMAND,
handle_execute_command,
vol.Schema(
{
vol.Required("entity_id"): cv.string,
vol.Required("command"): cv.string,
vol.Optional("args", default=[]): vol.All(
cv.ensure_list, [vol.Any(str, int)]
),
}
),
)

device_registry = await dr.async_get_registry(hass)

for gateway in gateways:
Expand Down Expand Up @@ -235,6 +209,32 @@ async def handle_execute_command(call):
sw_version=gateway.connectivity.protocol_version,
)

async def handle_execute_command(call):
"""Handle execute command service."""
entity_registry = await hass.helpers.entity_registry.async_get_registry()
entity = entity_registry.entities.get(call.data.get("entity_id"))
await tahoma_coordinator.client.execute_command(
entity.unique_id,
Command(call.data.get("command"), call.data.get("args")),
"Home Assistant Service",
)

service.async_register_admin_service(
hass,
DOMAIN,
SERVICE_EXECUTE_COMMAND,
handle_execute_command,
vol.Schema(
{
vol.Required("entity_id"): cv.string,
vol.Required("command"): cv.string,
vol.Optional("args", default=[]): vol.All(
cv.ensure_list, [vol.Any(str, int)]
),
}
),
)

async def handle_get_execution_history(call):
"""Handle get execution history service."""
await write_execution_history_to_log(tahoma_coordinator.client)
Expand All @@ -246,36 +246,6 @@ async def handle_get_execution_history(call):
handle_get_execution_history,
)

device_registry = await dr.async_get_registry(hass)

for gateway in gateways:
_LOGGER.debug(
"Added gateway (%s - %s - %s)",
gateway.id,
gateway.type,
gateway.sub_type,
)

gateway_model = (
beautify_name(gateway.sub_type.name)
if isinstance(gateway.sub_type, Enum)
else None
)
gateway_name = (
f"{beautify_name(gateway.type.name)} hub"
if isinstance(gateway.type, Enum)
else None
)

device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, gateway.id)},
model=gateway_model,
manufacturer="Somfy",
name=gateway_name,
sw_version=gateway.connectivity.protocol_version,
)

return True


Expand Down

0 comments on commit f92c19d

Please sign in to comment.