Help with astral

can anyone help me work out to use astral to do something at sunset and sunrise e.g. print or set a pin state,
here’s what i have so far,
any help will be greatly appreciated.

import sys
import datetime
sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')
from astral import Astral
city_name = 'London'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
print('Information for %s/%s\n' % (city_name, city.region))
timezone = city.timezone
print('Timezone: %s' % timezone)
print('Latitude: %.02f; Longitude: %.02f\n' % \
   (city.latitude, city.longitude))
sun = city.sun(date=datetime.date.today(), local=True)
print('Dawn:    %s' % str(sun['dawn']))
print('Sunrise: %s' % str(sun['sunrise']))
print('Noon:    %s' % str(sun['noon']))
print('Sunset:  %s' % str(sun['sunset']))
print('Dusk:    %s' % str(sun['dusk']))

Looks like all that’s left is to continually loop and check the current time against sun['sunset'].time(), using some method of blocking the action being run over and over once the sun has risen/set.

I’d probably do something like:

if datetime.datetime.now().time >= sun['sunrise'].time and sun_risen == False:
    # do sunrise action
    sun_risen = True
if datetime.datetime.now().time >= sun['sunset'].time and sun_risen == True:
    # do sunset action
    sun_risen = False

But you could possibly use a scheduler (https://docs.python.org/3/library/sched.html#scheduler-objects) to fire the functions at the right time, and hide that complexity.

import sys
sys.path.append ('/home/pi/Pimoroni/displayotron/examples')
sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')


import RPi.GPIO as GPIO
from astral import *
from datetime import datetime
import time

#Customise your city
city = 'London'

def setup_gpio():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(13, GPIO.OUT)
    GPIO.setup(5, GPIO.OUT)
    GPIO.output (13, False)
    GPIO.output (5, False)

def check_time():
    a = Astral()
    location = a[city]
    sun = location.sun(local=True, date=datetime.now())
    dusk = sun['dusk']
    sunrise = sun['sunrise']
    print ("Dusk today is at %s" % dusk)
    print ("Sunrise today is at")
    now = datetime.now()
    print ("Now it is %s" % now)
    td = (dusk - now).minutes
    if td < 0:
        print ("Need to check light")
        check_lights()
    else:
        print ("Need to sleep for %d' % td")
        time.sleep(td)
        check_lights()

def check_lights():
    print ("Checking if light is already on...")
    print ("GPIO.input(13)")
    if((GPIO.input(13) == 0  or (GPIO.input(13) == 1))):
        print ("Light is off")
        turn_on_light()
    else:
        print ("Light already is on")

def turn_on_light():
    print ("Switching light on")
    GPIO.output(13,True)
    time.sleep(2)
    GPIO.output (13, False)

setup_gpio()
check_time()

but i get this error

  File "/home/pi/Scripts/astest.py", line 63, in <module>
    check_time()
  File "/home/pi/Scripts/astest.py", line 38, in check_time
    td = (dusk - now).minutes
TypeError: can't subtract offset-naive and offset-aware datetimes

Seems to be answered here: https://stackoverflow.com/questions/796008/cant-subtract-offset-naive-and-offset-aware-datetimes

But depends on Python version.

i read that a few times

import sys
sys.path.append ('/home/pi/Pimoroni/displayotron/examples')
sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')


import RPi.GPIO as GPIO
from astral import *
from datetime import datetime
import time
from datetime import tzinfo, timedelta, datetime
ZERO = timedelta(0)

class UTC(tzinfo):
  def utcoffset(self, dt):
    return ZERO
  def tzname(self, dt):
    return "UTC"
  def dst(self, dt):
    return ZERO

utc = UTC()

#Customise your city
city = 'London'

def setup_gpio():
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(13, GPIO.OUT)
    GPIO.setup(5, GPIO.OUT)
    GPIO.output (13, False)
    GPIO.output (5, False)

def check_time():
    a = Astral()
    location = a[city]
    sun = location.sun(local=True, date=datetime.now())
    dusk = sun['dusk']
    sunrise = sun['sunrise']
    print ("Dusk today is at %s" % dusk)
    print ("Sunrise today is at")
    now = datetime.now(utc)
    print ("Now it is %s" % now)
    td = (dusk - now).seconds
    if td < 0:
        print ("Need to check light")
        check_lights()
    else:
        print ("Need to sleep for %d' % td")
        time.sleep(td)
        check_lights()

def check_lights():
    print ("Checking if light is already on...")
    print ("GPIO.input(13)")
    if((GPIO.input(13) == 0  or (GPIO.input(13) == 1))):
        print ("Light is off")
        turn_on_light()
    else:
        print ("Light already is on")

def turn_on_light():
    print ("Switching light on")
    GPIO.output(13,True)
    time.sleep(2)
    GPIO.output (13, False)

setup_gpio()
check_time()

shell gives back

Dusk today is at 2018-06-04 21:56:55+01:00
Sunrise today is at
Now it is 2018-06-04 11:53:23.004376+00:00
need to sleep for %d' % td
import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep
import datetime

sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')
sys.path.append('/home/pi/Pimoroni/displayotron/examples')

from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast
from astral import Astral
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)

print("""
Script Running:
Press CTRL+C to exit.
""")

city_name = 'London'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
sun = city.sun(date=datetime.date.today(), local=True)


suspend_draw = False

class Lights(MenuOption):
    def __init__(self):
        self._mode = 0
        self._message = ''
        self._timeout = 0
        self._status = False
        MenuOption.__init__(self)

    def redraw(self, menu):
        if self._mode == 0:
            menu.clear_row(0)
            menu.write_row(1, "Toggle Lights")
            menu.write_row(2, "Currently: {}".format("On" if self._status else "Off"))
        elif self._mode == 1:
            if self._timeout <= time.time():
                self._mode = 0
            menu.clear_row(0)
            menu.write_row(1, self._message)
            menu.clear_row(2)

    def display_message(self, message, timeout=1):
        self._message = message
        self._timeout = time.time() + timeout
        self._mode = 1

    def lights_on(self):
        GPIO.output(13,0)
        GPIO.output(5,1)
        GPIO.output(13,1)
        self._status = True
        self.display_message("Lights: On", 1)

    def lights_off(self):
        GPIO.output(5,1)
        GPIO.output(13,0)
        time.sleep(1)
        GPIO.output(13,1)
        time.sleep(1)
        GPIO.output(13,0)
        self._status = False
        self.display_message("Lights: Off", 1)

    def up(self):
        self.lights_on()

    def down(self):
        self.lights_off()

class autoLights():
    def __init__(self):
        self._mode = 0
        self._message = ''
        self._timeout = 0
        self._status = False
        MenuOption.__init__(self)

    def redraw(self, menu):
        if self._mode == 0:
            menu.clear_row(0)
            menu.write_row(1, "Light Status")
            menu.write_row(2, "Currently: {}".format("On" if self._status else "Off"))
        elif self._mode == 1:
            if self._timeout <= time.time():
                self._mode = 0
            menu.clear_row(0)
            menu.write_row(1, self._message)
            menu.clear_row(2)

    def display_message(self, message, timeout=1):
        self._message = message
        self._timeout = time.time() + timeout
        self._mode = 1

        def lights_on(self):
            if sun['sunrise'] == True:
                GPIO.output(13,0)
                GPIO.output(5,1)
                GPIO.output(13,1)
                self._status = True
                self.display_message("Lights: On", 1)

    def lights_off(self):
        GPIO.output(5,1)
        GPIO.output(13,0)
        time.sleep(1)
        GPIO.output(13,1)
        time.sleep(1)
        GPIO.output(13,0)
        self._status = False
        self.display_message("Lights: Off", 1)

menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Manual': Lights(),
                    'Auto': autoLights(),
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_handler=None,
    idle_timeout=30,
    input_handler=Text())

nav.bind_defaults(menu)

while 1:
    if not suspend_draw:
        menu.redraw()
    time.sleep(0.05)

This shows how you might handle auto lights on top of manual ones- Left/Right will enable/disable auto mode, and Up/Down will disable auto mode and toggle the lights on/off.

import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep
import datetime

sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')
sys.path.append('/home/pi/Pimoroni/displayotron/examples')

from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast
from astral import Astral
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)

a = Astral()
a.solar_depression = 'civil'
city = a['London']

class Lights(MenuOption):
    def __init__(self):
        self._mode = 0
        self._message = ''
        self._timeout = 0
        self._status = False
        self._auto = True
        MenuOption.__init__(self)

    def get_status(self):
        return self._status

    def is_auto(self):
        return self._auto

    def redraw(self, menu):
        if self._mode == 0:
            menu.write_row(0, "Toggle Lights")
            menu.write_row(1, "Currently: {}".format("On" if self._status else "Off"))
            menu.write_row(2, "Auto: {}".format("On" if self._auto else "Off"))
        elif self._mode == 1:
            if self._timeout <= time.time():
                self._mode = 0
            menu.clear_row(0)
            menu.write_row(1, self._message)
            menu.clear_row(2)

    def display_message(self, message, timeout=1):
        self._message = message
        self._timeout = time.time() + timeout
        self._mode = 1

    def lights_on(self):
        GPIO.output(13,0)
        GPIO.output(5,1)
        GPIO.output(13,1)
        self._status = True
        self._auto = False
        self.display_message("Lights: On", 1)

    def lights_off(self):
        GPIO.output(5,1)
        GPIO.output(13,0)
        time.sleep(1)
        GPIO.output(13,1)
        time.sleep(1)
        GPIO.output(13,0)
        self._status = False
        self._auto = False
        self.display_message("Lights: Off", 1)

    def auto_on(self):
        self._auto = True
        self.display_message("Auto: On", 1)

    def auto_off(self):
        self._auto = False
        self.display_message("Auto: Off", 1)

    def right(self):
        self.auto_on()

    def left(self):
        self.auto_off()
        return True

    def up(self):
        self.lights_on()

    def down(self):
        self.lights_off()

lights = Lights()


menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': Lights(),
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_handler=None,
    idle_timeout=30,
    input_handler=Text())

nav.bind_defaults(menu)

while True:
    menu.redraw()

    # Get the current sunrise/sunset time
    now = datetime.datetime.now()
    sun = city.sun(date=now.date(), local=True)
    sun_rise = sun['sunrise'].time()
    sun_set = sun['sunset'].time()

    if lights.is_auto() == True:
        # Turn the lights off if it's after sunrise and they're on
        if now.time() >= sun_rise and lights.get_status() == True:
            lights.lights_off()

        # Turn the lights on if it's after sunset and they're off
        if now.time() >= sun_set and lights.get_status() == Flase:
            lights.lights_on()

    time.sleep(1.0 / 20)

Tweaked to add display of current time, sunset and sunrise times, and also to display after 2sec of inactivity:

import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep
import datetime

sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')
sys.path.append('/home/pi/Pimoroni/displayotron/examples')

from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast
from astral import Astral
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)

a = Astral()
a.solar_depression = 'civil'
city = a['London']

class Lights(MenuOption):
    def __init__(self):
        self._mode = 0
        self._message = ''
        self._timeout = 0
        self._status = False
        self._auto = True
        self._sun_rise = None
        self._sun_set = None
        MenuOption.__init__(self)

    def get_status(self):
        return self._status

    def is_auto(self):
        return self._auto

    def set_times(self, sun_rise, sun_set):
        self._sun_rise = sun_rise
        self._sun_set = sun_set

    def redraw(self, menu):
        if self._mode == 0:
            menu.write_row(0, "Lights {}".format(str(datetime.datetime.now().time())[:8]))
            menu.write_row(1, "{} {}".format("[LIGHTS]" if self._status else " LIGHTS ", "[AUTO]" if self._auto else " AUTO "))
            menu.write_row(2, "R:{}  S:{}".format(str(self._sun_rise)[:-3], str(self._sun_set)[:-3]))
        elif self._mode == 1:
            if self._timeout <= time.time():
                self._mode = 0
            menu.clear_row(0)
            menu.write_row(1, self._message)
            menu.clear_row(2)

    def display_message(self, message, timeout=1):
        self._message = message
        self._timeout = time.time() + timeout
        self._mode = 1

    def lights_on(self):
        GPIO.output(13,0)
        GPIO.output(5,1)
        GPIO.output(13,1)
        self._status = True
        self.display_message("Lights: On", 1)

    def lights_off(self):
        GPIO.output(5,1)
        GPIO.output(13,0)
        time.sleep(1)
        GPIO.output(13,1)
        time.sleep(1)
        GPIO.output(13,0)
        self._status = False
        self.display_message("Lights: Off", 1)

    def auto_on(self):
        self._auto = True
        self.display_message("Auto: On", 1)

    def auto_off(self):
        self._auto = False
        self.display_message("Auto: Off", 1)

    def right(self):
        self.auto_on()

    def left(self):
        self.auto_off()
        return True

    def up(self):
        self._auto = False
        self.lights_on()

    def down(self):
        self._auto = False
        self.lights_off()

lights = Lights()


menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': lights,
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_handler=lights,
    idle_time=2,
    input_handler=Text())

nav.bind_defaults(menu)

while True:
    menu.redraw()

    now = datetime.datetime.now()
    sun = city.sun(date=now.date(), local=True)
    sun_rise = sun['sunrise'].time()
    sun_set = sun['sunset'].time()

    lights.set_times(sun_rise, sun_set)

    if lights.is_auto() == True:
        if now.time() >= sun_rise and lights.get_status() == False:
            lights.lights_on()

        if now.time() >= sun_set and lights.get_status() == True:
            lights.lights_off()

    time.sleep(1.0 / 20)


[quote=“gadgetoid, post:10, topic:7858”]

import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep
import datetime

sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')
sys.path.append('/home/pi/Pimoroni/displayotron/examples')

from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast
from astral import Astral
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)

a = Astral()
a.solar_depression = 'civil'
city = a['London']

class Lights(MenuOption):
    def __init__(self):
        self._mode = 0
        self._message = ''
        self._timeout = 0
        self._status = False
        self._auto = True
        self._sun_rise = None
        self._sun_set = None
        MenuOption.__init__(self)

    def get_status(self):
        return self._status

    def is_auto(self):
        return self._auto

    def set_times(self, sun_rise, sun_set):
        self._sun_rise = sun_rise
        self._sun_set = sun_set

    def redraw(self, menu):
        if self._mode == 0:
            menu.write_row(0, "Lights {}".format(str(datetime.datetime.now().time())[:8]))
            menu.write_row(1, "{} {}".format("[LIGHTS]" if self._status else " LIGHTS ", "[AUTO]" if self._auto else " AUTO "))
            menu.write_row(2, "R:{}  S:{}".format(str(self._sun_rise)[:-3], str(self._sun_set)[:-3]))
        elif self._mode == 1:
            if self._timeout <= time.time():
                self._mode = 0
            menu.clear_row(0)
            menu.write_row(1, self._message)
            menu.clear_row(2)

    def display_message(self, message, timeout=1):
        self._message = message
        self._timeout = time.time() + timeout
        self._mode = 1

    def lights_on(self):
        GPIO.output(13,0)
        GPIO.output(5,1)
        GPIO.output(13,1)
        self._status = True
        self.display_message("Lights: On", 1)

    def lights_off(self):
        GPIO.output(5,1)
        GPIO.output(13,0)
        time.sleep(1)
        GPIO.output(13,1)
        time.sleep(1)
        GPIO.output(13,0)
        self._status = False
        self.display_message("Lights: Off", 1)

    def auto_on(self):
        self._auto = True
        self.display_message("Auto: On", 1)

    def auto_off(self):
        self._auto = False
        self.display_message("Auto: Off", 1)

    def right(self):
        self.auto_on()

    def left(self):
        self.auto_off()
        return True

    def up(self):
        self._auto = False
        self.lights_on()

    def down(self):
        self._auto = False
        self.lights_off()

lights = Lights()


menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': lights,
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    idle_handler=lights,
    idle_time=2,
    input_handler=Text())

nav.bind_defaults(menu)

while True:
    menu.redraw()

    now = datetime.datetime.now()
    sun = city.sun(date=now.date(), local=True)
    sun_rise = sun['sunrise'].time()
    sun_set = sun['sunset'].time()

    lights.set_times(sun_rise, sun_set)

    if lights.is_auto() == True:
        if now.time() >= sun_rise and lights.get_status() == False:
            lights.lights_on()

        if now.time() >= sun_set and lights.get_status() == True:
            lights.lights_off()

    time.sleep(1.0 / 20)


[/quote] i change the menu structure so its in order, however the last 3 Backlight LEDs on the right hand side are at a different brightness to the first 3, any ideas why this happens as i only changed the structure

import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep
import datetime

sys.path.append ('/home/pi/.local/lib/python2.7/site-packages')
sys.path.append('/home/pi/Pimoroni/displayotron/examples')

from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast
from astral import Astral
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)

a = Astral()
a.solar_depression = 'civil'
city = a['London']

class Lights(MenuOption):
    def __init__(self):
        self._mode = 0
        self._message = ''
        self._timeout = 0
        self._status = False
        self._auto = True
        self._sun_rise = None
        self._sun_set = None
        MenuOption.__init__(self)

    def get_status(self):
        return self._status

    def is_auto(self):
        return self._auto

    def set_times(self, sun_rise, sun_set):
        self._sun_rise = sun_rise
        self._sun_set = sun_set

    def redraw(self, menu):
        if self._mode == 0:
            menu.write_row(0, "Lights {}".format(str(datetime.datetime.now().time())[:8]))
            menu.write_row(1, "{} {}".format("[LIGHTS]" if self._status else " LIGHTS ", "[AUTO]" if self._auto else " AUTO "))
            menu.write_row(2, "R:{}  S:{}".format(str(self._sun_rise)[:-3], str(self._sun_set)[:-3]))
        elif self._mode == 1:
            if self._timeout <= time.time():
                self._mode = 0
            menu.clear_row(0)
            menu.write_row(1, self._message)
            menu.clear_row(2)

    def display_message(self, message, timeout=1):
        self._message = message
        self._timeout = time.time() + timeout
        self._mode = 1

    def lights_on(self):
        GPIO.output(13,0)
        GPIO.output(5,1)
        GPIO.output(13,1)
        self._status = True
        self.display_message("Lights: On", 1)

    def lights_off(self):
        GPIO.output(5,1)
        GPIO.output(13,0)
        time.sleep(1)
        GPIO.output(13,1)
        time.sleep(1)
        GPIO.output(13,0)
        self._status = False
        self.display_message("Lights: Off", 1)

    def auto_on(self):
        self._auto = True
        self.display_message("Auto: On", 1)

    def auto_off(self):
        self._auto = False
        self.display_message("Auto: Off", 1)

    def right(self):
        self.auto_on()

    def left(self):
        self.auto_off()
        return True

    def up(self):
        self._auto = False
        self.lights_on()

    def down(self):
        self._auto = False
        self.lights_off()

lights = Lights()

menu = Menu(
    None,
    lcd,
    5)

menu.add_item('Lighting/Aquarium/Control', lights)
menu.add_item('Clock', Clock())
menu.add_item('Status/IP', IPAddress())
menu.add_item('Status/CPU', GraphCPU())
menu.add_item('Status/Temp', GraphTemp())
menu.add_item('Display/Settings/Contrast', Contrast(lcd)),
menu.add_item('Display/Settings/Backlight', Backlight(backlight))

"""
You can use anything to control dot3k.menu,
but you'll probably want to use dot3k.joystick
"""
REPEAT_DELAY = 0.5
nav.bind_defaults(menu)

while True:
    menu.redraw()

    now = datetime.datetime.now()
    sun = city.sun(date=now.date(), local=True)
    sun_rise = sun['sunrise'].time()
    sun_set = sun['sunset'].time()

    lights.set_times(sun_rise, sun_set)

    if lights.is_auto() == True:
        if now.time() >= sun_rise and lights.get_status() == False:
            lights.lights_on()

        if now.time() >= sun_set and lights.get_status() == True:
            lights.lights_off()

    time.sleep(1.0 / 20)