Skip to content

Commit

Permalink
Merge pull request #1 from lunDreame/develop
Browse files Browse the repository at this point in the history
Refactoring and bug fixes
  • Loading branch information
lunDreame authored May 11, 2024
2 parents db69740 + 492dd50 commit 0989b54
Show file tree
Hide file tree
Showing 14 changed files with 355 additions and 402 deletions.
2 changes: 0 additions & 2 deletions custom_components/kocom_smart_home/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN].pop(entry.entry_id)

return True


11 changes: 5 additions & 6 deletions custom_components/kocom_smart_home/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import LOGGER, TIMEOUT_SEC, MAX_ROOM_CNT, MAX_SWITCH_CNT
from .const import LOGGER, TIMEOUT_SEC
from .utils import generate_digest_header, generate_fcm_token


Expand Down Expand Up @@ -131,7 +131,7 @@ async def fetch_energy_stdcheck(self, path: str = "/energy/stdcheck/") -> dict:
session = async_get_clientsession(self.hass)

await self.fetch_apartment_server_token()

headers = {
"Authorization": generate_digest_header(
self.user_credentials["user_id"],
Expand Down Expand Up @@ -274,7 +274,7 @@ async def check_device_status(self, device: str, path: str = "/control/allstatus
session = async_get_clientsession(self.hass)

await self.fetch_apartment_server_token()

headers = {
"Authorization": generate_digest_header(
self.user_credentials["user_id"],
Expand Down Expand Up @@ -342,8 +342,8 @@ async def send_control_request(self, type: str, id: str, function: str, value: s
def extract_meaningful_data(self, response: dict) -> dict:
"""Remove meaningless data from lights/concents"""
try:
max_room_cnt = self.entry.data.get(MAX_ROOM_CNT)
max_switch_cnt = self.entry.data.get(MAX_SWITCH_CNT)
max_room_cnt = self.entry.data.get("max_room_cnt")
max_switch_cnt = self.entry.data.get("max_switch_cnt")

entry_list = response.get("entry", [])
response["entry"] = [entry for entry in entry_list if int(entry.get("id", "")[2:]) <= max_room_cnt]
Expand Down Expand Up @@ -376,4 +376,3 @@ def update_device_data(self, control_response: dict):
break
except Exception as ex:
LOGGER.error("Failed to update the device settings: %s", ex)

105 changes: 57 additions & 48 deletions custom_components/kocom_smart_home/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from homeassistant.components.climate.const import PRESET_NONE, PRESET_AWAY
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature, HVACMode

from .const import DOMAIN, LOGGER, BIT_OFF, BIT_ON
from .const import DOMAIN, LOGGER
from .coordinator import KocomCoordinator
from .device import KocomEntity


async def async_setup_entry(hass, config_entry, async_add_entities):
api = hass.data[DOMAIN][config_entry.entry_id]
entities_to_add: list = []
Expand All @@ -20,41 +21,51 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entities_to_add.extend(
KocomClimate(coordinator, device) for device in devices
)

if entities_to_add:
async_add_entities(entities_to_add)

async_add_entities(entities_to_add)

class KocomClimate(KocomEntity, ClimateEntity):
def __init__(self, coordinator, device) -> None:
self.device = device
self.device_id = device.get("device_id")
self.device_name = device.get("device_name")
if self.device["device_type"] == "heat":
self.mode = HVACMode.HEAT
self._device = device
self._device_id = device["device_id"]
self._device_name = device["device_name"]
self._device_type = device["device_type"]
self._hvac_mode = [HVACMode.OFF]
if self._device_type == "heat":
self._hvac_mode.append(HVACMode.HEAT)
else:
self.mode = HVACMode.COOL
self.features = (ClimateEntityFeature.TARGET_TEMPERATURE)
self._hvac_mode.append(HVACMode.COOL)
self._features = (
ClimateEntityFeature.TARGET_TEMPERATURE|
ClimateEntityFeature.TURN_ON|
ClimateEntityFeature.TURN_OFF
)
super().__init__(coordinator)

@property
def unique_id(self) -> str:
"""Return the entity ID."""
return self.device_id
return self._device_id

@property
def name(self) -> str:
"""Return the name of the device."""
return self.device_name
return self._device_name

@property
def current_temperature(self) -> float:
"""Return the current temperature."""
return self.coordinator._is_device_state(self.device_id, "nowtemp")
status = self.coordinator.get_device_status(self._device_id, "nowtemp")
return status

@property
def target_temperature(self) -> float:
"""Return the target temperature."""
return self.coordinator._is_device_state(self.device_id, "settemp")

status = self.coordinator.get_device_status(self._device_id, "settemp")
return status

@property
def temperature_unit(self):
"""Return the unit of measurement."""
Expand All @@ -68,84 +79,82 @@ def target_temperature_step(self):
@property
def min_temp(self):
"""Min tempreature."""
return self.device["min_temp"]
return self._device["min_temp"]

@property
def max_temp(self):
"""Max tempreature."""
return self.device["max_temp"]
return self._device["max_temp"]

@property
def hvac_modes(self) -> list:
"""Return the list of available hvac operation modes."""
return [HVACMode.OFF, self.mode]
return self._hvac_mode

@property
def hvac_mode(self):
"""Return hvac operation ie. heat, cool mode."""
if self.coordinator._is_device_state(self.device_id):
return self.mode
return HVACMode.OFF
status = self.coordinator.get_device_status(self._device_id)
if status:
return self._hvac_mode[1]
else:
return self._hvac_mode[0]

@property
def preset_modes(self) -> list:
"""Return the list of available preset modes."""
if self.device["device_type"] == "heat":
if self._device_type == "heat":
return [PRESET_AWAY, PRESET_NONE]
return None
else:
return []

@property
def preset_mode(self):
"""Return the current preset mode, e.g., home, away, temp.
Requires ClimateEntityFeature.PRESET_MODE.
"""
if self.coordinator._is_device_state(self.device_id, "mode"):
status = self.coordinator.get_device_status(self._device_id, "mode")
if status:
return PRESET_AWAY
return PRESET_NONE
else:
return PRESET_NONE

@property
def supported_features(self):
"""Return the list of supported features."""
if self.device["device_type"] == "heat":
self.features |= ClimateEntityFeature.PRESET_MODE
return self.features
if self._device_type == "heat":
self._features |= ClimateEntityFeature.PRESET_MODE

return self._features

@property
def extra_state_attributes(self):
"""Return the state attributes of the sensor."""
return {
"Device room": self.device["device_room"],
"Device type": self.device["device_type"],
"Registration Date": self.device["reg_date"],
"Sync date": self.coordinator._data["sync_date"]
"Device room": self._device["device_room"],
"Device type": self._device["device_type"],
"Registration Date": self._device["reg_date"],
"Sync date": self.coordinator._device_info["sync_date"]
}

async def async_set_hvac_mode(self, hvac_mode: str) -> None:
"""Set new target hvac mode."""
if hvac_mode == self.mode:
await self.coordinator.set_device_command(self.device_id, BIT_ON)
elif hvac_mode == HVACMode.OFF:
await self.coordinator.set_device_command(self.device_id, BIT_OFF)

await self.coordinator.async_request_refresh()
if hvac_mode == self._hvac_mode[1]:
await self.coordinator.set_device_command(self._device_id, 1)
elif hvac_mode == self._hvac_mode[0]:
await self.coordinator.set_device_command(self._device_id, 0)

async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new target preset mode."""
if preset_mode == PRESET_AWAY:
await self.coordinator.set_device_command(self.device_id, BIT_ON)

await self.coordinator.set_device_command(self.device_id, BIT_ON, "mode")
await self.coordinator.set_device_command(self._device_id, 1)
await self.coordinator.set_device_command(self._device_id, 1, "mode")
elif preset_mode == PRESET_NONE:
await self.coordinator.set_device_command(self.device_id, BIT_OFF, "mode")

await self.coordinator.async_request_refresh()
await self.coordinator.set_device_command(self._device_id, 0, "mode")

async def async_set_temperature(self, **kwargs):
"""Set new target temperature."""
await self.coordinator.set_device_command(self.device_id, BIT_ON)
await self.coordinator.set_device_command(self._device_id, 1)
await self.coordinator.set_device_command(
self.device_id, kwargs.get(ATTR_TEMPERATURE, 20), "settemp"
self._device_id, kwargs.get(ATTR_TEMPERATURE, 20), "settemp"
)

await self.coordinator.async_request_refresh()

Loading

0 comments on commit 0989b54

Please sign in to comment.