Skip to content

Commit

Permalink
Fix lint and blackened once again.
Browse files Browse the repository at this point in the history
  • Loading branch information
EAGrahamJr committed Jul 17, 2024
1 parent 4fb3ae3 commit dc65c41
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
HomeAssistant abstraction for use with MQTT. **Specifically** aimed at using [CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython) on microcontrollers with `asyncio` and `minimqtt`, but _should_ be playable on other platforms.

* Defines HA _entities_ so that actual MQTT communication is abstracted
* Uses the HA [discovery](https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery) mechansim to register and un-register entities
* Uses the HA [discovery](https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery) mechanism to register and un-register entities
* Wraps an MQTT client with delegation
* Swap out clients easier
* Allows use with things like _asyncio_ without modifying entities
Expand All @@ -12,24 +12,25 @@ For the curious, start with the [base](src/ha_minimqtt/__init__.py). Also, see [

The beginnings of [How to Use](How%20To%20Use.md) seem to be working.

## Origin
Since I come from an Object-Oriented and _functional_ background, I originally crafted these classes in [Kotlin](EAGrahamJr/kobots-parts). I ran across a situation where I could _not_ run a JVM, so I turned to the "next easiest thing" that seemed logical and went with Python. And, since I am more familar with and using [CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython), a conversion of sorts seemed pretty straight-forward.

This is where it got interesting -- I used a couple of different "AI code converrters" to migrate the Kotlin code to Python. It was a decent **starting** point, but it is _not_ anywhere near complete, nor does it actually capture the complexities of the _Python_ language. (Throw in `pylint` and you've got a right nightmare.)

## Design Philosophy
This module is heavily "class-based" as state needs to be preserved and consistent across all the things.

This module is heavily "class-based" as state needs to be preserved and consistent across all the things.

* All entities (should) provide some sort of "state". This should be settable from the system and will be appropriately formatted and published to HA.
* Entities that _receive_ from HA (e.g. `CommandEntity`) also use delegation to interface with the other systems.
* Everything is extensible: this is a _start_ -- I do not have all 20+ MQTT entities **currently** supported by HA, so ... have fun?
*

## Current Status
I _think_ I have the [project](@EAGrahamJr/Projects/Release 1) set up correctly?

* Most of the basic code is written for the HA entities, but not tested
* Need to determine what's on a micro and what's not (see [_compatibility](src/ha_minimqtt/_compatibility.py) - h/t @elpekenin)
* `pylint` and `black` are being run manaully
* `pylint` and `black` are being run manually
* Figure out how to unit test (that's why the decorator:bangbang:)
* Publish docs?
* Either add to Adafruit community bundle(s) or publish to PyPi

## Origin
Since I come from an Object-Oriented and _functional_ background, I originally crafted these classes in [Kotlin](EAGrahamJr/kobots-parts). I ran across a situation where I could _not_ run a JVM, so I turned to the "next easiest thing" that seemed logical and went with Python. And, since I am more familiar with and using [CircuitPython](https://learn.adafruit.com/welcome-to-circuitpython), a conversion of sorts seemed pretty straight-forward.

This is where it got interesting -- I used a couple of different "AI code converters" to migrate the Kotlin code to Python. It was a decent **starting** point, but it is _not_ anywhere near complete, nor does it actually capture the complexities of the _Python_ language. (Throw in `pylint` and you've got a right nightmare.)
4 changes: 4 additions & 0 deletions src/ha_minimqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def current_state(self) -> str:
return ""


# pylint: disable=R0903
class DeviceClass:
"""
An extension that is used for specific "types" as defined by HA.
Expand Down Expand Up @@ -153,11 +154,13 @@ def add_to_discovery(self, disco: dict) -> dict:
return disco


# pylint: disable=R0903,R0902
class BaseEntity:
"""
The root of all the evil that exists here.
"""

# pylint: disable=R0913
def __init__(
self,
component: str,
Expand Down Expand Up @@ -415,6 +418,7 @@ def remove(self):
self._deleted = True


# pylint: disable=R0903
class NumberDisplayMode(ConstantList):
"""
Defines how HA will display "numeric" entities.
Expand Down
10 changes: 3 additions & 7 deletions src/ha_minimqtt/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
CommandHandler,
)

# pylint: disable=R0801


# pylint: disable=R0801,C0103,R0903
class NumericDevice(DeviceClass, ConstantList):
"""
The various things HA knows about for numbers. See `Device class
Expand Down Expand Up @@ -89,17 +90,12 @@ class NumericDevice(DeviceClass, ConstantList):
WIND_SPEED = "wind_speed"


class NumericHandler(CommandHandler):
def __init__(self):
super().__init__()


# pylint: disable=R0902
class NumberEntity(BaseEntity):
"""
Manages a number entity.
"""

# pylint: disable=R0913
def __init__(
self,
unique_id: str,
Expand Down
3 changes: 1 addition & 2 deletions src/ha_minimqtt/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# 3
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -41,7 +41,6 @@ def options(self) -> List[str]:
raise NotImplementedError


# pylint: disable=C0116
class SelectEntity(BaseEntity):
"""
Defines an entity that presents a set of "selections" that can be executed by
Expand Down
23 changes: 8 additions & 15 deletions src/ha_minimqtt/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

from ha_minimqtt import BaseEntity, DeviceIdentifier, DeviceClass

# pylint: disable=R0801


class BinaryDevice(DeviceClass, ConstantList):
"""
Expand Down Expand Up @@ -68,6 +70,7 @@ class BinarySensor(BaseEntity):
An on/off sensor.
"""

# pylint: disable=R0913
def __init__(
self,
unique_id: str,
Expand Down Expand Up @@ -108,18 +111,6 @@ def __init__(
self.icon = "mdi:door"
self._sensor_state = "OFF"

def __add_to_discovery(disco: dict, expires: int = None) -> dict:
"""
"Helper" method to set stuff on discovery for sensors
:param disco:
:param expires:
:return:
"""
disco["entity_category"] = "diagnostic"
if expires:
disco["expire_after"] = expires
return disco

def _add_other_discovery(self, disco: dict) -> dict:
disco["entity_category"] = "diagnostic"
if self._expires:
Expand All @@ -145,14 +136,14 @@ def current_state(self, value):
Must be one of "ON"/"OFF" (case-insensitive) or True/False
:param value: the value to set/send
"""
if type(value) == str:
if isinstance(value, str):
v = value.upper()
if v != "ON" and v != "OFF":
if v not in ("ON", "OFF"):
raise ValueError(f"'state' {value} must be ON or OFF")
self._sensor_state = value
self.send_current_state()

elif type(value) == bool:
elif isinstance(value, bool):
self._sensor_state = "ON" if value else "OFF"
self.send_current_state()
else:
Expand All @@ -161,6 +152,7 @@ def current_state(self, value):
)


# pylint: disable=R0903
class StateClass(ConstantList):
"""
How analog data is accumulated/graphed. The default is MEASUREMENT.
Expand Down Expand Up @@ -234,6 +226,7 @@ class AnalogSensor(BaseEntity):
Sends variable numeric data, typically on a regular basis or when "triggered" by a change.
"""

# pylint: disable=R0913
def __init__(
self,
unique_id: str,
Expand Down

0 comments on commit dc65c41

Please sign in to comment.