Skip to content

Commit

Permalink
Add support for Shelly Pro 3EM (Gen2 EM devices in general)
Browse files Browse the repository at this point in the history
Closes #10

Still need to implement characteristics for these
  • Loading branch information
Jalle19 committed Oct 13, 2023
1 parent 20cfa01 commit bbf38e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/sensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum SensorType {
export enum ShellyType {
Gen1 = 'gen1',
Gen2PM = 'gen2-pm',
Gen2EM = 'gen2-em',
}

export enum CharacteristicsSensorType {
Expand Down Expand Up @@ -46,7 +47,10 @@ export interface IotawattCharacteristicsSensor extends CharacteristicsSensor {
interface ShellySensorSettings {
address: string
type: undefined | ShellyType
// For Gen1 devices and Gen2 devices implementing the "Switch" component
meter: number
// For devices implementing the "EM" component
phase: string | undefined
}

export interface ShellySensor extends PowerSensor {
Expand Down
42 changes: 38 additions & 4 deletions src/shelly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,28 @@ type Gen1StatusResult = {
meters: Gen1MeterResult[]
}

type Gen2GetStatusResult = {
type Gen2SwitchGetStatusResult = {
apower: number
}

type Gen2EMGetStatusResult = {
a_act_power: number
b_act_power: number
c_act_power: number
}

const getSensorDataUrl = (sensor: ShellySensor): string => {
const address = sensor.shelly.address
const meter = sensor.shelly.meter

// Request a different endpoint depending on what type of Shelly we're dealing with
switch (sensor.shelly.type as ShellyType) {
case ShellyType.Gen2PM:
return `http://${address}/rpc/Switch.GetStatus?id=${meter}`
case ShellyType.Gen1:
return `http://${address}/status`
case ShellyType.Gen2PM:
return `http://${address}/rpc/Switch.GetStatus?id=${meter}`
case ShellyType.Gen2EM:
return `http://${address}/rpc/EM.GetStatus?id=${meter}`
}
}

Expand All @@ -40,7 +48,7 @@ const parseGen1Response = (timestamp: number, circuit: Circuit, httpResponse: Ax
}

const parseGen2PMResponse = (timestamp: number, circuit: Circuit, httpResponse: AxiosResponse): PowerSensorData => {
const data = httpResponse.data as Gen2GetStatusResult
const data = httpResponse.data as Gen2SwitchGetStatusResult

return {
timestamp: timestamp,
Expand All @@ -49,6 +57,30 @@ const parseGen2PMResponse = (timestamp: number, circuit: Circuit, httpResponse:
}
}

const parseGen2EMResponse = (timestamp: number, circuit: Circuit, httpResponse: AxiosResponse): PowerSensorData => {
const sensor = circuit.sensor as ShellySensor
const data = httpResponse.data as Gen2EMGetStatusResult

let watts = 0
switch (sensor.shelly.phase) {
case 'a':
watts = data.a_act_power
break
case 'b':
watts = data.b_act_power
break
case 'c':
watts = data.c_act_power
break
}

return {
timestamp: timestamp,
circuit: circuit,
watts: watts,
}
}

export const getSensorData: PowerSensorPollFunction = async (
timestamp: number,
circuit: Circuit,
Expand All @@ -65,6 +97,8 @@ export const getSensorData: PowerSensorPollFunction = async (
return parseGen1Response(timestamp, circuit, httpResponse)
case ShellyType.Gen2PM:
return parseGen2PMResponse(timestamp, circuit, httpResponse)
case ShellyType.Gen2EM:
return parseGen2EMResponse(timestamp, circuit, httpResponse)
}
} catch (e) {
console.error((e as Error).message)
Expand Down

0 comments on commit bbf38e5

Please sign in to comment.