openshell-esphome-components/components/pcf85063/time.py

159 lines
4.6 KiB
Python

import esphome.config_validation as cv
import esphome.codegen as cg
from esphome import automation
from esphome.components import i2c, time
from esphome.const import CONF_ID, CONF_INTERVAL, CONF_VALUE
CODEOWNERS = ["@brogon"]
DEPENDENCIES = ["i2c"]
pcf85063_ns = cg.esphome_ns.namespace("pcf85063")
PCF85063Component = pcf85063_ns.class_(
"PCF85063Component", time.RealTimeClock, i2c.I2CDevice
)
WriteAction = pcf85063_ns.class_("WriteAction", automation.Action)
ReadAction = pcf85063_ns.class_("ReadAction", automation.Action)
StartTimerAction = pcf85063_ns.class_("StartTimerAction", automation.Action)
ClearTimerFlagAction = pcf85063_ns.class_("ClearTimerFlagAction", automation.Action)
ClearAlarmFlagAction = pcf85063_ns.class_("ClearAlarmFlagAction", automation.Action)
SetClockoutFrequencyAction = pcf85063_ns.class_("SetClockoutFrequencyAction", automation.Action)
CONFIG_SCHEMA = time.TIME_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(PCF85063Component),
}
).extend(i2c.i2c_device_schema(0x51))
#####
@automation.register_action(
"pcf85063.write_time",
WriteAction,
cv.Schema(
{
cv.GenerateID(): cv.use_id(PCF85063Component),
}
),
)
async def pcf85063_write_time_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
#####
@automation.register_action(
"pcf85063.read_time",
ReadAction,
automation.maybe_simple_id(
{
cv.GenerateID(): cv.use_id(PCF85063Component),
}
),
)
async def pcf85063_read_time_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
#####
def validate_timer_seconds(value):
value: cv.TimePeriodSeconds = cv.positive_time_period_seconds(value)
min_interval = cv.TimePeriod(seconds=1)
max_interval = cv.TimePeriod(minutes=255)
if value < min_interval:
raise cv.Invalid(
f"This timer interval is not possible, please choose a larger interval (at least {min_interval})"
)
if value > max_interval:
raise cv.Invalid(
f"This timer interval is not possible, please choose a lower interval (at most {max_interval})"
)
return value
@automation.register_action(
"pcf85063.start_timer",
StartTimerAction,
cv.Schema(
{
cv.GenerateID(): cv.use_id(PCF85063Component),
cv.Required(CONF_INTERVAL): cv.templatable(validate_timer_seconds),
}
),
)
async def pcf85063_start_timer_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
template_ = await cg.templatable(config[CONF_INTERVAL], args, cv.TimePeriodSeconds)
cg.add(var.set_timer_seconds(template_))
await cg.register_parented(var, config[CONF_ID])
return var
#####
def validate_clockout_frequency(value):
value = cv.int_range(0b000, 0b111)(value)
return value
@automation.register_action(
"pcf85063.clockout_frequency",
SetClockoutFrequencyAction,
cv.Schema(
{
cv.GenerateID(): cv.use_id(PCF85063Component),
cv.Required(CONF_VALUE): cv.templatable(validate_clockout_frequency),
}
),
)
async def pcf85063_clockout_frequency_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
template_ = await cg.templatable(config[CONF_VALUE], args, type(int))
cg.add(var.set_freq(template_))
await cg.register_parented(var, config[CONF_ID])
return var
#####
@automation.register_action(
"pcf85063.clear_timer_flag",
ClearTimerFlagAction,
cv.Schema(
{
cv.GenerateID(): cv.use_id(PCF85063Component),
}
),
)
async def pcf85063_clear_timer_flag_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
#####
@automation.register_action(
"pcf85063.clear_alarm_flag",
ClearAlarmFlagAction,
cv.Schema(
{
cv.GenerateID(): cv.use_id(PCF85063Component),
}
),
)
async def pcf85063_clear_alarm_flag_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
return var
#####
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
await time.register_time(var, config)