linariii / fritz-homeautomation-csharp

C# implementation of the AVM Home Automation API
MIT License
3 stars 1 forks source link

Responses contain an XML serialization error... #3

Open Tjossul opened 9 months ago

Tjossul commented 9 months ago

Hello,

I have a FritzBox Cable and a Fritz Dect 210 registered there. Requesting the session ID is successfull but requesting devices with GetDevices(...) or GetDeviceInfos(...) using this requested session ID gives me an XML serialization error.

    private async Task RunFritz()
    {
        try
        {
            var homeAutomationClient = new Fritz.HomeAutomation.HomeAutomationClient();

            var sessionId = await homeAutomationClient.GetSessionId("myUserName", "myPassword");

            var devices = await  homeAutomationClient.GetDevices(sessionId);

            var info = await homeAutomationClient.GetDeviceInfos(sessionId, "myFritzDectAid");
        }
        catch(Exception ex)
        {

        }
    }

Error: "Instance validation error: 'manuell' is not a valid value for SwitchMode."

If I run the request urls in the browser I get the right XML response, e.g.:

http://fritz.box/webservices/homeautoswitch.lua?sid=68986fb1a557923b&switchcmd=getdevicelistinfos

<devicelist version="1" fwversion="7.57">
<device identifier="11657 0760001" id="16" functionbitmask="35712" fwversion="04.25" manufacturer="AVM" productname="FRITZ!DECT 210">
<present>1</present>
<txbusy>0</txbusy>
<name>FRITZ!DECT 210 #1</name>
<switch>
<state>1</state>
<mode>manuell</mode>
<lock>0</lock>
<devicelock>0</devicelock>
</switch>
<simpleonoff>
<state>1</state>
</simpleonoff>
<powermeter>
<voltage>232781</voltage>
<power>0</power>
<energy>0</energy>
</powermeter>
<temperature>
<celsius>220</celsius>
<offset>0</offset>
</temperature>
</device>
<group synchronized="0" identifier="grp8FD40C-3F744C05E" id="900" functionbitmask="37504" fwversion="1.0" manufacturer="AVM" productname="">
<present>1</present>
<txbusy>0</txbusy>
<name>Solar</name>
<switch>
<state>0</state>
<mode>manuell</mode>
<lock>0</lock>
<devicelock>0</devicelock>
</switch>
<simpleonoff>
<state>0</state>
</simpleonoff>
<powermeter>
<voltage>232781</voltage>
<power>0</power>
<energy>0</energy>
</powermeter>
<groupinfo>
<masterdeviceid>0</masterdeviceid>
<members>16</members>
</groupinfo>
</group>
</devicelist>

As you can see this mode is not part of your SwitchMode enums and additionally localised as German. Maybe you cannot use mode/SwitchMode as enum?!

I do not really know what to do or what the problem is. Could you give me some advice?

Thanks. Sebastian.

Tjossul commented 9 months ago

Hello,

I have a solution for this:

    /// <summary>
    /// Mode of the 
    /// </summary>
    public enum SwitchMode
    {
        /// <summary>
        /// Device is off
        /// </summary>
        [XmlEnum(Name = "0")]
        Off,

        /// <summary>
        /// Device is on
        /// </summary>
        [XmlEnum(Name = "1")]
        On,

        /// <summary>
        /// Device is in auto mode
        /// </summary>
        [XmlEnum(Name = "auto")]
        Auto,

        /// <summary>
        /// Device is in manual mode
        /// </summary>
        Manual
    }
    /// <summary>
    /// Switch
    /// </summary>
    [Serializable]
    [XmlRoot(ElementName = "switch")]
    public class Switch
    {
        /// <summary>
        /// State
        /// </summary>
        [XmlElement("state")]
        public State State { get; set; }

        /// <summary>
        /// Mode state
        /// </summary>
        [XmlIgnore]
        public SwitchMode SwitchMode { get; set; }

        /// <summary>
        /// Mode state
        /// </summary>
        [XmlElement("mode")]
        public string SwitchModeParse
        {
            get { return SwitchMode.ToString(); }
            set { SwitchMode = Enum.TryParse(value, out SwitchMode enumValue) ? enumValue : SwitchMode.Manual; }
        }

        /// <summary>
        /// Lock state
        /// </summary>
        [XmlElement("lock")]
        public Lock Lock { get; set; }

        /// <summary>
        /// device lock state
        /// </summary>
        [XmlElement("devicelock")]
        public Lock DeviceLock { get; set; }
    }

This works for me.

Regards. Sebastian.