Compare commits

...
Sign in to create a new pull request.

5 commits

Author SHA1 Message Date
Robert Resch
f0387b6cb3
Fix imports 2024-07-03 08:19:30 +00:00
Robert Resch
be316cafab
Add import 2024-07-02 08:35:12 +00:00
Robert Resch
855f79cc72
improve 2024-07-01 12:57:23 +00:00
Robert Resch
ae1339d1ed
Another try 2024-07-01 07:53:23 +00:00
Robert Resch
c3d6215413
Add gritql runtime_data migration rule 2024-06-27 19:19:43 +00:00
3 changed files with 93 additions and 0 deletions

2
.grit/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.gritmodules*
*.log

3
.grit/grit.yaml Normal file
View file

@ -0,0 +1,3 @@
version: 0.0.1
patterns:
- name: github.com/getgrit/stdlib#*

View file

@ -0,0 +1,88 @@
---
tags: [migration, code_quality]
---
# Migrate integration from hass.data to entry.runtime_data
Migrate an integration from hass.data to entry.runtime_data
```grit
language python
pattern import_from_custom($source) {
$name where {
$program <: module($statements),
$statements <: contains or {
import_from_statement(),
import_statement(),
} as $import,
// Ruff will handle double imports and sorting for us
// So we add always the import
$import => `from $source import $name\n$import`,
}
}
pattern refactor_functions($config_entry_type, $file_name, $config_entry_type_defined) {
function_definition($parameters, $body) as $func where {
// change config entry type
$parameters <: contains typed_parameter(name=$entry_name, $type) where {
$type <: type(type="ConfigEntry"),
$type => $config_entry_type,
if ($file_name <: not `__init__`) {
$config_entry_type <: import_from_custom(source = `.`),
},
},
if (and {$file_name <: `__init__`, $config_entry_type_defined <: undefined}) {
if ($func <: within decorated_definition() as $decorated) {
// we need to insert the new type before all function decorators
$func = $decorated,
},
$config_entry_type_definition = `# TODO: Please add the correct type\n`,
$config_entry_type_definition += `type $config_entry_type = ConfigEntry`,
$func => `$config_entry_type_definition\n\n$func`,
$config_entry_type_defined = true,
},
// migrate hass.data to entry.runtime_data
$body <: maybe contains assignment($left, $right) as $assignment where {
or {
and {
$right <: `hass.data[$_][entry.entry_id]`,
$assignment => `$left = $entry_name.runtime_data`
},
and {
$left <: or {
`hass.data.setdefault($...)[entry.entry_id]`,
`hass.data[$_][entry.entry_id]`,
} as $runtime_data where {
$runtime_data => `$entry_name.runtime_data`
},
}
}
},
}
}
multifile {
bubble($domain_list) file($name, $body) where {
// find all integrations, which can be migrated
$filename <: r".*components/([^/]+)/__init__\.py$"($domain),
$body <: contains or {
`hass.data.setdefault($...)[entry.entry_id]`,
`hass.data[$_][entry.entry_id]`,
},
if ($domain_list <: undefined) {
$domain_list = []
},
$domain_list += $domain,
},
bubble($domain_list) file($name, $body) where {
// migrate files
$filename <: r".*components/([^/]+)/([^/]+)\.py$"($domain, $file_name),
$domain_list <: includes $domain,
$config_entry_type = capitalize($domain),
$config_entry_type += "ConfigEntry",
$body <: contains refactor_functions($config_entry_type, $file_name, $config_entry_type_defined),
},
}
```