1 """Support for SleepIQ from SleepNumber."""
3 from __future__
import annotations
8 from asyncsleepiq
import (
11 SleepIQLoginException,
12 SleepIQTimeoutException,
14 import voluptuous
as vol
25 from .const
import DOMAIN, IS_IN_BED, SLEEP_NUMBER
26 from .coordinator
import (
28 SleepIQDataUpdateCoordinator,
29 SleepIQPauseUpdateCoordinator,
32 _LOGGER = logging.getLogger(__name__)
35 Platform.BINARY_SENSOR,
44 CONFIG_SCHEMA = vol.Schema(
47 vol.Required(CONF_USERNAME): cv.string,
48 vol.Required(CONF_PASSWORD): cv.string,
51 extra=vol.ALLOW_EXTRA,
55 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
56 """Set up sleepiq component."""
58 hass.async_create_task(
59 hass.config_entries.flow.async_init(
60 DOMAIN, context={
"source": SOURCE_IMPORT}, data=config[DOMAIN]
68 """Set up the SleepIQ config entry."""
70 email = conf[CONF_USERNAME]
71 password = conf[CONF_PASSWORD]
75 gateway = AsyncSleepIQ(client_session=client_session)
78 await gateway.login(email, password)
79 except SleepIQLoginException
as err:
80 _LOGGER.error(
"Could not authenticate with SleepIQ server")
82 except SleepIQTimeoutException
as err:
84 str(err)
or "Timed out during authentication"
88 await gateway.init_beds()
89 except SleepIQTimeoutException
as err:
91 str(err)
or "Timed out during initialization"
93 except SleepIQAPIException
as err:
102 await coordinator.async_config_entry_first_refresh()
103 await pause_coordinator.async_config_entry_first_refresh()
105 hass.data.setdefault(DOMAIN, {})[entry.entry_id] =
SleepIQData(
106 data_coordinator=coordinator,
107 pause_coordinator=pause_coordinator,
111 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
117 """Unload the config entry."""
118 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
119 hass.data[DOMAIN].pop(entry.entry_id)
124 hass: HomeAssistant, entry: ConfigEntry, gateway: AsyncSleepIQ
126 """Migrate old unique ids."""
128 sleeper.name: sleeper.sleeper_id
129 for bed
in gateway.beds.values()
130 for sleeper
in bed.sleepers
133 bed_ids = {bed.id
for bed
in gateway.beds.values()}
136 def _async_migrator(entity_entry: er.RegistryEntry) -> dict[str, Any] |
None:
139 sensor_types = [IS_IN_BED, PRESSURE, SLEEP_NUMBER]
141 old_unique_id = entity_entry.unique_id
142 parts = old_unique_id.split(
"_")
146 if parts[0]
not in bed_ids
or not old_unique_id.endswith(
tuple(sensor_types)):
149 sensor_type = next(filter(old_unique_id.endswith, sensor_types))
150 sleeper_name =
"_".join(parts[1:]).removesuffix(f
"_{sensor_type}")
151 sleeper_id = names_to_ids.get(sleeper_name)
156 new_unique_id = f
"{sleeper_id}_{sensor_type}"
159 "Migrating unique_id from [%s] to [%s]",
163 return {
"new_unique_id": new_unique_id}
165 await er.async_migrate_entries(hass, entry.entry_id, _async_migrator)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None _async_migrate_unique_ids(HomeAssistant hass, ConfigEntry entry, AsyncSleepIQ gateway)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)