Skip to content

Commit

Permalink
Initial switch implementation (not working yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlo Dudnytskyi committed Jul 31, 2024
1 parent 60d926c commit 2ba4c95
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 35 deletions.
65 changes: 65 additions & 0 deletions components/haier/switch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import switch
from esphome.const import (
CONF_BEEPER,
CONF_DISPLAY,
ENTITY_CATEGORY_CONFIG,
)
from ..climate import (
CONF_HAIER_ID,
HaierClimateBase,
HonClimate,
haier_ns,
)

CODEOWNERS = ["@paveldn"]
BeeperSwitch = haier_ns.class_("BeeperSwitch", switch.Switch)
HealthModeSwitch = haier_ns.class_("HealthModeSwitch", switch.Switch)
DisplaySwitch = haier_ns.class_("DisplaySwitch", switch.Switch)

# Haier switches
CONF_HEALTH_MODE = "health_mode"

# Additional icons
ICON_LEAF = "mdi:leaf"
ICON_LED_ON = "mdi:led-on"
ICON_VOLUME_HIGH = "mdi:volume-high"

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_HAIER_ID): cv.use_id(HaierClimateBase),
cv.Optional(CONF_DISPLAY): switch.switch_schema(
DisplaySwitch,
icon=ICON_LED_ON,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_HEALTH_MODE): switch.switch_schema(
HealthModeSwitch,
icon=ICON_LEAF,
),
cv.Optional(CONF_BEEPER): switch.switch_schema(
BeeperSwitch,
icon=ICON_VOLUME_HIGH,
entity_category=ENTITY_CATEGORY_CONFIG,
),
}
)

from esphome.cpp_generator import MockObjClass


async def to_code(config):
full_id, parent = await cg.get_variable_with_full_id(config[CONF_HAIER_ID])

for switch_type in [CONF_DISPLAY, CONF_HEALTH_MODE]:
if conf := config.get(switch_type):
swtch = await switch.new_switch(conf)
await cg.register_parented(swtch, parent)
if conf := config.get(CONF_BEEPER):
if full_id.type is HonClimate:
swtch = await switch.new_switch(conf)
await cg.register_parented(swtch, parent)
else:
raise ValueError("Beeper switch is only supported for hon climate")

15 changes: 3 additions & 12 deletions configs/switch/beeper.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
switch:
- platform: template
id: ${device_id}_beeper_switch
entity_category: config
name: ${device_name} beeper
icon: mdi:volume-high
restore_mode: RESTORE_DEFAULT_ON
lambda: |-
return id(${device_id}).get_beeper_state();
turn_on_action:
climate.haier.beeper_on: ${device_id}
turn_off_action:
climate.haier.beeper_off: ${device_id}
- platform: haier
beeper:
name: ${device_name} beeper
15 changes: 3 additions & 12 deletions configs/switch/display.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
switch:
- platform: template
id: ${device_id}_display_switch
name: ${device_name} display
icon: mdi:led-on
entity_category: config
restore_mode: RESTORE_DEFAULT_ON
lambda: |-
return id(${device_id}).get_display_state();
turn_on_action:
climate.haier.display_on: ${device_id}
turn_off_action:
climate.haier.display_off: ${device_id}
- platform: haier
display:
name: ${device_name} display
14 changes: 3 additions & 11 deletions configs/switch/health_mode.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
switch:
- platform: template
id: ${device_id}_health_mode
name: ${device_name} health mode
icon: mdi:leaf
restore_mode: RESTORE_DEFAULT_OFF
lambda: |-
return id(${device_id}).get_health_mode();
turn_on_action:
climate.haier.health_on: ${device_id}
turn_off_action:
climate.haier.health_off: ${device_id}
- platform: haier
health_mode:
name: ${device_name} health mode

0 comments on commit 2ba4c95

Please sign in to comment.