1 """The Screenlogic integration."""
6 from screenlogicpy
import ScreenLogicError, ScreenLogicGateway
7 from screenlogicpy.const.data
import SHARED_VALUES
17 from .const
import DOMAIN
18 from .coordinator
import ScreenlogicDataUpdateCoordinator, async_get_connect_info
19 from .data
import ENTITY_MIGRATIONS
20 from .services
import async_load_screenlogic_services
21 from .util
import generate_unique_id
23 type ScreenLogicConfigEntry = ConfigEntry[ScreenlogicDataUpdateCoordinator]
26 _LOGGER = logging.getLogger(__name__)
29 REQUEST_REFRESH_DELAY = 2
30 HEATER_COOLDOWN_DELAY = 6
33 PRIMARY_CIRCUIT_IDS = [500, 505]
36 Platform.BINARY_SENSOR,
44 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
47 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
48 """Set up Screenlogic."""
56 """Set up Screenlogic from a config entry."""
60 gateway = ScreenLogicGateway()
65 await gateway.async_connect(**connect_info)
66 await gateway.async_update()
67 except ScreenLogicError
as ex:
71 hass, config_entry=entry, gateway=gateway
74 await coordinator.async_config_entry_first_refresh()
76 entry.async_on_unload(entry.add_update_listener(async_update_listener))
78 entry.runtime_data = coordinator
80 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
86 hass: HomeAssistant, entry: ScreenLogicConfigEntry
88 """Unload a config entry."""
89 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
91 coordinator = entry.runtime_data
92 await coordinator.gateway.async_disconnect()
97 hass: HomeAssistant, entry: ScreenLogicConfigEntry
99 """Handle options update."""
100 await hass.config_entries.async_reload(entry.entry_id)
104 hass: HomeAssistant, config_entry: ScreenLogicConfigEntry
106 """Migrate to new entity names."""
107 entity_registry = er.async_get(hass)
109 for entry
in er.async_entries_for_config_entry(
110 entity_registry, config_entry.entry_id
112 source_mac, source_key = entry.unique_id.split(
"_", 1)
116 len(key_parts := source_key.rsplit(
"_", 1)) == 2
117 and key_parts[1].isdecimal()
119 source_key, source_index = key_parts
122 "Checking migration status for '%s' against key '%s'",
127 if source_key
not in ENTITY_MIGRATIONS:
131 "Evaluating migration of '%s' from migration key '%s'",
135 migrations = ENTITY_MIGRATIONS[source_key]
136 updates: dict[str, Any] = {}
137 new_key = migrations[
"new_key"]
138 if new_key
in SHARED_VALUES:
139 if (device := migrations.get(
"device"))
is None:
141 "Shared key '%s' is missing required migration data 'device'",
145 if device ==
"pump" and source_index
is None:
147 "Unable to parse 'source_index' from existing unique_id for pump entity '%s'",
152 f
"{source_mac}_{generate_unique_id(device, source_index, new_key)}"
155 new_unique_id = entry.unique_id.replace(source_key, new_key)
157 if new_unique_id
and new_unique_id != entry.unique_id:
158 if existing_entity_id := entity_registry.async_get_entity_id(
159 entry.domain, entry.platform, new_unique_id
162 "Cannot migrate '%s' to unique_id '%s', already exists for entity '%s'. Aborting",
168 updates[
"new_unique_id"] = new_unique_id
170 if (old_name := migrations.get(
"old_name"))
is not None:
171 new_name = migrations[
"new_name"]
172 if (s_old_name :=
slugify(old_name))
in entry.entity_id:
173 new_entity_id = entry.entity_id.replace(s_old_name,
slugify(new_name))
174 if new_entity_id
and new_entity_id != entry.entity_id:
175 updates[
"new_entity_id"] = new_entity_id
177 if entry.original_name
and old_name
in entry.original_name:
178 new_original_name = entry.original_name.replace(old_name, new_name)
179 if new_original_name
and new_original_name != entry.original_name:
180 updates[
"original_name"] = new_original_name
184 "Migrating entity '%s' unique_id from '%s' to '%s'",
189 entity_registry.async_update_entity(entry.entity_id, **updates)
dict[str, str|int] async_get_connect_info(HomeAssistant hass, ConfigEntry entry)
def async_load_screenlogic_services(HomeAssistant hass)
None _async_migrate_entries(HomeAssistant hass, ScreenLogicConfigEntry config_entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_setup_entry(HomeAssistant hass, ScreenLogicConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ScreenLogicConfigEntry entry)
None async_update_listener(HomeAssistant hass, ScreenLogicConfigEntry entry)
str slugify(str|None text, *str separator="_")