Internal Devices

GPIO Zero also provides several “internal” devices which represent facilities provided by the operating system itself. These can be used to react to things like the time of day, or whether a server is available on the network.

Warning

These devices are experimental and their API is not yet considered stable. We welcome any comments from testers, especially regarding new “internal devices” that you’d find useful!

TimeOfDay

class gpiozero.TimeOfDay(start_time, end_time, utc=True)[source]

Extends InternalDevice to provide a device which is active when the computer’s clock indicates that the current time is between start_time and end_time (inclusive) which are time instances.

The following example turns on a lamp attached to an Energenie plug between 7 and 8 AM:

from datetime import time
from gpiozero import TimeOfDay, Energenie
from signal import pause

lamp = Energenie(0)
morning = TimeOfDay(time(7), time(8))
morning.when_activated = lamp.on
morning.when_deactivated = lamp.off
pause()
Parameters:
  • start_time (time) – The time from which the device will be considered active.
  • end_time (time) – The time after which the device will be considered inactive.
  • utc (bool) – If True (the default), a naive UTC time will be used for the comparison rather than a local time-zone reading.

PingServer

class gpiozero.PingServer(host)[source]

Extends InternalDevice to provide a device which is active when a host on the network can be pinged.

The following example lights an LED while a server is reachable (note the use of source_delay to ensure the server is not flooded with pings):

from gpiozero import PingServer, LED
from signal import pause

server = PingServer('my-server')
led = LED(4)
led.source_delay = 1
led.source = server.values
pause()
Parameters:host (str) – The hostname or IP address to attempt to ping.

Base Classes

The classes in the sections above are derived from a series of base classes, some of which are effectively abstract. The classes form the (partial) hierarchy displayed in the graph below:

_images/other_device_hierarchy.svg

The following sections document these base classes for advanced users that wish to construct classes for their own devices.

InternalDevice

class gpiozero.InternalDevice[source]

Extends Device to provide a basis for devices which have no specific hardware representation. These are effectively pseudo-devices and usually represent operating system services like the internal clock, file systems or network facilities.