def __init__(self): self.permit_join = True self.callbacks = {} self.devices = [] zigbee = configManager.getConfig('zigbee') if not zigbee: self.topic = "zigbee2mqtt" logger.error("no zigbee config") else: self.topic = zigbee['topic'] self.addcallback( '/'.join([self.topic,"bridge","response","device","rename"]), editAdressLincDevices ) self.addcallback( '/'.join([self.topic,"bridge","response","device","remove"]), decodRemove ) self.addcallback( '/'.join([self.topic,"bridge","event"]), decodEvent ) self.addcallback( '/'.join([self.topic,"bridge","devices"]), decodeZigbeeDevices ) self.addcallback( '/'.join([self.topic,"bridge","info"]), decodeZigbeeConfig )
def __init__(self): self.permit_join = True zigbee = configManager.getConfig('zigbee') if not zigbee: self.topic = "zigbee2mqtt" logger.error("no zigbee config") else: self.topic = zigbee['topic']
def updateTopic(self): zigbee = configManager.getConfig('zigbee') if not zigbee: logger.error("no zigbee config") return get("Mqtt_MqttConnect").publish( self.topic + "/bridge/request/options", '{"options": {"mqtt": {"base_topic":"' + zigbee['topic'] + '"}}}') self.topic = zigbee['topic']
async def decodeZigbeeDevices(self, topic, message): try: data = json.loads(message) config = configManager.getConfig('zigbee') self.devices = [] for item in data: dev = formatDev(item) self.addzigbeeDevices(dev.address,dev) await manager.send_information("zigbee",objectlist_to_dictlist(self.devices)) except Exception as e: logger.error(f'zigbee devices decod {e}')
async def updateWeather(): try: weatherConf = configManager.getConfig("weather") global dataWeather timeWeather = list() dateWeather = list() weatherNow = current_weather(weatherConf["city"], weatherConf["APPID"]) for item in weatherNow["list"]: time = datetime.utcfromtimestamp(item["dt"]) if (time.strftime('%H:%M:%S') == "12:00:00"): dateWeather.append( WeatherDateSchema(day=WeatherTimeSchema( date=time.strftime('%Y-%m-%d'), time=time.strftime('%H:%M:%S'), temp=item.get("main").get("temp"), temp_max=item.get("main").get("temp_max"), temp_min=item.get("main").get("temp_min"), weather=item.get("weather")[0].get("main"), wind=item.get("wind")))) if (time.strftime('%H:%M:%S') == "21:00:00" and len(dateWeather) > 0): dateWeather[len(dateWeather) - 1].night = WeatherTimeSchema( date=time.strftime('%Y-%m-%d'), time=time.strftime('%H:%M:%S'), temp=item.get("main").get("temp"), temp_max=item.get("main").get("temp_max"), temp_min=item.get("main").get("temp_min"), weather=item.get("weather")[0].get("main"), wind=item.get("wind")) for item in weatherNow["list"]: time = datetime.utcfromtimestamp(item["dt"]) timeWeather.append( WeatherTimeSchema(date=time.strftime('%Y-%m-%d'), time=time.strftime('%H:%M:%S'), temp=item.get("main").get("temp"), temp_max=item.get("main").get("temp_max"), temp_min=item.get("main").get("temp_min"), weather=item.get("weather")[0].get("main"), wind=item.get("wind"))) dataWeather = WeatherSchema( city=weatherConf["city"], now=NowWeatherSchema( temp=weatherNow.get("list")[0].get("main").get("temp"), weather=weatherNow.get("list")[0].get("weather")[0].get( "main")), forecastTime=timeWeather, forecastDate=dateWeather) except Exception as e: logger.warning(f'error get weather forecast. detail {e}') return None
def connect(self): get("Mqtt_MqttValue").addConnect(DEVICE_NAME) try: logger.debug("mqtt conecting...") conf = configManager.getConfig("mqttBroker") self.mqttClient = mqtt.Client() self.mqttClient.username_pw_set(conf["user"], conf["password"]) self.mqttClient.connect(conf["host"], int(conf["port"])) self.mqttClient.loop_start() self.mqttClient.on_message = self.on_message self.mqttClient.subscribe("#") logger.debug("mqtt conect") return self.mqttClient except Exception as e: logger.error(f'error connecting to mqtt {e}')
async def editAdressLincDevices(obj, topic, message): data = json.loads(message) zigbee = configManager.getConfig("zigbee2mqtt") oldadress = data["data"] oldadress = oldadress["from"] newadress = data["data"] newadress = newadress["to"] address = '/'.join([zigbee["topic"], oldadress]) devs = Devices.all() devs2 = devs for item in devs2: if item.address != address: devs.remove(item) newadress = '/'.join([zigbee["topic"], newadress]) for item in devs: item.address = newadress item.save()
async def startup() -> None: await initdir() call_functions.subscribe("weather", updateWeather, 43200) call_functions.subscribe("script", runTimeScript, 60) call_functions.subscribe("serverData", sendServerData, 30) call_functions.subscribe("saveDevice", saveDevice, 120) confinit() base = configManager.getConfig("base") if "frequency" in base: call_functions.subscribe("devices", sendDevice, int(base['frequency'])) else: call_functions.subscribe("devices", sendDevice, 6) init_moduls() database_ = app.state.database if not database_.is_connected: await database_.connect() loop = asyncio.get_running_loop() loop.create_task(call_functions.run()) await initAdmin() logger.info("starting")
def formatDev(item): config = configManager.getConfig("zigbee") d = {} if ("definition" in item): d = item["definition"] dev = ZigbeeDeviceSchema(name=item["friendly_name"], root_address=config["topic"], address=item["ieee_address"], allAddress=config["topic"] + "/" + item["friendly_name"]) if ("type" in item): dev.type = item["type"] if "power_source" in item: dev.power_source = item["power_source"] if (d and ("model" in d)): dev.model = d["model"] dev.description = d["description"] dev.vendor = d["vendor"] if ("exposes" in d): dev.exposes = d["exposes"] return dev
async def updataSendTime(): base = configManager.getConfig("base") call_functions.subscribe("devices", sendDevice, int(base['frequency']))