Announcement

Collapse
No announcement yet.

HS3 Script for controlling Sonos Alarms

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • HS3 Script for controlling Sonos Alarms

    This HS3 script is for use with the PI-SonosController plug-in by dcorsus.

    It allows Homeseer to enable/disable Sonos alarms and set the time of the alarms. It does not create new alarms or set the other properties. Create the alarm using the Sonos software (PC, IOS, Android) with the music, volume, etc.

    Then, using this script you can change the time and enable/disable as needed. When run it will adjust the first alarm settings. If any other alarms are found for a Sonos device they will disabled.

    In my setup, I have one alarm per Sonos player that I never delete. I use a touchpad on the wall to set the time and enable the wake alarm as desired. I also integrate this into my security alarm (Elk) to enable the wake alarm when I set the security alarm to "go to sleep" mode. Also one command will shut off both alarms. The system also turns on the coffee maker 15 minutes before the Somos alarm time (if enabled).

    The script requires three parameters separated by the pipe character. The first is the room. This is a name that must be configured in the top of the script along with the Sonos player instance info. This can be found on the Sonos plug in configuration page. The name can be anything you like. Look at the top of the script to see an example.

    The second parameter is the alarm time. It is in 24 hour format such as 07:00 or 22:15. This may be left blank and the existing alarm time will not be changed.

    The third parameter is set whether the alarm is enabled or disabled. passing "E" will enabled the alarm, "D" will disable the alarm. This can be left blank and the existing setting will be retained.

    Here are a few examples:

    MBR|07:00|E - This will set the MBR alarm to 7:00 AM and enable it.

    MBR||D - This will disable the MBR alarm.

    MBR||E - This will enable the MBR alarm with the existing alarm time unchanged.

    Code:
    Imports System.IO
    Imports System.Text
    Imports System.Net
    
    'Define room to instance lookup table
    Function GetHashtable() As Hashtable
        Dim rooms As Hashtable = New Hashtable
          rooms.Add("MBR", "RINCON_000E58138A8A01400")
          rooms.Add("FAMILY", "RINCON_000E5821A94201400")
          rooms.Add("PATIO", "RINCON_000E58A344A001400")
          Return rooms
    End Function
    
    Sub Main(ByVal parmString As Object)
    
        'For debug only
        'parmString = "FAMILY|05:00|D"
    
        'Confirm that parameters string passed, if not we're done
        If parmString Is Nothing or parmString = "" Then
            hs.writelog("Alarms","ABORTED: Parameters ROOM|TIME|ENABLED must be passed")
            return
        End If
        
        'Split passed parameters into array
        Dim parmlist() As String = Split(parmString.ToString,"|")
        
        'Confirm that three parameters passed, if not we're done
        if parmList.length <> 3 Then
            hs.writelog("Alarms","ABORTED: Invalid parameters """ & parmString & """ passed")
            return
        End If
        
        'Load room lookup table
        Dim roomHash As Hashtable = GetHashtable()
        
        'Check if passed room is in table, if not error
        If Not roomHash.ContainsKey(parmList(0)) Then
              hs.writelog("Alarms","ABORTED: Room """ & parmList(0) & """ not configured")
              return
        End If
        
        'Check if passed time is valid time string or empty, otherwise error
        Dim d As Date
        If parmList(1) <> "" And Not Date.TryParseExact(parmList(1), "HH:mm", Nothing, Globalization.DateTimeStyles.NoCurrentDateDefault, d) Then
              hs.writelog("Alarms","ABORTED: Alarm time """ & parmList(1) & """ is not valid")
            return
        End If
    
        'Check if passed enabled value is valid string or empty, otherwise error
        If parmList(2) <> "E" And parmList(2) <> "D" And parmList(2) <> "" Then
              hs.writelog("Alarms","ABORTED: Alarm enabled """ & parmList(2) & """ is not valid")
            return
        End If
        
        'Initialize alarm counter
        Dim alarmCnt As Integer = 0
        
        'Get current alarm info from Sonos (XML string)
        Dim alarmData as String = hs.PluginFunction("Sonos", roomHash(parmList(0)), "ListAlarms", nothing)
        
        'Load alarm info XML string into reader
        Dim alarmXML As XmlTextReader = New XmlTextReader( New StringReader(alarmData) )
        
        'Disable whitespace so that you don't have to read over whitespaces
        alarmXML.WhiteSpaceHandling = WhiteSpaceHandling.NONE    
    
        'Read the xml declaration and advance to Alarms tag
        alarmXML.Read()
    
        'Loop through XML data
        While Not alarmXML.EOF
        
          'Go to the Alarm tag
          alarmXML.Read()
          
          'If not start element exit while loop
          If Not alarmXML.IsStartElement() Then
            Exit While
          End If
          
          'Get Attributes Values
          Dim alarmParams(10) As String
          alarmParams(0)  = alarmXML.GetAttribute("ID")
          alarmParams(1)  = alarmXML.GetAttribute("StartTime")
          alarmParams(2)  = alarmXML.GetAttribute("Duration")
          alarmParams(3)  = alarmXML.GetAttribute("Recurrence")
          alarmParams(4)  = alarmXML.GetAttribute("Enabled")
          alarmParams(5)  = alarmXML.GetAttribute("RoomUUID")
          alarmParams(6)  = alarmXML.GetAttribute("ProgramURI")
          alarmParams(7)  = alarmXML.GetAttribute("ProgramMetaData")
          alarmParams(8)  = alarmXML.GetAttribute("PlayMode")
          alarmParams(9)  = alarmXML.GetAttribute("Volume")
          alarmParams(10) = alarmXML.GetAttribute("IncludeLinkedZones")
          
          'Make sure alarm is for correct room
          If alarmParams(5) <> roomHash(parmList(0)) Then
              Continue While
          End If
          
          'If first alarm, update with passed params
            If alarmCnt = 0 Then
                
                If parmList(2) = "E" Then
                  alarmParams(4) = "1"
              ElseIf parmList(2) = "D" Then
                    alarmParams(4) = "0"
              End If
              
              'If alarm time was passed, update
              If parmList(1) <> "" Then
                  alarmParams(1) = parmList(1) & ":00" 
              End If 
              
            'If "xtra" alarm, just disable
            Else
            alarmParams(4) = "0"
            End If      
          
          'Log Basic Alarm info
          hs.writelog("Alarms","ID: " & alarmParams(0))
          hs.writelog("Alarms","StartTime: " & alarmParams(1))
          hs.writelog("Alarms","Enabled: " & alarmParams(4) )
    
          ' Set Sonos alarm params, get result
          Dim alarmResult as String = hs.PluginFunction("Sonos", roomHash(parmList(0)), "SetAlarmParms", alarmParams)
          
          'Log result
          hs.writelog("Alarms","Alarm " & alarmParams(0) & " set result: " & alarmResult)
    
          'Increment alarm counter
          alarmCnt += 1
    
        End While
        
        'Close the reader
        alarmXML.Close()
        
        If alarmCnt = 0 Then
            hs.writelog("Alarms","Warning: No alarms were found for Room """ & parmList(0) & """")
        End If     
        
    End Sub
    I hope someone finds this to be a useful example.
Working...
X