From 1aa2ce86084fb926249a70ff047b90635960d5ad Mon Sep 17 00:00:00 2001 From: oarcher Date: Wed, 7 Aug 2024 22:38:02 +0200 Subject: [PATCH] add some gnss sensors --- esphome/components/modem/sensor/__init__.py | 52 +++++++++++++++++-- .../components/modem/sensor/modem_sensor.cpp | 42 +++++++-------- .../components/modem/sensor/modem_sensor.h | 10 ++-- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/esphome/components/modem/sensor/__init__.py b/esphome/components/modem/sensor/__init__.py index c47f2db8c0..84df43ae0b 100644 --- a/esphome/components/modem/sensor/__init__.py +++ b/esphome/components/modem/sensor/__init__.py @@ -2,14 +2,19 @@ import esphome.codegen as cg from esphome.components import sensor import esphome.config_validation as cv from esphome.const import ( + CONF_ACCURACY, CONF_ALTITUDE, + CONF_COURSE, CONF_ID, CONF_LATITUDE, CONF_LONGITUDE, + CONF_SPEED, DEVICE_CLASS_SIGNAL_STRENGTH, + ENTITY_CATEGORY_DIAGNOSTIC, STATE_CLASS_MEASUREMENT, UNIT_DECIBEL, UNIT_DEGREES, + UNIT_KILOMETER_PER_HOUR, UNIT_METER, UNIT_PERCENT, ) @@ -29,6 +34,14 @@ IS_PLATFORM_COMPONENT = True CONF_BER = "ber" CONF_RSSI = "rssi" +ICON_LATITUDE = "mdi:latitude" +ICON_LONGITUDE = "mdi:longitude" +ICON_LOCATION = "mdi:map-marker" +ICON_COMPASS = "mdi:compass" +ICON_LOCATION_RADIUS = "mdi:map-marker-radius" +ICON_LOCATION_UP = "mdi:map-marker-up" +ICON_SPEED = "mdi:speedometer" +ICON_SIGNAL_BAR = "mdi:signal" modem_sensor_ns = cg.esphome_ns.namespace("modem_sensor") ModemSensorComponent = modem_sensor_ns.class_("ModemSensor", cg.PollingComponent) @@ -41,31 +54,52 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_RSSI): sensor.sensor_schema( unit_of_measurement=UNIT_DECIBEL, accuracy_decimals=0, + icon=ICON_SIGNAL_BAR, device_class=DEVICE_CLASS_SIGNAL_STRENGTH, state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), cv.Optional(CONF_BER): sensor.sensor_schema( unit_of_measurement=UNIT_PERCENT, accuracy_decimals=0, device_class=DEVICE_CLASS_SIGNAL_STRENGTH, state_class=STATE_CLASS_MEASUREMENT, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, ), cv.Optional(CONF_LATITUDE): sensor.sensor_schema( unit_of_measurement=UNIT_DEGREES, accuracy_decimals=5, - # device_class=DEVICE_CLASS_SIGNAL_STRENGTH, + icon=ICON_LATITUDE, state_class=STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_LONGITUDE): sensor.sensor_schema( unit_of_measurement=UNIT_DEGREES, accuracy_decimals=5, - # device_class=DEVICE_CLASS_SIGNAL_STRENGTH, + icon=ICON_LONGITUDE, state_class=STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ALTITUDE): sensor.sensor_schema( unit_of_measurement=UNIT_METER, accuracy_decimals=1, - # device_class=DEVICE_CLASS_SIGNAL_STRENGTH, + icon=ICON_LOCATION_UP, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_SPEED): sensor.sensor_schema( + unit_of_measurement=UNIT_KILOMETER_PER_HOUR, + accuracy_decimals=1, + icon=ICON_SPEED, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_ACCURACY): sensor.sensor_schema( + unit_of_measurement=UNIT_METER, + accuracy_decimals=1, + icon=ICON_LOCATION_RADIUS, + state_class=STATE_CLASS_MEASUREMENT, + ), + cv.Optional(CONF_COURSE): sensor.sensor_schema( + unit_of_measurement=UNIT_DEGREES, + accuracy_decimals=1, + icon=ICON_COMPASS, state_class=STATE_CLASS_MEASUREMENT, ), } @@ -112,4 +146,16 @@ async def to_code(config): altitude_sensor = await sensor.new_sensor(altitude) cg.add(var.set_altitude_sensor(altitude_sensor)) + if speed := config.get(CONF_SPEED, None): + speed_sensor = await sensor.new_sensor(speed) + cg.add(var.set_altitude_sensor(speed_sensor)) + + if course := config.get(CONF_COURSE, None): + course_sensor = await sensor.new_sensor(course) + cg.add(var.set_course_sensor(course_sensor)) + + if accuracy := config.get(CONF_ACCURACY, None): + accuracy_sensor = await sensor.new_sensor(accuracy) + cg.add(var.set_accuracy_sensor(accuracy_sensor)) + await cg.register_component(var, config) diff --git a/esphome/components/modem/sensor/modem_sensor.cpp b/esphome/components/modem/sensor/modem_sensor.cpp index 0a02a191f4..d80bc965d1 100644 --- a/esphome/components/modem/sensor/modem_sensor.cpp +++ b/esphome/components/modem/sensor/modem_sensor.cpp @@ -76,7 +76,7 @@ std::map get_gnssinfo_tokens(const std::string &gnss_i std::map gnss_data; if (data.find(",,,,,,") != std::string::npos) { - // no data recieved + ESP_LOGW(TAG, "No GNSS location available"); return gnss_data; } @@ -202,19 +202,19 @@ void ModemSensor::update_gnss_sensors_() { int minute = std::stoi(parts["time"].substr(2, 2)); int second = std::stoi(parts["time"].substr(4, 2)); - ESP_LOGD(TAG, "Latitude: %f, Longitude: %f", lat, lon); - ESP_LOGD(TAG, "Altitude: %f m", alt); - ESP_LOGD(TAG, "Speed: %f km/h", speed_kmh); - ESP_LOGD(TAG, "COG: %f degrees", cog); - ESP_LOGD(TAG, "PDOP: %f", pdop); - ESP_LOGD(TAG, "HDOP: %f", hdop); - ESP_LOGD(TAG, "VDOP: %f", vdop); - ESP_LOGD(TAG, "GPS SVs: %d", gps_svs); - ESP_LOGD(TAG, "GLONASS SVs: %d", glonass_svs); - ESP_LOGD(TAG, "BEIDOU SVs: %d", beidou_svs); - ESP_LOGD(TAG, "Fix mode: %d", mode); - ESP_LOGD(TAG, "Date: %04d-%02d-%02d", year, month, day); - ESP_LOGD(TAG, "Time: %02d:%02d:%02d", hour, minute, second); + ESP_LOGV(TAG, "Latitude: %f, Longitude: %f", lat, lon); + ESP_LOGV(TAG, "Altitude: %f m", alt); + ESP_LOGV(TAG, "Speed: %f km/h", speed_kmh); + ESP_LOGV(TAG, "COG: %f degrees", cog); + ESP_LOGV(TAG, "PDOP: %f", pdop); + ESP_LOGV(TAG, "HDOP: %f", hdop); + ESP_LOGV(TAG, "VDOP: %f", vdop); + ESP_LOGV(TAG, "GPS SVs: %d", gps_svs); + ESP_LOGV(TAG, "GLONASS SVs: %d", glonass_svs); + ESP_LOGV(TAG, "BEIDOU SVs: %d", beidou_svs); + ESP_LOGV(TAG, "Fix mode: %d", mode); + ESP_LOGV(TAG, "Date: %04d-%02d-%02d", year, month, day); + ESP_LOGV(TAG, "Time: %02d:%02d:%02d", hour, minute, second); // Sensors update if (this->gnss_latitude_sensor_) @@ -225,16 +225,10 @@ void ModemSensor::update_gnss_sensors_() { this->gnss_altitude_sensor_->publish_state(alt); if (this->gnss_speed_sensor_) this->gnss_speed_sensor_->publish_state(speed_kmh); - if (this->gnss_cog_sensor_) - this->gnss_cog_sensor_->publish_state(speed_kmh); - if (this->gnss_pdop_sensor_) - this->gnss_pdop_sensor_->publish_state(pdop); - if (this->gnss_hdop_sensor_) - this->gnss_hdop_sensor_->publish_state(hdop); - if (this->gnss_vdop_sensor_) - this->gnss_vdop_sensor_->publish_state(vdop); - if (this->gnss_mode_sensor_) - this->gnss_mode_sensor_->publish_state(mode); + if (this->gnss_course_sensor_) + this->gnss_course_sensor_->publish_state(cog); + if (this->gnss_accuracy_sensor_) + this->gnss_accuracy_sensor_->publish_state(hdop * 5); } } } diff --git a/esphome/components/modem/sensor/modem_sensor.h b/esphome/components/modem/sensor/modem_sensor.h index 07dc6b1a75..e5cc4abf5c 100644 --- a/esphome/components/modem/sensor/modem_sensor.h +++ b/esphome/components/modem/sensor/modem_sensor.h @@ -24,6 +24,9 @@ class ModemSensor : public PollingComponent { void set_latitude_sensor(sensor::Sensor *latitude_sensor) { this->gnss_latitude_sensor_ = latitude_sensor; } void set_longitude_sensor(sensor::Sensor *longitude_sensor) { this->gnss_longitude_sensor_ = longitude_sensor; } void set_altitude_sensor(sensor::Sensor *altitude_sensor) { this->gnss_altitude_sensor_ = altitude_sensor; } + void set_course_sensor(sensor::Sensor *course_sensor) { this->gnss_course_sensor_ = course_sensor; } + void set_speed_sensor(sensor::Sensor *speed_sensor) { this->gnss_speed_sensor_ = speed_sensor; } + void set_accuracy_sensor(sensor::Sensor *accuracy_sensor) { this->gnss_accuracy_sensor_ = accuracy_sensor; } #endif // USE_MODEM_GNSS // ========== INTERNAL METHODS ========== @@ -44,11 +47,8 @@ class ModemSensor : public PollingComponent { sensor::Sensor *gnss_longitude_sensor_{nullptr}; sensor::Sensor *gnss_altitude_sensor_{nullptr}; sensor::Sensor *gnss_speed_sensor_{nullptr}; - sensor::Sensor *gnss_cog_sensor_{nullptr}; - sensor::Sensor *gnss_pdop_sensor_{nullptr}; - sensor::Sensor *gnss_hdop_sensor_{nullptr}; - sensor::Sensor *gnss_vdop_sensor_{nullptr}; - sensor::Sensor *gnss_mode_sensor_{nullptr}; + sensor::Sensor *gnss_course_sensor_{nullptr}; + sensor::Sensor *gnss_accuracy_sensor_{nullptr}; void update_gnss_sensors_(); #endif // USE_MODEM_GNSS };