Skip to content

Commit

Permalink
Merge pull request #42 from xrh0905/add-humi
Browse files Browse the repository at this point in the history
Fix #32 & #40
  • Loading branch information
mypal committed Jan 16, 2023
2 parents ba4bb79 + 650a4b6 commit 41239b0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 33 deletions.
1 change: 0 additions & 1 deletion 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 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
7 changes: 4 additions & 3 deletions custom_components/ds_air/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
"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": "\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668\u5173\u8054",
"description": "\u53ef\u4ee5\u4e3a\u7a7a\u8c03\u5173\u8054\u6e29\u6e7f\u5ea6\u4f20\u611f\u5668",
"data": {
"climate": "climate name",
"sensor": "sensor entity_id"
"sensor_temp": "Temperature sensor entity_id",
"sensor_humi": "Humidity sensor entity_id"
}
}
}
Expand Down

0 comments on commit 41239b0

Please sign in to comment.