Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding battery level to attributes #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions custom_components/cometblue/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)

ATTR_BATTERY = 'battery_level'

DEVICE_SCHEMA = vol.Schema({
vol.Required(CONF_MAC): cv.string,
Expand Down Expand Up @@ -132,6 +133,11 @@ def current_temperature(self):
"""Return current temperature"""
return self._thermostat.current_temperature

@property
def battery_level(self):
"""Return battery"""
return self._thermostat.battery_level

@property
def target_temperature(self):
"""Return the temperature we try to reach."""
Expand Down Expand Up @@ -174,6 +180,13 @@ def set_hvac_mode(self,hvac_mode):
def hvac_modes(self):
return (HVAC_MODE_HEAT, HVAC_MODE_AUTO)

@property
def device_state_attributes(self):
"""Return the device specific state attributes."""
return {
ATTR_BATTERY: self._thermostat.battery_level
}

def update(self):
"""Update the data from the thermostat."""
_LOGGER.info("Update called {}".format(self._mac))
Expand Down
19 changes: 14 additions & 5 deletions custom_components/cometblue/cometblue.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@

PASSWORD_HANDLE = 0x48
TEMPERATURE_HANDLE = 0x3d
BATTERY_HANDLE = 0x3f
_TEMPERATURES_STRUCT_PACKING = '<bbbbbbb'
_PIN_STRUCT_PACKING = '<I'
_DATETIME_STRUCT_PACKING = '<BBBBB'
_BATTERY_STRUCT_PACKING = '<B'
_DAY_STRUCT_PACKING = '<BBBBBBBB'


Expand All @@ -59,6 +59,7 @@ def __init__(self, address, pin):
self._manual_temp = None
self._cur_temp = None
self._temperature = None
self._batt_level = None
self.available = False
self._manual_mode=False
# self.update()
Expand Down Expand Up @@ -100,6 +101,11 @@ def current_temperature(self):
else:
return None


@property
def battery_level(self):
return self._batt_level

@property
def manual_mode(self):
if self._manual_mode:
Expand All @@ -116,25 +122,28 @@ def update(self):
self._cur_temp, self._manual_temp, self._target_low, self._target_high, self._offset_temp, \
self._window_open_detect, self._window_open_minutes = struct.unpack(
_TEMPERATURES_STRUCT_PACKING, data)

if self._temperature:
_LOGGER.debug("Updating Temperature for device %s to %d", self._address, self._temperature)
self.write_temperature()
self.available = True


bat_data = self._conn.readCharacteristic(BATTERY_HANDLE)
self._batt_level = ord(bat_data)
if self._batt_level:
_LOGGER.debug("Updating Battery level for device %s to %d", self._address, self._batt_level)

except btle.BTLEGattError:
_LOGGER.error("Can't read cometblue data (%s). Did you set the correct PIN?", self._address)
self.available = False
finally:
self.disconnect()
_LOGGER.debug("Disconnected from device %s", self._address)


@manual_temperature.setter
def manual_temperature(self, temperature):
self._temperature = temperature


def write_temperature(self):
self._manual_temp = int(self._temperature * 2.0)
data = struct.pack(
Expand Down