Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smartir class #192

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion codes/light/1070.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"brightness": [
16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 255
],
"colorTemperature": [],
"commands": {
"on": "BZgjrRE/AuAXAQGDBuAFA0AB4AcTQA/gFwFAI+ALAwcQoZgj0Qg/Ag==",
"off": "B34jkRFpAhQC4BUDAWcG4AUDQC/gAxMAFCABQA8BFAJAA0ABQAfgAQMFZwYUAhQCQAfgDQMHOKF+I6MIaQI=",
Expand Down
54 changes: 6 additions & 48 deletions custom_components/smartir/climate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging

import voluptuous as vol
import voluptuous as vol # type: ignore
from numbers import Number

from homeassistant.components.climate import ClimateEntity
Expand Down Expand Up @@ -177,6 +177,8 @@ def __init__(self, hass: HomeAssistant, config: ConfigType, device_data):
self._support_flags = self._support_flags | ClimateEntityFeature.SWING_MODE
self._swing_mode = self._swing_modes[0]

self.__update_entity = self._async_update_hvac_action

async def async_added_to_hass(self):
"""Run when entity about to be added."""
await super().async_added_to_hass()
Expand Down Expand Up @@ -493,52 +495,10 @@ async def _send_command(
_LOGGER.debug("Found '%s' operation mode command.", off_mode)
await self._controller.send(self._commands[off_mode])
await asyncio.sleep(self._delay)
elif "off" in self._commands.keys() and isinstance(
self._commands["off"], str
):
if (
"on" in self._commands.keys()
and isinstance(self._commands["on"], str)
and self._commands["on"] == self._commands["off"]
and self._state == STATE_OFF
):
# prevent to resend 'off' command if same as 'on' and device is already off
_LOGGER.debug(
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
self._state,
"off",
)
else:
_LOGGER.debug("Found 'off' operation mode command.")
await self._controller.send(self._commands["off"])
await asyncio.sleep(self._delay)
else:
_LOGGER.error(
"Missing device IR code for 'off' or '%s' operation mode.",
off_mode,
)
return
await self._async_power_off()
else:
if "on" in self._commands.keys() and isinstance(
self._commands["on"], str
):
if (
"off" in self._commands.keys()
and isinstance(self._commands["off"], str)
and self._commands["off"] == self._commands["on"]
and self._state == STATE_ON
):
# prevent to resend 'on' command if same as 'off' and device is already on
_LOGGER.debug(
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
self._state,
"on",
)
else:
# if on code is not present, the on bit can be still set later in the all operation/fan codes"""
_LOGGER.debug("Found 'on' operation mode command.")
await self._controller.send(self._commands["on"])
await asyncio.sleep(self._delay)
await self._async_power_on()

commands = self._commands
if hvac_mode in commands.keys():
Expand Down Expand Up @@ -703,9 +663,7 @@ async def _send_command(
self.async_write_ha_state()

except Exception as e:
_LOGGER.exception(
"Exception raised in the in the _send_command '%s'", e
)
_LOGGER.exception("Exception raised in the in the send_command '%s'", e)

async def _async_temp_sensor_changed(
self, event: Event[EventStateChangedData]
Expand Down
151 changes: 151 additions & 0 deletions custom_components/smartir/device_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,155 @@ def check_file_media_player(file_name, device_data, device_class, check_data):

@staticmethod
def check_file_light(file_name, device_data, device_class, check_data):
color_modes = {}

if not (
"commands" in device_data
and isinstance(device_data["commands"], dict)
and len(device_data["commands"])
):
_LOGGER.error(
"Invalid %s device JSON file '%s': missing or invalid attribute 'commands'.",
device_class,
file_name,
)
return False
commands = device_data["commands"]

if "brightness" in device_data:
if not (
isinstance(device_data["brightness"], list)
and len(device_data["brightness"])
and sorted(device_data["brightness"]) == device_data["brightness"]
):
_LOGGER.error(
"Invalid %s device JSON file '%s': invalid parameter 'brightness'",
device_class,
file_name,
)
return False
color_modes["brightness"] = True
else:
for cmd in ["brightness", "dim", "brighten"]:
if cmd in commands:
_LOGGER.error(
"Invalid %s device JSON file '%s': invalid command '%s'",
device_class,
file_name,
cmd,
)
return False

if "colorTemperature" in device_data:
if not (
isinstance(device_data["colorTemperature"], list)
and len(device_data["colorTemperature"])
and sorted(device_data["colorTemperature"])
== device_data["colorTemperature"]
):
_LOGGER.error(
"Invalid %s device JSON file '%s': invalid parameter 'colorTemperature'",
device_class,
file_name,
)
return False
color_modes["color_temp"] = True
else:
for cmd in ["colorTemperature", "colder", "warmer"]:
if cmd in commands:
_LOGGER.error(
"Invalid %s device JSON file '%s': invalid command '%s'",
device_class,
file_name,
cmd,
)
return False

if "brightness" in color_modes:
if "brightness" in commands:
if not (
isinstance(commands["brightness"], list)
and len(commands["brightness"])
):
_LOGGER.error(
"Invalid %s device JSON file '%s': invalid commands 'brightness'",
device_class,
file_name,
)
return False

# check if same IR command were find in the device data
if not (
len(device_data["brightness"]) == len(commands["brightness"])
and all(
key in commands["brightness"]
for key in device_data["brightness"]
)
):
_LOGGER.info(
"Invalid %s device JSON file '%s': parameter 'brightness' and commands 'brightness' contain different keys.",
device_class,
file_name,
)
return False
elif not (
"dim" in commands
and isinstance(commands["dim"], str)
and "brighten" in commands
and isinstance(commands["brighten"], str)
):
_LOGGER.info(
"Invalid %s device JSON file '%s': parameter 'brightness' require either command 'brightness' or 'dim' and 'brighten' commands to be defined.",
device_class,
file_name,
)
return False

elif len(color_modes) > 1:
_LOGGER.error(
"Invalid %s device JSON file '%s': Any color mode require parameter 'brightness' to be defined as well",
device_class,
file_name,
)
return False

if "color_temp" in color_modes:
if "colorTemperature" in commands:
if not (
isinstance(commands["colorTemperature"], list)
and len(commands["colorTemperature"])
):
_LOGGER.error(
"Invalid %s device JSON file '%s': invalid commands 'colorTemperature'",
device_class,
file_name,
)
return False

# check if same IR command were find in the device data
if len(device_data["colorTemperature"]) == len(
commands["colorTemperature"]
) and all(
key in commands["colorTemperature"]
for key in device_data["colorTemperature"]
):
_LOGGER.info(
"Invalid %s device JSON file '%s': parameter 'colorTemperature' and commands 'colorTemperature' contain different keys.",
device_class,
file_name,
)
return False
elif not (
"colder" in commands
and isinstance(commands["colder"], str)
and "warmer" in commands
and isinstance(commands["warmer"], str)
):
_LOGGER.info(
"Invalid %s device JSON file '%s': parameter 'colorTemperature' require either command 'colorTemperature' or 'warmer' and 'colder' to be defined.",
device_class,
file_name,
)
return False

return True
64 changes: 11 additions & 53 deletions custom_components/smartir/fan.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import asyncio
import logging

import voluptuous as vol
import voluptuous as vol # type: ignore

from homeassistant.components.fan import (
FanEntity,
FanEntityFeature,
DIRECTION_REVERSE,
DIRECTION_FORWARD,
)
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant, Event, EventStateChangedData
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
Expand Down Expand Up @@ -52,7 +52,7 @@ async def async_setup_platform(
async_add_entities([SmartIRFan(hass, config, device_data)])


class SmartIRFan(FanEntity, RestoreEntity):
class SmartIRFan(SmartIR, FanEntity, RestoreEntity):
_enable_turn_on_off_backwards_compatibility = False

def __init__(self, hass: HomeAssistant, config: ConfigType, device_data):
Expand Down Expand Up @@ -161,7 +161,7 @@ async def async_set_percentage(self, percentage: int) -> None:
state = STATE_ON
speed = percentage_to_ordered_list_item(self._speed_list, percentage)

await self._send_command(
await self.send_command(
state, speed, self._current_direction, self._oscillating
)

Expand All @@ -170,7 +170,7 @@ async def async_oscillate(self, oscillating: bool) -> None:
if not self._support_flags & FanEntityFeature.OSCILLATE:
return

await self._send_command(
await self.send_command(
self._state, self._speed, self._current_direction, oscillating
)

Expand All @@ -179,7 +179,7 @@ async def async_set_direction(self, direction: str):
if not self._support_flags & FanEntityFeature.DIRECTION:
return

await self._send_command(self._state, self._speed, direction, self._oscillating)
await self.send_command(self._state, self._speed, direction, self._oscillating)

async def async_turn_on(
self, percentage: int = None, preset_mode: str = None, **kwargs
Expand All @@ -194,57 +194,17 @@ async def async_turn_off(self):
"""Turn off the fan."""
await self.async_set_percentage(0)

async def _send_command(self, state, speed, direction, oscillate):
async def send_command(self, state, speed, direction, oscillate):
async with self._temp_lock:

if self._power_sensor and self._state != state:
self._async_power_sensor_check_schedule(state)

try:
if state == STATE_OFF:
if "off" in self._commands.keys() and isinstance(
self._commands["off"], str
):
if (
"on" in self._commands.keys()
and isinstance(self._commands["on"], str)
and self._commands["on"] == self._commands["off"]
and self._state == STATE_OFF
):
# prevent to resend 'off' command if same as 'on' and device is already off
_LOGGER.debug(
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
self._state,
"off",
)
else:
_LOGGER.debug("Found 'off' operation mode command.")
await self._controller.send(self._commands["off"])
await asyncio.sleep(self._delay)
else:
_LOGGER.error("Missing device IR code for 'off' mode.")
return
await self._async_power_off()
else:
if "on" in self._commands.keys() and isinstance(
self._commands["on"], str
):
if (
"off" in self._commands.keys()
and isinstance(self._commands["off"], str)
and self._commands["off"] == self._commands["on"]
and self._state == STATE_ON
):
# prevent to resend 'on' command if same as 'off' and device is already on
_LOGGER.debug(
"As 'on' and 'off' commands are identical and device is already in requested '%s' state, skipping sending '%s' command",
self._state,
"on",
)
else:
# if on code is not present, the on bit can be still set later in the all operation/fan codes"""
_LOGGER.debug("Found 'on' operation mode command.")
await self._controller.send(self._commands["on"])
await asyncio.sleep(self._delay)
await self._async_power_on()

if oscillate:
if "oscillate" in self._commands:
Expand Down Expand Up @@ -281,6 +241,4 @@ async def _send_command(self, state, speed, direction, oscillate):
self.async_write_ha_state()

except Exception as e:
_LOGGER.exception(
"Exception raised in the in the _send_command '%s'", e
)
_LOGGER.exception("Exception raised in the in the send_command '%s'", e)
Loading
Loading