Skip to content

Commit

Permalink
initial split of hass config and state
Browse files Browse the repository at this point in the history
  • Loading branch information
jblance authored Jan 14, 2025
1 parent 630bb5f commit d6fbe07
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions powermon/outputformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
class FormatterType(StrEnum):
""" enumeration of valid formatter types """
HASS = auto()
HASS_AUTODISCOVERY = auto()
HASS_STATE = auto()
HTMLTABLE = auto()
JSON = auto()
RAW = auto()
Expand All @@ -37,6 +39,10 @@ def get_formatter(format_type):
from powermon.outputformats.htmltable import HtmlTable as fmt
case FormatterType.HASS:
from powermon.outputformats.hass import Hass as fmt
case FormatterType.HASS_AUTODISCOVERY:
from powermon.outputformats.hass import HassAutoDiscovery as fmt
case FormatterType.HASS_STATE:
from powermon.outputformats.hass import HassState as fmt
case FormatterType.JSON:
from powermon.outputformats.json_fmt import Json as fmt
# case FormatterType.TOPICS:
Expand Down
34 changes: 30 additions & 4 deletions powermon/outputformats/hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ def format(self, command: Command, result: Result, device_info) -> list:
icon = response.icon
device_class = response.device_class
state_class = response.state_class
component = response.component or 'sensor'

# Set component type
if unit == "bool" or value == "enabled" or value == "disabled":
print(f'updating sensor to binary sensor for {data_name}')
component = "binary_sensor"
else:
component = "sensor"
# else:
# component = "sensor"

# Make value adjustments
if component == "binary_sensor":
Expand Down Expand Up @@ -86,8 +88,8 @@ def format(self, command: Command, result: Result, device_info) -> list:
"name": f"{name}",
"state_topic": f"{state_topic}",
"unique_id": f"{object_id}_{device_info.serial_number}",
"force_update": "true",
"last_reset": str(datetime.now()),
# "force_update": "true",
# "last_reset": str(datetime.now()),
}

# Add device info
Expand Down Expand Up @@ -115,6 +117,10 @@ def format(self, command: Command, result: Result, device_info) -> list:
if state_class:
payload["state_class"] = state_class

# Add options
if response.definition.options is not None:
payload["options"] = list(response.definition.options.values())

payloads = js.dumps(payload)
# print(payloads)
msg = {"topic": topic, "payload": payloads}
Expand All @@ -126,3 +132,23 @@ def format(self, command: Command, result: Result, device_info) -> list:

# order value msgs after config to allow HA time to build entity before state data arrives
return config_msgs + value_msgs


class HassAutoDiscovery(Hass):
""" formatter to generate home assistant auto config mqtt messages """
def __init__(self, config):
super().__init__(config)
self.name = "hass_autodiscovery"

def __str__(self):
return f"{self.name}: generates Home Assistant auto config (only) mqtt messages"


class HassState(Hass):
""" formatter to generate home assistant state update mqtt messages """
def __init__(self, config):
super().__init__(config)
self.name = "hass_state"

def __str__(self):
return f"{self.name}: generates Home Assistant state update mqtt messages (requires entities to exist or HassAutoDiscovery to have been run first)"
4 changes: 2 additions & 2 deletions tests/config/hass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ commands:
outputs:
- type: screen
format:
type: hass
type: hass_autodiscovery
entity_id_prefix: inverter42
discovery_prefix: homeassistant
filter: ^serial|^work|^charger|^fault
filter: ^work

0 comments on commit d6fbe07

Please sign in to comment.