ESPHome
2026.3.0-dev
Loading...
Searching...
No Matches
esphome
components
sy6970
binary_sensor
sy6970_binary_sensor.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
../sy6970.h
"
4
#include "
esphome/components/binary_sensor/binary_sensor.h
"
5
6
namespace
esphome::sy6970
{
7
8
template
<u
int
8_t REG, u
int
8_t SHIFT, u
int
8_t MASK, u
int
8_t TRUE_VALUE>
9
class
StatusBinarySensor
:
public
SY6970Listener
,
public
binary_sensor::BinarySensor
{
10
public
:
11
void
on_data
(
const
SY6970Data
&data)
override
{
12
uint8_t value = (data.
registers
[REG] >> SHIFT) & MASK;
13
this->
publish_state
(value == TRUE_VALUE);
14
}
15
};
16
17
template
<u
int
8_t REG, u
int
8_t SHIFT, u
int
8_t MASK, u
int
8_t FALSE_VALUE>
18
class
InverseStatusBinarySensor
:
public
SY6970Listener
,
public
binary_sensor::BinarySensor
{
19
public
:
20
void
on_data
(
const
SY6970Data
&data)
override
{
21
uint8_t value = (data.
registers
[REG] >> SHIFT) & MASK;
22
this->
publish_state
(value != FALSE_VALUE);
23
}
24
};
25
26
// Custom binary sensor for charging (true when pre-charge or fast charge)
27
class
SY6970ChargingBinarySensor
:
public
SY6970Listener
,
public
binary_sensor::BinarySensor
{
28
public
:
29
void
on_data
(
const
SY6970Data
&data)
override
{
30
uint8_t chrg_stat = (data.
registers
[SY6970_REG_STATUS] >> 3) & 0x03;
31
bool
charging = chrg_stat !=
CHARGE_STATUS_NOT_CHARGING
&& chrg_stat !=
CHARGE_STATUS_CHARGE_DONE
;
32
this->
publish_state
(charging);
33
}
34
};
35
36
// Specialized sensor types using templates
37
// VBUS connected: BUS_STATUS != NO_INPUT
38
using
SY6970VbusConnectedBinarySensor
=
InverseStatusBinarySensor<SY6970_REG_STATUS, 5, 0x07, BUS_STATUS_NO_INPUT>
;
39
40
// Charge done: CHARGE_STATUS == CHARGE_DONE
41
using
SY6970ChargeDoneBinarySensor
=
StatusBinarySensor<SY6970_REG_STATUS, 3, 0x03, CHARGE_STATUS_CHARGE_DONE>
;
42
43
}
// namespace esphome::sy6970
binary_sensor.h
esphome::binary_sensor::BinarySensor
Base class for all binary_sensor-type classes.
Definition
binary_sensor.h:31
esphome::binary_sensor::BinarySensor::publish_state
void publish_state(bool new_state)
Publish a new state to the front-end.
Definition
binary_sensor.cpp:20
esphome::sy6970::InverseStatusBinarySensor
Definition
sy6970_binary_sensor.h:18
esphome::sy6970::InverseStatusBinarySensor::on_data
void on_data(const SY6970Data &data) override
Definition
sy6970_binary_sensor.h:20
esphome::sy6970::SY6970ChargingBinarySensor
Definition
sy6970_binary_sensor.h:27
esphome::sy6970::SY6970ChargingBinarySensor::on_data
void on_data(const SY6970Data &data) override
Definition
sy6970_binary_sensor.h:29
esphome::sy6970::SY6970Listener
Definition
sy6970.h:71
esphome::sy6970::StatusBinarySensor
Definition
sy6970_binary_sensor.h:9
esphome::sy6970::StatusBinarySensor::on_data
void on_data(const SY6970Data &data) override
Definition
sy6970_binary_sensor.h:11
esphome::sy6970
Definition
sy6970_binary_sensor.h:6
esphome::sy6970::CHARGE_STATUS_CHARGE_DONE
@ CHARGE_STATUS_CHARGE_DONE
Definition
sy6970.h:62
esphome::sy6970::CHARGE_STATUS_NOT_CHARGING
@ CHARGE_STATUS_NOT_CHARGING
Definition
sy6970.h:59
esphome::sy6970::SY6970Data
Definition
sy6970.h:66
esphome::sy6970::SY6970Data::registers
uint8_t registers[21]
Definition
sy6970.h:67
sy6970.h
Generated by
1.12.0