Jelajahi Sumber

Add sensor to show number of connected clients

Oxan van Leeuwen 3 tahun lalu
induk
melakukan
43ade65d08

+ 14 - 11
components/stream_server/sensor.py

@@ -17,7 +17,8 @@ import esphome.codegen as cg
 import esphome.config_validation as cv
 from esphome.components import sensor
 from esphome.const import (
-	ENTITY_CATEGORY_DIAGNOSTIC,
+    STATE_CLASS_MEASUREMENT,
+    ENTITY_CATEGORY_DIAGNOSTIC,
 )
 from . import ns, StreamServerComponent
 
@@ -25,17 +26,19 @@ CONF_CONNECTION_COUNT = "connection_count"
 CONF_STREAM_SERVER = "stream_server"
 
 CONFIG_SCHEMA = cv.Schema(
-	{
-		cv.GenerateID(CONF_STREAM_SERVER): cv.use_id(StreamServerComponent),
-		cv.Required(CONF_CONNECTION_COUNT): sensor.sensor_schema(
-			accuracy_decimals=0,
-			entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
-		)
-	}
+    {
+        cv.GenerateID(CONF_STREAM_SERVER): cv.use_id(StreamServerComponent),
+        cv.Required(CONF_CONNECTION_COUNT): sensor.sensor_schema(
+            accuracy_decimals=0,
+            state_class=STATE_CLASS_MEASUREMENT,
+            entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
+        ),
+    }
 )
 
+
 async def to_code(config):
-	server = await cg.get_variable(config[CONF_STREAM_SERVER])
+    server = await cg.get_variable(config[CONF_STREAM_SERVER])
 
-	sens = await sensor.new_sensor(config[CONF_CONNECTION_COUNT])
-	cg.add(server.set_connection_count_sensor(sens))
+    sens = await sensor.new_sensor(config[CONF_CONNECTION_COUNT])
+    cg.add(server.set_connection_count_sensor(sens))

+ 7 - 0
components/stream_server/stream_server.cpp

@@ -56,6 +56,9 @@ void StreamServerComponent::dump_config() {
 #ifdef USE_BINARY_SENSOR
     LOG_BINARY_SENSOR("  ", "Connected:", this->connected_sensor_);
 #endif
+#ifdef USE_SENSOR
+    LOG_SENSOR("  ", "Connection count:", this->connection_count_sensor_);
+#endif
 }
 
 void StreamServerComponent::on_shutdown() {
@@ -68,6 +71,10 @@ void StreamServerComponent::publish_sensor() {
     if (this->connected_sensor_)
         this->connected_sensor_->publish_state(this->clients_.size() > 0);
 #endif
+#ifdef USE_SENSOR
+    if (this->connection_count_sensor_)
+        this->connection_count_sensor_->publish_state(this->clients_.size());
+#endif
 }
 
 void StreamServerComponent::accept() {

+ 9 - 0
components/stream_server/stream_server.h

@@ -23,6 +23,9 @@
 #ifdef USE_BINARY_SENSOR
 #include "esphome/components/binary_sensor/binary_sensor.h"
 #endif
+#ifdef USE_BINARY_SENSOR
+#include "esphome/components/sensor/sensor.h"
+#endif
 
 #include <memory>
 #include <string>
@@ -38,6 +41,9 @@ public:
 #ifdef USE_BINARY_SENSOR
     void set_connected_sensor(esphome::binary_sensor::BinarySensor *connected) { this->connected_sensor_ = connected; }
 #endif
+#ifdef USE_SENSOR
+    void set_connection_count_sensor(esphome::sensor::Sensor *connection_count) { this->connection_count_sensor_ = connection_count; }
+#endif
 
     void setup() override;
     void loop() override;
@@ -77,6 +83,9 @@ protected:
 #ifdef USE_BINARY_SENSOR
     esphome::binary_sensor::BinarySensor *connected_sensor_;
 #endif
+#ifdef USE_SENSOR
+    esphome::sensor::Sensor *connection_count_sensor_;
+#endif
 
     std::unique_ptr<uint8_t[]> buf_{};
     size_t buf_head_{0};