Skip to content

Commit

Permalink
[BREAKING CHANGE]Attempt to support muli-instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
xrh0905 committed Aug 18, 2024
1 parent e78ba05 commit 63b651f
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 139 deletions.
8 changes: 5 additions & 3 deletions custom_components/ds_air/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.core import HomeAssistant

from .hass_inst import GetHass
from .const import CONF_GW, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_GW, DOMAIN
from .const import CONF_GW, CONF_ID, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_GW, DOMAIN
from .ds_air_service.config import Config

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -46,7 +46,8 @@ async def async_setup_entry(
Config.is_c611 = gw == DEFAULT_GW

from .ds_air_service.service import Service
await hass.async_add_executor_job(Service.init, host, port, scan_interval)
instance_id = entry.data[CONF_ID] # Use CONF_ID as the instance ID
await hass.async_add_executor_job(Service.init, instance_id, host, port, scan_interval)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))

Expand All @@ -59,7 +60,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].get("listener")()
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
from .ds_air_service.service import Service
Service.destroy()
instance_id = entry.data[CONF_ID] # Use CONF_ID as the instance ID
Service.destroy(instance_id)

return unload_ok

Expand Down
24 changes: 13 additions & 11 deletions custom_components/ds_air/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event

from .const import DOMAIN
from .const import DOMAIN, CONF_ID
from .ds_air_service.config import Config
from .ds_air_service.ctrl_enum import EnumControl
from .ds_air_service.dao import AirCon, AirConStatus
Expand Down Expand Up @@ -56,10 +56,11 @@ async def async_setup_entry(
) -> None:
"""Set up the climate devices."""

instance_id = entry.data[CONF_ID] # Use CONF_ID as the instance ID
from .ds_air_service.service import Service
climates = []
for aircon in Service.get_aircons():
climates.append(DsAir(aircon))
for aircon in Service.get_aircons(instance_id):
climates.append(DsAir(aircon, instance_id))
async_add_entities(climates)
link = entry.options.get("link")
sensor_temp_map: dict[str, list[DsAir]] = {}
Expand Down Expand Up @@ -93,7 +94,7 @@ class DsAir(ClimateEntity):

_enable_turn_on_off_backwards_compatibility = False # used in 2024.2~2024.12

def __init__(self, aircon: AirCon):
def __init__(self, aircon: AirCon, instance_id: str):
_log('create aircon:')
_log(str(aircon.__dict__))
_log(str(aircon.status.__dict__))
Expand All @@ -107,6 +108,7 @@ def __init__(self, aircon: AirCon):
self._link_cur_humi = False
self._cur_temp = None
self._cur_humi = None
self._instance_id = instance_id
from .ds_air_service.service import Service
Service.register_status_hook(aircon, self._status_change_hook)

Expand Down Expand Up @@ -331,7 +333,7 @@ def set_temperature(self, **kwargs):
status.setted_temp = round(kwargs.get(ATTR_TEMPERATURE) * 10.0)
new_status.setted_temp = round(kwargs.get(ATTR_TEMPERATURE) * 10.0)
from .ds_air_service.service import Service
Service.control(self._device_info, new_status)
Service.control(self._instance_id, self._device_info, new_status)
self.schedule_update_ha_state()

def set_humidity(self, humidity):
Expand All @@ -343,7 +345,7 @@ def set_humidity(self, humidity):
status.humidity = EnumControl.Humidity(humidity)
new_status.humidity = EnumControl.Humidity(humidity)
from .ds_air_service.service import Service
Service.control(self._device_info, new_status)
Service.control(self._instance_id, self._device_info, new_status)
self.schedule_update_ha_state()

def set_fan_mode(self, fan_mode):
Expand All @@ -355,7 +357,7 @@ def set_fan_mode(self, fan_mode):
status.air_flow = EnumControl.get_air_flow_enum(fan_mode)
new_status.air_flow = EnumControl.get_air_flow_enum(fan_mode)
from .ds_air_service.service import Service
Service.control(self._device_info, new_status)
Service.control(self._instance_id, self._device_info, new_status)
self.schedule_update_ha_state()

def set_hvac_mode(self, hvac_mode: str) -> None:
Expand All @@ -367,7 +369,7 @@ def set_hvac_mode(self, hvac_mode: str) -> None:
status.switch = EnumControl.Switch.OFF
new_status.switch = EnumControl.Switch.OFF
from .ds_air_service.service import Service
Service.control(self._device_info, new_status)
Service.control(self._instance_id, self._device_info, new_status)
else:
status.switch = EnumControl.Switch.ON
new_status.switch = EnumControl.Switch.ON
Expand Down Expand Up @@ -399,7 +401,7 @@ def set_hvac_mode(self, hvac_mode: str) -> None:
status.mode = mode
new_status.mode = mode
from .ds_air_service.service import Service
Service.control(self._device_info, new_status)
Service.control(self._instance_id, self._device_info, new_status)
self.schedule_update_ha_state()

def set_swing_mode(self, swing_mode):
Expand All @@ -412,7 +414,7 @@ def set_swing_mode(self, swing_mode):
status.fan_direction2 = EnumControl.get_fan_direction_enum(swing_mode)
new_status.fan_direction2 = EnumControl.get_fan_direction_enum(swing_mode)
from .ds_air_service.service import Service
Service.control(self._device_info, new_status)
Service.control(self._instance_id, self._device_info, new_status)
self.schedule_update_ha_state()

def set_preset_mode(self, preset_mode: str) -> None:
Expand All @@ -436,7 +438,7 @@ def set_preset_mode(self, preset_mode: str) -> None:
status.mode = mode
new_status.mode = mode
from .ds_air_service.service import Service
Service.control(self._device_info, new_status)
Service.control(self._instance_id, self._device_info, new_status)
self.schedule_update_ha_state()

def turn_aux_heat_on(self) -> None:
Expand Down
18 changes: 9 additions & 9 deletions custom_components/ds_air/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from homeassistant.core import HomeAssistant, 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, CONF_ID, DEFAULT_ID, DEFAULT_GW, DEFAULT_HOST, DEFAULT_PORT, DOMAIN, GW_LIST
from .ds_air_service.service import Service
from .hass_inst import GetHass

Expand All @@ -42,14 +42,12 @@ def __init__(self):
self.user_input = {}

async def async_step_user(self, user_input: dict[str, Any] | None = None) -> FlowResult:
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

errors = {}
if user_input is not None:
instance_id = self.context.get("instance_id", self.flow_id)
self.context["instance_id"] = instance_id
self.user_input.update(user_input)
if user_input.get(CONF_SENSORS) == False or user_input.get("temp") is not None:
return self.async_create_entry(title="金制空气", data=self.user_input)
return self.async_create_entry(title="金制空气@"+str(self.user_input[CONF_HOST]), data=self.user_input)
else:
return self.async_show_form(
step_id="user",
Expand All @@ -64,21 +62,22 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo
vol.Required("hcho", default=False): bool,
}
),
errors=errors,
errors={},
)

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_ID, default=DEFAULT_ID): str,
vol.Required(CONF_HOST, default=DEFAULT_HOST): str,
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
vol.Required(CONF_GW, default=DEFAULT_GW): vol.In(GW_LIST),
vol.Required(CONF_SCAN_INTERVAL, default=5): int,
vol.Required(CONF_SENSORS, default=True): bool,
}
),
errors=errors,
errors={},
)

@staticmethod
Expand All @@ -96,7 +95,7 @@ def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
self.config_entry = config_entry
self._config_data = []
hass: HomeAssistant = GetHass.get_hash()
self._climates = list(map(lambda state: state.alias, Service.get_aircons()))
self._climates = list(map(lambda state: state.alias, Service.get_aircons(config_entry.entry_id)))
sensors = hass.states.async_all("sensor")
self._sensors_temp = {
None: 'None',
Expand Down Expand Up @@ -144,6 +143,7 @@ async def async_step_adjust_config(self, user_input: dict[str, Any] | None = Non
step_id="adjust_config",
data_schema=vol.Schema(
{
vol.Required(CONF_ID, default=DEFAULT_ID): str,
vol.Required(CONF_HOST, default=self.config_entry.data[CONF_HOST]): str,
vol.Required(CONF_PORT, default=self.config_entry.data[CONF_PORT]): int,
vol.Required(CONF_GW, default=self.config_entry.data[CONF_GW]): vol.In(GW_LIST),
Expand Down
2 changes: 2 additions & 0 deletions custom_components/ds_air/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

DOMAIN = "ds_air"
CONF_GW = "gw"
CONF_ID = "eid"
DEFAULT_ID = "gw1"
DEFAULT_HOST = "192.168.1."
DEFAULT_PORT = 8008
DEFAULT_GW = "DTA117C611"
Expand Down
3 changes: 2 additions & 1 deletion custom_components/ds_air/ds_air_service/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def __init__(self):
self.room_id: int = 0
self.unit_id: int = 0
self.mac: str = ""
self.instance_id: str = ""

@property
def unique_id(self):
return "daikin_%d_%d" % (self.room_id, self.unit_id)
return "daikin_%s_%d_%d" % (self.instance_id, self.room_id, self.unit_id)


def _nothing():
Expand Down
Loading

0 comments on commit 63b651f

Please sign in to comment.