Skip to content

Commit

Permalink
Merge pull request #191 from litinoveweedle/smartir_class
Browse files Browse the repository at this point in the history
code refactoring
  • Loading branch information
litinoveweedle authored Jan 19, 2025
2 parents 8db57fd + 5c7a956 commit 653b87b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 42 deletions.
46 changes: 41 additions & 5 deletions custom_components/smartir/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
DIRECTION_FORWARD,
)
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant, Event, EventStateChangedData, callback
from homeassistant.helpers.event import async_track_state_change_event, async_call_later
from homeassistant.core import HomeAssistant, Event, EventStateChangedData
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
Expand Down Expand Up @@ -203,13 +202,50 @@ async def _send_command(self, state, speed, direction, oscillate):

try:
if state == STATE_OFF:
if "off" in self._commands:
await self._controller.send(self._commands["off"])
await asyncio.sleep(self._delay)
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
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)

if oscillate:
if "oscillate" in self._commands:
await self._controller.send(self._commands["oscillate"])
Expand Down
3 changes: 1 addition & 2 deletions custom_components/smartir/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
LightEntity,
)
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant, Event, EventStateChangedData, callback
from homeassistant.helpers.event import async_track_state_change_event, async_call_later
from homeassistant.core import HomeAssistant, Event, EventStateChangedData
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
Expand Down
72 changes: 40 additions & 32 deletions custom_components/smartir/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
MediaType,
)
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant, Event, EventStateChangedData, callback
from homeassistant.helpers.event import async_track_state_change_event, async_call_later
from homeassistant.core import HomeAssistant, Event, EventStateChangedData
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import ConfigType
Expand Down Expand Up @@ -224,40 +223,49 @@ async def _send_command(self, state, commands):

try:
if state == STATE_OFF:
if (
"on" in self._commands.keys()
and isinstance(self._commands["on"], str)
and self._commands["on"] == self._commands["off"]
and self._state == STATE_OFF
if "off" in self._commands.keys() and isinstance(
self._commands["off"], str
):
# 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",
)
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.debug("Found 'off' operation mode command.")
await self._controller.send(self._commands["off"])
await asyncio.sleep(self._delay)
_LOGGER.error("Missing device IR code for 'off' mode.")
return
else:
if (
"off" in self._commands.keys()
and isinstance(self._commands["off"], str)
and self._commands["off"] == self._commands["on"]
and self._state == STATE_ON
if "on" in self._commands.keys() and isinstance(
self._commands["on"], str
):
# 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)
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)

for keys in commands:
data = self._commands
Expand Down
4 changes: 1 addition & 3 deletions custom_components/smartir/smartir_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from homeassistant.const import (
CONF_NAME,
STATE_ON,
STATE_OFF,
STATE_UNKNOWN,
STATE_UNAVAILABLE,
STATE_OFF
)
from homeassistant.helpers.event import async_track_state_change_event, async_call_later
import homeassistant.helpers.config_validation as cv
Expand Down

0 comments on commit 653b87b

Please sign in to comment.