ESPHome-Devices/components/tuya_dimmer_as_fan/fan.py
Chris Nussbaum 1b6d90d399
A bunch of misc changes (#32)
* Temporarily disable the heater

* Shell of new garage fridge component

* Core code should be mostly complete

* Add power monitoring and freezer temp control

* Wrap up core temperature management code

* Add fixed InkBird component

* The new fridge manages temp in the freezer better so we are able to greatly simplify things

* Finish garage fridge

* Misc changes

Co-authored-by: Chris Nussbaum <chris.nussbaum@protolabs.com>
2022-03-29 13:54:34 -05:00

69 lines
2.3 KiB
Python

from esphome.components import fan, sensor
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.components.tuya import tuya_ns, CONF_TUYA_ID, Tuya
from esphome.const import (
CONF_OUTPUT_ID,
CONF_SWITCH_DATAPOINT,
CONF_POWER,
UNIT_WATT,
DEVICE_CLASS_POWER,
STATE_CLASS_MEASUREMENT,
ICON_POWER,
CONF_UPDATE_INTERVAL,
)
DEPENDENCIES = ["tuya"]
CONF_DIMMER_DATAPOINT = "dimmer_datapoint"
CONF_MAX_VALUE = "dimmer_max_value"
CONF_FAN_WATTAGE = "fan_wattage"
TuyaFan = tuya_ns.class_("TuyaDimmerAsFan", cg.PollingComponent, fan.Fan)
CONFIG_SCHEMA = cv.All(
fan.FAN_SCHEMA.extend(
{
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(TuyaFan),
cv.GenerateID(CONF_TUYA_ID): cv.use_id(Tuya),
cv.Required(CONF_SWITCH_DATAPOINT): cv.uint8_t,
cv.Required(CONF_DIMMER_DATAPOINT): cv.uint8_t,
cv.Optional(CONF_MAX_VALUE): cv.int_,
cv.Optional(CONF_POWER): sensor.sensor_schema(
unit_of_measurement=UNIT_WATT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
icon=ICON_POWER,
).extend(
{
cv.Optional(CONF_FAN_WATTAGE): cv.positive_float,
cv.Optional(CONF_UPDATE_INTERVAL, default="60s"): cv.update_interval,
}
),
}
).extend(cv.COMPONENT_SCHEMA),
)
async def to_code(config):
parent = await cg.get_variable(config[CONF_TUYA_ID])
var = cg.new_Pvariable(config[CONF_OUTPUT_ID], parent)
await cg.register_component(var, config)
await fan.register_fan(var, config)
cg.add(var.set_switch_id(config[CONF_SWITCH_DATAPOINT]))
cg.add(var.set_dimmer_id(config[CONF_DIMMER_DATAPOINT]))
if CONF_MAX_VALUE in config:
cg.add(var.set_dimmer_max_value(config[CONF_MAX_VALUE]))
if CONF_POWER in config:
power_config = config[CONF_POWER]
power_sensor = await sensor.new_sensor(power_config)
cg.add(var.set_fan_wattage(power_config[CONF_FAN_WATTAGE]))
cg.add(var.set_power_sensor(power_sensor))
cg.add(var.set_update_interval(power_config[CONF_UPDATE_INTERVAL]))
else:
cg.add(var.set_update_interval(4294967295)) # uint32_t max