@MikeA, @Odianosen25, I have tried to restructure in accordance with your guidelines and to keep it simple I have set up two apps with minimal code. The classes are TestHass and TestMqtt. The corresponding modules are test_hass and test_mqtt. I am trying to make the hass app the main focus with mqtt the supporting app. There is still an “AttributeError: ‘NoneType’ object has no attribute” error. I am out of my python depth and really need to put more effort into understanding objects and inheritance. So while I value your help I could also wait for the new release of AD (in Hassio) and possibly bypass the tricky object passing.
Here is where I am at.
Objects YAML
module: test_mqtt
class: TestMqtt
plugin: mqtt
mqtt_namespace: "mqtt"
mq_light: "office/rgb2/light"
test_hass:
module: test_hass
class: TestHass
plugin: hass
hass_namespace: "hass"
mq_light: "office/rgb2/light"
motion_sensor_1: "binary_sensor.office_motion"
motion_sensor_2: None
auto_switch: "input_boolean.office_auto"
test_hass.py
test_hass.py
import appdaemon.plugins.hass.hassapi as hass
import datetime
import appdaemon.plugins.mqtt.mqttapi as mqtt
class TestHass(mqtt.Mqtt, hass.Hass):
def initialize(self):
#gather instance parameters
self.set_namespace("hass")
self.mqtt = None
self.mq_light = self.args["mq_light"]
self.mq_light_color = self.mq_light + "/cmnd/color"
self.mq_light_fade = self.mq_light + "/cmnd/fade"
self.mq_light_speed = self.mq_light + "/cmnd/speed"
self.motion_sensor_1 = self.args["motion_sensor_1"]
self.auto_switch = self.args["auto_switch"] # input boolean
#Setup motion, switch and daily callback functions
self.set_state(self.auto_switch, state = "on")
# self.listen_state(self.switch, self.auto_switch)
# self.listen_state(self.motion, self.motion_sensor_1, new = "on")
#Initialise variables
# self.turn_off(self.mq_light)
self.mqtt.mqtt_publish(self.mq_light_fade, payload = "On", qos = 0, retain = True, namespace = "mqtt")
self.mqtt.mqtt_publish(self.mq_light_speed, payload = "5", qos = 0, retain = True, namespace = "mqtt")
self.mqtt.mqtt_publish(self.mq_light_color, payload = self.color_str(260,10), qos = 0, retain = True, namespace = "mqtt")
# # self.mqtt.mqtt_publish(self.mq_light_color, payload = self.color_str(500,10), qos = 0, retain = True, namespace = "mqtt")
self.log("***** initialise {} sensor cb - complete*****".format(self.room))
# self.logit("**********mqtt 1***********")
def save_mqtt(self, Mqtt):
self.mqtt = Mqtt
test.mqtt.py
import appdaemon.plugins.hass.hassapi as hass
import datetime
import appdaemon.plugins.mqtt.mqttapi as mqtt
class TestMqtt(mqtt.Mqtt, hass.Hass):
def initialize(self):
self.set_namespace('mqtt')
self.hass = self.get_app("TestHass")
self.hass.save_mqtt(self) # Pass this object to the Hass app
self.mqtt_subscribe("office/rgb2/light")
AD Errors
2019-04-26 12:23:25.901841 WARNING AppDaemon: ------------------------------------------------------------
2019-04-26 12:23:25.901978 WARNING AppDaemon: Unexpected error running initialize() for test_mqtt
2019-04-26 12:23:25.902085 WARNING AppDaemon: ------------------------------------------------------------
2019-04-26 12:23:25.903216 WARNING AppDaemon: Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 1581, in init_object
init()
File "/config/appdaemon/apps/test_mqtt.py", line 9, in initialize
self.hass.save_mqtt(self) # Pass this object to the Hass app
AttributeError: 'NoneType' object has no attribute 'save_mqtt'
2019-04-26 12:23:25.903331 WARNING AppDaemon: ------------------------------------------------------------
2019-04-26 12:23:25.903660 WARNING AppDaemon: ------------------------------------------------------------
2019-04-26 12:23:25.903764 WARNING AppDaemon: Unexpected error running initialize() for test_hass
2019-04-26 12:23:25.903866 WARNING AppDaemon: ------------------------------------------------------------
2019-04-26 12:23:25.904530 WARNING AppDaemon: Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/appdaemon/appdaemon.py", line 1581, in init_object
init()
File "/config/appdaemon/apps/test_hass.py", line 36, in initialize
self.set_state(self.auto_switch, state = "on")
File "/usr/local/lib/python3.7/site-packages/appdaemon/plugins/hass/hassapi.py", line 109, in set_state
if entity_id in self.get_state(namespace = namespace):
File "/usr/local/lib/python3.7/site-packages/appdaemon/plugins/mqtt/mqttapi.py", line 82, in get_state
return super(Mqtt, self).get_state(namespace, entity, **kwargs)
TypeError: get_state() takes from 1 to 2 positional arguments but 3 were given
2019-04-26 12:23:25.904647 WARNING AppDaemon: -----------------------------------------------------------
AD Log
2019-04-26 12:23:19.623961 INFO AppDaemon Version 3.0.4 starting
2019-04-26 12:23:19.624257 INFO Configuration read from: /config/appdaemon/appdaemon.yaml
2019-04-26 12:23:19.625435 INFO AppDaemon: Starting Apps
2019-04-26 12:23:19.630576 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2019-04-26 12:23:19.796342 INFO AppDaemon: HASS: HASS Plugin Initializing
2019-04-26 12:23:19.796795 INFO AppDaemon: HASS: HASS Plugin initialization complete
2019-04-26 12:23:19.796976 INFO AppDaemon: Loading Plugin MQTT using class MqttPlugin from module mqttplugin
2019-04-26 12:23:19.830285 INFO AppDaemon: MQTT: MQTT Plugin Initializing
2019-04-26 12:23:19.830533 INFO AppDaemon: MQTT: Using 'mqtt client status' as Will Topic
2019-04-26 12:23:19.830645 INFO AppDaemon: MQTT: Using 'mqtt client status' as Birth Topic
2019-04-26 12:23:19.830815 INFO AppDaemon: MQTT: Using 'appdaemon_mqtt_client' as Client ID
2019-04-26 12:23:19.831411 INFO Dashboards are disabled
2019-04-26 12:23:19.831517 INFO API is disabled
2019-04-26 12:23:19.838055 INFO AppDaemon: HASS: Connected to Home Assistant 0.90.2
2019-04-26 12:23:19.964545 INFO AppDaemon: HASS: Delaying app initialization for 4 seconds
2019-04-26 12:23:20.082229 INFO AppDaemon: MQTT: Connected to Broker at URL 192.168.0.22:1883
2019-04-26 12:23:20.840151 INFO AppDaemon: Got initial state from namespace mqtt
2019-04-26 12:23:20.840648 INFO AppDaemon: MQTT: MQTT Plugin initialization complete
2019-04-26 12:23:23.993087 INFO AppDaemon: Got initial state from namespace hass
2019-04-26 12:23:25.879212 INFO AppDaemon: Reading config
2019-04-26 12:23:25.883634 INFO AppDaemon: /config/appdaemon/apps/Test Hass MQTT Objects.yaml added or modified
2019-04-26 12:23:25.883757 INFO AppDaemon: /config/appdaemon/apps/Test Hass MQTT Objects.yaml added or modified
2019-04-26 12:23:25.883869 INFO AppDaemon: App 'test_mqtt' added
2019-04-26 12:23:25.883975 INFO AppDaemon: App 'test_hass' added
2019-04-26 12:23:25.884139 INFO AppDaemon: Adding /config/appdaemon/apps to module import path
2019-04-26 12:23:25.884489 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/test_hass.py
2019-04-26 12:23:25.901092 INFO AppDaemon: Loading App Module: /config/appdaemon/apps/test_mqtt.py
2019-04-26 12:23:25.901635 INFO AppDaemon: Initializing app test_mqtt using class TestMqtt from module test_mqtt
2019-04-26 12:23:25.903442 INFO AppDaemon: Initializing app test_hass using class TestHass from module test_hass
2019-04-26 12:23:25.905249 INFO AppDaemon: App initialization complete