1 """The SwitchBee Smart Home integration."""
3 from __future__
import annotations
8 from aiohttp
import ClientSession
9 from switchbee.api
import CentralUnitPolling, CentralUnitWsRPC, is_wsrpc_api
10 from switchbee.api.central_unit
import SwitchBeeError
20 from .const
import DOMAIN
21 from .coordinator
import SwitchBeeCoordinator
23 _LOGGER = logging.getLogger(__name__)
26 PLATFORMS: list[Platform] = [
36 central_unit: str, user: str, password: str, websession: ClientSession
37 ) -> CentralUnitPolling | CentralUnitWsRPC:
38 """Return SwitchBee API object."""
40 api: CentralUnitPolling | CentralUnitWsRPC = CentralUnitPolling(
41 central_unit, user, password, websession
46 except SwitchBeeError
as exp:
51 api = CentralUnitWsRPC(central_unit, user, password, websession)
58 """Set up SwitchBee Smart Home from a config entry."""
60 hass.data.setdefault(DOMAIN, {})
61 central_unit = entry.data[CONF_HOST]
62 user = entry.data[CONF_USERNAME]
63 password = entry.data[CONF_PASSWORD]
65 api = await
get_api_object(central_unit, user, password, websession)
72 await coordinator.async_config_entry_first_refresh()
73 entry.async_on_unload(entry.add_update_listener(update_listener))
74 hass.data[DOMAIN][entry.entry_id] = coordinator
76 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
82 """Unload a config entry."""
83 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
84 hass.data[DOMAIN].pop(entry.entry_id)
89 async
def update_listener(hass: HomeAssistant, config_entry: ConfigEntry) ->
None:
90 """Update listener."""
91 await hass.config_entries.async_reload(config_entry.entry_id)
95 """Migrate old entry."""
96 _LOGGER.debug(
"Migrating from version %s", config_entry.version)
98 if config_entry.version == 1:
99 dev_reg = dr.async_get(hass)
101 old_unique_id = config_entry.unique_id
102 assert isinstance(old_unique_id, str)
104 config_entry.data[CONF_HOST],
105 config_entry.data[CONF_USERNAME],
106 config_entry.data[CONF_PASSWORD],
109 new_unique_id = api.unique_id
112 def update_unique_id(entity_entry: er.RegistryEntry) -> dict[str, str] |
None:
113 """Update unique ID of entity entry."""
114 if match := re.match(
115 rf
"(?:{old_unique_id})-(?P<id>\d+)", entity_entry.unique_id
117 entity_new_unique_id = f
'{new_unique_id}-{match.group("id")}'
119 "Migrating entity %s from %s to new id %s",
120 entity_entry.entity_id,
121 entity_entry.unique_id,
122 entity_new_unique_id,
124 return {
"new_unique_id": entity_new_unique_id}
130 for device_entry
in dr.async_entries_for_config_entry(
131 dev_reg, config_entry.entry_id
133 assert isinstance(device_entry, dr.DeviceEntry)
134 for identifier
in device_entry.identifiers:
135 if match := re.match(
136 rf
"(?P<id>.+)-{old_unique_id}$", identifier[1]
141 f
"{match.group('id')}-{new_unique_id}",
145 "Migrating device %s identifiers from %s to %s",
147 device_entry.identifiers,
150 dev_reg.async_update_device(
151 device_entry.id, new_identifiers=new_identifiers
155 await er.async_migrate_entries(
156 hass, config_entry.entry_id, update_unique_id
159 hass.config_entries.async_update_entry(config_entry, version=2)
161 _LOGGER.debug(
"Migration to version %s successful", config_entry.version)
dict[str, str]|None update_unique_id(er.RegistryEntry entity_entry, str unique_id)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry config_entry)
None update_listener(HomeAssistant hass, ConfigEntry config_entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
CentralUnitPolling|CentralUnitWsRPC get_api_object(str central_unit, str user, str password, ClientSession websession)
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)