Skip to content

Commit

Permalink
Added: electricity sensors, support for IR button on light entity, su…
Browse files Browse the repository at this point in the history
…pport for empty temperature on climate entity, test mode when debugger attached.
  • Loading branch information
Haoyu-UT committed Mar 14, 2024
1 parent cd6219d commit d2e76fa
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 129 deletions.
63 changes: 45 additions & 18 deletions custom_components/nature_remo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import requests

from .const import MODE_REVERSE_MAP, Api, Appliances, NetworkError, SensorData
from .const import HVAC_MODE_REVERSE_MAP, Api, Appliances, NetworkError, SensorData
from .util import debugger_is_active

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -40,6 +41,13 @@ async def get(self, api: Api):
"""An async vesion of requests.get()"""
loop = asyncio.get_running_loop()
url = self.base_url + api.url
if debugger_is_active():
try:
from .test import mock_responses

return mock_responses[api]
except ImportError:
pass
try:
response = await loop.run_in_executor(None, blocking_get, url, self.header)
except Exception as err:
Expand All @@ -54,6 +62,14 @@ async def post(self, api: Api, params: list[str], data: dict):
"""An async vesion of requests.post()"""
loop = asyncio.get_running_loop()
url = self.base_url + api.url.format(*params)
if debugger_is_active():
try:
from .test import mock_responses

_LOGGER.debug("Posting to %s with data %s", url, str(data))
return None
except ImportError:
pass
try:
response = await loop.run_in_executor(
None, blocking_post, url, self.header, data
Expand Down Expand Up @@ -84,8 +100,12 @@ async def fecth_sensor_data(self) -> dict[str, SensorData]:
async def fetch_device_name(self) -> dict[str, str]:
"""Fetch device name for all remo devices"""
data = {}
response = await self.get(self.apis["devices"])
_LOGGER.debug(f"{self.base_url}{self.apis["devices"]} gives the following response: %s", str(response))
remote_api = self.apis["devices"]
response = await self.get(remote_api)
_LOGGER.debug(
f"{self.base_url}{remote_api.url} gives the following response: %s",
str(response),
)
for device_response in response:
mac = device_response["mac_address"]
name = device_response["name"]
Expand All @@ -95,22 +115,21 @@ async def fetch_device_name(self) -> dict[str, str]:
async def fetch_appliance(self) -> Appliances:
"""Fetch all registered appliances"""
ac_list, light_list, electricity_meter_list, others_list = [], [], [], []
response = await self.get(self.apis["appliances"])
_LOGGER.debug(f"{self.base_url}{self.apis["appliances"]} gives the following response: %s", str(response))

remote_api = self.apis["appliances"]
response = await self.get(remote_api)
_LOGGER.debug(
f"{self.base_url}{remote_api.url} gives the following response: %s",
str(response),
)
for appliance_response in response:
properties = {k: v for k, v in appliance_response.items() if v}
if "aircon" in properties:
ac_list.append(properties)
elif "light" in properties:
light_list.append(properties)
elif "smart_meter" in properties:
if any(
p["epc"] == 231
for p in properties["smart_meter"]["echonetlite_properties"]
):
electricity_meter_list.append(properties)
else:
electricity_meter_list.append(properties)
elif "signals" in properties:
others_list.append(properties)
return Appliances(ac_list, light_list, electricity_meter_list, others_list)

Expand All @@ -122,10 +141,14 @@ async def send_ac_signal(self, ac):
"""Control AC using information from AirConditioner object"""
data = {
"button": "power-off" if ac.hvac_mode == "off" else "",
"air_direction": ac.swing_mode,
"operation_mode": MODE_REVERSE_MAP[ac.last_hvac_mode],
"temperature": str(round(ac.target_temperature)),
"air_volume": ac.fan_mode,
"air_direction": ac.mode_target_swingmodepair[ac.last_hvac_mode].v,
"air_direction_h": ac.mode_target_swingmodepair[ac.last_hvac_mode].h,
"operation_mode": HVAC_MODE_REVERSE_MAP[ac.last_hvac_mode],
"temperature": ac.data.modes[ac.last_hvac_mode].temps_str[
ac.mode_target_temp_idx[ac.last_hvac_mode]
],
"air_volume": ac.mode_target_fan_mode[ac.last_hvac_mode],
"temperature_unit": "c",
}
_LOGGER.debug(data)
return await self.post(self.apis["setac"], [ac.data.id], data)
Expand All @@ -136,6 +159,10 @@ async def send_light_signal(self, app_id: str, button: str):

async def authenticate(self) -> bool:
"""Test if we can authenticate with the host"""
response = await self.get(self.apis["user"])
_LOGGER.debug(f"{self.base_url}{self.apis["user"]} gives the following response: %s", str(response))
remote_api = self.apis["user"]
response = await self.get(remote_api)
_LOGGER.debug(
f"{self.base_url}{remote_api.url} gives the following response: %s",
str(response),
)
return response is not None
Loading

0 comments on commit d2e76fa

Please sign in to comment.