Skip to content

Commit

Permalink
Merge pull request #47 from mypal/test
Browse files Browse the repository at this point in the history
version 1.3.3
  • Loading branch information
mypal authored Feb 6, 2023
2 parents 6838f05 + c469979 commit 3a721d5
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 53 deletions.
3 changes: 1 addition & 2 deletions custom_components/ds_air/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def _log(s: str):
for i in s.split("\n"):
_LOGGER.debug(i)


def setup(hass, config):
hass.data[DOMAIN] = {}
GetHass.set_hass(hass)
Expand All @@ -48,7 +47,7 @@ async def async_setup_entry(

from .ds_air_service.service import Service
await hass.async_add_executor_job(Service.init, host, port, scan_interval)
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(update_listener))

return True
Expand Down
68 changes: 50 additions & 18 deletions custom_components/ds_air/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
SUPPORT_SWING_MODE,
SUPPORT_TARGET_HUMIDITY, HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL, HVAC_MODE_HEAT_COOL, HVAC_MODE_AUTO,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY)
HVAC_MODE_FAN_ONLY,
FAN_AUTO, FAN_LOW, FAN_MEDIUM, FAN_HIGH)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS, ATTR_TEMPERATURE, CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant, Event
Expand All @@ -33,7 +34,8 @@

SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE | SUPPORT_SWING_MODE \
| SUPPORT_SWING_MODE | SUPPORT_TARGET_HUMIDITY
FAN_LIST = ['最弱', '稍弱', '中等', '稍强', '最强', '自动']
#FAN_LIST = ['最弱', '稍弱', '中等', '稍强', '最强', '自动']
FAN_LIST = [FAN_LOW, '稍弱', FAN_MEDIUM, '稍强', FAN_HIGH, FAN_AUTO]
SWING_LIST = ['➡️', '↘️', '⬇️', '↙️', '⬅️', '↔️', '🔄']

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
Expand All @@ -43,50 +45,67 @@

_LOGGER = logging.getLogger(__name__)


def _log(s: str):
s = str(s)
for i in s.split("\n"):
_LOGGER.debug(i)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the Demo climate devices."""
"""Set up the climate devices."""

from .ds_air_service.service import Service
climates = []
for aircon in Service.get_aircons():
climates.append(DsAir(aircon))
async_add_entities(climates)
link = entry.options.get("link")
sensor_map = {}
sensor_temp_map = {}
sensor_humi_map = {}
if link is not None:
for i in link:
if i.get("sensor") is not None:
if i.get("sensor_temp") is not None:
climate = None
for j in climates:
if i.get("climate") == j.name:
climate = j
break
if sensor_map.get(i.get("sensor")) is not None:
sensor_map[i.get("sensor")].append(climate)
if sensor_temp_map.get(i.get("sensor_temp")) is not None:
sensor_temp_map[i.get("sensor_temp")].append(climate)
else:
sensor_map[i.get("sensor")] = [climate]
sensor_temp_map[i.get("sensor_temp")] = [climate]
if i.get("sensor_humi") is not None:
climate = None
for j in climates:
if i.get("climate") == j.name:
climate = j
break
if sensor_humi_map.get(i.get("sensor_humi")) is not None:
sensor_humi_map[i.get("sensor_humi")].append(climate)
else:
sensor_humi_map[i.get("sensor_humi")] = [climate]

async def listener(event: Event):
for climate in sensor_map[event.data.get("entity_id")]:
climate.update_cur_temp(event.data.get("new_state").state)
async def listner(event: Event):
if event.data.get("entity_id") in sensor_temp_map:
for climate in sensor_temp_map[event.data.get("entity_id")]:
climate.update_cur_temp(event.data.get("new_state").state)
elif event.data.get("entity_id") in sensor_humi_map:
for climate in sensor_humi_map[event.data.get("entity_id")]:
climate.update_cur_humi(event.data.get("new_state").state)

remove_listener = async_track_state_change_event(hass, list(sensor_map.keys()), listener)
remove_listener = async_track_state_change_event(hass, list(sensor_temp_map.keys()) + list(sensor_humi_map.keys()), listner)
hass.data[DOMAIN]["listener"] = remove_listener
for entity_id in sensor_map.keys():
for entity_id in sensor_temp_map.keys():
state = hass.states.get(entity_id)
if state is not None:
for climate in sensor_map[entity_id]:
for climate in sensor_temp_map[entity_id]:
climate.update_cur_temp(state.state)

for entity_id in sensor_humi_map.keys():
state = hass.states.get(entity_id)
if state is not None:
for climate in sensor_humi_map[entity_id]:
climate.update_cur_humi(state.state)

class DsAir(ClimateEntity):
"""Representation of a demo climate device."""
Expand All @@ -100,7 +119,9 @@ def __init__(self, aircon: AirCon):
self._device_info = aircon
self._unique_id = aircon.unique_id
self._link_cur_temp = False
self._link_cur_humi = False
self._cur_temp = None
self._cur_humi = None
from .ds_air_service.service import Service
Service.register_status_hook(aircon, self._status_change_hook)

Expand Down Expand Up @@ -144,6 +165,14 @@ def update_cur_temp(self, value):
"""Ignore"""
self.schedule_update_ha_state()

def update_cur_humi(self, value):
self._link_cur_humi = value is not None
try:
self._cur_humi = int(float(value))
except ValueError:
"""Ignore"""
self.schedule_update_ha_state()

@property
def should_poll(self):
"""Return the polling state."""
Expand Down Expand Up @@ -234,7 +263,10 @@ def target_temperature_low(self):
@property
def current_humidity(self):
"""Return the current humidity."""
return None
if self._link_cur_humi:
return self._cur_humi
else:
return None

@property
def preset_mode(self) -> Optional[str]:
Expand Down
16 changes: 10 additions & 6 deletions custom_components/ds_air/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,25 @@ async def async_step_user(
def async_get_options_flow(
config_entry: ConfigEntry,
) -> DsAirOptionsFlowHandler:
"""Options callback for AccuWeather."""
"""Options callback for DS-AIR."""
return DsAirOptionsFlowHandler(config_entry)


class DsAirOptionsFlowHandler(config_entries.OptionsFlow):
"""Config flow options for AccuWeather."""
"""Config flow options for sensors binding."""

def __init__(self, entry: ConfigEntry) -> None:
"""Initialize AccuWeather options flow."""
"""Initialize DSAir options flow."""
self.config_entry = entry
self._len = 3
self._cur = 0
hass: HomeAssistant = GetHass.get_hash()
self._climates = list(map(lambda state: state.alias, Service.get_aircons()))
sensors = hass.states.async_all("sensor")
self._sensors = list(map(lambda state: state.entity_id,
self._sensors_temp = list(map(lambda state: state.entity_id,
filter(lambda state: state.attributes.get("device_class") == "temperature", sensors)))
self._sensors_humi = list(map(lambda state: state.entity_id,
filter(lambda state: state.attributes.get("device_class") == "humidity", sensors)))
self._config_data = []

async def async_step_init(
Expand All @@ -111,7 +113,8 @@ async def async_step_user(
if user_input is not None:
self._config_data.append({
"climate": user_input.get("climate"),
"sensor": user_input.get("sensor")
"sensor_temp": user_input.get("sensor_temp"),
"sensor_humi": user_input.get("sensor_humi")
})
if self._cur == self._len:
return self.async_create_entry(title="", data={"link": self._config_data})
Expand All @@ -124,7 +127,8 @@ async def async_step_user(
"climate",
default=self._climates[self._cur]
): vol.In([self._climates[self._cur]]),
vol.Optional("sensor"): vol.In(self._sensors)
vol.Optional("sensor_temp"): vol.In(self._sensors_temp),
vol.Optional("sensor_humi"): vol.In(self._sensors_humi)
}
)
)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/ds_air/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from homeassistant.const import TEMP_CELSIUS, PERCENTAGE, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, \
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER, DEVICE_CLASS_HUMIDITY, \
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_CO2
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_CO2, DEVICE_CLASS_PM25

from .ds_air_service.ctrl_enum import EnumSensor

Expand All @@ -13,7 +13,7 @@
SENSOR_TYPES = {
"temp": [TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE, 10],
"humidity": [PERCENTAGE, None, DEVICE_CLASS_HUMIDITY, 10],
"pm25": [CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, None, None, 1],
"pm25": [CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, None, DEVICE_CLASS_PM25, 1],
"co2": [CONCENTRATION_PARTS_PER_MILLION, None, DEVICE_CLASS_CO2, 1],
"tvoc": [CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER, None, None, 100],
"voc": [None, None, None, EnumSensor.Voc],
Expand Down
7 changes: 4 additions & 3 deletions custom_components/ds_air/ds_air_service/ctrl_enum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from enum import Enum, IntEnum

from homeassistant.components.climate.const import \
HVAC_MODE_COOL, HVAC_MODE_FAN_ONLY, HVAC_MODE_HEAT, HVAC_MODE_DRY, HVAC_MODE_AUTO, HVAC_MODE_HEAT_COOL
HVAC_MODE_COOL, HVAC_MODE_FAN_ONLY, HVAC_MODE_HEAT, HVAC_MODE_DRY, HVAC_MODE_AUTO, HVAC_MODE_HEAT_COOL, \
FAN_AUTO, FAN_LOW, FAN_MEDIUM, FAN_HIGH


class EnumCmdType(IntEnum):
Expand Down Expand Up @@ -235,8 +236,8 @@ class AirFlow(IntEnum):
AUTO = 5


_AIR_FLOW_NAME_LIST = ['最弱', '稍弱', '中等', '稍强', '最强', '自动']

#_AIR_FLOW_NAME_LIST = ['最弱', '稍弱', '中等', '稍强', '最强', '自动']
_AIR_FLOW_NAME_LIST = [FAN_LOW, '稍弱', FAN_MEDIUM, '稍强', FAN_HIGH, FAN_AUTO]

class Breathe(IntEnum):
CLOSE = 0
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ds_air/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"dependencies": [],
"codeowners": [],
"requirements": [],
"version": "1.3.2",
"version": "1.3.3",
"config_flow": true
}
43 changes: 22 additions & 21 deletions custom_components/ds_air/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@
"config": {
"step": {
"user": {
"title": "\u91d1\u5236\u7a7a\u6c14",
"description": "\u672c\u63d2\u4ef6\u652f\u6301\u5927\u91d1DTA117B611/DTA117C611\u4e24\u6b3e\u7a7a\u8c03\u7f51\u5173",
"title": "DS-AIR",
"description": "Support DTA117B611/DTA117C611",
"data": {
"host": "host",
"port": "port",
"gw": "\u7f51\u5173\u578b\u53f7",
"scan_interval": "\u4f20\u611f\u5668\u66f4\u65b0\u9891\u7387\uff08\u5355\u4f4d\uff1a\u5206\u949f\uff09",
"sensors": "\u662f\u5426\u6709\u4f20\u611f\u5668",
"temp": "\u521b\u5efatemperature\u5b9e\u4f53",
"humidity": "\u521b\u5efahumidity\u5b9e\u4f53",
"pm25": "\u521b\u5efapm25\u5b9e\u4f53",
"co2": "\u521b\u5efaco2\u5b9e\u4f53",
"tvoc": "\u521b\u5efatvoc\u5b9e\u4f53",
"voc": "\u521b\u5efavoc\u5b9e\u4f53",
"hcho": "\u521b\u5efahcho\u5b9e\u4f53"
"host": "Host",
"port": "Port",
"gw": "Gateway model",
"scan_interval": "Sensor update frequency (minutes)",
"sensors": "Has sensor",
"temp": "Create Temperature entity",
"humidity": "Create Humidity entity",
"pm25": "Create PM2.5 entity",
"co2": "Create CO2 entity",
"tvoc": "Create TVOC entity",
"voc": "Create VOC entity",
"hcho": "Create HCHO entity"
}
}
},
"error": {
},
"abort": {
"single_instance_allowed": "\u53ea\u5141\u8bb8\u521b\u5efa\u4e00\u4e2a"
"single_instance_allowed": "Only one instance is allowed."
},
"flow_title": "\u91d1\u5236\u7a7a\u6c14"
"flow_title": "DS-AIR"
},
"options": {
"step": {
"user": {
"title": "\u6e29\u5ea6\u4f20\u611f\u5668\u5173\u8054",
"description": "\u53ef\u4ee5\u4e3a\u7a7a\u8c03\u5173\u8054\u6e29\u5ea6\u4f20\u611f\u5668",
"title": "Binding sensors",
"description": "Bind sensors to climate",
"data": {
"climate": "climate name",
"sensor": "sensor entity_id"
"climate": "Climate name",
"sensor_temp": "Temperature sensor entity_id",
"sensor_humi": "Humidity sensor entity_id"
}
}
}
}
}
}
43 changes: 43 additions & 0 deletions custom_components/ds_air/translations/zh-Hans.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"config": {
"step": {
"user": {
"title": "金制空气",
"description": "本插件支持大金DTA117B611/DTA117C611两款空调网关",
"data": {
"host": "网关IP",
"port": "网关端口",
"gw": "网关型号",
"scan_interval": "传感器更新频率(单位:分钟)",
"sensors": "是否有传感器",
"temp": "创建温度实体",
"humidity": "创建湿度实体",
"pm25": "创建PM2.5实体",
"co2": "创建CO2实体",
"tvoc": "创建tvoc实体",
"voc": "创建voc实体",
"hcho": "创建hcho实体"
}
}
},
"error": {
},
"abort": {
"single_instance_allowed": "只允许创建一个实例"
},
"flow_title": "金制空气"
},
"options": {
"step": {
"user": {
"title": "传感器关联",
"description": "为空调关联温湿度传感器",
"data": {
"climate": "空调名称",
"sensor_temp": "温度传感器实体ID",
"sensor_humi": "湿度传感器实体ID"
}
}
}
}
}

0 comments on commit 3a721d5

Please sign in to comment.