Skip to content

Commit

Permalink
Merge pull request #17 from joshepw/feature/add_new_entities
Browse files Browse the repository at this point in the history
Added support to select HVAC modes
  • Loading branch information
joshepw committed Oct 8, 2023
2 parents 3a39781 + ec81137 commit 15d50ae
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 44 deletions.
30 changes: 10 additions & 20 deletions custom_components/switchbotremote/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,28 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
customize_commands = options.get("customize_commands", "")

if (remote.type in IR_CAMERA_TYPES):
entities.append(SwitchBotRemoteButton(
hass, remote, "SHUTTER", "mdi:camera-iris"))
entities.append(SwitchBotRemoteButton(
hass, remote, "MENU", "mdi:menu"))
entities.append(SwitchBotRemoteButton(
hass, remote, "TIMER", "mdi:timer"))
entities.append(SwitchBotRemoteButton(hass, remote, "SHUTTER", "mdi:camera-iris"))
entities.append(SwitchBotRemoteButton(hass, remote, "MENU", "mdi:menu"))
entities.append(SwitchBotRemoteButton(hass, remote, "TIMER", "mdi:timer"))

if (remote.type in IR_FAN_TYPES):
if (options.get("with_ion", False)):
entities.append(SwitchBotRemoteButton(
hass, remote, "ION", "mdi:air-filter"))
entities.append(SwitchBotRemoteButton(hass, remote, "ION", "mdi:air-filter"))
if (options.get("with_timer", False)):
entities.append(SwitchBotRemoteButton(
hass, remote, "TIMER", "mdi:timer"))
entities.append(SwitchBotRemoteButton(hass, remote, "TIMER", "mdi:timer"))

if (remote.type in IR_LIGHT_TYPES):
if (options.get("with_brightness", False)):
entities.append(SwitchBotRemoteButton(
hass, remote, "DARKER", "mdi:brightness-4"))
entities.append(SwitchBotRemoteButton(
hass, remote, "BRIGHTER", "mdi:brightness-6"))
entities.append(SwitchBotRemoteButton(hass, remote, "DARKER", "mdi:brightness-4"))
entities.append(SwitchBotRemoteButton(hass, remote, "BRIGHTER", "mdi:brightness-6"))

if (options.get("with_temperature", False)):
entities.append(SwitchBotRemoteButton(
hass, remote, "WARM", "mdi:octagram-minus"))
entities.append(SwitchBotRemoteButton(
hass, remote, "WHITE", "mdi:octagram-plus"))
entities.append(SwitchBotRemoteButton(hass, remote, "WARM", "mdi:octagram-minus"))
entities.append(SwitchBotRemoteButton(hass, remote, "WHITE", "mdi:octagram-plus"))

for command in customize_commands:
if (command and command.strip()):
entities.append(SwitchBotRemoteButton(
hass, remote, command, "mdi:remote"))
entities.append(SwitchBotRemoteButton(hass, remote, command, "mdi:remote"))

async_add_entities(entities)

Expand Down
20 changes: 8 additions & 12 deletions custom_components/switchbotremote/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from homeassistant.config_entries import ConfigEntry
from .client.remote import SupportedRemote

from .const import (DOMAIN, IR_CLIMATE_TYPES, AIR_CONDITIONER_CLASS)
from .const import DOMAIN, IR_CLIMATE_TYPES, AIR_CONDITIONER_CLASS
from .config_flow import DEFAULT_HVAC_MODES

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -52,13 +53,10 @@ def __init__(self, sb: SupportedRemote, options: dict = {}) -> None:
self.options = options

self._last_on_operation = None
self._operation_modes = [
HVACMode.OFF,
HVACMode.COOL,
HVACMode.DRY,
HVACMode.FAN_ONLY,
HVACMode.HEAT,
]
self._operation_modes = options.get("hvac_modes", DEFAULT_HVAC_MODES)

if HVACMode.OFF not in self._operation_modes:
self._operation_modes.append(HVACMode.OFF)

self._hvac_mode = HVACMode.OFF

Expand Down Expand Up @@ -282,10 +280,8 @@ async def async_added_to_hass(self):
if last_state is not None:
self._hvac_mode = last_state.state
self._fan_mode = last_state.attributes.get('fan_mode') or FAN_AUTO
self._target_temperature = last_state.attributes.get(
'temperature') or 28
self._last_on_operation = last_state.attributes.get(
'last_on_operation')
self._target_temperature = last_state.attributes.get('temperature') or 28
self._last_on_operation = last_state.attributes.get('last_on_operation')

if self._temperature_sensor:
async_track_state_change(self.hass, self._temperature_sensor, self._async_temp_sensor_changed)
Expand Down
26 changes: 20 additions & 6 deletions custom_components/switchbotremote/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.selector import selector
from homeassistant.components.climate.const import HVACMode
from .client import SwitchBot

from .const import (
Expand All @@ -27,6 +28,22 @@
OTHERS_CLASS,
)

DEFAULT_HVAC_MODES = [
HVACMode.AUTO,
HVACMode.COOL,
HVACMode.DRY,
HVACMode.FAN_ONLY,
HVACMode.HEAT,
]

HVAC_MODES = [
{"label": "Auto", "value": HVACMode.AUTO},
{"label": "Cool", "value": HVACMode.COOL},
{"label": "Dry", "value": HVACMode.DRY},
{"label": "Fan Only", "value": HVACMode.FAN_ONLY},
{"label": "Heat", "value": HVACMode.HEAT},
]

_LOGGER = logging.getLogger(__name__)

STEP_USER_DATA_SCHEMA = vol.Schema(
Expand All @@ -49,6 +66,7 @@
vol.Optional("temp_min", default=x.get("temp_min", 16)): int,
vol.Optional("temp_max", default=x.get("temp_max", 30)): int,
vol.Optional("temp_step", default=x.get("temp_step", 1.0)): selector({"number": {"min": 0.1, "max": 2.0, "step": 0.1, "mode": "slider"}}),
vol.Optional("hvac_modes", default=x.get("hvac_modes", DEFAULT_HVAC_MODES)): vol.All(selector({"select": {"multiple": True, "options": HVAC_MODES}})),
vol.Optional("customize_commands", default=x.get("customize_commands", [])): selector({"select": {"multiple": True, "custom_value": True, "options": []}}),
}),
MEDIA_CLASS: lambda x: vol.Schema({
Expand Down Expand Up @@ -146,8 +164,7 @@ def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
self.config_entry = config_entry

self.data = config_entry.data
self.sb = SwitchBot(
token=self.data["token"], secret=self.data["secret"])
self.sb = SwitchBot(token=self.data["token"], secret=self.data["secret"])
self.discovered_devices = []
self.selected_device = None

Expand All @@ -168,10 +185,7 @@ async def async_step_init(self, user_input: dict[str, Any] | None = None) -> Flo
for remote in self.discovered_devices:
devices[remote.id] = remote.name

return self.async_show_form(
step_id="init",
data_schema=vol.Schema({vol.Required("selected_device"): vol.In(devices)})
)
return self.async_show_form(step_id="init", data_schema=vol.Schema({vol.Required("selected_device"): vol.In(devices)}))

async def async_step_edit_device(self, user_input=None):
"""Handle editing a device."""
Expand Down
3 changes: 1 addition & 2 deletions custom_components/switchbotremote/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from homeassistant.helpers.event import async_track_state_change
from .client.remote import SupportedRemote

from .const import (DOMAIN, IR_FAN_TYPES, FAN_CLASS,
AIR_PURIFIER_TYPE, DIY_AIR_PURIFIER_TYPE)
from .const import DOMAIN, IR_FAN_TYPES, FAN_CLASS, AIR_PURIFIER_TYPE, DIY_AIR_PURIFIER_TYPE

_LOGGER = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/switchbotremote/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, STATE_OFF, STATE_ON
from .client.remote import SupportedRemote

from .const import (DOMAIN, IR_LIGHT_TYPES, LIGHT_CLASS)
from .const import DOMAIN, IR_LIGHT_TYPES, LIGHT_CLASS

_LOGGER = logging.getLogger(__name__)

Expand Down
8 changes: 5 additions & 3 deletions custom_components/switchbotremote/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"@KiraPC",
"@joshepw"
],
"config_flow": true,
"dependencies": [],
"documentation": "https://www.home-assistant.io/integrations/switchbotremote",
"integration_type": "hub",
"config_flow": true,
"documentation": "https://github.com/KiraPC/ha-switchbot-remote#readme",
"issue_tracker": "https://github.com/KiraPC/ha-switchbot-remote/issues",
"iot_class": "cloud_push",
"requirements": [
"pyhumps"
],
"version": "0.0.1-alfa.2"
"version": "1.0.0"
}
1 change: 1 addition & 0 deletions custom_components/switchbotremote/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"humidity_sensor": "Humidity sensor ID to be used as air conditioner actual humidity",
"power_sensor": "Power sensor ID to get device status",
"customize_commands": "Button names (case sensitive)",
"hvac_modes": "Supported modes",
"temp_min": "Minimum temperature",
"temp_max": "Maximum temperature",
"temp_step": "Temperature step factor",
Expand Down
1 change: 1 addition & 0 deletions custom_components/switchbotremote/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"humidity_sensor": "ID del sensor de humedad que se utilizará como humedad real del aire acondicionado",
"power_sensor": "ID del sensor de encendido para obtener el estado del dispositivo",
"customize_commands": "Nombre de botones (distingue mayúsculas y minúsculas)",
"hvac_modes": "Modos soportados",
"temp_min": "Temperatura mínima",
"temp_max": "Temperatura máxima",
"temp_step": "Factor de pasos en temperatura",
Expand Down
1 change: 1 addition & 0 deletions custom_components/switchbotremote/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"humidity_sensor": "ID sensore umidità da utilizzare",
"power_sensor": "ID sensore per indicare se dispositivo è accesa o spenta",
"customize_commands": "Nomi dei pulsanti (maiuscole e minuscole)",
"hvac_modes": "Modalità supportate",
"temp_min": "Temperatura minima",
"temp_max": "Temperatura massima",
"temp_step": "Fattore di incremento della temperatura",
Expand Down

0 comments on commit 15d50ae

Please sign in to comment.