Skip to content

Commit

Permalink
add passive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tillsteinbach committed Jul 14, 2022
1 parent 761d0d0 commit 7f81234
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ All notable changes to this project will be documented in this file.
## [Unreleased]
- No unreleased changes so far

## [0.38.1] - 2022-07-14
### Added
- Passive mode

## [0.38.0] - 2022-07-14
### Fixed
- Regular relogin to fix problem with data not showing anymore

### Changed
- Refactoring to use WeConnect-mqtt from inside VWsFriend
- Updated API to 0.45.0

## [0.37.2] - 2022-06-28
### Fixed
Expand Down Expand Up @@ -626,7 +631,8 @@ Send empty message when topic is disabled
## [0.1.0] - 2021-05-27
Initial release

[unreleased]: https://github.com/tillsteinbach/WeConnect-mqtt/compare/v0.38.0...HEAD
[unreleased]: https://github.com/tillsteinbach/WeConnect-mqtt/compare/v0.38.1...HEAD
[0.38.1]: https://github.com/tillsteinbach/WeConnect-mqtt/releases/tag/v0.38.1
[0.38.0]: https://github.com/tillsteinbach/WeConnect-mqtt/releases/tag/v0.38.0
[0.37.2]: https://github.com/tillsteinbach/WeConnect-mqtt/releases/tag/v0.37.2
[0.37.1]: https://github.com/tillsteinbach/WeConnect-mqtt/releases/tag/v0.37.1
Expand Down
30 changes: 17 additions & 13 deletions weconnect_mqtt/weconnect_mqtt_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def main(): # noqa: C901 # pylint: disable=too-many-branches,too-many-statemen
class WeConnectMQTTClient(paho.mqtt.client.Client): # pylint: disable=too-many-instance-attributes
def __init__(self, clientId=None, transport='tcp', interval=300, prefix='weconnect/0', ignore=0, # pylint: disable=too-many-arguments
updateCapabilities=True, updatePictures=True, selective=None, listNewTopics=False, republishOnUpdate=False,
pictureFormat=None, topicFilterRegex=None, convertTimezone=None, timeFormat=None, withRawJsonTopic=False):
pictureFormat=None, topicFilterRegex=None, convertTimezone=None, timeFormat=None, withRawJsonTopic=False, passive=False):
super().__init__(client_id=clientId, transport=transport)
self.weConnect = None
self.prefix = prefix
Expand All @@ -342,6 +342,7 @@ def __init__(self, clientId=None, transport='tcp', interval=300, prefix='weconne
self.timeFormat = timeFormat
self.hasChanges = False
self.withRawJsonTopic = withRawJsonTopic
self.passive = passive

self.on_connect = self.on_connect_callback
self.on_message = self.on_message_callback
Expand Down Expand Up @@ -405,6 +406,8 @@ def connectWeConnect(self, weConnect):
self.setError(code=WeConnectErrors.SUCCESS)

def updateWeConnect(self): # noqa: C901
if self.passive:
return
LOG.info('Update data from WeConnect')
self.hasChanges = False
try:
Expand Down Expand Up @@ -532,17 +535,18 @@ def on_connect_callback(self, mqttc, obj, flags, rc):

if rc == 0:
LOG.info('Connected to MQTT broker')
topic = f'{self.prefix}/mqtt/weconnectForceUpdate_writetopic'
self.subscribe(topic, qos=2)
if topic not in self.topics:
self.addTopic(topic, writeable=True)
if not self.passive:
topic = f'{self.prefix}/mqtt/weconnectForceUpdate_writetopic'
self.subscribe(topic, qos=2)
if topic not in self.topics:
self.addTopic(topic, writeable=True)

topic = f'{self.prefix}/mqtt/weconnectUpdateInterval_s'
self.publish(topic=topic, qos=1, retain=True,
payload=self.interval)
self.subscribe(topic + '_writetopic', qos=1)
if topic not in self.topics:
self.addTopic(topic + '_writetopic', writeable=True)
topic = f'{self.prefix}/mqtt/weconnectUpdateInterval_s'
self.publish(topic=topic, qos=1, retain=True,
payload=self.interval)
self.subscribe(topic + '_writetopic', qos=1)
if topic not in self.topics:
self.addTopic(topic + '_writetopic', writeable=True)

self.setConnected()

Expand Down Expand Up @@ -599,12 +603,12 @@ def on_message_callback(self, mqttc, obj, msg): # noqa: C901
LOG.info('ignoring message from broker as it is within --ignore-for delta')
elif len(msg.payload) == 0:
LOG.debug('ignoring empty message')
elif msg.topic == f'{self.prefix}/mqtt/weconnectForceUpdate_writetopic':
elif msg.topic == f'{self.prefix}/mqtt/weconnectForceUpdate_writetopic' and not self.passive:
if msg.payload.lower() == b'True'.lower():
LOG.info('Update triggered by MQTT message')
self.updateWeConnect()
self.publish(topic=f'{self.prefix}/mqtt/weconnectForceUpdate', qos=2, payload=False)
elif msg.topic == f'{self.prefix}/mqtt/weconnectUpdateInterval_s_writetopic':
elif msg.topic == f'{self.prefix}/mqtt/weconnectUpdateInterval_s_writetopic' and not self.passive:
if str(msg.payload.decode()).isnumeric():
newInterval = int(msg.payload)
if newInterval < 300:
Expand Down

0 comments on commit 7f81234

Please sign in to comment.