1 """Support for Switchbot devices."""
25 CONNECTABLE_SUPPORTED_MODEL_TYPES,
27 HASS_SENSOR_TYPE_TO_SWITCHBOT_MODEL,
30 from .coordinator
import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
33 SupportedModels.BULB.value: [Platform.SENSOR, Platform.LIGHT],
34 SupportedModels.LIGHT_STRIP.value: [Platform.SENSOR, Platform.LIGHT],
35 SupportedModels.CEILING_LIGHT.value: [Platform.SENSOR, Platform.LIGHT],
36 SupportedModels.BOT.value: [Platform.SWITCH, Platform.SENSOR],
37 SupportedModels.PLUG.value: [Platform.SWITCH, Platform.SENSOR],
38 SupportedModels.CURTAIN.value: [
40 Platform.BINARY_SENSOR,
43 SupportedModels.HYGROMETER.value: [Platform.SENSOR],
44 SupportedModels.HYGROMETER_CO2.value: [Platform.SENSOR],
45 SupportedModels.CONTACT.value: [Platform.BINARY_SENSOR, Platform.SENSOR],
46 SupportedModels.MOTION.value: [Platform.BINARY_SENSOR, Platform.SENSOR],
47 SupportedModels.HUMIDIFIER.value: [Platform.HUMIDIFIER, Platform.SENSOR],
48 SupportedModels.LOCK.value: [
49 Platform.BINARY_SENSOR,
53 SupportedModels.LOCK_PRO.value: [
54 Platform.BINARY_SENSOR,
58 SupportedModels.BLIND_TILT.value: [
60 Platform.BINARY_SENSOR,
63 SupportedModels.HUB2.value: [Platform.SENSOR],
66 SupportedModels.CEILING_LIGHT.value: switchbot.SwitchbotCeilingLight,
67 SupportedModels.CURTAIN.value: switchbot.SwitchbotCurtain,
68 SupportedModels.BOT.value: switchbot.Switchbot,
69 SupportedModels.PLUG.value: switchbot.SwitchbotPlugMini,
70 SupportedModels.BULB.value: switchbot.SwitchbotBulb,
71 SupportedModels.LIGHT_STRIP.value: switchbot.SwitchbotLightStrip,
72 SupportedModels.HUMIDIFIER.value: switchbot.SwitchbotHumidifier,
73 SupportedModels.LOCK.value: switchbot.SwitchbotLock,
74 SupportedModels.LOCK_PRO.value: switchbot.SwitchbotLock,
75 SupportedModels.BLIND_TILT.value: switchbot.SwitchbotBlindTilt,
79 _LOGGER = logging.getLogger(__name__)
83 """Set up Switchbot from a config entry."""
84 assert entry.unique_id
is not None
85 if CONF_ADDRESS
not in entry.data
and CONF_MAC
in entry.data:
88 mac = entry.data[CONF_MAC]
90 mac = dr.format_mac(mac)
91 hass.config_entries.async_update_entry(
93 data={**entry.data, CONF_ADDRESS: mac},
97 hass.config_entries.async_update_entry(
99 options={CONF_RETRY_COUNT: DEFAULT_RETRY_COUNT},
102 sensor_type: str = entry.data[CONF_SENSOR_TYPE]
103 switchbot_model = HASS_SENSOR_TYPE_TO_SWITCHBOT_MODEL[sensor_type]
105 connectable = switchbot_model
in CONNECTABLE_SUPPORTED_MODEL_TYPES
106 address: str = entry.data[CONF_ADDRESS]
108 await switchbot.close_stale_connections_by_address(address)
110 ble_device = bluetooth.async_ble_device_from_address(
111 hass, address.upper(), connectable
115 f
"Could not find Switchbot {sensor_type} with address {address}"
118 cls = CLASS_BY_DEVICE.get(sensor_type, switchbot.SwitchbotDevice)
119 if cls
is switchbot.SwitchbotLock:
121 device = switchbot.SwitchbotLock(
123 key_id=entry.data.get(CONF_KEY_ID),
124 encryption_key=entry.data.get(CONF_ENCRYPTION_KEY),
125 retry_count=entry.options[CONF_RETRY_COUNT],
126 model=switchbot_model,
128 except ValueError
as error:
130 "Invalid encryption configuration provided"
135 password=entry.data.get(CONF_PASSWORD),
136 retry_count=entry.options[CONF_RETRY_COUNT],
145 entry.data.get(CONF_NAME, entry.title),
149 entry.async_on_unload(coordinator.async_start())
150 if not await coordinator.async_wait_ready():
153 entry.async_on_unload(entry.add_update_listener(_async_update_listener))
154 await hass.config_entries.async_forward_entry_setups(
155 entry, PLATFORMS_BY_TYPE[sensor_type]
162 """Handle options update."""
163 await hass.config_entries.async_reload(entry.entry_id)
167 """Unload a config entry."""
168 sensor_type = entry.data[CONF_SENSOR_TYPE]
169 return await hass.config_entries.async_unload_platforms(
170 entry, PLATFORMS_BY_TYPE[sensor_type]
bool async_setup_entry(HomeAssistant hass, SwitchbotConfigEntry entry)
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)