Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary help
#1
I am working on a simple telnet program that interacts with a Lutron lighting system. The Lutron system allows me to enter a monitoring mode where it will display the status of a device/area. When a device activates, it changes state and triggers an area scene.

The following is output to the telnet command line:
Output:
~DEVICE,179,1,3 ~AREA,161,6,3
Currently I have 2 dictionaries
device_ids = {'179,1': 'Dressing 3', '179,2': 'Dressing 2', '179,3': 'Dressing 1'}
area_status ={'161': 'Dressing 3', '11': 'Dressing 2', '170': 'Dressing 1'}
Using regex I extract the information I need to print the location name. I would like to incorporate some error checking into my code to ensure that when a device activates, the scene is triggered too. Something like - If Device 179,1 is active check to see if Area 161 is active. The dictionaries are quite large so manually writing 'if' statements seems like the wrong way to go about things.

If I were to store the information like this:

locations = {'Dressing Room 3': {'Device ID': '279,1', 'Device Status': '1',
'Area ID': '161', 'Area Status': '1' }}

1. Is there a way to print the Key name (Dressing Room 3)from the value? e.g when the system reports '~DEVICE,179,1,3,' how do i make it print' Dressing Room 3

2. Could this be done a better way with a database?

Thank you.

Here's the full code:
def lms():
    occ_pattern = re.compile("^[DEVIC]+[,]([(0-9)]+[,][0-9]+)[,]([0-9]+)")
    area_pattern = re.compile("^[ARE]+[,]([(0-9)]+)[,][0-9]+[,]([0-9]+)")

    tn.write(occupancy_mon_enable.encode('ascii') + b'\r\n')
    print('Occupancy Monitoring Enabled')

    tn.read_until(b'QNET> ')
    tn.write(scene_mon_enable.encode('ascii') + b'\r\n\n')
    print('Scene Monitoring Enabled')

    while True:
        tn.read_until(b'~')
        output = tn.read_very_eager().decode('utf-8')

        try:
            status = ' '
            occ_id = occ_pattern.search(output).group(1)
            occ_status = occ_pattern.search(output).group(2)

            if occ_status == '3':
                status = 'Active'
            elif occ_status == '4':
                status = 'Inactive'

            print('DEVICE,' + occ_id + "," + occ_status + ": " + device_ids[occ_id] + " - " + status)

        except AttributeError:
            pass

        try:
            scene = ' '
            area_id = area_pattern.search(output).group(1)
            area_scene = area_pattern.search(output).group(2)

            if area_scene == '1':
                scene = 'Midnight'
            elif area_scene == '2':
                scene = 'Sunrise'
            elif area_scene == '3':
                scene = 'Day'
            elif area_scene == '4':
                scene = 'Sunset'
            elif area_scene == '5':
                scene = 'Evening'
            elif area_scene == '0':
                scene = 'Off'
            else:
                pass

            print("AREA," + area_id + "," + area_scene + ": " + area_status[area_id] + " - " + scene)

        except AttributeError:
            pass
Yoriz write Dec-06-2022, 02:15 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Dictionary help - by iuno - Dec-06-2022, 11:00 AM
RE: Dictionary help - by Larz60+ - Dec-06-2022, 12:00 PM
RE: Dictionary help - by iuno - Dec-06-2022, 07:05 PM
RE: Dictionary help - by deanhystad - Dec-06-2022, 08:57 PM
RE: Dictionary help - by iuno - Dec-07-2022, 03:37 PM
RE: Dictionary help - by iuno - Dec-07-2022, 03:33 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020