-
Notifications
You must be signed in to change notification settings - Fork 3
修复多网关下相同分机编号的实体冲突 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
41c0fb0
1792994
7fd2b62
6720117
447b4e5
52d17ac
37dd537
dbd6ea1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,15 @@ | |
| from homeassistant.core import callback | ||
| from homeassistant.data_entry_flow import FlowResult | ||
|
|
||
| from .const import CONF_GW, DEFAULT_GW, DEFAULT_HOST, DEFAULT_PORT, DOMAIN, GW_LIST | ||
| from .const import ( | ||
| CONF_GW, | ||
| DEFAULT_GW, | ||
| DEFAULT_HOST, | ||
| DEFAULT_PORT, | ||
| DOMAIN, | ||
| GW_LIST, | ||
| get_default_gateway_name, | ||
| ) | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -30,7 +38,7 @@ def _log(s: str) -> None: | |
|
|
||
|
|
||
| class DsAirFlowHandler(ConfigFlow, domain=DOMAIN): | ||
| VERSION = 1 | ||
| VERSION = 2 | ||
|
|
||
| def __init__(self): | ||
| self.user_input = {} | ||
|
|
@@ -42,7 +50,10 @@ async def async_step_user( | |
| if user_input is not None: | ||
| self.user_input.update(user_input) | ||
| if not user_input.get(CONF_SENSORS) or user_input.get("temp") is not None: | ||
| return self.async_create_entry(title="金制空气", data=self.user_input) | ||
| return self.async_create_entry( | ||
| title=get_default_gateway_name(), | ||
| data=self.user_input, | ||
| ) | ||
|
Comment on lines
+53
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用IP地址作为实体名称有些奇怪,实际上最好的方案是用金制空气APP中的名称或者MAC地址,不过相关方法目前没有实现。我也想过改成“智能网关”作为默认名,不过还涉及到一些本地化的修改,就暂时没动。 |
||
|
|
||
| return self.async_show_form( | ||
| step_id="user", | ||
|
|
@@ -88,7 +99,8 @@ def __init__(self, config_entry: ConfigEntry) -> None: | |
| """Initialize options flow.""" | ||
| self._config_entry = config_entry | ||
| self._config_data = [] | ||
| self._climates: list[str] = [] # set in async_step_init | ||
| self._climates: dict[str, str] = {} # set in async_step_init | ||
| self._climate_ids: list[str] = [] # set in async_step_init | ||
| self._len: int = 0 # set in async_step_init | ||
| self._sensors_temp: dict[str, str] = {} | ||
| self._sensors_humi: dict[str, str] = {} | ||
|
|
@@ -100,8 +112,13 @@ async def async_step_init( | |
| ) -> FlowResult: | ||
| """Manage the options.""" | ||
| service = self.hass.data[DOMAIN][self._config_entry.entry_id] | ||
| self._climates = [state.alias for state in service.get_aircons()] | ||
| self._len = len(self._climates) | ||
| host = self._config_entry.data[CONF_HOST] | ||
| self._climates = { | ||
| state.unique_id: f"{state.alias} ({host} {state.room_id}-{state.unit_id:02d})" | ||
| for state in service.get_aircons() | ||
| } | ||
| self._climate_ids = list(self._climates) | ||
| self._len = len(self._climate_ids) | ||
|
|
||
| sensors = self.hass.states.async_all("sensor") | ||
| self._sensors_temp = { | ||
|
|
@@ -201,18 +218,20 @@ async def async_step_bind_sensors( | |
| self._cur = self._cur + 1 | ||
| if self._cur > (self._len - 1): | ||
| return self.async_create_entry(title="", data={"link": self._config_data}) | ||
| cur_climate: str = self._climates[self._cur] | ||
| cur_climate: str = self._climate_ids[self._cur] | ||
| cur_links = self._config_entry.options.get("link", []) | ||
| cur_link = next( | ||
| (link for link in cur_links if link["climate"] == cur_climate), None | ||
| (link for link in cur_links if link.get("climate") == cur_climate), None | ||
| ) | ||
|
Comment on lines
223
to
225
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 与气候实体绑定类似,在编辑选项(OptionsFlow)时, service = self.hass.data[DOMAIN][self._config_entry.entry_id]
aircon = next((ac for ac in service.get_aircons() if ac.unique_id == cur_climate), None)
cur_link = next(
(
link for link in cur_links
if link.get("climate") == cur_climate
or (aircon and link.get("climate") == aircon.alias)
),
None
)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当前为了避免代码过度复杂化,没有加向后兼容的逻辑。如果后续讨论认为有这个必要,可以再加入完整的向后兼容代码。 |
||
| cur_sensor_temp = cur_link.get("sensor_temp") if cur_link else None | ||
| cur_sensor_humi = cur_link.get("sensor_humi") if cur_link else None | ||
| return self.async_show_form( | ||
| step_id="bind_sensors", | ||
| data_schema=vol.Schema( | ||
| { | ||
| vol.Required("climate", default=cur_climate): vol.In([cur_climate]), | ||
| vol.Required("climate", default=cur_climate): vol.In( | ||
| {cur_climate: self._climates[cur_climate]} | ||
| ), | ||
| vol.Optional("sensor_temp", default=cur_sensor_temp): vol.In( | ||
| self._sensors_temp | ||
| ), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| class Config: | ||
| gateway_id: str = "" | ||
| is_new_version: bool = False | ||
| is_c611: bool = True # 金制空气c611 or ds-air b611 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对于已经配置了传感器绑定的现有用户,其绑定关系中存储的是空调的
alias(名称)而不是新的unique_id。升级到此版本后,由于climate_by_unique_id.get(climate_id)会返回None,这些已有的绑定关系将会静默失效。建议在此处增加一个基于alias匹配的兼容逻辑,以确保向后兼容性。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前为了避免代码过度复杂化,没有加向后兼容的逻辑。如果后续讨论认为有这个必要,可以再加入完整的向后兼容代码。