1
0
Fork 0
cherry-switch/cherry/switch/zwave.py

52 lines
1.9 KiB
Python

# cherry-switch - listener for switches and remote actions
# Copyright (C) 2021 Bob Carroll <bob.carroll@alum.rit.edu>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import logging
import json
async def receive_central_scene(client, mappings, messages):
"""
Event handler for central scene commands.
:param client: mqtt client
:param mappings: map of switch configuration
:param messages: mqtt message generator
"""
async for m in messages:
try:
platform, device_id, cmd_cls, _, _, scene_id = m.topic.split('/')
logging.debug(f'Received device {device_id} scene {scene_id}')
value = json.loads(m.payload).get('value')
if value is None:
logging.debug('No value found in payload')
continue
scene_name = mappings.get(int(device_id), {}).get(f'{scene_id}-{value}')
if scene_name is None:
logging.info(f'No configuration for device {device_id} scene {scene_id}')
continue
except KeyError:
continue
try:
logging.debug(f'Triggering scene {scene_name}')
await client.publish(f'scene/switch/{scene_name}/state/set', 1)
except Exception as ex:
logging.error(str(ex))