Skip to content

Commit

Permalink
Merge pull request #233 from unixorn/dependabot/pip/pre-commit-3.8.0
Browse files Browse the repository at this point in the history
build(deps-dev): Bump pre-commit from 3.7.1 to 3.8.0
  • Loading branch information
unixorn authored Aug 14, 2024
2 parents 8eeb32d + e0b6512 commit 2fa842c
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 110 deletions.
11 changes: 11 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Description](#description)
- [License Acceptance](#license-acceptance)
- [Type of changes](#type-of-changes)
- [Checklist](#checklist)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

<!--- Provide a general summary of your changes in the Title above -->

# Description
Expand Down
19 changes: 19 additions & 0 deletions Code_Of_Conduct.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Contributor Covenant Code of Conduct](#contributor-covenant-code-of-conduct)
- [Our Pledge](#our-pledge)
- [Our Standards](#our-standards)
- [Enforcement Responsibilities](#enforcement-responsibilities)
- [Scope](#scope)
- [Enforcement](#enforcement)
- [Enforcement Guidelines](#enforcement-guidelines)
- [1. Correction](#1-correction)
- [2. Warning](#2-warning)
- [3. Temporary Ban](#3-temporary-ban)
- [4. Permanent Ban](#4-permanent-ban)
- [Attribution](#attribution)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Contributor Covenant Code of Conduct

## Our Pledge
Expand Down
11 changes: 11 additions & 0 deletions Contributing.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Contributing](#contributing)
- [Setup](#setup)
- [Contribution Guidelines](#contribution-guidelines)
- [Linting the project files](#linting-the-project-files)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

# Contributing

## Setup
Expand Down
67 changes: 17 additions & 50 deletions ha_mqtt_discoverable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,7 @@ class Discoverable(Generic[EntityType]):
availability_topic: str
attributes_topic: str

def __init__(
self, settings: Settings[EntityType], on_connect: Optional[Callable] = None
) -> None:
def __init__(self, settings: Settings[EntityType], on_connect: Optional[Callable] = None) -> None:
"""
Creates a basic discoverable object.
Expand All @@ -613,39 +611,29 @@ def __init__(
# e.g. `binary_sensor`
self._entity_topic = f"{self._entity.component}"
# If present, append the device name, e.g. `binary_sensor/mydevice`
self._entity_topic += (
f"/{clean_string(self._entity.device.name)}" if self._entity.device else ""
)
self._entity_topic += f"/{clean_string(self._entity.device.name)}" if self._entity.device else ""
# Append the sensor name, e.g. `binary_sensor/mydevice/mysensor`
self._entity_topic += f"/{clean_string(self._entity.name)}"

# Full topic where we publish the configuration message to be picked up by HA
# Prepend the `discovery_prefix`, default: `homeassistant`
# e.g. homeassistant/binary_sensor/mydevice/mysensor
self.config_topic = (
f"{self._settings.mqtt.discovery_prefix}/{self._entity_topic}/config"
)
self.config_topic = f"{self._settings.mqtt.discovery_prefix}/{self._entity_topic}/config"
# Full topic where we publish our own state messages
# Prepend the `state_prefix`, default: `hmd`
# e.g. hmd/binary_sensor/mydevice/mysensor
self.state_topic = (
f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/state"
)
self.state_topic = f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/state"

# Full topic where we publish our own attributes as JSON messages
# Prepend the `state_prefix`, default: `hmd`
# e.g. hmd/binary_sensor/mydevice/mysensor
self.attributes_topic = (
f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/attributes"
)
self.attributes_topic = f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/attributes"

logger.info(f"config_topic: {self.config_topic}")
logger.info(f"state_topic: {self.state_topic}")
if self._settings.manual_availability:
# Define the availability topic, using `hmd` topic prefix
self.availability_topic = (
f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/availability"
)
self.availability_topic = f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/availability"
logger.debug(f"availability_topic: {self.availability_topic}")

# Create the MQTT client, registering the user `on_connect` callback
Expand All @@ -671,15 +659,10 @@ def __str__(self) -> str:
def _setup_client(self, on_connect: Optional[Callable] = None) -> None:
"""Create an MQTT client and setup some basic properties on it"""
mqtt_settings = self._settings.mqtt
logger.debug(
f"Creating mqtt client ({mqtt_settings.client_name}) for "
f"{mqtt_settings.host}:{mqtt_settings.port}"
)
logger.debug(f"Creating mqtt client ({mqtt_settings.client_name}) for " f"{mqtt_settings.host}:{mqtt_settings.port}")
self.mqtt_client = mqtt.Client(mqtt_settings.client_name)
if mqtt_settings.tls_key:
logger.info(
f"Connecting to {mqtt_settings.host}:{mqtt_settings.port} with SSL and client certificate authentication"
)
logger.info(f"Connecting to {mqtt_settings.host}:{mqtt_settings.port} with SSL and client certificate authentication")
logger.debug(f"ca_certs={mqtt_settings.tls_ca_cert}")
logger.debug(f"certfile={mqtt_settings.tls_certfile}")
logger.debug(f"keyfile={mqtt_settings.tls_key}")
Expand All @@ -691,9 +674,7 @@ def _setup_client(self, on_connect: Optional[Callable] = None) -> None:
tls_version=ssl.PROTOCOL_TLS,
)
elif mqtt_settings.use_tls:
logger.info(
f"Connecting to {mqtt_settings.host}:{mqtt_settings.port} with SSL and username/password authentication"
)
logger.info(f"Connecting to {mqtt_settings.host}:{mqtt_settings.port} with SSL and username/password authentication")
logger.debug(f"ca_certs={mqtt_settings.tls_ca_cert}")
if mqtt_settings.tls_ca_cert:
self.mqtt_client.tls_set(
Expand All @@ -707,17 +688,11 @@ def _setup_client(self, on_connect: Optional[Callable] = None) -> None:
tls_version=ssl.PROTOCOL_TLS,
)
if mqtt_settings.username:
self.mqtt_client.username_pw_set(
mqtt_settings.username, password=mqtt_settings.password
)
self.mqtt_client.username_pw_set(mqtt_settings.username, password=mqtt_settings.password)
else:
logger.debug(
f"Connecting to {mqtt_settings.host}:{mqtt_settings.port} without SSL"
)
logger.debug(f"Connecting to {mqtt_settings.host}:{mqtt_settings.port} without SSL")
if mqtt_settings.username:
self.mqtt_client.username_pw_set(
mqtt_settings.username, password=mqtt_settings.password
)
self.mqtt_client.username_pw_set(mqtt_settings.username, password=mqtt_settings.password)
if on_connect:
logger.debug("Registering custom callback function")
self.mqtt_client.on_connect = on_connect
Expand All @@ -728,9 +703,7 @@ def _setup_client(self, on_connect: Optional[Callable] = None) -> None:
def _connect_client(self) -> None:
"""Connect the client to the MQTT broker, start its onw internal loop in
a separate thread"""
result = self.mqtt_client.connect(
self._settings.mqtt.host, self._settings.mqtt.port
)
result = self.mqtt_client.connect(self._settings.mqtt.host, self._settings.mqtt.port)
# Check if we have established a connection
if result != mqtt.MQTT_ERR_SUCCESS:
raise RuntimeError("Error while connecting to MQTT broker")
Expand All @@ -739,9 +712,7 @@ def _connect_client(self) -> None:
# messages in a separate thread
self.mqtt_client.loop_start()

def _state_helper(
self, state: Optional[str], topic: Optional[str] = None, retain=True
) -> MQTTMessageInfo:
def _state_helper(self, state: Optional[str], topic: Optional[str] = None, retain=True) -> MQTTMessageInfo:
"""
Write a state to the given MQTT topic, returning the result of client.publish()
"""
Expand Down Expand Up @@ -779,8 +750,7 @@ def delete(self) -> None:

config_message = ""
logger.info(
f"Writing '{config_message}' to topic {self.config_topic} on "
"{self._settings.mqtt.host}:{self._settings.mqtt.port}"
f"Writing '{config_message}' to topic {self.config_topic} on " "{self._settings.mqtt.host}:{self._settings.mqtt.port}"
)
self.mqtt_client.publish(self.config_topic, config_message, retain=True)

Expand Down Expand Up @@ -813,8 +783,7 @@ def write_config(self):
config_message = json.dumps(self.generate_config())

logger.debug(
f"Writing '{config_message}' to topic {self.config_topic} on "
f"{self._settings.mqtt.host}:{self._settings.mqtt.port}"
f"Writing '{config_message}' to topic {self.config_topic} on " f"{self._settings.mqtt.host}:{self._settings.mqtt.port}"
)
self.wrote_configuration = True
self.config_message = config_message
Expand Down Expand Up @@ -892,9 +861,7 @@ def on_client_connected(client: mqtt.Client, *args):
# Invoke the parent init
super().__init__(settings, on_client_connected)
# Define the command topic to receive commands from HA, using `hmd` topic prefix
self._command_topic = (
f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/command"
)
self._command_topic = f"{self._settings.mqtt.state_prefix}/{self._entity_topic}/command"

# Register the user-supplied callback function with its user_data
self.mqtt_client.user_data_set(user_data)
Expand Down
12 changes: 3 additions & 9 deletions ha_mqtt_discoverable/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def create_base_parser(description: str = "Base parser"):
choices=["DEBUG", "INFO", "ERROR", "WARNING", "CRITICAL"],
default="INFO",
)
parser.add_argument(
"--client-name", type=str, help="MQTT client name", default="cephalopod"
)
parser.add_argument("--client-name", type=str, help="MQTT client name", default="cephalopod")
parser.add_argument(
"--device-class",
type=str,
Expand All @@ -46,9 +44,7 @@ def create_base_parser(description: str = "Base parser"):
parser.add_argument("--device-id", type=str, help="Device ID")
parser.add_argument("--device-name", type=str, help="Device Name")
parser.add_argument("--icon", type=str, help="Icon")
parser.add_argument(
"--mqtt-prefix", type=str, default="homeassistant", help="MQTT prefix"
)
parser.add_argument("--mqtt-prefix", type=str, default="homeassistant", help="MQTT prefix")
parser.add_argument("--mqtt-user", type=str, help="MQTT user.")
parser.add_argument("--mqtt-password", type=str, help="MQTT password.")
parser.add_argument("--mqtt-server", type=str, help="MQTT server.")
Expand All @@ -60,9 +56,7 @@ def create_base_parser(description: str = "Base parser"):
parser.add_argument("--tls-certfile", type=str, help="Path to certfile.")
parser.add_argument("--tls-key", type=str, help="Path to tls key.")

parser.add_argument(
"--version", "-v", help="Show version and exit", action="store_true"
)
parser.add_argument("--version", "-v", help="Show version and exit", action="store_true")
return parser


Expand Down
36 changes: 9 additions & 27 deletions ha_mqtt_discoverable/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ class LightInfo(EntityInfo):

component: str = "light"

state_schema: str = Field(
default="json", alias="schema"
) # 'schema' is a reserved word by pydantic
state_schema: str = Field(default="json", alias="schema") # 'schema' is a reserved word by pydantic
"""Sets the schema of the state topic, ie the 'schema' field in the configuration"""
optimistic: Optional[bool] = None
"""Flag that defines if light works in optimistic mode.
Expand Down Expand Up @@ -251,9 +249,7 @@ def update_state(self, state: bool) -> None:
state_message = self._entity.payload_on
else:
state_message = self._entity.payload_off
logger.info(
f"Setting {self._entity.name} to {state_message} using {self.state_topic}"
)
logger.info(f"Setting {self._entity.name} to {state_message} using {self.state_topic}")
self._state_helper(state=state_message)


Expand Down Expand Up @@ -320,9 +316,7 @@ def brightness(self, brightness: int) -> None:
brightness(int): Brightness value of [0,255]
"""
if brightness < 0 or brightness > 255:
raise RuntimeError(
f"Brightness for light {self._entity.name} is out of range"
)
raise RuntimeError(f"Brightness for light {self._entity.name} is out of range")

state_payload = {
"brightness": brightness,
Expand All @@ -342,13 +336,9 @@ def color(self, color_mode: str, color: dict[str, Any]) -> None:
color(Dict[str, Any]): Color to set, according to color_mode format
"""
if not self._entity.color_mode:
raise RuntimeError(
f"Light {self._entity.name} does not support setting color"
)
raise RuntimeError(f"Light {self._entity.name} does not support setting color")
if color_mode not in self._entity.supported_color_modes:
raise RuntimeError(
f"Color is not in configured supported_color_modes {str(self._entity.supported_color_modes)}"
)
raise RuntimeError(f"Color is not in configured supported_color_modes {str(self._entity.supported_color_modes)}")
# We do not check if color schema conforms to color mode formatting, it is up to the caller
state_payload = {
"color_mode": color_mode,
Expand All @@ -367,9 +357,7 @@ def effect(self, effect: str) -> None:
if not self._entity.effect:
raise RuntimeError(f"Light {self._entity.name} does not support effects")
if effect not in self._entity.effect_list:
raise RuntimeError(
f"Effect is not within configured effect_list {str(self._entity.effect_list)}"
)
raise RuntimeError(f"Effect is not within configured effect_list {str(self._entity.effect_list)}")
state_payload = {
"effect": effect,
"state": self._entity.payload_on,
Expand All @@ -385,9 +373,7 @@ def _update_state(self, state: dict[str, Any]) -> None:
"""
logger.info(f"Setting {self._entity.name} to {state} using {self.state_topic}")
json_state = json.dumps(state)
self._state_helper(
state=json_state, topic=self.state_topic, retain=self._entity.retain
)
self._state_helper(state=json_state, topic=self.state_topic, retain=self._entity.retain)


class Cover(Subscriber[CoverInfo]):
Expand Down Expand Up @@ -424,9 +410,7 @@ def _update_state(self, state: str) -> None:
"""
print("State: " + state)
logger.info(f"Setting {self._entity.name} to {state} using {self.state_topic}")
self._state_helper(
state=state, topic=self.state_topic, retain=self._entity.retain
)
self._state_helper(state=state, topic=self.state_topic, retain=self._entity.retain)


class Button(Subscriber[ButtonInfo]):
Expand Down Expand Up @@ -476,9 +460,7 @@ def set_text(self, text: str) -> None:
"""
if not self._entity.min <= len(text) <= self._entity.max:
bound = f"[{self._entity.min}, {self._entity.max}]"
raise RuntimeError(
f"Text is not within configured length boundaries {bound}"
)
raise RuntimeError(f"Text is not within configured length boundaries {bound}")

logger.info(f"Setting {self._entity.name} to {text} using {self.state_topic}")
self._state_helper(str(text))
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pydantic = "^2.7.2"
[tool.poetry.group.dev.dependencies]
pytest = ">=7.2.2,<9.0.0"
flake8 = ">=6,<8"
pre-commit = "^3.1.1"
pre-commit = "^3.8.0"
ruff = "^0.5.7"


Expand Down
Loading

0 comments on commit 2fa842c

Please sign in to comment.