Switch#

class lvmnps.switch.powerswitchbase.PowerSwitchBase(name, config, log=None)[source]#

Bases: object

PowerSwitchBase class for multiple power switches from different manufacturers.

The Powerswitch classes will inherit from the PowerSwitchBase class.

Parameters
  • name (str) – A name identifying the power switch.

  • config (dict) – The configuration defined on the .yaml file under /etc/lvmnps.yml.

  • log (SDSSLogger | None) – The logger for logging.

collectOutletsByNameAndPort(name=None, portnum=None)[source]#

Collects the outlet by the name and ports, comparing with the name and ports from the Outlet object.

Parameters
  • name (str | None) – The string to compare with the name in Outlet instance.

  • portnum (int | None) – The integer for indicating each Outlet instances. If zero or None, identifies the outlet only by name.

Returns

outlets – A list of Outlet that match the name and port number. If name=None, the outlet matching the port number is returned. If both name and portnum are None, a list with all the outlets connected to this switch is returned.

config_get(key, default=None)[source]#

Read the configuration and extract the data as a structure that we want.

Notice: DOESN’T work for keys with dots !!!

Parameters

key

The tree structure as a string to extract the data. For example, if the configuration structure is

ports:
  1:
      desc: "Hg-Ar spectral callibration lamp"

You can input the key as ports.1.desc to take the information “Hg-Ar spectral callibration lamp”.

findOutletByName(name)[source]#

Find the outlet by the name, comparing with the name from the Outlet object.

Parameters

name (str) – The string to compare with the name in Outlet instance.

abstract async isReachable()[source]#

Verify we can reach the switch. Returns True if ok.

async setState(state, name=None, portnum=None)[source]#

Set the state of the Outlet instance to On/Off. (On = 1, Off = 0).

Note that dependending on the values passed to name and portnum, multiple outlets may be commanded.

Parameters
  • state (bool | int) – The boolean value (True, False) to set the state inside the Outlet object.

  • name (str | None) – The string to compare with the name in Outlet instance.

  • portnum (int | None) – The integer for indicating each Outlet instances.

abstract async start()[source]#

Starts the switch instance, potentially connecting to the device server.

async statusAsDict(name=None, portnum=None)[source]#

Get the status of the Outlets as a dictionary.

Parameters
  • name (str | None) – The string to compare with the name in Outlet instance. name can be a switch or an outlet name.

  • portnum (int | None) – The integer for indicating each Outlet instances.

abstract async stop()[source]#

Stops the connection to the switch server.

abstract async switch(state, outlets)[source]#

Changes the state of an outlet.

Parameters
abstract async update(outlets)[source]#

Retrieves the status of a list of outlets and updates the internal mapping.

Parameters

outlets (list[Outlet] | None) – A list of Outlets to update. If None, all outlets are updated.

class lvmnps.switch.outlet.Outlet(switch, name, portnum, description=None, state=0)[source]#

Bases: object

Outlet class to manage the power switch.

Parameters
  • switch (PowerSwitchBase) – The parent PowerSwitchBase instance to which this outlet is associated with.

  • name (str) – The name of the outlet.

  • portnum (int) – The number of the port.

  • description (str | None) – The description about the outlet.

  • state (int) – The state of the outlet (on: 1, off: 0).

isOff()[source]#

Return the state of the outlet.

isOn()[source]#

Return the state of the outlet.

isValid()[source]#

Return the validity of the outlet.

static parse(value)[source]#

Parse the input data for ON/OFF.

setState(value)[source]#

Class method: Set the state of the outlet inside the class.

toDict()[source]#

Return the dictionary describing the status of the outlet.

class lvmnps.switch.dli.dli.DLI(hostname, user, password, name=None, log=None, onoff_timeout=3)[source]#

Bases: object

Powerswitch class to manage the DLI power switch.

Parameters
  • hostname (str) – The hostname from the configuration (the IP address for connection).

  • user (str) – The username from the configuration (the id for login).

  • password (str) – The password from the configuration (the password for login).

  • name (str | None) – The name of the DLI Controller.

  • log (SDSSLogger | None) – The logger for logging.

  • onoff_timeout – The timeout, in seconds, before failing an on/off command.

add_client(password)[source]#

Add the httpx.AsyncClient to the DLI object.

Parameters

password (str) –

compare(json, outlets)[source]#

Compares the names of the outlets with the response JSON object.

Parameters
  • json (dict) – The json list from the restful API. The current status of the power switch is contained here.

  • outlets (list[Outlet]) – List of Outlet objects to compare.

async get_outlets_response()[source]#

Returns the raw response to a relay/outlets GET request..

async off(outlet=0)[source]#

Turn off the power to the outlet.

Set the value of the outlet state by using a PUT request. Note that the outlets in the RESTful API are zero-indexed.

Parameters

outlet – The number indicating the outlet (1-indexed).

async on(outlet=0)[source]#

Turn on the power to the outlet.

Set the value of the outlet state by using a PUT request. Note that the outlets in the RESTful API are zero-indexed.

Parameters

outlet (int) – The number indicating the outlet (1-indexed).

async status()[source]#

Returns the status as a dictionary.

Receives the data from the switch by the GET method as a JSON. Note that this method returns the status of all the outlets (ports 1-8).

async verify(outlets)[source]#

Verifies if we can reach the switch by the “get” method.

Also compares the outlet lists with the configuration, and returns true if it’s identical.

Parameters

outlets (list[Outlet]) – The list of Outlet instance to check.

class lvmnps.switch.dli.powerswitch.DLIPowerSwitch(name, config, log=None)[source]#

Bases: lvmnps.switch.powerswitchbase.PowerSwitchBase

A DLI power switch.

Parameters
  • name (str) – A name identifying the power switch.

  • config (dict) – The configuration defined on the .yaml file under /etc/lvmnps_dli.yml.

  • log (SDSSLogger | None) – The logger for logging.

async isReachable()[source]#

Check if the power switch is reachable.

async start()[source]#

Adds the client controlling the DLI Power Switch.

Checks if the Power switch is reachable. If the Power switch is reachable, updates the data of Outlet objects.

async stop()[source]#

Closes the connection to the client.

async switch(state, outlets)[source]#

Controls the switch (turning on or off).

Parameters
  • state (bool) – The state to which to switch the outlet(s).

  • outlets (list[Outlet]) – List of outlets to command.

async update(outlets=None)[source]#

Updates the data based on the received status dictionary from the DLI class.

Parameters

outlets (list[Outlet] | None) – List of Outlet objects. If None, updates the status of all outlets.