Custom Switch (MaxSmart) - "__init__.py (ERROR)"

Hi

I have startet my hass.io project one week ago and it works fine for the usual stuff like homeamticIP, Hue etc. Now I am working at some specialties like my MaxSmart - switches :sleepy:

Dooing some research I was hable to find a discussion on the topic and a github repoitory with the script for Home Assistant. I have added the Repository and comfigured the switch in my configuration.yaml to get the entities. (c.f. at the end)

Unfortenatly the reboot did always bring the follwing errormessage:

Component error: Switch - Integration ‘Switch’ not found.
20:38 components/hassio/init.py (ERROR)

I have som comding expirience, but as I am new to Hass.io and python I have no idea how I could fixt that. Could somebody give me some input?

Thank you and best regards
apg

config.ymal

Switch:

  • platform: maxsmart
    name: NAME
    host: 192.168.IP.IP
    ports: 6
    outlets:
    1: Out1
    2: Out2
    3: Out3
    4: Out4
    5: Out5
    6: Out6

“Switch” needs to be lowercase.

switch:

Also use backticks (```) above and below the code to make it a code block.

Thank you Tediore for the input. I have corrected the mentioned errors. Unfortunately the error message is still the same at the end.

Just to clarify, the “format as code” comment was only referring to your post here, not what’s actually in configuration.yaml. Can you post your full config?

Oh I see. Thank you for the clarification. I’ll try to do better this time. :smiley:

Shure I can but there is nothing special in the config it is still mostly unchanged

Configure a default setup of Home Assistant (frontend, api, etc)

default_config:

Uncomment this if you are using SSL/TLS, running in Docker container, etc.

http:

base_url: example.duckdns.org:8123

Text to speech

tts:

  • platform: google_translate

switch:

  • platform: maxsmart
    name: NAME
    host: 192.168.IP.IP
    ports: 6
    outlets:
    1: Out1
    2: Out2
    3: Out3
    4: Out4
    5: Out5
    6: Out6

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

But the speciality is more in the file I have downloaded from Github. It stands now alone in the folder:
addons\git\229c8dfc which causes the error I assume?!

Support for a MaxSmart Power Switch.
import logging

import requests
import voluptuous as vol

from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_NAME, CONF_HOST)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(name)

DEFAULT_NAME = ‘MaxSmart switch’

ATTR_CURRENT_CONSUMPTION = ‘Current Consumption’
ATTR_CURRENT_CONSUMPTION_UNIT = ‘W’

ATTR_CURRENT_AMPAGE = ‘Current Amps’
ATTR_CURRENT_AMPAGE_UNIT = ‘A’

CONF_PORTS = ‘ports’
CONF_OUTLETS = ‘outlets’
REQ_CONF = [CONF_HOST, CONF_OUTLETS]

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PORTS): cv.positive_int,
vol.Optional(CONF_OUTLETS): {cv.positive_int: cv.string},
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string
})

def setup_platform(hass, config, add_devices, discovery_info=None):
“”“Set up the outlets.”“”

dev =

number_outlets = config.get(CONF_PORTS) # MaxSmart Power Strip EU
for number in range(1, number_outlets+1):
dev.append(SmartSwitch(
config.get(CONF_HOST),
config.get(CONF_NAME),
number))

add_devices(dev)

class SmartSwitch(SwitchDevice):
“”“Representation of a single outlet”“”

def init(self, host, name, number):
“”“Initialize the switch.”“”

   self._host = host
   self._name = name
   self._number = number
   self._now_power = 0.0
   self._now_amp = 0.0
   self._state = False

@property
def name(self):
“”“Return the name of the outlet.”“”
return self._name+" #"+str(self._number)

@property
def state_attributes(self):
“”“Return the state attributes of the device.”“”
attrs = {}
attrs[ATTR_CURRENT_CONSUMPTION] = “%.1f %s” %
(self._now_power, ATTR_CURRENT_CONSUMPTION_UNIT)
attrs[ATTR_CURRENT_AMPAGE] = “%.1f %s” %
(self._now_amp, ATTR_CURRENT_AMPAGE_UNIT)
return attrs

@property
def current_power_watt(self):
“”“Return the current power usage in watt.”“”
return self._now_power

@property
def is_on(self):
“”“Return Switch State”“”
return self._state
def turn_on(self):
“”“Turn the switch on.”“”
requests.get(‘http://’+self._host+‘/?cmd=200&json={“port”:’+str(self._number)+
‘, “state”:1}’)

def turn_off(self):
“”“Turn the switch off.”“”
requests.get(‘http://’+self._host+‘/?cmd=200&json={“port”:’+str(self._number)+
‘, “state”:0}’)

def update(self):
“”“get state from outlet.”“”
try:
request = requests.get(‘http://’+self._host+‘/?cmd=511’).json()[‘data’]
_LOGGER.debug(request)
self._state = request[‘switch’][self._number-1]
self._now_power = float(request[‘watt’][self._number-1])
self._now_amp = float(request[‘amp’][self._number-1])
except (TypeError, ValueError):
self._now_power = None
_LOGGER.error(‘Error while reading from MaxSmart Power Outlet’)

Still not formatted correctly, but regardless the file in that GitHub repo needs to go in the custom_components directory. Did you do that?

Sorry, but the file has several sections with “”" which cuts the format :frowning:

custom_components => I don’t have this folder. But I will try to move the file there, as soon as I get home. Thank you!

Backticks, not quotes. These are backticks: ( ``` )

Also the custom_components folder needs to go in the same directory as configuration.yaml. That thread you linked has more details on what to do.

1 Like

Thank you for your help - it was the beginning of the solution! :star_struck:

I have moved and renamed the github script named maxsmart.py to
[config folder ]\custom_components\maxsmart\switch.py

In addition I had to crate an an nearly empty
[config folder ]\custom_components\maxsmart\ init.py
with only docstring introducing (cf)

At the end I had to put two spaces in front of the outlet-names so they stud under the title “outlets” :wink:

Now my installation is able to switch the plugs and I got the information for the consumption eventhough the name is still not transferred correctly! :partying_face: :partying_face: :partying_face:

So this one is solved, with 2 tasks for an other night :wink:
1: Outlet: EG 2: ‘Smart TV’ => becomes EG#2 for the friendly name.
2: Entities are not shown in the entity list?!

That’s why it’s important to format as code when posting here, otherwise we can’t tell if your indentation is correct :wink:

What entity list? You should see them at Developer Tools > States.

I don’t see them in 8123/config/entity_registry, but they are visible in 8123/developer-tools/state => so I soupose like id should be :smiley:

Yes you are right :smiley: But I have discovered Visual Studio Code for me now, which helps a lot in this points…

Hey @apg .What is your situation today ? I try to create a new custom component for the latest Hass version.

Have you this still working ?

Hi @superkikim
Thank you for asking! Unfortunately my two MaxSmart 6 switches-boxes stopped working properly and I hade to remove dem from my setup.
best regards & good luck for your project
apg

Have you updated the Maxsmart app to version 2 by any chance ?

If so, the firmware has also been update rendering them useless without Max Hauri cloud v2 :confounded:

Also, in my case, app version 1 can’t connect to cloud anymore. Seems they have deleted or lost my cloud account and refuse to restore it.

However I have no problem locally.

Yes I have, but that was not e Problem. More and more Switches physically stopped from switching. With the Version 2 of the app or the home assistant made no Difference.