Custom integration for a weather station. Collects data from the station and populates the WeatherEntity based class I have created. This all works. I also publish a DeviceInfo and the device shows up. Can I somehow get the battery level of the weather station to be published for the device? The data is collected and parsed OK, just need to know how to publish it so it gets displayed.
The hardware is a small weather station. The data is transmitted via another app and is just the voltage of the battery. The data has being collected and reported for several years using the same app - so that’s not relevant to the question.
I’d like to simply report that in a way that allows me to view it - ideally on the Device list where there is a battery column on the right hand side?
The class is a simple class.
class WeatherDataSensor(WeatherEntity):
....
It sets the various attributes via a small async_update() function that reads the data from the station. It works well and is very simple. The device info comes from a device_info function.
Of course. I wouldn’t expect you to have that information and that wasn’t what I was asking
I have the battery level information available in HA. What I am asking is how I display that information in the front end? The WeatherEntity derived class I have written publishes a DeviceInfo and that appears as a device in the Devices tab. My question was/is how I get the battery information from that WeatherEntity to appear on the devices tab or anywhere else appropriate in HA. Do I need to publish another Entity and if so how do I link it to the DeviceInfo?
Given how many classes of devices have batteries and could publish this information I was assuming it would be a generic, well known mechanism - hence my initial generic question.
Following this through, I guess I add a new class to represent the battery (in sensor.py) with the appropriate device class function. This new class should then export the DeviceInfo rather than the WeatherEntity derived one?
Yes to add a new class for your battery sensor. For your device info, this class will also need that but you make it the same as the device infomfor your weather station. Example below of a battery sensor and link to integration i write if it helps you.
class WiserBatterySensor(WiserSensor):
"""Definition of a battery sensor for wiser iTRVs and RoomStats."""
def __init__(self, data, device_id=0, sensor_type=""):
"""Initialise the battery sensor."""
self._attr_device_class = SensorDeviceClass.BATTERY
super().__init__(data, device_id, sensor_type)
self._device = self._data.wiserhub.devices.get_by_id(self._device_id)
self._state = self._device.battery.percent
@callback
def _handle_coordinator_update(self) -> None:
"""Fetch new state data for the sensor."""
super()._handle_coordinator_update()
self._device = self._data.wiserhub.devices.get_by_id(self._device_id)
self._state = self._device.battery.percent
self.async_write_ha_state()
@property
def device_class(self):
"""Return the class of the sensor."""
return SensorDeviceClass.BATTERY
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity."""
return "%"
@property
def extra_state_attributes(self):
"""Return the state attributes of the battery."""
attrs = {}
attrs["battery_voltage"] = self._device.battery.voltage
attrs[ATTR_BATTERY_LEVEL] = self._device.battery.level
return attrs
@property
def name(self):
"""Return the name of the sensor."""
return f"{get_device_name(self._data, self._device_id)} {self._sensor_type}"
@property
def device_info(self):
"""Return device specific attributes."""
return {
"name": get_device_name(self._data, self._device_id),
"identifiers": {(DOMAIN, get_identifier(self._data, self._device_id))},
"manufacturer": MANUFACTURER,
"model": self._device.product_type,
"sw_version": self._device.firmware_version,
"via_device": (DOMAIN, self._data.wiserhub.system.name),
}