forked from mykhailog/hacs_waveshare_ups_hat
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- fix: sensor.hassio_ups_remaining_capacity mAh to Wh #12 - feat: "addr" config parameter for device i2c address #6 - feat: ina219 mock for dev env - feat: readme update
- Loading branch information
Showing
11 changed files
with
178 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
custom_components/ina219_ups_hat/__pycache__ | ||
custom_components/ina219_ups_hat/ina219/__pycache__/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
from .ina219 import INA219 | ||
from .ina219_mock import MockINA219 | ||
|
||
def get_ina219_class(): | ||
if os.getenv('ENV') == 'dev': | ||
return MockINA219 | ||
return INA219 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
custom_components/ina219_ups_hat/ina219/ina219_interface.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
# Define the interface for INA219 | ||
class INA219Interface(ABC): | ||
@abstractmethod | ||
def read(self, address): | ||
pass | ||
|
||
@abstractmethod | ||
def write(self, address, data): | ||
pass | ||
|
||
@abstractmethod | ||
def set_calibration_32V_2A(self): | ||
pass | ||
|
||
@abstractmethod | ||
def getShuntVoltage_mV(self): | ||
pass | ||
|
||
@abstractmethod | ||
def getBusVoltage_V(self): | ||
pass | ||
|
||
@abstractmethod | ||
def getCurrent_mA(self): | ||
pass | ||
|
||
@abstractmethod | ||
def getPower_W(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from .ina219_interface import INA219Interface | ||
|
||
|
||
class MockSMBus: | ||
def __init__(self, bus): | ||
self.regs = {} | ||
self.addr = None | ||
|
||
def read_i2c_block_data(self, addr, reg, length): | ||
self.addr = addr | ||
return [(self.regs.get(reg, 0) >> 8) & 0xFF, self.regs.get(reg, 0) & 0xFF] | ||
|
||
def write_i2c_block_data(self, addr, reg, data): | ||
self.addr = addr | ||
self.regs[reg] = (data[0] << 8) | data[1] | ||
|
||
# Constants | ||
_REG_CONFIG = 0x00 | ||
_REG_SHUNTVOLTAGE = 0x01 | ||
_REG_BUSVOLTAGE = 0x02 | ||
_REG_POWER = 0x03 | ||
_REG_CURRENT = 0x04 | ||
_REG_CALIBRATION = 0x05 | ||
|
||
class MockINA219(INA219Interface): | ||
def __init__(self, i2c_bus=1, addr=0x40): | ||
self.bus = MockSMBus(i2c_bus) | ||
self.addr = addr | ||
self._cal_value = 0 | ||
self._current_lsb = 0 | ||
self._power_lsb = 0 | ||
self.set_calibration_32V_2A() | ||
|
||
def read(self, address): | ||
data = self.bus.read_i2c_block_data(self.addr, address, 2) | ||
return (data[0] << 8) + data[1] | ||
|
||
def write(self, address, data): | ||
self.bus.write_i2c_block_data(self.addr, address, [(data >> 8) & 0xFF, data & 0xFF]) | ||
|
||
def set_calibration_32V_2A(self): | ||
self._current_lsb = 0.1 | ||
self._cal_value = 4096 | ||
self._power_lsb = 0.002 | ||
self.write(_REG_CALIBRATION, self._cal_value) | ||
self.config = 0x399F # example config value | ||
self.write(_REG_CONFIG, self.config) | ||
|
||
def getShuntVoltage_mV(self): | ||
value = self.read(_REG_SHUNTVOLTAGE) | ||
if value > 32767: | ||
value -= 65535 | ||
return value * 0.01 | ||
|
||
def getBusVoltage_V(self): | ||
value = self.read(_REG_BUSVOLTAGE) | ||
return (value >> 3) * 0.004 | ||
|
||
def getCurrent_mA(self): | ||
value = self.read(_REG_CURRENT) | ||
if value > 32767: | ||
value -= 65535 | ||
return value * self._current_lsb | ||
|
||
def getPower_W(self): | ||
value = self.read(_REG_POWER) | ||
if value > 32767: | ||
value -= 65535 | ||
return value * self._power_lsb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters